Skip to content

Commit eeda6e2

Browse files
committed
Adds fetchOptions to support REST endpoints needing cookie authentication
1 parent bfdcb19 commit eeda6e2

File tree

5 files changed

+8206
-55
lines changed

5 files changed

+8206
-55
lines changed

README.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# vue-wordpress
22

3-
> A collection of Vue components and mixins for use with WordPress and the WP-REST API
3+
> A vue plugin that facilitates use of the WordPress REST API as a back-end Content Management System (CMS) with a Vue front-end
4+
5+
# Design overview
6+
47

58
# Example:
69

@@ -10,9 +13,26 @@ You can run the example by running npm run dev (see below)
1013

1114
You can see the components used 'in the wild' at http://pixelthin.com
1215

13-
You can see the example.vue app live at http://pixelthin.com/vue-wordpress
16+
## Getting Started
17+
18+
### Vue installation
19+
20+
```
21+
vue add vue-wordpress
22+
23+
```
24+
25+
### How to use vue-wordpress in your Vue app
26+
27+
Anywhere you wish to pull in content from WordPress, simply do the following:
28+
```
29+
30+
<div v-for="(post,index) of postLoader.pages[0].content" :key="index">
31+
<post-summary imgSize="thumbnail" :post="post" class="post-summary"></post-summary>
32+
</br>
33+
</div>
1434
15-
## Build Setup
35+
```
1636

1737
#### &#x1F537; Note: You may experience a babel-preset-env error when running 'npm run dev'
1838
-- to fix this, please execute 'npm install --save-dev babel-preset-env'. See Babel issue 186 for more information <https://github.com/babel/babel-preset-env/issues/186>

package.json

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,25 @@
2323
"babel-core": "^6.22.1",
2424
"babel-eslint": "^7.1.1",
2525
"babel-loader": "^7.1.1",
26+
"babel-plugin-istanbul": "^4.1.1",
2627
"babel-plugin-transform-runtime": "^6.22.0",
2728
"babel-preset-env": "^1.3.2",
2829
"babel-preset-stage-2": "^6.22.0",
2930
"babel-register": "^6.22.0",
31+
"chai": "^3.5.0",
3032
"chalk": "^2.0.1",
33+
"chromedriver": "^2.27.2",
3134
"connect-history-api-fallback": "^1.3.0",
3235
"copy-webpack-plugin": "^4.0.1",
36+
"cross-env": "^5.0.1",
37+
"cross-spawn": "^5.0.1",
3338
"css-loader": "^0.28.0",
3439
"cssnano": "^3.10.0",
3540
"eslint": "^3.19.0",
41+
"eslint-config-standard": "^6.2.1",
3642
"eslint-friendly-formatter": "^3.0.0",
3743
"eslint-loader": "^1.7.1",
3844
"eslint-plugin-html": "^3.0.0",
39-
"eslint-config-standard": "^6.2.1",
4045
"eslint-plugin-promise": "^3.4.0",
4146
"eslint-plugin-standard": "^2.0.1",
4247
"eventsource-polyfill": "^0.9.6",
@@ -46,8 +51,7 @@
4651
"friendly-errors-webpack-plugin": "^1.1.3",
4752
"html-webpack-plugin": "^2.28.0",
4853
"http-proxy-middleware": "^0.17.3",
49-
"webpack-bundle-analyzer": "^2.2.1",
50-
"cross-env": "^5.0.1",
54+
"inject-loader": "^3.0.0",
5155
"karma": "^1.4.1",
5256
"karma-coverage": "^1.1.1",
5357
"karma-mocha": "^1.3.0",
@@ -59,27 +63,23 @@
5963
"karma-webpack": "^2.0.2",
6064
"lolex": "^1.5.2",
6165
"mocha": "^3.2.0",
62-
"chai": "^3.5.0",
63-
"sinon": "^2.1.0",
64-
"sinon-chai": "^2.8.0",
65-
"inject-loader": "^3.0.0",
66-
"babel-plugin-istanbul": "^4.1.1",
67-
"phantomjs-prebuilt": "^2.1.14",
68-
"chromedriver": "^2.27.2",
69-
"cross-spawn": "^5.0.1",
7066
"nightwatch": "^0.9.12",
71-
"selenium-server": "^3.0.1",
72-
"semver": "^5.3.0",
73-
"shelljs": "^0.7.6",
7467
"opn": "^5.1.0",
7568
"optimize-css-assets-webpack-plugin": "^2.0.0",
7669
"ora": "^1.2.0",
70+
"phantomjs-prebuilt": "^2.1.14",
7771
"rimraf": "^2.6.0",
72+
"selenium-server": "^3.0.1",
73+
"semver": "^5.3.0",
74+
"shelljs": "^0.7.6",
75+
"sinon": "^2.1.0",
76+
"sinon-chai": "^2.8.0",
7877
"url-loader": "^0.5.8",
7978
"vue-loader": "^12.1.0",
8079
"vue-style-loader": "^3.0.1",
8180
"vue-template-compiler": "^2.3.3",
8281
"webpack": "^2.6.1",
82+
"webpack-bundle-analyzer": "^2.2.1",
8383
"webpack-dev-middleware": "^1.10.0",
8484
"webpack-hot-middleware": "^2.18.0",
8585
"webpack-merge": "^4.1.0"

