diff --git a/packages/nodes-base/credentials/RedditOAuth2Api.credentials.ts b/packages/nodes-base/credentials/RedditOAuth2Api.credentials.ts new file mode 100644 index 0000000000..b10b1bccf3 --- /dev/null +++ b/packages/nodes-base/credentials/RedditOAuth2Api.credentials.ts @@ -0,0 +1,75 @@ +import { + ICredentialType, + NodePropertyTypes, +} from 'n8n-workflow'; + +const scopes = [ + 'identity', + 'edit', + 'flair', + 'history', + 'modconfig', + 'modflair', + 'modlog', + 'modposts', + 'modwiki', + 'mysubreddits', + 'privatemessages', + 'read', + 'report', + 'save', + 'submit', + 'subscribe', + 'vote', + 'wikiedit', + 'wikiread', +]; + +// https://github.com/reddit-archive/reddit/wiki/OAuth2 + +export class RedditOAuth2Api implements ICredentialType { + name = 'redditOAuth2Api'; + extends = [ + 'oAuth2Api', + ]; + displayName = 'Reddit OAuth2 API'; + documentationUrl = 'reddit'; + properties = [ + { + displayName: 'Auth URI Query Parameters', + name: 'authQueryParameters', + type: 'hidden' as NodePropertyTypes, + default: 'response_type=code', + }, + { + displayName: 'Auth URI Query Parameters', + name: 'authQueryParameters', + type: 'hidden' as NodePropertyTypes, + default: 'duration=permanent', + }, + { + displayName: 'Authorization URL', + name: 'authUrl', + type: 'hidden' as NodePropertyTypes, + default: 'https://www.reddit.com/api/v1/authorize', + }, + { + displayName: 'Access Token URL', + name: 'accessTokenUrl', + type: 'hidden' as NodePropertyTypes, + default: 'https://www.reddit.com/api/v1/access_token', + }, + { + displayName: 'Scope', + name: 'scope', + type: 'hidden' as NodePropertyTypes, + default: scopes.join(' '), + }, + { + displayName: 'Authentication', + name: 'authentication', + type: 'hidden' as NodePropertyTypes, + default: 'header', + }, + ]; +} diff --git a/packages/nodes-base/nodes/Reddit/GenericFunctions.ts b/packages/nodes-base/nodes/Reddit/GenericFunctions.ts new file mode 100644 index 0000000000..3e204519f7 --- /dev/null +++ b/packages/nodes-base/nodes/Reddit/GenericFunctions.ts @@ -0,0 +1,71 @@ +import { + IExecuteFunctions, + IHookFunctions, +} from 'n8n-core'; + +import { + IDataObject, +} from 'n8n-workflow'; + +import { + OptionsWithUri, +} from 'request'; + + +/** + * Make an API request to Reddit + * + * @param { IHookFunctions } this + * @param { string } method + * @param { string } endpoint + * @param { IDataObject } qs + * @returns { Promise } + */ +export async function redditApiRequest( + this: IHookFunctions | IExecuteFunctions, + method: string, + endpoint: string, + qs?: IDataObject, +): Promise { // tslint:disable-line:no-any + const options: OptionsWithUri = { + method, + qs, + uri: `https://www.reddit.com/api/v1/${endpoint}`, + json: true, + }; + + if (options.qs === undefined) { + delete options.qs; + } + + console.log(options); + + try { + return await this.helpers.request!(options); + } catch (error) { + + // ... + 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 } + */ +export async function redditApiRequestAllItems( + this: IHookFunctions | IExecuteFunctions, + method: string, + endpoint: string, + qs: IDataObject, +): Promise { // tslint:disable-line:no-any + + // ... +} diff --git a/packages/nodes-base/nodes/Reddit/Reddit.node.ts b/packages/nodes-base/nodes/Reddit/Reddit.node.ts new file mode 100644 index 0000000000..26b87fc4de --- /dev/null +++ b/packages/nodes-base/nodes/Reddit/Reddit.node.ts @@ -0,0 +1,107 @@ +import { + IExecuteFunctions, +} from 'n8n-core'; + +import { + IDataObject, + INodeExecutionData, + INodeType, + INodeTypeDescription, +} from 'n8n-workflow'; + +import { + redditApiRequest, + redditApiRequestAllItems, +} from './GenericFunctions'; + +export class Reddit implements INodeType { + description: INodeTypeDescription = { + displayName: 'Reddit', + name: 'reddit', + icon: 'file:reddit.svg', + group: ['transform'], + version: 1, + subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}', + description: 'Consume the Reddit API', + defaults: { + name: 'Reddit', + color: '#ff5700', + }, + inputs: ['main'], + outputs: ['main'], + credentials: [ + { + name: 'redditOAuth2Api', + required: true, + }, + ], + properties: [ + // ---------------------------------- + // Resources + // ---------------------------------- + { + displayName: 'Resource', + name: 'resource', + type: 'options', + options: [ + { + name: 'Account', + value: 'account', + }, + ], + default: 'account', + description: 'Resource to consume', + }, + + // ---------------------------------- + // Operations + // ---------------------------------- + { + displayName: 'Operation', + name: 'operation', + type: 'options', + displayOptions: { + show: { + resource: [ + 'account', + ], + }, + }, + options: [ + { + name: 'Get self', + value: 'getSelf', + description: 'Return the identity of the user', + }, + ], + default: 'getSelf', + description: 'Operation to perform', + }, + ], + }; + + async execute(this: IExecuteFunctions): Promise { + const items = this.getInputData(); + const returnData: IDataObject[] = []; + + const resource = this.getNodeParameter('resource', 0) as string; + const operation = this.getNodeParameter('operation', 0) as string; + + for (let i = 0; i < items.length; i++) { + + if (resource === 'account') { + + if (operation === 'getSelf') { + const requestMethod = 'GET'; + const endpoint = 'me'; + const responseData = await redditApiRequest.call(this, requestMethod, endpoint); + console.log(responseData); + returnData.push(responseData as IDataObject); + + } + } + } + + return [this.helpers.returnJsonArray(returnData)]; + } +} \ No newline at end of file diff --git a/packages/nodes-base/nodes/Reddit/reddit.svg b/packages/nodes-base/nodes/Reddit/reddit.svg new file mode 100644 index 0000000000..507bbb9ed7 --- /dev/null +++ b/packages/nodes-base/nodes/Reddit/reddit.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/packages/nodes-base/package.json b/packages/nodes-base/package.json index 30d8d8dba6..f0c0771754 100644 --- a/packages/nodes-base/package.json +++ b/packages/nodes-base/package.json @@ -176,6 +176,7 @@ "dist/credentials/QuestDb.credentials.js", "dist/credentials/QuickBaseApi.credentials.js", "dist/credentials/RabbitMQ.credentials.js", + "dist/credentials/RedditOAuth2Api.credentials.js", "dist/credentials/Redis.credentials.js", "dist/credentials/RocketchatApi.credentials.js", "dist/credentials/RundeckApi.credentials.js", @@ -419,6 +420,7 @@ "dist/nodes/ReadBinaryFile.node.js", "dist/nodes/ReadBinaryFiles.node.js", "dist/nodes/ReadPdf.node.js", + "dist/nodes/Reddit/Reddit.node.js", "dist/nodes/Redis/Redis.node.js", "dist/nodes/RenameKeys.node.js", "dist/nodes/Rocketchat/Rocketchat.node.js",