pull:初次提交
This commit is contained in:
65
n8n-n8n-1.109.2/packages/nodes-base/nodes/HackerNews/GenericFunctions.ts
Executable file
65
n8n-n8n-1.109.2/packages/nodes-base/nodes/HackerNews/GenericFunctions.ts
Executable file
@@ -0,0 +1,65 @@
|
||||
import type {
|
||||
IExecuteFunctions,
|
||||
IHookFunctions,
|
||||
IDataObject,
|
||||
ILoadOptionsFunctions,
|
||||
JsonObject,
|
||||
IHttpRequestMethods,
|
||||
IRequestOptions,
|
||||
} from 'n8n-workflow';
|
||||
import { NodeApiError } from 'n8n-workflow';
|
||||
|
||||
/**
|
||||
* Make an API request to HackerNews
|
||||
*
|
||||
*/
|
||||
export async function hackerNewsApiRequest(
|
||||
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
||||
method: IHttpRequestMethods,
|
||||
endpoint: string,
|
||||
qs: IDataObject,
|
||||
): Promise<any> {
|
||||
const options: IRequestOptions = {
|
||||
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: IHttpRequestMethods,
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user