Skip to content

Commit

Permalink
updated docs for postWpeApi method and added support for passing form…
Browse files Browse the repository at this point in the history
…data
  • Loading branch information
RostiMelk committed May 21, 2021
1 parent ede353e commit 5e5cbd4
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 37 deletions.
92 changes: 65 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,6 @@ const wpe = new WpeApi(user, pass);

## API

.getWpeApi

Get custom WP Engine data.

**Params**

- `...args` **{Any}**: Api arguments. Docs: https://wpengineapi.com.
- `returns` **{Array}**: Returns api data.

**Examples**

```js
wpe.getWpeApi('installs', { limit: 10 })
.then((res) => {
console.log(res);
})
.catch((err) => console.error(`Error: ${err}`));
```

```js
wpe.getWpeApi('installs', id, 'domains')
.then((res) => {
console.log(res);
})
.catch((err) => console.error(`Error: ${err}`));
```

.id

Get WP Engine install ID by `name`.
Expand Down Expand Up @@ -192,3 +165,68 @@ Check if WP Engine install is a multisite environment by `id`.
```js
wpe.isMultisite(id);
```

.newBackup

Creates a new WP Engine Backup by `id`.

**Params**

- `id` **{String}**: The WP Engine install ID.
- `description` **{String}**: Backup description.
- `notification_emails` **{Array}**: Backup notification email addresses.
- `returns` **{Boolean}**: Returns backup response.
**Example**

```js
wpe.newBackup(id, description, notification_emails);
```

.getWpeApi

Get custom WP Engine data.

**Params**

- `...args` **{Any}**: Api arguments. Docs: https://wpengineapi.com.
- `returns` **{Object}**: Returns api data.

**Examples**

```js
wpe.getWpeApi('installs', { limit: 10 })
.then((res) => {
console.log(res);
})
.catch((err) => console.error(`Error: ${err}`));
```

```js
wpe.getWpeApi('installs', id, 'domains')
.then((res) => {
console.log(res);
})
.catch((err) => console.error(`Error: ${err}`));
```

.postWpeApi

Post custom WP Engine data.

**Params**

- `...args` **{Any}**: Api arguments. Docs: https://wpengineapi.com.
- `returns` **{Object}**: Returns api response.

**Examples**

```js
wpe.postWpeApi('installs', id, 'backups', {
description,
notification_emails,
})
.then((res) => {
console.log(res);
})
.catch((err) => console.error(`Error: ${err}`));
```
26 changes: 25 additions & 1 deletion classes/class-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Helper {
* @param {any} args The API arguments
* @return {string} Returns a formatted query string.
*/
handleApiArgs = (args) => {
handleGetApiArgs = (args) => {
let names = [];
let queries = [];
args.forEach((arg) => {
Expand All @@ -40,6 +40,30 @@ class Helper {
queries = `?${querystring.encode(queries)}`;
return names + queries;
};

/**
* Return a object from arguments.
* Used for posting data to API requests / Axios.
*
* @param {any} args The API arguments
* @return {string} Returns a an object containing slug and formData.
*/
handlePostApiArgs = (args) => {
let slug = [];
let formData = [];
args.forEach((arg) => {
if (this.isObject(arg) === false) {
slug.push(arg);
} else {
formData.push(arg);
}
});
slug = slug.join('/');
return {
slug,
formData,
};
};
}

export default Helper;
17 changes: 8 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ class WpeApi {
* Get custom WP Engine data.
*
* @param {any} args Api arguments. Docs: https://wpengineapi.com.
* @return {array} Returns api data.
* @return {object} Returns api data.
*/
getWpeApi = async (...args) => {
args = new Helper().handleApiArgs(args);
args = new Helper().handleGetApiArgs(args);
const urlAxios = `https://api.wpengineapi.com/v1/${args}`;

const optionAxios = {
headers: {
Authorization:
Expand All @@ -45,12 +44,13 @@ class WpeApi {
* Post custom WP Engine data.
*
* @param {any} args Api arguments. Docs: https://wpengineapi.com.
* @return {array} Returns api response.
* @return {object} Returns api response.
*/
postWpeApi = async (...args) => {
args = new Helper().handleApiArgs(args);
const urlAxios = `https://api.wpengineapi.com/v1/${args}`;
args = new Helper().handlePostApiArgs(args);

const urlAxios = `https://api.wpengineapi.com/v1/${args.slug}`;
const formDataAxios = args.formData[0];
const optionAxios = {
headers: {
Authorization:
Expand All @@ -59,7 +59,7 @@ class WpeApi {
};

return await axios
.post(urlAxios, optionAxios)
.post(urlAxios, formDataAxios, optionAxios)
.then((res) => res.data)
.catch((error) => {
throw new Error(error);
Expand Down Expand Up @@ -173,7 +173,7 @@ class WpeApi {
};

/**
* Creates a new WP Engine Backup by install ID.
* Creates a new WP Engine Backup by ID.
*
* @param {string} id The WP Engine install ID.
* @param {string} description Backup description.
Expand All @@ -185,7 +185,6 @@ class WpeApi {
description,
notification_emails,
});

return res;
};
}
Expand Down

0 comments on commit 5e5cbd4

Please sign in to comment.