/
wpreader.js
235 lines (188 loc) · 6.19 KB
/
wpreader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import axios from 'axios';
import WPAuth from './wpauthentication';
// - VARIABILI GLOBALI
var defaultArgument = {
'context': 'view',
'page': 1,
'per_page': 10,
'order': 'asc'
};
/**
* This class implement all major endpoint in the WP REST API.
* The request to the Wordpress endpoints is made by axios library
* @class WPReader
*/
export default class WPReader {
/**
* Constructor of the class, it sets the basic options.
* Options: {
* endpoint: URL of Wordpress,
* auth: implement JWT token,
* debug: print in console the endpoint
* }
* @param {Object} options
*/
constructor (options = {}) {
this.settings = Object.assign({}, { endpoint: 'http://prova-wp/' }, options);
this.settings.completeEndpoint = this.settings.endpoint +
(!(this.settings.endpoint.endsWith('/')) ? '/' : '') + 'wp-json/wp/v2/';
if (this.settings.auth) {
this.auth = new WPAuth(this.settings);
if (this.auth.loggedin) {
axios.defaults.headers.common.Authorization = this.auth.token;
}
}
}
/**
* posts endpoint 'wp-json/wp/v2/posts'
* @param {Object} args
*/
async posts (args = {}) {
let method = 'posts';
const def = Object.assign({}, defaultArgument, { status: 'publish' });
if (typeof def.id !== 'undefined') {
method += '/' + def.id;
}
return this._doRequest(method, args, def);
}
/**
* categories endpoint 'wp-json/wp/v2/categories'
* @param {Object} args
*/
async categories (args = {}) {
let method = 'categories';
const def = Object.assign({}, defaultArgument, { 'orderby': 'name' });
return this._doRequest(method, args, def);
}
/**
* tags endpoint 'wp-json/wp/v2/tags'
* @param {Object} args
*/
async tags (args = {}) {
let method = 'tags';
const def = Object.assign({}, defaultArgument, { orderby: 'name' });
return this._doRequest(method, args, def);
}
/**
* pages endpoint 'wp-json/wp/v2/pages'
* @param {Object} args
*/
async pages (args = {}) {
let method = 'pages';
const def = Object.assign({}, defaultArgument, {
orderby: 'title',
status: 'publish'
});
return this._doRequest(method, args, def);
}
/**
* taxonomies endpoint 'wp-json/wp/v2/taxonomies'
* @param {Object} args
*/
async taxonomies (args = {}) {
let method = 'taxonomies';
const def = Object.assign({}, defaultArgument, { context: 'view' });
return this._doRequest(method, args, def);
}
/**
* media endpoint 'wp-json/wp/v2/media'
* @param {Object} args
*/
async media (args = {}) {
let method = 'media';
const def = Object.assign({}, defaultArgument);
return this._doRequest(method, args, def);
}
/**
* users endpoint 'wp-json/wp/v2/users'
* @param {Object} args
*/
async users (args = {}) {
let method = 'users';
const def = Object.assign({}, defaultArgument, {
'orderby': 'id'
});
return this._doRequest(method, args, def);
}
/**
* types endpoint 'wp-json/wp/v2/types'
* @param {Object} args
*/
async postTypes (args = {}) {
let method = 'types';
return this._doRequest(method, args, {'context': 'view'});
}
async comments (args = {}) {
let method = 'comments';
const def = Object.assign({}, defaultArgument, {
'orderby': 'date_gmt'
});
return this._doRequest(method, args, def);
}
/**
* Make login to wordpress, this method register a token for the other request
* @param {String} username username
* @param {String} password password
* @param {Function} callback function for successed login
*/
doAuth (username = '', password = '', callback = null) {
this.auth.doAuth(username, password, callback);
}
/**
* This method implement a custom method for a custom endpoint in the WP
* REST API.
* @param {String} funName name of the functions
* @param {String} method partial URL of the endpoin:
* es. 'wp-json/wp/v2/${method}'
* @param {String} baseEndpoint custom endpoint. Default 'wp-json/wp/v2/'
*/
addCustomMethod (funName = 'custom', method = '', baseEndpoint = 'wp-json/wp/v2/') {
let completeEndpoint = this.settings.endpoint +
(!(this.settings.endpoint.endsWith('/')) ? '/' : '') + baseEndpoint;
let proto = Object.getPrototypeOf(this);
proto[funName] = async args => {
return this._doRequest(method, args, defaultArgument, completeEndpoint);
};
Object.setPrototypeOf(this, proto);
}
/**
*
* @param {Object} params
*/
_queryParams (params) {
return Object.keys(params)
.map(k => encodeURIComponent(k) + '=' + encodeURIComponent(params[k]))
.join('&');
}
/**
*
* @param {String} method
* @param {Object} args
* @param {Object} def
*/
_doRequest (method, args, def, completeEndpoint = false) {
if (completeEndpoint === false) {
completeEndpoint = this.settings.completeEndpoint;
}
if (typeof args.id !== 'undefined') {
method += '/' + args.id;
delete args.id;
}
let params = Object.assign({}, def, args);
if (this.settings.debug) {
console.log(`${completeEndpoint}${method}/?${this._queryParams(params)}`, this.auth);
}
if (!this.settings.auth) {
return axios.get(`${completeEndpoint}${method}/?${this._queryParams(params)}`);
}
if (this.auth.loggedin) {
return axios.get(`${completeEndpoint}${method}/?${this._queryParams(params)}`, {
headers: {
Authorization: `Token ${this.auth.token}`
}
});
} else {
throw new WPAuth.AuthException('La chiamata necessita di autenticazione, non sei autenticato');
}
}
}