2023-02-23 07:16:05 -08:00
|
|
|
import get from 'lodash.get';
|
2022-10-07 06:08:55 -07:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IDataObject,
|
2022-10-07 06:08:55 -07:00
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
IWebhookFunctions,
|
2023-03-09 09:13:15 -08:00
|
|
|
IHttpRequestOptions,
|
|
|
|
JsonObject,
|
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { jsonParse, NodeApiError } from 'n8n-workflow';
|
2022-10-07 06:08:55 -07:00
|
|
|
|
|
|
|
export async function awsApiRequest(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions,
|
|
|
|
service: string,
|
|
|
|
method: string,
|
|
|
|
path: string,
|
|
|
|
body?: string | Buffer,
|
|
|
|
query: IDataObject = {},
|
|
|
|
headers?: object,
|
2022-10-21 11:52:43 -07:00
|
|
|
): Promise<any> {
|
2022-10-07 06:08:55 -07:00
|
|
|
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) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2022-10-07 06:08:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function awsApiRequestREST(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
service: string,
|
|
|
|
method: string,
|
|
|
|
path: string,
|
|
|
|
body?: string,
|
|
|
|
query: IDataObject = {},
|
|
|
|
headers?: object,
|
2022-10-21 11:52:43 -07:00
|
|
|
): Promise<any> {
|
2022-10-07 06:08:55 -07:00
|
|
|
const response = await awsApiRequest.call(this, service, method, path, body, query, headers);
|
|
|
|
try {
|
2023-02-27 19:39:43 -08:00
|
|
|
return JSON.parse(response as string);
|
2022-10-07 06:08:55 -07:00
|
|
|
} 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 {
|
2022-10-21 11:52:43 -07:00
|
|
|
responseData = await awsApiRequestREST.call(this, service, method, path, body, query, headers);
|
2022-10-07 06:08:55 -07:00
|
|
|
if (responseData.NextToken) {
|
2022-10-21 11:52:43 -07:00
|
|
|
const data = jsonParse<any>(body as string, {
|
|
|
|
errorMessage: 'Response body is not valid JSON',
|
|
|
|
});
|
2022-12-02 12:54:28 -08:00
|
|
|
data.NextToken = responseData.NextToken;
|
2022-10-07 06:08:55 -07:00
|
|
|
}
|
2023-02-27 19:39:43 -08:00
|
|
|
returnData.push.apply(returnData, get(responseData, propertyName) as IDataObject[]);
|
2022-10-07 06:08:55 -07:00
|
|
|
} while (responseData.NextToken !== undefined);
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|