Set up initial scaffolding and auth

This commit is contained in:
Iván Ovejero 2021-01-15 18:06:17 -03:00
parent e525c79032
commit 871fe10d53
5 changed files with 256 additions and 0 deletions

View file

@ -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',
},
];
}

View file

@ -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<any> }
*/
export async function redditApiRequest(
this: IHookFunctions | IExecuteFunctions,
method: string,
endpoint: string,
qs?: IDataObject,
): Promise<any> { // 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<any> }
*/
export async function redditApiRequestAllItems(
this: IHookFunctions | IExecuteFunctions,
method: string,
endpoint: string,
qs: IDataObject,
): Promise<any> { // tslint:disable-line:no-any
// ...
}

View file

@ -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<INodeExecutionData[][]> {
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)];
}
}

View file

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 513 514" fill="#fff" fill-rule="evenodd" stroke="#000" stroke-linecap="round" stroke-linejoin="round"><use xlink:href="#A" x=".5" y=".5"/><symbol id="A" overflow="visible"><g stroke="none" fill-rule="nonzero"><path d="M0 76.8C0 34.253 34.253 0 76.8 0h358.4C477.747 0 512 34.253 512 76.8v358.4c0 42.547-34.253 76.8-76.8 76.8H76.8C34.253 512 0 477.747 0 435.2z" fill="#ff4500"/><path d="M79 305c0-68.142 78.942-123 177-123s177 54.858 177 123-78.942 123-177 123S79 373.142 79 305z"/><g fill="#ff4500"><path d="M199 347c35 29 79 29 114 0l12 11c-42 35-96 35-138 0z"/><use xlink:href="#C"/><use xlink:href="#C" x="-118"/></g></g><g stroke="#fff" fill="none"><use xlink:href="#C" x="75" y="-160" stroke-width="25"/><path d="M87 282c-45-22-5-92 40-50m298 50c45-22 5-92-40-50m-127-45l24-83 80 16" stroke-width="22"/></g></symbol><defs ><path id="C" d="M287 285a27.94 27.94 0 1 1 56 0 27.94 27.94 0 1 1-56 0z"/></defs></svg>

After

Width:  |  Height:  |  Size: 1,010 B

View file

@ -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",