2022-08-17 08:50:24 -07:00
|
|
|
import { IExecuteFunctions, IHookFunctions } from 'n8n-core';
|
2020-06-12 13:23:36 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { IDataObject, ILoadOptionsFunctions, NodeApiError } from 'n8n-workflow';
|
2020-06-12 13:23:36 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { OptionsWithUri } from 'request';
|
2020-06-12 13:23:36 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Make an API request to HackerNews
|
|
|
|
*
|
|
|
|
*/
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function hackerNewsApiRequest(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
|
|
|
qs: IDataObject,
|
|
|
|
): Promise<any> {
|
2020-06-12 13:23:36 -07:00
|
|
|
const options: OptionsWithUri = {
|
2020-06-24 11:02:17 -07:00
|
|
|
method,
|
2020-06-12 13:23:36 -07:00
|
|
|
qs,
|
|
|
|
uri: `http://hn.algolia.com/api/v1/${endpoint}`,
|
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
2020-06-24 11:02:17 -07:00
|
|
|
try {
|
2022-12-02 12:54:28 -08:00
|
|
|
return this.helpers.request!(options);
|
2020-06-24 11:02:17 -07:00
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2020-06-24 11:02:17 -07:00
|
|
|
}
|
2020-06-12 13:23:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make an API request to HackerNews
|
|
|
|
* and return all results
|
|
|
|
*
|
|
|
|
* @param {(IHookFunctions | IExecuteFunctions)} this
|
|
|
|
*/
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function hackerNewsApiRequestAllItems(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
|
|
|
qs: IDataObject,
|
|
|
|
): Promise<any> {
|
2020-06-12 13:23:36 -07:00
|
|
|
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;
|
|
|
|
}
|
2022-08-17 08:50:24 -07:00
|
|
|
} while (responseData.nbHits > itemsReceived);
|
2020-06-12 13:23:36 -07:00
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|