mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-27 13:39:44 -08:00
100d9bc087
Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
81 lines
1.9 KiB
TypeScript
81 lines
1.9 KiB
TypeScript
import type {
|
|
IDataObject,
|
|
IExecuteFunctions,
|
|
IHookFunctions,
|
|
IHttpRequestMethods,
|
|
ILoadOptionsFunctions,
|
|
IRequestOptions,
|
|
IWebhookFunctions,
|
|
JsonObject,
|
|
} from 'n8n-workflow';
|
|
import { NodeApiError } from 'n8n-workflow';
|
|
|
|
export async function getresponseApiRequest(
|
|
this: IWebhookFunctions | IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
|
method: IHttpRequestMethods,
|
|
resource: string,
|
|
|
|
body: any = {},
|
|
qs: IDataObject = {},
|
|
uri?: string,
|
|
option: IDataObject = {},
|
|
): Promise<any> {
|
|
const authentication = this.getNodeParameter('authentication', 0, 'apiKey') as string;
|
|
|
|
let options: IRequestOptions = {
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
method,
|
|
body,
|
|
qs,
|
|
uri: uri || `https://api.getresponse.com/v3${resource}`,
|
|
json: true,
|
|
};
|
|
try {
|
|
options = Object.assign({}, options, option);
|
|
if (Object.keys(body as IDataObject).length === 0) {
|
|
delete options.body;
|
|
}
|
|
|
|
if (authentication === 'apiKey') {
|
|
return await this.helpers.requestWithAuthentication.call(this, 'getResponseApi', options);
|
|
} else {
|
|
//@ts-ignore
|
|
return await this.helpers.requestOAuth2.call(this, 'getResponseOAuth2Api', options);
|
|
}
|
|
} catch (error) {
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
|
}
|
|
}
|
|
|
|
export async function getResponseApiRequestAllItems(
|
|
this: IExecuteFunctions | ILoadOptionsFunctions,
|
|
method: IHttpRequestMethods,
|
|
endpoint: string,
|
|
|
|
body: any = {},
|
|
query: IDataObject = {},
|
|
): Promise<any> {
|
|
const returnData: IDataObject[] = [];
|
|
|
|
let responseData;
|
|
query.page = 1;
|
|
|
|
do {
|
|
responseData = await getresponseApiRequest.call(
|
|
this,
|
|
method,
|
|
endpoint,
|
|
body,
|
|
query,
|
|
undefined,
|
|
{ resolveWithFullResponse: true },
|
|
);
|
|
query.page++;
|
|
returnData.push.apply(returnData, responseData.body as IDataObject[]);
|
|
} while (responseData.headers.TotalPages !== responseData.headers.CurrentPage);
|
|
|
|
return returnData;
|
|
}
|