-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
205 lines (183 loc) · 5.17 KB
/
index.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
// Import external deps
import axios from 'axios';
// Import internal deps
import { handleGetApiArgs, handlePostApiArgs, generateTimestamp } from './lib/util.js';
/**
* Class for communicating with the WP Engine API using JavaScript.
*
* @param {string} user The WP Engine API User.
* @param {string} pass The WP Engine API Password/key.
*/
class WpeApi {
constructor(user, pass) {
this.user = user;
this.pass = pass;
this.cache = null;
}
/**
* Get custom WP Engine data.
*
* @param {any} args Api arguments. Docs: https://wpengineapi.com.
* @return {object} Returns api data.
*/
getWpeApi = async (...args) => {
args = handleGetApiArgs(args);
// Check if we have a cached response that is not older than 10 seconds, return that.
const timeNow = generateTimestamp();
if (this?.cache?.args === args && timeNow - this?.cache?.time < 10000) {
return this.cache.data;
}
const urlAxios = `https://api.wpengineapi.com/v1/${args}`;
const optionAxios = {
headers: {
Authorization: 'Basic ' + Buffer.from(this.user + ':' + this.pass).toString('base64'),
},
};
try {
const res = await axios.get(urlAxios, optionAxios);
this.cache = {
args: args,
data: res.data,
time: timeNow,
};
return res.data;
} catch (error) {
throw new Error(error);
}
};
/**
* Post custom WP Engine data.
*
* @param {any} args Api arguments. Docs: https://wpengineapi.com.
* @return {object} Returns api response.
*/
postWpeApi = async (...args) => {
args = handlePostApiArgs(args);
const urlAxios = `https://api.wpengineapi.com/v1/${args.slug}`;
const formDataAxios = args.formData[0];
const optionAxios = {
headers: {
Authorization: 'Basic ' + Buffer.from(this.user + ':' + this.pass).toString('base64'),
},
};
try {
const res = axios.post(urlAxios, formDataAxios, optionAxios);
return res.data;
} catch (error) {
throw new Error(error);
}
};
/**
* Get WP Engine install ID by name.
*
* @param {string} name The WP Engine install Name.
* @return {string} Returns the WP Engine install ID.
*/
id = async (name) => {
const data = await this.getWpeApi('installs', { limit: 1000 });
const installs = data.results;
const install = installs.find((installObj) => {
return installObj.name === name;
});
return install['id'];
};
/**
* Get WP Engine install name by ID.
*
* @param {string} id The WP Engine install ID.
* @return {string} Returns the WP Engine install name.
*/
name = async (id) => {
const res = await this.getWpeApi('installs', id);
return res['name'];
};
/**
* Get WP Engine install domains by ID.
*
* @param {string} id The WP Engine install ID.
* @return {array} Returns the WP Engine install domains.
*/
domains = async (id) => {
const domainsObj = await this.getWpeApi('installs', id, 'domains');
const domains = domainsObj.results.map((item) => {
return item['name'];
});
return domains;
};
/**
* Get the PHP version of the WP Engine install by ID.
*
* @param {string} id The WP Engine install ID.
* @return {string} Returns the PHP version of the WP Engine install.
*/
phpVersion = async (id) => {
const res = await this.getWpeApi('installs', id);
return res['php_version'];
};
/**
* Get the status of the WP Engine install by ID.
*
* @param {string} id The WP Engine install ID.
* @return {string} Returns the status of the WP Engine install.
*/
status = async (id) => {
const res = await this.getWpeApi('installs', id);
return res['status'];
};
/**
* Get the CNAME of the WP Engine install by ID.
*
* @param {string} id The WP Engine install ID.
* @return {string} Returns the CNAME of the WP Engine install.
*/
cname = async (id) => {
const res = await this.getWpeApi('installs', id);
return res['cname'];
};
/**
* Get the WP Engine install environment by ID.
*
* @param {string} id The WP Engine install ID.
* @return {string} Returns the WP Engine install environment.
*/
environment = async (id) => {
const res = await this.getWpeApi('installs', id);
return res['environment'];
};
/**
* Get WP Engine primary install domain by ID.
*
* @param {string} id The WP Engine install ID.
* @return {string} Returns the WP Engine install primary domain.
*/
primaryDomain = async (id) => {
const res = await this.getWpeApi('installs', id);
return res['primary_domain'];
};
/**
* Check if WP Engine install is a multisite environment by ID.
*
* @param {string} id The WP Engine install ID.
* @return {bool} Returns boolean true/false depending on if install is a multisite environment.
*/
isMultisite = async (id) => {
const res = await this.getWpeApi('installs', id);
return res['is_multisite'];
};
/**
* Creates a new WP Engine Backup by ID.
*
* @param {string} id The WP Engine install ID.
* @param {string} description Backup description.
* @param {Array} notification_emails Backup notification email addresses.
* @return {object} Returns backup response.
*/
newBackup = async (id, description, notification_emails) => {
const res = await this.postWpeApi('installs', id, 'backups', {
description,
notification_emails,
});
return res;
};
}
export default WpeApi;