2023-01-27 03:22:44 -08:00
|
|
|
import type { OptionsWithUri } from 'request';
|
2020-12-10 02:30:13 -08:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { IExecuteFunctions, IHookFunctions } from 'n8n-core';
|
2020-12-10 02:30:13 -08:00
|
|
|
|
2023-02-27 19:39:43 -08:00
|
|
|
import type { IDataObject, JsonObject } from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeApiError } from 'n8n-workflow';
|
2020-12-10 02:30:13 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function nasaApiRequest(
|
|
|
|
this: IHookFunctions | IExecuteFunctions,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
|
|
|
qs: IDataObject,
|
|
|
|
option: IDataObject = {},
|
|
|
|
uri?: string | undefined,
|
|
|
|
): Promise<any> {
|
2022-04-14 23:00:47 -07:00
|
|
|
const credentials = await this.getCredentials('nasaApi');
|
2020-12-10 02:30:13 -08:00
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
qs.api_key = credentials.api_key as string;
|
2020-12-10 02:30:13 -08:00
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
method,
|
|
|
|
qs,
|
2023-01-19 04:37:19 -08:00
|
|
|
uri: uri || `https://api.nasa.gov${endpoint}`,
|
2020-12-10 02:30:13 -08:00
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (Object.keys(option)) {
|
|
|
|
Object.assign(options, option);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return await this.helpers.request(options);
|
|
|
|
} catch (error) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2020-12-10 02:30:13 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function nasaApiRequestAllItems(
|
|
|
|
this: IHookFunctions | IExecuteFunctions,
|
|
|
|
propertyName: string,
|
|
|
|
method: string,
|
|
|
|
resource: string,
|
|
|
|
query: IDataObject = {},
|
|
|
|
): Promise<any> {
|
2020-12-10 02:30:13 -08:00
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
|
|
|
|
query.size = 20;
|
|
|
|
|
|
|
|
let uri: string | undefined = undefined;
|
|
|
|
|
|
|
|
do {
|
|
|
|
responseData = await nasaApiRequest.call(this, method, resource, query, {}, uri);
|
|
|
|
uri = responseData.links.next;
|
2023-02-27 19:39:43 -08:00
|
|
|
returnData.push.apply(returnData, responseData[propertyName] as IDataObject[]);
|
2022-08-17 08:50:24 -07:00
|
|
|
} while (responseData.links.next !== undefined);
|
2020-12-10 02:30:13 -08:00
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|