2020-11-11 00:44:53 -08:00
|
|
|
import {
|
|
|
|
OptionsWithUri,
|
|
|
|
} from 'request';
|
|
|
|
|
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
IWebhookFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
|
|
|
import {
|
2021-04-16 09:33:36 -07:00
|
|
|
IDataObject, NodeApiError,
|
2020-11-11 00:44:53 -08:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
export async function strapiApiRequest(this: IExecuteFunctions | ILoadOptionsFunctions | IHookFunctions | IWebhookFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, headers: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
|
|
|
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('strapiApi') as IDataObject;
|
2020-11-11 00:45:50 -08:00
|
|
|
|
2020-11-11 00:44:53 -08:00
|
|
|
try {
|
|
|
|
const options: OptionsWithUri = {
|
2020-11-11 00:52:05 -08:00
|
|
|
headers: {},
|
2020-11-11 00:44:53 -08:00
|
|
|
method,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
uri: uri || `${credentials.url}${resource}`,
|
|
|
|
json: true,
|
2021-10-21 17:14:52 -07:00
|
|
|
qsStringifyOptions: {
|
|
|
|
arrayFormat: 'indice',
|
|
|
|
},
|
2020-11-11 00:44:53 -08:00
|
|
|
};
|
|
|
|
if (Object.keys(headers).length !== 0) {
|
|
|
|
options.headers = Object.assign({}, options.headers, headers);
|
|
|
|
}
|
|
|
|
if (Object.keys(body).length === 0) {
|
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
|
|
|
|
//@ts-ignore
|
|
|
|
return await this.helpers?.request(options);
|
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2020-11-11 00:44:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getToken(this: IExecuteFunctions | ILoadOptionsFunctions | IHookFunctions | IWebhookFunctions): Promise<any> { // tslint:disable-line:no-any
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('strapiApi') as IDataObject;
|
2020-11-11 00:44:53 -08:00
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
'content-type': `application/json`,
|
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
uri: `${credentials.url}/auth/local`,
|
|
|
|
body: {
|
|
|
|
identifier: credentials.email,
|
|
|
|
password: credentials.password,
|
|
|
|
},
|
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
return this.helpers.request!(options);
|
|
|
|
}
|
|
|
|
|
2020-11-11 00:52:05 -08:00
|
|
|
export async function strapiApiRequestAllItems(this: IHookFunctions | ILoadOptionsFunctions | IExecuteFunctions, method: string, resource: string, body: any = {}, query: IDataObject = {}, headers: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
2020-11-11 00:44:53 -08:00
|
|
|
|
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
|
|
|
|
query._limit = 20;
|
|
|
|
|
|
|
|
query._start = 0;
|
|
|
|
|
|
|
|
do {
|
2020-11-11 00:52:05 -08:00
|
|
|
responseData = await strapiApiRequest.call(this, method, resource, body, query, undefined, headers);
|
2020-11-11 00:44:53 -08:00
|
|
|
query._start += query._limit;
|
|
|
|
returnData.push.apply(returnData, responseData);
|
|
|
|
} while (
|
|
|
|
responseData.length !== 0
|
|
|
|
);
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function validateJSON(json: string | undefined): any { // tslint:disable-line:no-any
|
|
|
|
let result;
|
|
|
|
try {
|
|
|
|
result = JSON.parse(json!);
|
|
|
|
} catch (exception) {
|
|
|
|
result = undefined;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|