From dfd29f2e14a2657a9adf0146cf019363a0d9641b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Ovejero?= Date: Sun, 17 Jan 2021 16:17:17 -0300 Subject: [PATCH] Add request for all items --- .../nodes/Reddit/GenericFunctions.ts | 57 ++++++++++--------- .../nodes/Reddit/ListingDescription.ts | 48 +++++++++++++++- .../nodes-base/nodes/Reddit/Reddit.node.ts | 22 +++++-- 3 files changed, 95 insertions(+), 32 deletions(-) diff --git a/packages/nodes-base/nodes/Reddit/GenericFunctions.ts b/packages/nodes-base/nodes/Reddit/GenericFunctions.ts index 8d1bfdb7b0..b546084677 100644 --- a/packages/nodes-base/nodes/Reddit/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Reddit/GenericFunctions.ts @@ -5,7 +5,6 @@ import { import { IDataObject, - IOAuth2Options, } from 'n8n-workflow'; import { @@ -14,21 +13,15 @@ import { /** - * Make an API request to Reddit - * - * @param { IHookFunctions } this - * @param { string } method - * @param { string } endpoint - * @param { IDataObject } qs - * @returns { Promise } + * Make an API request to Reddit. */ export async function redditApiRequest( this: IHookFunctions | IExecuteFunctions, method: string, endpoint: string, - body?: IDataObject, - qs?: IDataObject, - noAuthRequired?: boolean, + qs: IDataObject, + body: IDataObject, + noAuth: boolean, ): Promise { // tslint:disable-line:no-any const options: OptionsWithUri = { @@ -36,20 +29,22 @@ export async function redditApiRequest( 'user-agent': 'n8n', }, method, - uri: noAuthRequired ? `https://www.reddit.com/${endpoint}` : `https://oauth.reddit.com/api/v1/${endpoint}`, + uri: noAuth ? `https://www.reddit.com/${endpoint}` : `https://oauth.reddit.com/api/v1/${endpoint}`, + qs, + body, json: true, }; - if (body && Object.keys(body).length) { - options.body = body; + if (!Object.keys(body).length) { + delete options.body; } - if (qs && Object.keys(qs).length) { - options.qs = qs; + if (!Object.keys(qs).length) { + delete options.qs; } try { - return noAuthRequired + return noAuth ? await this.helpers.request.call(this, options) : await this.helpers.requestOAuth2.call(this, 'redditOAuth2Api', options); } catch (error) { @@ -59,26 +54,34 @@ export async function redditApiRequest( } throw error; } - } /** - * Make an API request to Reddit and return all results - * - * @export - * @param { (IHookFunctions | IExecuteFunctions) } this - * @param { string } method - * @param { string } endpoint - * @param { IDataObject } qs - * @returns { Promise } + * Make an API request to Reddit and return all results. */ export async function redditApiRequestAllItems( this: IHookFunctions | IExecuteFunctions, method: string, endpoint: string, qs: IDataObject, + body: IDataObject, + noAuth: boolean, ): Promise { // tslint:disable-line:no-any - // ... + let responseData; + const returnData: IDataObject[] = []; + + do { + responseData = await redditApiRequest.call(this, method, endpoint, qs, body, noAuth); + qs.after = responseData.after; + responseData.data.children.forEach((child: any) => returnData.push(child.data)); // tslint:disable-line:no-any + + if (qs.limit && returnData.length >= qs.limit) { + return returnData; + } + + } while (responseData.after); + + return returnData; } diff --git a/packages/nodes-base/nodes/Reddit/ListingDescription.ts b/packages/nodes-base/nodes/Reddit/ListingDescription.ts index ed4319b66d..9a5c494f3b 100644 --- a/packages/nodes-base/nodes/Reddit/ListingDescription.ts +++ b/packages/nodes-base/nodes/Reddit/ListingDescription.ts @@ -54,5 +54,51 @@ export const listingFields = [ }, }, }, - + { + displayName: 'Return All', + name: 'returnAll', + type: 'boolean', + default: false, + description: 'Return all results', + displayOptions: { + show: { + resource: [ + 'listing', + ], + operation: [ + 'get', + ], + type: [ + 'best', + ], + }, + }, + }, + { + displayName: 'Limit', + name: 'limit', + type: 'number', + default: 5, + description: 'The number of results to return', + typeOptions: { + minValue: 1, + maxValue: 100, + }, + displayOptions: { + show: { + resource: [ + 'listing', + ], + operation: [ + 'get', + ], + type: [ + 'best', + ], + returnAll: [ + false, + ], + }, + }, + }, ] as INodeProperties[]; diff --git a/packages/nodes-base/nodes/Reddit/Reddit.node.ts b/packages/nodes-base/nodes/Reddit/Reddit.node.ts index 52df8c5d8d..413649bf96 100644 --- a/packages/nodes-base/nodes/Reddit/Reddit.node.ts +++ b/packages/nodes-base/nodes/Reddit/Reddit.node.ts @@ -117,7 +117,7 @@ export class Reddit implements INodeType { trophies: 'me/trophies', }; - responseData = await redditApiRequest.call(this, 'GET', endpoints[details]); + responseData = await redditApiRequest.call(this, 'GET', endpoints[details], {}, {}, false); if (details === 'identity') { responseData = responseData.features; @@ -145,7 +145,7 @@ export class Reddit implements INodeType { body.resubmit = true; } - responseData = await redditApiRequest.call(this, 'POST', 'submit', body); + responseData = await redditApiRequest.call(this, 'POST', 'submit', {}, body, false); } @@ -162,9 +162,23 @@ export class Reddit implements INodeType { } else if (type === 'best') { - const endpoint = 'best.json'; - responseData = await redditApiRequest.call(this, 'GET', endpoint, {}, {}, true); + const returnAll = this.getNodeParameter('returnAll', i); + if (returnAll) { + + const endpoint = 'best.json'; + responseData = await redditApiRequestAllItems.call(this, 'GET', endpoint, {}, {}, true); + + } else { + + const qs: IDataObject = { + limit: this.getNodeParameter('limit', i), + }; + const endpoint = 'best.json'; + responseData = await redditApiRequestAllItems.call(this, 'GET', endpoint, qs, {}, true); + responseData = responseData.splice(0, qs.limit); + + } } }