mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-27 05:29:42 -08:00
1111c915f2
Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
89 lines
2.1 KiB
TypeScript
89 lines
2.1 KiB
TypeScript
import get from 'lodash/get';
|
|
|
|
import type {
|
|
IDataObject,
|
|
IExecuteFunctions,
|
|
IHookFunctions,
|
|
ILoadOptionsFunctions,
|
|
IWebhookFunctions,
|
|
IHttpRequestOptions,
|
|
JsonObject,
|
|
} from 'n8n-workflow';
|
|
import { jsonParse, NodeApiError } from 'n8n-workflow';
|
|
|
|
export async function awsApiRequest(
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions,
|
|
service: string,
|
|
method: string,
|
|
path: string,
|
|
body?: string | Buffer,
|
|
query: IDataObject = {},
|
|
headers?: object,
|
|
): Promise<any> {
|
|
const credentials = await this.getCredentials('aws');
|
|
|
|
const requestOptions = {
|
|
qs: {
|
|
service,
|
|
path,
|
|
...query,
|
|
},
|
|
headers,
|
|
method,
|
|
url: '',
|
|
body,
|
|
region: credentials?.region as string,
|
|
} as IHttpRequestOptions;
|
|
|
|
try {
|
|
return await this.helpers.requestWithAuthentication.call(this, 'aws', requestOptions);
|
|
} catch (error) {
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
|
}
|
|
}
|
|
|
|
export async function awsApiRequestREST(
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
|
service: string,
|
|
method: string,
|
|
path: string,
|
|
body?: string,
|
|
query: IDataObject = {},
|
|
headers?: object,
|
|
): Promise<any> {
|
|
const response = await awsApiRequest.call(this, service, method, path, body, query, headers);
|
|
try {
|
|
return JSON.parse(response as string);
|
|
} catch (e) {
|
|
return response;
|
|
}
|
|
}
|
|
|
|
export async function awsApiRequestAllItems(
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
|
propertyName: string,
|
|
service: string,
|
|
method: string,
|
|
path: string,
|
|
body?: string,
|
|
query: IDataObject = {},
|
|
headers: IDataObject = {},
|
|
): Promise<any> {
|
|
const returnData: IDataObject[] = [];
|
|
|
|
let responseData;
|
|
|
|
do {
|
|
responseData = await awsApiRequestREST.call(this, service, method, path, body, query, headers);
|
|
if (responseData.NextToken) {
|
|
const data = jsonParse<any>(body as string, {
|
|
errorMessage: 'Response body is not valid JSON',
|
|
});
|
|
data.NextToken = responseData.NextToken;
|
|
}
|
|
returnData.push.apply(returnData, get(responseData, propertyName) as IDataObject[]);
|
|
} while (responseData.NextToken !== undefined);
|
|
|
|
return returnData;
|
|
}
|