mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-11 07:04:06 -08:00
4d4db7f805
* Wordpress: Add status to getAll: post * Fix status name used in query string * Alphabetically order options for getAll: post * 🐛 Fixed pagination issue when there is no posts for the status * ✨ Updated password field to use a password type Co-authored-by: Jonathan Bennetts <jonathan.bennetts@gmail.com>
69 lines
2 KiB
TypeScript
69 lines
2 KiB
TypeScript
import {
|
|
OptionsWithUri,
|
|
} from 'request';
|
|
|
|
import {
|
|
IExecuteFunctions,
|
|
IExecuteSingleFunctions,
|
|
ILoadOptionsFunctions,
|
|
} from 'n8n-core';
|
|
|
|
import {
|
|
IDataObject, NodeApiError, NodeOperationError,
|
|
} from 'n8n-workflow';
|
|
|
|
export async function wordpressApiRequest(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
|
const credentials = await this.getCredentials('wordpressApi');
|
|
if (credentials === undefined) {
|
|
throw new NodeOperationError(this.getNode(), 'No credentials got returned!');
|
|
}
|
|
|
|
let options: OptionsWithUri = {
|
|
headers: {
|
|
Accept: 'application/json',
|
|
'Content-Type': 'application/json',
|
|
'User-Agent': 'n8n',
|
|
},
|
|
auth: {
|
|
user: credentials!.username as string,
|
|
password: credentials!.password as string,
|
|
},
|
|
method,
|
|
qs,
|
|
body,
|
|
uri: uri || `${credentials!.url}/wp-json/wp/v2${resource}`,
|
|
json: true,
|
|
};
|
|
options = Object.assign({}, options, option);
|
|
if (Object.keys(options.body).length === 0) {
|
|
delete options.body;
|
|
}
|
|
try {
|
|
return await this.helpers.request!(options);
|
|
} catch (error) {
|
|
throw new NodeApiError(this.getNode(), error);
|
|
}
|
|
}
|
|
|
|
export async function wordpressApiRequestAllItems(this: IExecuteFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
let responseData;
|
|
|
|
query.per_page = 10;
|
|
query.page = 0;
|
|
|
|
do {
|
|
query.page++;
|
|
responseData = await wordpressApiRequest.call(this, method, endpoint, body, query, undefined, { resolveWithFullResponse: true });
|
|
returnData.push.apply(returnData, responseData.body);
|
|
} while (
|
|
responseData.headers['x-wp-totalpages'] !== undefined &&
|
|
responseData.headers['x-wp-totalpages'] !== '0' &&
|
|
parseInt(responseData.headers['x-wp-totalpages'], 10) !== query.page
|
|
);
|
|
|
|
return returnData;
|
|
}
|