-
Notifications
You must be signed in to change notification settings - Fork 1
/
wp-request.js
59 lines (54 loc) · 1.75 KB
/
wp-request.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
import * as Models from './src/model'
class WPRequest {
constructor(config) {
this.url = config.url;
this.headers = null;
this.auth = null;
this.oauth = null;
if (config.auth) {
switch(config.auth.type) {
case 'basic':
this.auth = {
user: config.auth.data.username,
pass: config.auth.data.password
};
break;
case 'bearer':
this.auth = {
bearer: config.auth.data.bearerToken
};
break;
case 'oauth':
this.oauth = {
signature_method: this.auth.data.signature_method,
consumer_key : this.auth.data.consumer_key,
token : this.auth.data.oauth_token,
token_secret : this.auth.data.oauth_token_secret
};
if ('RSA-SHA1' === this.auth.data.signature_method) {
this.oauth['private_key'] = this.auth.data.private_key;
} else {
this.oauth['consumer_secret'] = this.auth.data.consumer_secret;
}
break;
default:
break;
}
}
}
query(config, callback) {
let method = config.method.toLowerCase() || 'get';
let postType = config.post_type.toLowerCase() || 'post';
let modelName = postType.charAt(0).toUpperCase() + postType.slice(1);
let data = config.data || {};
let filter = config.filter || {};
let model = null;
if (Models[modelName]) {
model = new Models[modelName]();
} else {
model = new Models.CustomPost(modelName.toLowerCase());
}
model[method](this.url, this.auth, this.oauth, this.headers, filter, data, callback);
}
}
export default WPRequest;