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-08-31 03:46:28 -07:00
|
|
|
ICredentialDataDecryptedObject,
|
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
|
|
|
|
2023-05-23 02:00:02 -07:00
|
|
|
export const removeTrailingSlash = (url: string) => {
|
|
|
|
if (url.endsWith('/')) {
|
|
|
|
return url.slice(0, -1);
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
};
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function strapiApiRequest(
|
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions | IHookFunctions | IWebhookFunctions,
|
|
|
|
method: string,
|
|
|
|
resource: string,
|
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
|
|
|
) {
|
2023-08-31 03:46:28 -07:00
|
|
|
const authenticationMethod = this.getNodeParameter('authentication', 0);
|
|
|
|
let credentials: ICredentialDataDecryptedObject;
|
|
|
|
|
|
|
|
if (authenticationMethod === 'password') {
|
|
|
|
credentials = await this.getCredentials('strapiApi');
|
|
|
|
} else {
|
|
|
|
credentials = await this.getCredentials('strapiTokenApi');
|
|
|
|
}
|
2020-11-11 00:45:50 -08:00
|
|
|
|
2023-05-23 02:00:02 -07:00
|
|
|
const url = removeTrailingSlash(credentials.url as string);
|
|
|
|
|
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,
|
2023-05-23 02:00:02 -07:00
|
|
|
uri: uri || credentials.apiVersion === 'v4' ? `${url}/api${resource}` : `${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;
|
|
|
|
}
|
|
|
|
|
2023-08-31 03:46:28 -07:00
|
|
|
return await this.helpers.requestWithAuthentication.call(
|
|
|
|
this,
|
|
|
|
authenticationMethod === 'password' ? 'strapiApi' : 'strapiTokenApi',
|
|
|
|
options,
|
|
|
|
);
|
2020-11-11 00:44:53 -08:00
|
|
|
} 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,
|
2023-08-31 03:46:28 -07:00
|
|
|
): Promise<{ jwt: string }> {
|
2022-04-14 23:00:47 -07:00
|
|
|
const credentials = await this.getCredentials('strapiApi');
|
2023-05-23 02:00:02 -07:00
|
|
|
|
|
|
|
const url = removeTrailingSlash(credentials.url as string);
|
|
|
|
|
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,
|
|
|
|
},
|
2023-05-23 02:00:02 -07:00
|
|
|
uri: credentials.apiVersion === 'v4' ? `${url}/api/auth/local` : `${url}/auth/local`,
|
2022-08-17 08:50:24 -07:00
|
|
|
json: true,
|
|
|
|
};
|
2023-08-31 03:46:28 -07:00
|
|
|
return this.helpers.request(options) as Promise<{ jwt: string }>;
|
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-08-31 03:46:28 -07:00
|
|
|
apiVersion: string = 'v3',
|
2023-02-27 19:39:43 -08:00
|
|
|
) {
|
2020-11-11 00:44:53 -08:00
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
2022-03-12 01:56:34 -08:00
|
|
|
if (apiVersion === 'v4') {
|
|
|
|
query['pagination[pageSize]'] = 20;
|
2023-07-03 08:05:28 -07:00
|
|
|
query['pagination[page]'] = 1;
|
2022-03-12 01:56:34 -08:00
|
|
|
do {
|
2022-08-17 08:50:24 -07:00
|
|
|
({ data: responseData } = await strapiApiRequest.call(
|
|
|
|
this,
|
|
|
|
method,
|
|
|
|
resource,
|
|
|
|
body,
|
|
|
|
query,
|
|
|
|
undefined,
|
|
|
|
headers,
|
|
|
|
));
|
2023-07-03 08:05:28 -07:00
|
|
|
query['pagination[page]']++;
|
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;
|
|
|
|
}
|