Refactor subreddit:getAll

This commit is contained in:
Iván Ovejero 2021-01-29 18:22:57 -03:00
parent 2918e01c18
commit 19d11664e6
4 changed files with 27 additions and 53 deletions

View file

@ -8,13 +8,9 @@ const scopes = [
'edit',
'history',
'mysubreddits',
'privatemessages',
'read',
'report',
'save',
'submit',
'subscribe',
'vote',
];
// https://github.com/reddit-archive/reddit/wiki/OAuth2

View file

@ -98,25 +98,12 @@ export async function handleListing(
this: IExecuteFunctions,
i: number,
endpoint: string,
qs: IDataObject = {},
requestMethod: 'GET' | 'POST' = 'GET',
): Promise<any> { // tslint:disable-line:no-any
let responseData;
const resource = this.getNodeParameter('resource', 0);
const operation = this.getNodeParameter('operation', 0);
const qs: IDataObject = {};
let requestMethod = 'GET';
if (resource === 'subreddit' && operation === 'getAll') {
const filters = this.getNodeParameter('filters', i) as IDataObject;
if (filters.keyword) {
qs.query = filters.keyword;
}
requestMethod = 'POST';
}
const returnAll = this.getNodeParameter('returnAll', i);
if (returnAll) {

View file

@ -75,10 +75,6 @@ export class Reddit implements INodeType {
name: 'resource',
type: 'options',
options: [
{
name: 'Comment',
value: 'comment',
},
{
name: 'Post',
value: 'post',
@ -274,7 +270,6 @@ export class Reddit implements INodeType {
}
// *********************************************************************
// profile
// *********************************************************************
@ -350,14 +345,27 @@ export class Reddit implements INodeType {
// https://www.reddit.com/dev/api/#GET_api_trending_subreddits
// https://www.reddit.com/dev/api/#POST_api_search_subreddits
// https://www.reddit.com/r/subreddits.json
const trending = this.getNodeParameter('trending', i) as IDataObject;
const filters = this.getNodeParameter('filters', i) as IDataObject;
if (filters.trending) {
if (trending) {
const endpoint = 'api/trending_subreddits.json';
responseData = await redditApiRequest.call(this, 'GET', endpoint, {});
} else {
responseData = responseData.subreddit_names.map((name: string) => ({ name }));
} else if (filters.keyword) {
const qs: IDataObject = {};
const endpoint = 'api/search_subreddits.json';
qs.query = filters.keyword;
responseData = await handleListing.call(this, i, endpoint, qs, 'POST');
} else {
const endpoint = 'r/subreddits.json';
responseData = await handleListing.call(this, i, endpoint);
}
}

View file

@ -13,10 +13,12 @@ export const subredditOperations = [
{
name: 'Get',
value: 'get',
description: 'Retrieve background information about a subreddit.',
},
{
name: 'Get All',
value: 'getAll',
description: 'Retrieve information about subreddits from all of Reddit.',
},
],
displayOptions: {
@ -86,23 +88,6 @@ export const subredditFields = [
// ----------------------------------
// subreddit: getAll
// ----------------------------------
{
displayName: 'Trending',
name: 'trending',
type: 'boolean',
default: false,
description: 'Currently trending subreddits in all of Reddit.',
displayOptions: {
show: {
resource: [
'subreddit',
],
operation: [
'getAll',
],
},
},
},
{
displayName: 'Return All',
name: 'returnAll',
@ -117,9 +102,6 @@ export const subredditFields = [
operation: [
'getAll',
],
trending: [
false,
],
},
},
},
@ -144,9 +126,6 @@ export const subredditFields = [
returnAll: [
false,
],
trending: [
false,
],
},
},
},
@ -164,6 +143,13 @@ export const subredditFields = [
default: '',
description: 'The keyword for the subreddit search.',
},
{
displayName: 'Trending',
name: 'trending',
type: 'boolean',
default: false,
description: 'Currently trending subreddits in all of Reddit.',
},
],
displayOptions: {
show: {
@ -173,9 +159,6 @@ export const subredditFields = [
operation: [
'getAll',
],
trending: [
false,
],
},
},
},