From 7d99ed0be7b3476af2662fbd7b127a5447d750c0 Mon Sep 17 00:00:00 2001 From: ricardo Date: Sun, 31 Jan 2021 17:28:46 -0500 Subject: [PATCH] :zap: Improvements --- .../nodes/Reddit/GenericFunctions.ts | 51 +++++++++----- .../nodes/Reddit/PostDescription.ts | 16 +---- .../nodes-base/nodes/Reddit/Reddit.node.ts | 69 +++++++++---------- .../nodes/Reddit/SubredditDescription.ts | 4 -- 4 files changed, 68 insertions(+), 72 deletions(-) diff --git a/packages/nodes-base/nodes/Reddit/GenericFunctions.ts b/packages/nodes-base/nodes/Reddit/GenericFunctions.ts index e0ac77f409..ced6ff39f9 100644 --- a/packages/nodes-base/nodes/Reddit/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Reddit/GenericFunctions.ts @@ -11,7 +11,6 @@ import { OptionsWithUri, } from 'request'; - /** * Make an authenticated or unauthenticated API request to Reddit. */ @@ -26,6 +25,8 @@ export async function redditApiRequest( const authRequired = ['profile', 'post', 'postComment'].includes(resource); + qs.api_type = 'json'; + const options: OptionsWithUri = { headers: { 'user-agent': 'n8n', @@ -40,21 +41,32 @@ export async function redditApiRequest( delete options.qs; } - try { - return authRequired - ? await this.helpers.requestOAuth2.call(this, 'redditOAuth2Api', options) - : await this.helpers.request.call(this, options); + if (authRequired) { + let response; - } catch (error) { - if (error.message) { - const errorObject = JSON.parse(error.message.match(/{.*}/)[0]); - throw new Error(`Reddit error response [${errorObject.error}]: ${errorObject.message}`); + try { + response = await this.helpers.requestOAuth2.call(this, 'redditOAuth2Api', options); + } catch (error) { + if (error.response.body && error.response.body.message) { + const message = error.response.body.message; + throw new Error(`Reddit error response [${error.statusCode}]: ${message}`); + } } - throw error; + + if ((response.errors && response.errors.length !== 0) || (response.json && response.json.errors && response.json.errors.length !== 0)) { + const errors = response?.errors || response?.json?.errors; + const errorMessage = errors.map((error: []) => error.join('-')); + + throw new Error(`Reddit error response [400]: ${errorMessage.join('|')}`); + } + + return response; + + } else { + return await this.helpers.request.call(this, options); } } - /** * Make an unauthenticated API request to Reddit and return all results. */ @@ -71,9 +83,13 @@ export async function redditApiRequestAllItems( const resource = this.getNodeParameter('resource', 0) as string; const operation = this.getNodeParameter('operation', 0) as string; + qs.limit = 100; + do { responseData = await redditApiRequest.call(this, method, endpoint, qs); - qs.after = responseData.after; + if (!Array.isArray(responseData)) { + qs.after = responseData.data.after; + } if (endpoint === 'api/search_subreddits.json') { responseData.subreddits.forEach((child: any) => returnData.push(child)); // tslint:disable-line:no-any @@ -83,11 +99,7 @@ export async function redditApiRequestAllItems( 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); + } while (responseData.data && responseData.data.after); return returnData; } @@ -110,9 +122,10 @@ export async function handleListing( if (returnAll) { responseData = await redditApiRequestAllItems.call(this, requestMethod, endpoint, qs); } else { - qs.limit = this.getNodeParameter('limit', i); + const limit = this.getNodeParameter('limit', i); + qs.limit = limit; responseData = await redditApiRequestAllItems.call(this, requestMethod, endpoint, qs); - responseData = responseData.splice(0, qs.limit); + responseData = responseData.slice(0, limit); } return responseData; diff --git a/packages/nodes-base/nodes/Reddit/PostDescription.ts b/packages/nodes-base/nodes/Reddit/PostDescription.ts index 5af1db9651..d98dcdc962 100644 --- a/packages/nodes-base/nodes/Reddit/PostDescription.ts +++ b/packages/nodes-base/nodes/Reddit/PostDescription.ts @@ -80,14 +80,6 @@ export const postFields = [ name: 'Image Post', value: 'image', }, - { - name: 'Video Post', - value: 'video', - }, - { - name: 'Video GIF Post', - value: 'videogif', - }, ], default: 'self', description: 'The kind of the post to create', @@ -138,8 +130,6 @@ export const postFields = [ kind: [ 'link', 'image', - 'video', - 'videogif', ], }, }, @@ -182,8 +172,6 @@ export const postFields = [ kind: [ 'link', 'image', - 'video', - 'videogif', ], }, }, @@ -313,8 +301,8 @@ export const postFields = [ }, }, { - displayName: 'Additional Fields', - name: 'additionalFields', + displayName: 'Filters', + name: 'filters', type: 'collection', displayOptions: { show: { diff --git a/packages/nodes-base/nodes/Reddit/Reddit.node.ts b/packages/nodes-base/nodes/Reddit/Reddit.node.ts index daee96dcc7..d7911d8dba 100644 --- a/packages/nodes-base/nodes/Reddit/Reddit.node.ts +++ b/packages/nodes-base/nodes/Reddit/Reddit.node.ts @@ -152,11 +152,11 @@ export class Reddit implements INodeType { } responseData = await redditApiRequest.call(this, 'POST', 'api/submit', qs); - delete responseData.jquery; - // ---------------------------------- - // post: delete - // ---------------------------------- + responseData = responseData.json.data; + // ---------------------------------- + // post: delete + // ---------------------------------- } else if (operation === 'delete') { @@ -172,9 +172,9 @@ export class Reddit implements INodeType { responseData = { success: true }; - // ---------------------------------- - // post: get - // ---------------------------------- + // ---------------------------------- + // post: get + // ---------------------------------- } else if (operation === 'get') { @@ -185,9 +185,9 @@ export class Reddit implements INodeType { responseData = await redditApiRequest.call(this, 'GET', endpoint, {}); responseData = responseData[0].data.children[0].data; - // ---------------------------------- - // post: getAll - // ---------------------------------- + // ---------------------------------- + // post: getAll + // ---------------------------------- } else if (operation === 'getAll') { @@ -199,7 +199,7 @@ export class Reddit implements INodeType { const subreddit = this.getNodeParameter('subreddit', i); let endpoint = `r/${subreddit}.json`; - const { category } = this.getNodeParameter('additionalFields', i) as { category: string }; + const { category } = this.getNodeParameter('filters', i) as { category: string }; if (category) { endpoint = `r/${subreddit}/${category}.json`; } @@ -208,9 +208,9 @@ export class Reddit implements INodeType { } - // ********************************************************************* - // postComment - // ********************************************************************* + // ********************************************************************* + // postComment + // ********************************************************************* } else if (resource === 'postComment') { @@ -230,7 +230,8 @@ export class Reddit implements INodeType { }; responseData = await redditApiRequest.call(this, 'POST', 'api/comment', qs); - delete responseData.jquery; + + responseData = responseData.data.things; } else if (operation === 'getAll') { @@ -268,13 +269,13 @@ export class Reddit implements INodeType { }; responseData = await redditApiRequest.call(this, 'POST', 'api/comment', qs); - delete responseData.jquery; + responseData = responseData.json.data.things; } - // ********************************************************************* - // profile - // ********************************************************************* + // ********************************************************************* + // profile + // ********************************************************************* } else if (resource === 'profile') { @@ -312,12 +313,11 @@ export class Reddit implements INodeType { } else if (details === 'trophies') { responseData = responseData.data.trophies.map((trophy: IDataObject) => trophy.data); } - } - // ********************************************************************* - // subreddit - // ********************************************************************* + // ********************************************************************* + // subreddit + // ********************************************************************* } else if (resource === 'subreddit') { @@ -329,7 +329,6 @@ export class Reddit implements INodeType { // https://www.reddit.com/dev/api/#GET_r_{subreddit}_about // https://www.reddit.com/dev/api/#GET_r_{subreddit}_about_rules - // https://www.reddit.com/dev/api/#GET_sticky const subreddit = this.getNodeParameter('subreddit', i); const content = this.getNodeParameter('content', i) as string; @@ -341,13 +340,11 @@ export class Reddit implements INodeType { responseData = responseData.rules; } else if (content === 'about') { responseData = responseData.data; - } else if (content === 'sticky') { - responseData = responseData.map((item: any) => item.data.children[0].data); // tslint:disable-line:no-any } - // ---------------------------------- - // subreddit: getAll - // ---------------------------------- + // ---------------------------------- + // subreddit: getAll + // ---------------------------------- } else if (operation === 'getAll') { @@ -358,10 +355,14 @@ export class Reddit implements INodeType { const filters = this.getNodeParameter('filters', i) as IDataObject; if (filters.trending) { - + const returnAll = this.getNodeParameter('returnAll', 0) as boolean; const endpoint = 'api/trending_subreddits.json'; responseData = await redditApiRequest.call(this, 'GET', endpoint, {}); responseData = responseData.subreddit_names.map((name: string) => ({ name })); + if (returnAll === false) { + const limit = this.getNodeParameter('limit', 0) as number; + responseData = responseData.splice(0, limit); + } } else if (filters.keyword) { @@ -378,9 +379,9 @@ export class Reddit implements INodeType { } } - // ********************************************************************* - // user - // ********************************************************************* + // ********************************************************************* + // user + // ********************************************************************* } else if (resource === 'user') { @@ -405,9 +406,7 @@ export class Reddit implements INodeType { } else if (details === 'about') { responseData = responseData.data; } - } - } Array.isArray(responseData) diff --git a/packages/nodes-base/nodes/Reddit/SubredditDescription.ts b/packages/nodes-base/nodes/Reddit/SubredditDescription.ts index 81520a88cb..afaf41f625 100644 --- a/packages/nodes-base/nodes/Reddit/SubredditDescription.ts +++ b/packages/nodes-base/nodes/Reddit/SubredditDescription.ts @@ -51,10 +51,6 @@ export const subredditFields = [ name: 'Rules', value: 'rules', }, - { - name: 'Sticky Posts', - value: 'sticky', - }, ], displayOptions: { show: {