src/example/example.vue

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,50 @@
11
<template>
22
<div>
33
<h1>vue-wordpress</h1>
4-
<p>A collection of vue.js components and mixins for interfacing with the WordPress REST API. For information on how to use vue-wordpress, go to the github site. The most used and useful pieces of this repo are probably methods in the mixin:
5-
<ul>
6-
<li>createWpLoader simplifies pulling data from the WP-REST API with WP loaders. WP loaders support pagination and simplify async fetching of data. Support custom mappers.</li>
7-
<li>mapPosts is a default map function for use with createWpLoader to map the REST-API response for posts into a simpler data format.
8-
<li>getTags is a function that can be used in the map function of a post loader to get all tags associated with a post.</li>
9-
<li>getImageSource is a function that maps simple image sizes to WP-REST mapper properties</li>
10-
<li>getPostRestUrl is a function that will take a url and map it to the corresponding WP-REST API. This can be used to pull posts from the WP-REST API based upon the route in vue-router. For an example see <a href="https://pixelthin.com">Pixelthin.com</a></li>
11-
</ul>
12-
</p>
4+
<p>A collection of vue.js components and mixins for interfacing with the WordPress REST API. For information on how to use vue-wordpress, go to the github site. The most used and useful pieces of this repo are probably methods in the mixin:</p>
5+
<ul>
6+
<li>createWpLoader simplifies pulling data from the WP-REST API with WP loaders. WP loaders support pagination and simplify async fetching of data. Support custom mappers.</li>
7+
<li>mapPosts is a default map function for use with createWpLoader to map the REST-API response for posts into a simpler data format.</li>
8+
<li>getTags is a function that can be used in the map function of a post loader to get all tags associated with a post.</li>
9+
<li>getImageSource is a function that maps simple image sizes to WP-REST mapper properties</li>
10+
<li>
11+
getPostRestUrl is a function that will take a url and map it to the corresponding WP-REST API. This can be used to pull posts from the WP-REST API based upon the route in vue-router. For an example see
12+
<a
13+
href="https://pixelthin.com"
14+
>Pixelthin.com</a>
15+
</li>
16+
</ul>
1317
<h2>Examples:</h2>
14-
<p>Each of the following examples utilizes the wp-mixin and the async-content component. Check out the example source for more information on how to use the mixins. The post visual vue components may also be of use but, if you want to really customize them it may be easier to just use them as a template and create your own components using the mixins.
15-
</p>
18+
<p>Each of the following examples utilizes the wp-mixin and the async-content component. Check out the example source for more information on how to use the mixins. The post visual vue components may also be of use but, if you want to really customize them it may be easier to just use them as a template and create your own components using the mixins.</p>
1619
<div class="flex-container">
1720
<div class="col-sm">
1821
<h4>post-summary</h4>
19-
<!-- In this example, we're just loading one page of results from the WP-REST API.
20-
For an example of loading more than one post, see the single-post section-->
21-
<async-content :loaded="postLoader.pages[0].loaded">
22+
<!-- In this example, we're loading the results from the WP-REST API. -->
23+
<!-- <async-content :loaded="postLoader.pages[0].loaded"> -->
24+
<div v-for="(pages, index) of postLoader.pages" :key="index">
2225
<div v-for="(post,index) of postLoader.pages[0].content" :key="index">
2326
<post-summary imgSize="thumbnail" :post="post" class="post-summary"></post-summary>
24-
</br>
27+
<br />
2528
</div>
26-
</async-content>
27-
</div>
28-
<div class="col-sm">
29-
<h4>single-post</h4>
30-
<!-- In this example we're loading one post at a time and letting the user pull a new on in dynamically
31-
whenever they click a button -->
32-
<div v-for="(page,index) of singlePostLoader.pages" :key="index">
33-
<async-content :loaded="page.loaded">
34-
<!-- This post loader only contains one post per page (we set per_page=1 in the post loader query params) -->
35-
<post :post="page.content[0]"></post>
36-
</async-content>
3729
</div>
38-
<button type="button" @click="singlePostLoader.loadPage()"> Load Another Post</button>
39-
</div>
40-
<div class="col-lg">
41-
<h4>all-posts</h4>
42-
<all-posts :loader="allPostLoader"></all-posts>
30+
<div class="col-sm">
31+
<h4>single-post</h4>
32+
<!-- In this example we're loading one post at a time and letting the user pull a new on in dynamically
33+
whenever they click a button-->
34+
<div v-for="(page,index) of singlePostLoader.pages" :key="index">
35+
<async-content :loaded="page.loaded">
36+
<!-- This post loader only contains one post per page (we set per_page=1 in the post loader query params) -->
37+
<post :post="page.content[0]"></post>
38+
</async-content>
39+
</div>
40+
<button type="button" @click="singlePostLoader.loadPage()">Load Another Post</button>
41+
</div>
42+
<div class="col-lg">
43+
<h4>all-posts</h4>
44+
<all-posts :loader="allPostLoader"></all-posts>
45+
</div>
4346
</div>
4447
</div>
45-
4648
</div>
4749
</template>
4850

