2020-06-12 13:23:36 -07:00
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
|
|
|
import {
|
2020-06-24 11:02:17 -07:00
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
2021-04-16 09:33:36 -07:00
|
|
|
NodeApiError,
|
2020-06-12 13:23:36 -07:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
import {
|
2020-06-24 11:02:17 -07:00
|
|
|
OptionsWithUri,
|
2020-06-12 13:23:36 -07:00
|
|
|
} 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): Promise<any> { // tslint:disable-line:no-any
|
|
|
|
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 {
|
|
|
|
return await this.helpers.request!(options);
|
|
|
|
} 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
|
|
|
|
*
|
|
|
|
* @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): Promise<any> { // tslint:disable-line:no-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;
|
|
|
|
}
|