n8n/packages/nodes-base/nodes/HackerNews/GenericFunctions.ts
कारतोफ्फेलस्क्रिप्ट™ 7a4e9ef5fa
refactor: Remove n8n-core dependency in nodes-base (no-changelog) (#5649)
2023-03-09 18:13:15 +01:00

66 lines
1.4 KiB
TypeScript

import type {
IExecuteFunctions,
IHookFunctions,
IDataObject,
ILoadOptionsFunctions,
JsonObject,
} from 'n8n-workflow';
import { NodeApiError } from 'n8n-workflow';
import type { OptionsWithUri } from 'request';
/**
* Make an API request to HackerNews
*
*/
export async function hackerNewsApiRequest(
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
method: string,
endpoint: string,
qs: IDataObject,
): Promise<any> {
const options: OptionsWithUri = {
method,
qs,
uri: `http://hn.algolia.com/api/v1/${endpoint}`,
json: true,
};
try {
return await this.helpers.request(options);
} catch (error) {
throw new NodeApiError(this.getNode(), error as JsonObject);
}
}
/**
* Make an API request to HackerNews
* and return all results
*
* @param {(IHookFunctions | IExecuteFunctions)} this
*/
export async function hackerNewsApiRequestAllItems(
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
method: string,
endpoint: string,
qs: IDataObject,
): Promise<any> {
qs.hitsPerPage = 100;
const returnData: IDataObject[] = [];
let responseData;
let itemsReceived = 0;
do {
responseData = await hackerNewsApiRequest.call(this, method, endpoint, qs);
returnData.push.apply(returnData, responseData.hits as IDataObject[]);
if (returnData !== undefined) {
itemsReceived += returnData.length;
}
} while (responseData.nbHits > itemsReceived);
return returnData;
}