n8n/packages/nodes-base/nodes/Reddit/Reddit.node.ts

186 lines
4.2 KiB
TypeScript
Raw Normal View History

2021-01-15 13:06:17 -08:00
import {
IExecuteFunctions,
} from 'n8n-core';
import {
IDataObject,
INodeExecutionData,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import {
redditApiRequest,
redditApiRequestAllItems,
} from './GenericFunctions';
2021-01-17 12:18:17 -08:00
import {
listingFields,
listingOperations,
} from './ListingDescription';
import {
myAccountFields,
myAccountOperations,
} from './MyAccountDescription';
import {
submissionFields,
submissionOperations,
} from './SubmissionDescription';
2021-01-15 13:06:17 -08:00
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: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
options: [
{
2021-01-15 16:20:37 -08:00
name: 'My Account',
value: 'myAccount',
},
{
name: 'Submission',
value: 'submission',
2021-01-15 13:06:17 -08:00
},
2021-01-17 10:01:18 -08:00
{
name: 'Listing',
value: 'listing',
},
2021-01-15 13:06:17 -08:00
],
2021-01-15 16:20:37 -08:00
default: 'myAccount',
2021-01-15 13:06:17 -08:00
description: 'Resource to consume',
},
// myAccount
...myAccountOperations,
...myAccountFields,
2021-01-15 16:20:37 -08:00
// submission
...submissionOperations,
2021-01-17 10:01:18 -08:00
...submissionFields,
// listing
...listingOperations,
...listingFields,
2021-01-15 13:06:17 -08:00
],
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
let responseData;
const returnData: IDataObject[] = [];
2021-01-15 13:06:17 -08:00
for (let i = 0; i < items.length; i++) {
2021-01-15 16:20:37 -08:00
if (resource === 'myAccount') {
2021-01-15 13:06:17 -08:00
if (operation === 'get') {
const endpoints: {[key: string]: string} = {
identity: 'me',
blockedUsers: 'me/blocked',
friends: 'me/friends',
karma: 'me/karma',
prefs: 'me/prefs',
trophies: 'me/trophies',
};
2021-01-17 12:18:17 -08:00
const details = this.getNodeParameter('details', i) as string;
2021-01-17 11:17:17 -08:00
responseData = await redditApiRequest.call(this, 'GET', endpoints[details], {}, {}, false);
if (details === 'identity') {
responseData = responseData.features;
}
2021-01-15 13:06:17 -08:00
}
2021-01-17 10:01:18 -08:00
} else if (resource === 'submission') {
2021-01-15 16:20:37 -08:00
2021-01-17 10:01:18 -08:00
if (operation === 'post') {
2021-01-15 16:20:37 -08:00
const body: IDataObject = {
title: this.getNodeParameter('title', i),
sr: this.getNodeParameter('subreddit', i),
kind: this.getNodeParameter('kind', i),
};
body.kind === 'self'
? body.text = this.getNodeParameter('text', i)
: body.url = this.getNodeParameter('url', i);
const resubmit = this.getNodeParameter('resubmit', i);
if (resubmit) {
body.resubmit = true;
}
2021-01-17 11:17:17 -08:00
responseData = await redditApiRequest.call(this, 'POST', 'submit', {}, body, false);
2021-01-15 16:20:37 -08:00
}
2021-01-17 10:01:18 -08:00
} else if (resource === 'listing') {
if (operation === 'get') {
const type = this.getNodeParameter('type', i) as string;
if (type === 'trending') {
const endpoint = 'api/trending_subreddits.json';
responseData = await redditApiRequest.call(this, 'GET', endpoint, {}, {}, true);
2021-01-17 11:48:50 -08:00
} else {
const endpoint = type === 'best'
? 'best.json'
: `r/${this.getNodeParameter('subreddit', i)}/${type}.json`;
2021-01-17 10:01:18 -08:00
2021-01-17 11:17:17 -08:00
const returnAll = this.getNodeParameter('returnAll', i);
if (returnAll) {
responseData = await redditApiRequestAllItems.call(this, 'GET', endpoint, {}, {}, true);
} else {
2021-01-17 11:48:50 -08:00
const qs: IDataObject = { limit: this.getNodeParameter('limit', i) };
2021-01-17 11:17:17 -08:00
responseData = await redditApiRequestAllItems.call(this, 'GET', endpoint, qs, {}, true);
responseData = responseData.splice(0, qs.limit);
}
2021-01-17 10:01:18 -08:00
2021-01-17 11:48:50 -08:00
}
2021-01-17 10:01:18 -08:00
}
2021-01-15 13:06:17 -08:00
}
Array.isArray(responseData)
? returnData.push(...responseData)
: returnData.push(responseData);
2021-01-15 13:06:17 -08:00
}
return [this.helpers.returnJsonArray(returnData)];
}
}