mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-26 21:19:43 -08:00
100d9bc087
Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
76 lines
1.7 KiB
TypeScript
76 lines
1.7 KiB
TypeScript
import type {
|
|
IDataObject,
|
|
IExecuteFunctions,
|
|
IHookFunctions,
|
|
ILoadOptionsFunctions,
|
|
JsonObject,
|
|
IRequestOptions,
|
|
IHttpRequestMethods,
|
|
} from 'n8n-workflow';
|
|
import { NodeApiError } from 'n8n-workflow';
|
|
|
|
export async function netlifyApiRequest(
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
|
method: IHttpRequestMethods,
|
|
endpoint: string,
|
|
|
|
body: any = {},
|
|
query: IDataObject = {},
|
|
uri?: string,
|
|
option: IDataObject = {},
|
|
): Promise<any> {
|
|
const options: IRequestOptions = {
|
|
method,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
qs: query,
|
|
body,
|
|
uri: uri || `https://api.netlify.com/api/v1${endpoint}`,
|
|
json: true,
|
|
};
|
|
|
|
if (!Object.keys(body as IDataObject).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 as JsonObject);
|
|
}
|
|
}
|
|
|
|
export async function netlifyRequestAllItems(
|
|
this: IExecuteFunctions | ILoadOptionsFunctions,
|
|
method: IHttpRequestMethods,
|
|
endpoint: string,
|
|
|
|
body: any = {},
|
|
query: IDataObject = {},
|
|
): 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 as IDataObject[]);
|
|
} while (responseData.headers.link.includes('next'));
|
|
|
|
return returnData;
|
|
}
|