n8n/packages/nodes-base/nodes/Netlify/GenericFunctions.ts
Michael Kret 91d7e16c81
n8n-3867-progressively-apply-prettier-to-all (#3873)
* 🔨 formatting nodes with prettier
2022-08-17 17:50:24 +02:00

78 lines
1.8 KiB
TypeScript

import { OptionsWithUri } from 'request';
import {
IExecuteFunctions,
IExecuteSingleFunctions,
IHookFunctions,
ILoadOptionsFunctions,
} from 'n8n-core';
import { IDataObject, NodeApiError, NodeOperationError } from 'n8n-workflow';
export async function netlifyApiRequest(
this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
method: string,
endpoint: string,
// tslint:disable-next-line:no-any
body: any = {},
query: IDataObject = {},
uri?: string,
option: IDataObject = {},
// tslint:disable-next-line:no-any
): Promise<any> {
const options: OptionsWithUri = {
method,
headers: {
'Content-Type': 'application/json',
},
qs: query,
body,
uri: uri || `https://api.netlify.com/api/v1${endpoint}`,
json: true,
};
if (!Object.keys(body).length) {
delete options.body;
}
if (Object.keys(option)) {
Object.assign(options, option);
}
try {
const credentials = await this.getCredentials('netlifyApi');
options.headers!['Authorization'] = `Bearer ${credentials.accessToken}`;
return await this.helpers.request!(options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
}
export async function netlifyRequestAllItems(
this: IExecuteFunctions | ILoadOptionsFunctions,
method: string,
endpoint: 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.page = 0;
query.per_page = 100;
do {
responseData = await netlifyApiRequest.call(this, method, endpoint, body, query, undefined, {
resolveWithFullResponse: true,
});
query.page++;
returnData.push.apply(returnData, responseData.body);
} while (responseData.headers.link.includes('next'));
return returnData;
}