2023-01-27 03:22:44 -08:00
|
|
|
import type { OptionsWithUri } from 'request';
|
2020-08-04 12:07:54 -07:00
|
|
|
|
2023-03-09 09:13:15 -08:00
|
|
|
import type {
|
|
|
|
IDataObject,
|
|
|
|
IExecuteFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
JsonObject,
|
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeApiError } from 'n8n-workflow';
|
2020-08-04 12:07:54 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
export async function contentfulApiRequest(
|
2023-08-16 06:52:41 -07:00
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions,
|
2022-08-01 13:47:55 -07:00
|
|
|
method: string,
|
|
|
|
resource: string,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
body: any = {},
|
|
|
|
qs: IDataObject = {},
|
|
|
|
uri?: string,
|
2022-11-08 06:28:21 -08:00
|
|
|
_option: IDataObject = {},
|
2022-08-01 13:47:55 -07:00
|
|
|
): Promise<any> {
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('contentfulApi');
|
2020-08-04 12:07:54 -07:00
|
|
|
const source = this.getNodeParameter('source', 0) as string;
|
|
|
|
const isPreview = source === 'previewApi';
|
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
method,
|
|
|
|
qs,
|
|
|
|
body,
|
2023-01-19 04:37:19 -08:00
|
|
|
uri: uri || `https://${isPreview ? 'preview' : 'cdn'}.contentful.com${resource}`,
|
2020-10-22 06:46:03 -07:00
|
|
|
json: true,
|
2020-08-04 12:07:54 -07:00
|
|
|
};
|
|
|
|
|
2020-07-09 03:10:22 -07:00
|
|
|
if (isPreview) {
|
2020-08-04 12:07:54 -07:00
|
|
|
qs.access_token = credentials.ContentPreviewaccessToken as string;
|
|
|
|
} else {
|
|
|
|
qs.access_token = credentials.ContentDeliveryaccessToken as string;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-12-23 10:09:52 -08:00
|
|
|
return await this.helpers.request(options);
|
2020-08-04 12:07:54 -07:00
|
|
|
} catch (error) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2020-07-09 03:10:22 -07:00
|
|
|
}
|
2020-08-04 12:07:54 -07:00
|
|
|
}
|
|
|
|
|
2023-07-19 01:41:50 -07:00
|
|
|
export async function contentfulApiRequestAllItems(
|
2022-08-01 13:47:55 -07:00
|
|
|
this: ILoadOptionsFunctions | IExecuteFunctions,
|
|
|
|
propertyName: string,
|
|
|
|
method: string,
|
|
|
|
resource: string,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
body: any = {},
|
|
|
|
query: IDataObject = {},
|
|
|
|
): Promise<any> {
|
2020-08-04 12:07:54 -07:00
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
|
|
|
|
query.limit = 100;
|
|
|
|
query.skip = 0;
|
2020-07-09 02:36:28 -07:00
|
|
|
|
2020-08-04 12:07:54 -07:00
|
|
|
do {
|
|
|
|
responseData = await contentfulApiRequest.call(this, method, resource, body, query);
|
|
|
|
query.skip = (query.skip + 1) * query.limit;
|
2023-02-27 19:39:43 -08:00
|
|
|
returnData.push.apply(returnData, responseData[propertyName] as IDataObject[]);
|
2022-08-01 13:47:55 -07:00
|
|
|
} while (returnData.length < responseData.total);
|
2020-07-09 02:36:28 -07:00
|
|
|
|
2020-08-04 12:07:54 -07:00
|
|
|
return returnData;
|
|
|
|
}
|