2022-08-17 08:50:24 -07:00
|
|
|
import { OptionsWithUri } from 'request';
|
2020-11-11 00:44:53 -08:00
|
|
|
|
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
IWebhookFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { IDataObject, NodeApiError } from 'n8n-workflow';
|
2020-11-11 00:44:53 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function strapiApiRequest(
|
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions | IHookFunctions | IWebhookFunctions,
|
|
|
|
method: string,
|
|
|
|
resource: string,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
body: any = {},
|
|
|
|
qs: IDataObject = {},
|
|
|
|
uri?: string,
|
|
|
|
headers: IDataObject = {},
|
|
|
|
): Promise<any> {
|
2022-04-14 23:00:47 -07:00
|
|
|
const credentials = await this.getCredentials('strapiApi');
|
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,
|
2022-08-17 08:50:24 -07:00
|
|
|
uri:
|
|
|
|
uri || credentials.apiVersion === 'v4'
|
|
|
|
? `${credentials.url}/api${resource}`
|
|
|
|
: `${credentials.url}${resource}`,
|
2020-11-11 00:44:53 -08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function getToken(
|
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions | IHookFunctions | IWebhookFunctions,
|
|
|
|
): Promise<any> {
|
2022-04-14 23:00:47 -07:00
|
|
|
const credentials = await this.getCredentials('strapiApi');
|
2022-03-12 01:56:34 -08:00
|
|
|
let options = {} as OptionsWithUri;
|
2022-08-17 08:50:24 -07:00
|
|
|
options = {
|
|
|
|
headers: {
|
|
|
|
'content-type': 'application/json',
|
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
body: {
|
|
|
|
identifier: credentials.email,
|
|
|
|
password: credentials.password,
|
|
|
|
},
|
|
|
|
uri:
|
|
|
|
credentials.apiVersion === 'v4'
|
|
|
|
? `${credentials.url}/api/auth/local`
|
|
|
|
: `${credentials.url}/auth/local`,
|
|
|
|
json: true,
|
|
|
|
};
|
2020-11-11 00:44:53 -08:00
|
|
|
return this.helpers.request!(options);
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function strapiApiRequestAllItems(
|
|
|
|
this: IHookFunctions | ILoadOptionsFunctions | IExecuteFunctions,
|
|
|
|
method: string,
|
|
|
|
resource: string,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
body: any = {},
|
|
|
|
query: IDataObject = {},
|
|
|
|
headers: IDataObject = {},
|
|
|
|
): Promise<any> {
|
2020-11-11 00:44:53 -08:00
|
|
|
const returnData: IDataObject[] = [];
|
2022-08-17 08:50:24 -07:00
|
|
|
const { apiVersion } = await this.getCredentials('strapiApi');
|
2020-11-11 00:44:53 -08:00
|
|
|
|
|
|
|
let responseData;
|
2022-03-12 01:56:34 -08:00
|
|
|
if (apiVersion === 'v4') {
|
|
|
|
query['pagination[pageSize]'] = 20;
|
|
|
|
query['pagination[page]'] = 0;
|
|
|
|
do {
|
2022-08-17 08:50:24 -07:00
|
|
|
({ data: responseData } = await strapiApiRequest.call(
|
|
|
|
this,
|
|
|
|
method,
|
|
|
|
resource,
|
|
|
|
body,
|
|
|
|
query,
|
|
|
|
undefined,
|
|
|
|
headers,
|
|
|
|
));
|
2022-03-12 01:56:34 -08:00
|
|
|
query['pagination[page]'] += query['pagination[pageSize]'];
|
|
|
|
returnData.push.apply(returnData, responseData);
|
2022-08-17 08:50:24 -07:00
|
|
|
} while (responseData.length !== 0);
|
2022-03-12 01:56:34 -08:00
|
|
|
} else {
|
|
|
|
query._limit = 20;
|
|
|
|
query._start = 0;
|
|
|
|
do {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await strapiApiRequest.call(
|
|
|
|
this,
|
|
|
|
method,
|
|
|
|
resource,
|
|
|
|
body,
|
|
|
|
query,
|
|
|
|
undefined,
|
|
|
|
headers,
|
|
|
|
);
|
2022-03-12 01:56:34 -08:00
|
|
|
query._start += query._limit;
|
|
|
|
returnData.push.apply(returnData, responseData);
|
2022-08-17 08:50:24 -07:00
|
|
|
} while (responseData.length !== 0);
|
2022-03-12 01:56:34 -08:00
|
|
|
}
|
2020-11-11 00:44:53 -08:00
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export function validateJSON(json: string | undefined): any {
|
2020-11-11 00:44:53 -08:00
|
|
|
let result;
|
|
|
|
try {
|
|
|
|
result = JSON.parse(json!);
|
|
|
|
} catch (exception) {
|
|
|
|
result = undefined;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|