Skip to content

Commit

Permalink
feat(Reddit Node): Add possibility to query saved posts (#3034)
Browse files Browse the repository at this point in the history
* chore: add nvmrc with required node version

* feat: added saved posts to reddit node with credentials on User resource

* Changed Details order

* Fixed lint issue

* Moved saved posts to profile as it only works for the logged in user, This avoids the breaking change

* Removed .nvmrc

* ⚡ Improvements

Co-authored-by: Yassine Fathi <hi@m4tt72.com>
Co-authored-by: ricardo <ricardoespinoza105@gmail.com>
  • Loading branch information
3 people authored Mar 27, 2022
1 parent b5ecccb commit 5ba4c27
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
6 changes: 3 additions & 3 deletions packages/nodes-base/nodes/Reddit/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
} from 'n8n-core';

import {
IDataObject, NodeApiError, NodeOperationError,
IDataObject, JsonObject, NodeApiError, NodeOperationError,
} from 'n8n-workflow';

import {
Expand Down Expand Up @@ -45,15 +45,15 @@ export async function redditApiRequest(
try {
return await this.helpers.requestOAuth2.call(this, 'redditOAuth2Api', options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
throw new NodeApiError(this.getNode(), error as JsonObject);
}

} else {

try {
return await this.helpers.request.call(this, options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
throw new NodeApiError(this.getNode(), error as JsonObject);
}
}
}
Expand Down
52 changes: 52 additions & 0 deletions packages/nodes-base/nodes/Reddit/ProfileDescription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ export const profileFields: INodeProperties[] = [
value: 'prefs',
description: 'Return the settings preferences of the logged-in user',
},
{
name: 'Saved',
value: 'saved',
description: 'Return the saved posts for the user',
},
{
name: 'Trophies',
value: 'trophies',
Expand All @@ -77,4 +82,51 @@ export const profileFields: INodeProperties[] = [
},
},
},
{
displayName: 'Return All',
name: 'returnAll',
type: 'boolean',
default: false,
description: 'Return all results.',
displayOptions: {
show: {
resource: [
'profile',
],
operation: [
'get',
],
details: [
'saved',
],
},
},
},
{
displayName: 'Limit',
name: 'limit',
type: 'number',
default: 100,
description: 'The number of results to return.',
typeOptions: {
minValue: 1,
maxValue: 100,
},
displayOptions: {
show: {
resource: [
'profile',
],
operation: [
'get',
],
details: [
'saved',
],
returnAll: [
false,
],
},
},
},
];
10 changes: 9 additions & 1 deletion packages/nodes-base/nodes/Reddit/Reddit.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,15 @@ export class Reddit implements INodeType {

const details = this.getNodeParameter('details', i) as string;
const endpoint = `api/v1/${endpoints[details]}`;
responseData = await redditApiRequest.call(this, 'GET', endpoint, {});
let username;

if (details === 'saved') {
({ name: username } = await redditApiRequest.call(this, 'GET', `api/v1/me`, {}));
}

responseData = details === 'saved'
? await handleListing.call(this, i, `user/${username}/saved.json`)
: await redditApiRequest.call(this, 'GET', endpoint, {});

if (details === 'identity') {
responseData = responseData.features;
Expand Down

0 comments on commit 5ba4c27

Please sign in to comment.