2023-01-27 03:22:44 -08:00
|
|
|
import type { OptionsWithUri } from 'request';
|
2020-11-11 00:44:53 -08:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IDataObject,
|
2020-11-11 00:44:53 -08:00
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
IWebhookFunctions,
|
2023-03-09 09:13:15 -08:00
|
|
|
JsonObject,
|
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { 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
|
|
|
|
2023-02-27 19:39:43 -08:00
|
|
|
body: IDataObject = {},
|
2022-08-17 08:50:24 -07:00
|
|
|
qs: IDataObject = {},
|
|
|
|
uri?: string,
|
|
|
|
headers: IDataObject = {},
|
2023-02-27 19:39:43 -08:00
|
|
|
) {
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return await this.helpers?.request(options);
|
|
|
|
} catch (error) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
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,
|
|
|
|
};
|
2022-12-23 10:09:52 -08:00
|
|
|
return this.helpers.request(options);
|
2020-11-11 00:44:53 -08:00
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function strapiApiRequestAllItems(
|
|
|
|
this: IHookFunctions | ILoadOptionsFunctions | IExecuteFunctions,
|
|
|
|
method: string,
|
|
|
|
resource: string,
|
2023-02-27 19:39:43 -08:00
|
|
|
body: IDataObject = {},
|
2022-08-17 08:50:24 -07:00
|
|
|
query: IDataObject = {},
|
|
|
|
headers: IDataObject = {},
|
2023-02-27 19:39:43 -08:00
|
|
|
) {
|
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]'];
|
2023-02-27 19:39:43 -08:00
|
|
|
returnData.push.apply(returnData, responseData as IDataObject[]);
|
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;
|
2023-02-27 19:39:43 -08:00
|
|
|
returnData.push.apply(returnData, responseData as IDataObject[]);
|
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;
|
|
|
|
}
|