Improvements

This commit is contained in:
ricardo 2021-01-31 17:28:46 -05:00
parent 50b5305a9c
commit 7d99ed0be7
4 changed files with 68 additions and 72 deletions

View file

@ -11,7 +11,6 @@ import {
OptionsWithUri, OptionsWithUri,
} from 'request'; } from 'request';
/** /**
* Make an authenticated or unauthenticated API request to Reddit. * Make an authenticated or unauthenticated API request to Reddit.
*/ */
@ -26,6 +25,8 @@ export async function redditApiRequest(
const authRequired = ['profile', 'post', 'postComment'].includes(resource); const authRequired = ['profile', 'post', 'postComment'].includes(resource);
qs.api_type = 'json';
const options: OptionsWithUri = { const options: OptionsWithUri = {
headers: { headers: {
'user-agent': 'n8n', 'user-agent': 'n8n',
@ -40,20 +41,31 @@ export async function redditApiRequest(
delete options.qs; delete options.qs;
} }
if (authRequired) {
let response;
try { try {
return authRequired response = await this.helpers.requestOAuth2.call(this, 'redditOAuth2Api', options);
? await this.helpers.requestOAuth2.call(this, 'redditOAuth2Api', options)
: await this.helpers.request.call(this, options);
} catch (error) { } catch (error) {
if (error.message) { if (error.response.body && error.response.body.message) {
const errorObject = JSON.parse(error.message.match(/{.*}/)[0]); const message = error.response.body.message;
throw new Error(`Reddit error response [${errorObject.error}]: ${errorObject.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. * 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 resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string; const operation = this.getNodeParameter('operation', 0) as string;
qs.limit = 100;
do { do {
responseData = await redditApiRequest.call(this, method, endpoint, qs); 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') { if (endpoint === 'api/search_subreddits.json') {
responseData.subreddits.forEach((child: any) => returnData.push(child)); // tslint:disable-line:no-any 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 responseData.data.children.forEach((child: any) => returnData.push(child.data)); // tslint:disable-line:no-any
} }
if (qs.limit && returnData.length >= qs.limit) { } while (responseData.data && responseData.data.after);
return returnData;
}
} while (responseData.after);
return returnData; return returnData;
} }
@ -110,9 +122,10 @@ export async function handleListing(
if (returnAll) { if (returnAll) {
responseData = await redditApiRequestAllItems.call(this, requestMethod, endpoint, qs); responseData = await redditApiRequestAllItems.call(this, requestMethod, endpoint, qs);
} else { } 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 = await redditApiRequestAllItems.call(this, requestMethod, endpoint, qs);
responseData = responseData.splice(0, qs.limit); responseData = responseData.slice(0, limit);
} }
return responseData; return responseData;

View file

@ -80,14 +80,6 @@ export const postFields = [
name: 'Image Post', name: 'Image Post',
value: 'image', value: 'image',
}, },
{
name: 'Video Post',
value: 'video',
},
{
name: 'Video GIF Post',
value: 'videogif',
},
], ],
default: 'self', default: 'self',
description: 'The kind of the post to create', description: 'The kind of the post to create',
@ -138,8 +130,6 @@ export const postFields = [
kind: [ kind: [
'link', 'link',
'image', 'image',
'video',
'videogif',
], ],
}, },
}, },
@ -182,8 +172,6 @@ export const postFields = [
kind: [ kind: [
'link', 'link',
'image', 'image',
'video',
'videogif',
], ],
}, },
}, },
@ -313,8 +301,8 @@ export const postFields = [
}, },
}, },
{ {
displayName: 'Additional Fields', displayName: 'Filters',
name: 'additionalFields', name: 'filters',
type: 'collection', type: 'collection',
displayOptions: { displayOptions: {
show: { show: {

View file

@ -152,8 +152,8 @@ export class Reddit implements INodeType {
} }
responseData = await redditApiRequest.call(this, 'POST', 'api/submit', qs); responseData = await redditApiRequest.call(this, 'POST', 'api/submit', qs);
delete responseData.jquery;
responseData = responseData.json.data;
// ---------------------------------- // ----------------------------------
// post: delete // post: delete
// ---------------------------------- // ----------------------------------
@ -199,7 +199,7 @@ export class Reddit implements INodeType {
const subreddit = this.getNodeParameter('subreddit', i); const subreddit = this.getNodeParameter('subreddit', i);
let endpoint = `r/${subreddit}.json`; 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) { if (category) {
endpoint = `r/${subreddit}/${category}.json`; endpoint = `r/${subreddit}/${category}.json`;
} }
@ -230,7 +230,8 @@ export class Reddit implements INodeType {
}; };
responseData = await redditApiRequest.call(this, 'POST', 'api/comment', qs); responseData = await redditApiRequest.call(this, 'POST', 'api/comment', qs);
delete responseData.jquery;
responseData = responseData.data.things;
} else if (operation === 'getAll') { } else if (operation === 'getAll') {
@ -268,8 +269,8 @@ export class Reddit implements INodeType {
}; };
responseData = await redditApiRequest.call(this, 'POST', 'api/comment', qs); responseData = await redditApiRequest.call(this, 'POST', 'api/comment', qs);
delete responseData.jquery;
responseData = responseData.json.data.things;
} }
// ********************************************************************* // *********************************************************************
@ -312,7 +313,6 @@ export class Reddit implements INodeType {
} else if (details === 'trophies') { } else if (details === 'trophies') {
responseData = responseData.data.trophies.map((trophy: IDataObject) => trophy.data); responseData = responseData.data.trophies.map((trophy: IDataObject) => trophy.data);
} }
} }
// ********************************************************************* // *********************************************************************
@ -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
// https://www.reddit.com/dev/api/#GET_r_{subreddit}_about_rules // 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 subreddit = this.getNodeParameter('subreddit', i);
const content = this.getNodeParameter('content', i) as string; const content = this.getNodeParameter('content', i) as string;
@ -341,8 +340,6 @@ export class Reddit implements INodeType {
responseData = responseData.rules; responseData = responseData.rules;
} else if (content === 'about') { } else if (content === 'about') {
responseData = responseData.data; responseData = responseData.data;
} else if (content === 'sticky') {
responseData = responseData.map((item: any) => item.data.children[0].data); // tslint:disable-line:no-any
} }
// ---------------------------------- // ----------------------------------
@ -358,10 +355,14 @@ export class Reddit implements INodeType {
const filters = this.getNodeParameter('filters', i) as IDataObject; const filters = this.getNodeParameter('filters', i) as IDataObject;
if (filters.trending) { if (filters.trending) {
const returnAll = this.getNodeParameter('returnAll', 0) as boolean;
const endpoint = 'api/trending_subreddits.json'; const endpoint = 'api/trending_subreddits.json';
responseData = await redditApiRequest.call(this, 'GET', endpoint, {}); responseData = await redditApiRequest.call(this, 'GET', endpoint, {});
responseData = responseData.subreddit_names.map((name: string) => ({ name })); 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) { } else if (filters.keyword) {
@ -405,9 +406,7 @@ export class Reddit implements INodeType {
} else if (details === 'about') { } else if (details === 'about') {
responseData = responseData.data; responseData = responseData.data;
} }
} }
} }
Array.isArray(responseData) Array.isArray(responseData)

View file

@ -51,10 +51,6 @@ export const subredditFields = [
name: 'Rules', name: 'Rules',
value: 'rules', value: 'rules',
}, },
{
name: 'Sticky Posts',
value: 'sticky',
},
], ],
displayOptions: { displayOptions: {
show: { show: {