mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-10 14:44:05 -08:00
91d7e16c81
* 🔨 formatting nodes with prettier
73 lines
1.7 KiB
TypeScript
73 lines
1.7 KiB
TypeScript
import { IExecuteFunctions, IHookFunctions } from 'n8n-core';
|
|
|
|
import { IDataObject, ILoadOptionsFunctions, NodeApiError } from 'n8n-workflow';
|
|
|
|
import { OptionsWithUri } from 'request';
|
|
|
|
/**
|
|
* Make an API request to HackerNews
|
|
*
|
|
* @param {IHookFunctions} this
|
|
* @param {string} method
|
|
* @param {string} endpoint
|
|
* @param {IDataObject} qs
|
|
* @returns {Promise<any>}
|
|
*/
|
|
export async function hackerNewsApiRequest(
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
|
method: string,
|
|
endpoint: string,
|
|
qs: IDataObject,
|
|
// tslint:disable-next-line:no-any
|
|
): 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);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Make an API request to HackerNews
|
|
* and return all results
|
|
*
|
|
* @export
|
|
* @param {(IHookFunctions | IExecuteFunctions)} this
|
|
* @param {string} method
|
|
* @param {string} endpoint
|
|
* @param {IDataObject} qs
|
|
* @returns {Promise<any>}
|
|
*/
|
|
export async function hackerNewsApiRequestAllItems(
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
|
method: string,
|
|
endpoint: string,
|
|
qs: IDataObject,
|
|
// tslint:disable-next-line:no-any
|
|
): 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);
|
|
|
|
if (returnData !== undefined) {
|
|
itemsReceived += returnData.length;
|
|
}
|
|
} while (responseData.nbHits > itemsReceived);
|
|
|
|
return returnData;
|
|
}
|