n8n/packages/nodes-base/nodes/Contentful/GenericFunctions.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

70 lines
1.7 KiB
TypeScript
Raw Normal View History

import type {
IDataObject,
IExecuteFunctions,
IHttpRequestMethods,
ILoadOptionsFunctions,
IRequestOptions,
JsonObject,
} from 'n8n-workflow';
import { NodeApiError } from 'n8n-workflow';
2020-08-04 12:07:54 -07:00
export async function contentfulApiRequest(
this: IExecuteFunctions | ILoadOptionsFunctions,
method: IHttpRequestMethods,
resource: string,
body: any = {},
qs: IDataObject = {},
uri?: string,
_option: IDataObject = {},
): Promise<any> {
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: IRequestOptions = {
2020-08-04 12:07:54 -07:00
method,
qs,
body,
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 {
return await this.helpers.request(options);
2020-08-04 12:07:54 -07:00
} catch (error) {
throw new NodeApiError(this.getNode(), error as JsonObject);
2020-07-09 03:10:22 -07:00
}
2020-08-04 12:07:54 -07:00
}
export async function contentfulApiRequestAllItems(
this: ILoadOptionsFunctions | IExecuteFunctions,
propertyName: string,
method: IHttpRequestMethods,
resource: string,
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;
returnData.push.apply(returnData, responseData[propertyName] as IDataObject[]);
} while (returnData.length < responseData.total);
2020-07-09 02:36:28 -07:00
2020-08-04 12:07:54 -07:00
return returnData;
}