@@ -121,13 +123,13 @@ h4 {
121123
.col-sm {
122124
width: 45%;
123125
margin: 2px;
124-
background-color: #CCDCDC;
126+
background-color: #ccdcdc;
125127
min-width: 400px;
126128
}
127129
128130
.col-lg {
129131
width: 90%;
130132
margin: 4px;
131-
background-color: #CCDCDC;
133+
background-color: #ccdcdc;
132134
}
133135
</style>

src/mixins/wp-mixin.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import safeGet from 'safe-get'
33
import 'whatwg-fetch'
44

55
// wpGet just wraps some of the basic fetch boilerplate
6-
const wpGet = function ({ url, mapper }) {
7-
return fetch(url)
6+
const wpGet = function ({ url, mapper, fetchOptions }) {
7+
return fetch(url, fetchOptions)
88
.then(res => res.json())
99
.then(json => {
1010
return mapper(json)
@@ -20,8 +20,10 @@ export default {
2020
* mapper: a function that takes JSON and maps it to an object. See mapPosts for an example. Useful for custom types.
2121
* queryParams: an array containing any additional query parameter key-value pairs (see example.vue).
2222
* embed: a boolean indicating whether media links and other links should be embedded in the response. **Default is true.**
23+
* @param {object} fetchOptions - An object that contains options for the fetch call. See https://github.github.io/fetch/ for more
24+
* informaiton. NOTE: Must set {credentials: 'include' | 'same-site'} for endpoints requiring cookie authentication.
2325
*/
24-
createWpLoader(url, options) {
26+
createWpLoader(url, options, fetchOptions) {
2527
let mapper = options.mapper ? options.mapper : this.mapPosts
2628
url += options.embed !== false ? '?_embed&' : '?'
2729
if (safeGet(options, 'queryParams.length') > 0) {
@@ -35,7 +37,7 @@ export default {
3537
const pageToLoad = loader.pagesLoaded + 1
3638
const newPage = { loaded: false, content: [] }
3739
loader.pages.push(newPage)
38-
wpGet({ url: url + 'page=' + pageToLoad, mapper }).then(content => {
40+
wpGet({ url: url + 'page=' + pageToLoad, mapper, fetchOptions }).then(content => {
3941
newPage.content = content
4042
newPage.loaded = true
4143
loader.pagesLoaded++

0 commit comments

Comments
 (0)