n8n/packages/nodes-base/nodes/Reddit/Reddit.node.ts
2021-01-15 18:07:35 -03:00

107 lines
2.3 KiB
TypeScript

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)];
}
}