n8n/packages/nodes-base/nodes/Contentful/GenericFunctions.ts
Michael Kret 0ecbb4a19d
refactor: Format nodes-base package (A-F) (#3800)
* 🔨 prettier formated nodes - A

* 🔨 prettier formated nodes - B

*  prettier formated nodes - C

*  prettier formated nodes - D

*  prettier formated nodes - E-F

* 🎨 Adjust nodes-base formatting command (#3805)

* Format additional files in nodes A-F (#3811)

*  fixes

* 🎨 Add Mindee to ignored dirs

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>
2022-08-01 22:47:55 +02:00

68 lines
1.8 KiB
TypeScript

import { IExecuteFunctions, IExecuteSingleFunctions, ILoadOptionsFunctions } from 'n8n-core';
import { OptionsWithUri } from 'request';
import { IDataObject, NodeApiError, NodeOperationError } from 'n8n-workflow';
export async function contentfulApiRequest(
this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
method: string,
resource: string,
// tslint:disable-next-line:no-any
body: any = {},
qs: IDataObject = {},
uri?: string,
option: IDataObject = {},
// tslint:disable-next-line:no-any
): Promise<any> {
const credentials = await this.getCredentials('contentfulApi');
const source = this.getNodeParameter('source', 0) as string;
const isPreview = source === 'previewApi';
const options: OptionsWithUri = {
method,
qs,
body,
uri: uri || `https://${isPreview ? 'preview' : 'cdn'}.contentful.com${resource}`,
json: true,
};
if (isPreview) {
qs.access_token = credentials.ContentPreviewaccessToken as string;
} else {
qs.access_token = credentials.ContentDeliveryaccessToken as string;
}
try {
return await this.helpers.request!(options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
}
export async function contenfulApiRequestAllItems(
this: ILoadOptionsFunctions | IExecuteFunctions,
propertyName: string,
method: string,
resource: string,
// tslint:disable-next-line:no-any
body: any = {},
query: IDataObject = {},
// tslint:disable-next-line:no-any
): Promise<any> {
const returnData: IDataObject[] = [];
let responseData;
query.limit = 100;
query.skip = 0;
do {
responseData = await contentfulApiRequest.call(this, method, resource, body, query);
query.skip = (query.skip + 1) * query.limit;
returnData.push.apply(returnData, responseData[propertyName]);
} while (returnData.length < responseData.total);
return returnData;
}