n8n/packages/nodes-base/nodes/HackerNews/HackerNews.node.ts
Iván Ovejero 0448feec56
refactor: Apply eslint-plugin-n8n-nodes-base autofixable rules (#3174)
*  Initial setup

* 👕 Update `.eslintignore`

* 👕 Autofix node-param-default-missing (#3173)

* 🔥 Remove duplicate key

* 👕 Add exceptions

* 📦 Update package-lock.json

* 👕 Apply `node-class-description-inputs-wrong-trigger-node` (#3176)

* 👕 Apply `node-class-description-inputs-wrong-regular-node` (#3177)

* 👕 Apply `node-class-description-outputs-wrong` (#3178)

* 👕 Apply `node-execute-block-double-assertion-for-items` (#3179)

* 👕 Apply `node-param-default-wrong-for-collection` (#3180)

* 👕 Apply node-param-default-wrong-for-boolean (#3181)

* Autofixed default missing

* Autofixed booleans, worked well

*  Fix params

*  Undo exempted autofixes

* 📦 Update package-lock.json

* 👕 Apply node-class-description-missing-subtitle (#3182)

*  Fix missing comma

* 👕 Apply `node-param-default-wrong-for-fixed-collection` (#3184)

* 👕 Add exception for `node-class-description-missing-subtitle`

* 👕 Apply `node-param-default-wrong-for-multi-options` (#3185)

* 👕 Apply `node-param-collection-type-unsorted-items` (#3186)

* Missing coma

* 👕 Apply `node-param-default-wrong-for-simplify` (#3187)

* 👕 Apply `node-param-description-comma-separated-hyphen` (#3190)

* 👕 Apply `node-param-description-empty-string` (#3189)

* 👕 Apply `node-param-description-excess-inner-whitespace` (#3191)

* Rule looks good

* Add whitespace rule in eslint config

* :zao: fix

* 👕 Apply `node-param-description-identical-to-display-name` (#3193)

* 👕 Apply `node-param-description-missing-for-ignore-ssl-issues` (#3195)

*  Revert ":zao: fix"

This reverts commit ef8a76f3df.

* 👕 Apply `node-param-description-missing-for-simplify`  (#3196)

* 👕 Apply `node-param-description-missing-final-period` (#3194)

* Rule working as intended

* Add rule to eslint

* 👕 Apply node-param-description-missing-for-return-all (#3197)

*  Restore `lintfix` command

Co-authored-by: agobrech <45268029+agobrech@users.noreply.github.com>
Co-authored-by: Omar Ajoue <krynble@gmail.com>
Co-authored-by: agobrech <ael.gobrecht@gmail.com>
Co-authored-by: Michael Kret <michael.k@radency.com>
2022-04-22 18:29:51 +02:00

391 lines
8.5 KiB
TypeScript

import {
IExecuteFunctions,
} from 'n8n-core';
import {
IDataObject,
INodeExecutionData,
INodeType,
INodeTypeDescription,
NodeOperationError,
} from 'n8n-workflow';
import {
hackerNewsApiRequest,
hackerNewsApiRequestAllItems,
} from './GenericFunctions';
export class HackerNews implements INodeType {
description: INodeTypeDescription = {
displayName: 'Hacker News',
name: 'hackerNews',
icon: 'file:hackernews.png',
group: ['transform'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Consume Hacker News API',
defaults: {
name: 'Hacker News',
},
inputs: ['main'],
outputs: ['main'],
properties: [
// ----------------------------------
// Resources
// ----------------------------------
{
displayName: 'Resource',
name: 'resource',
type: 'options',
options: [
{
name: 'All',
value: 'all',
},
{
name: 'Article',
value: 'article',
},
{
name: 'User',
value: 'user',
},
],
default: 'article',
description: 'Resource to consume.',
},
// ----------------------------------
// Operations
// ----------------------------------
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'all',
],
},
},
options: [
{
name: 'Get All',
value: 'getAll',
description: 'Get all items',
},
],
default: 'getAll',
description: 'Operation to perform.',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'article',
],
},
},
options: [
{
name: 'Get',
value: 'get',
description: 'Get a Hacker News article',
},
],
default: 'get',
description: 'Operation to perform.',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'user',
],
},
},
options: [
{
name: 'Get',
value: 'get',
description: 'Get a Hacker News user',
},
],
default: 'get',
description: 'Operation to perform.',
},
// ----------------------------------
// Fields
// ----------------------------------
{
displayName: 'Article ID',
name: 'articleId',
type: 'string',
required: true,
default: '',
description: 'The ID of the Hacker News article to be returned',
displayOptions: {
show: {
resource: [
'article',
],
operation: [
'get',
],
},
},
},
{
displayName: 'Username',
name: 'username',
type: 'string',
required: true,
default: '',
description: 'The Hacker News user to be returned',
displayOptions: {
show: {
resource: [
'user',
],
operation: [
'get',
],
},
},
},
{
displayName: 'Return All',
name: 'returnAll',
type: 'boolean',
default: false,
description: 'Whether to return all results for the query or only up to a limit.',
displayOptions: {
show: {
resource: [
'all',
],
operation: [
'getAll',
],
},
},
},
{
displayName: 'Limit',
name: 'limit',
type: 'number',
default: 100,
description: 'Limit of Hacker News articles to be returned for the query.',
displayOptions: {
show: {
resource: [
'all',
],
operation: [
'getAll',
],
returnAll: [
false,
],
},
},
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
resource: [
'article',
],
operation: [
'get',
],
},
},
options: [
{
displayName: 'Include comments',
name: 'includeComments',
type: 'boolean',
default: false,
description: 'Whether to include all the comments in a Hacker News article.',
},
],
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
resource: [
'all',
],
operation: [
'getAll',
],
},
},
options: [
{
displayName: 'Keyword',
name: 'keyword',
type: 'string',
default: '',
description: 'The keyword for filtering the results of the query.',
},
{
displayName: 'Tags',
name: 'tags',
type: 'multiOptions',
options: [
{
name: 'Story',
value: 'story',
description: 'Returns query results filtered by story tag',
},
{
name: 'Comment',
value: 'comment',
description: 'Returns query results filtered by comment tag',
},
{
name: 'Poll',
value: 'poll',
description: 'Returns query results filtered by poll tag',
},
{
name: 'Show HN',
value: 'show_hn', // snake case per HN tags
description: 'Returns query results filtered by Show HN tag',
},
{
name: 'Ask HN',
value: 'ask_hn', // snake case per HN tags
description: 'Returns query results filtered by Ask HN tag',
},
{
name: 'Front Page',
value: 'front_page', // snake case per HN tags
description: 'Returns query results filtered by Front Page tag',
},
],
default: [],
description: 'Tags for filtering the results of the query.',
},
],
},
],
};
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;
let returnAll = false;
for (let i = 0; i < items.length; i++) {
try {
let qs: IDataObject = {};
let endpoint = '';
let includeComments = false;
if (resource === 'all') {
if (operation === 'getAll') {
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
const keyword = additionalFields.keyword as string;
const tags = additionalFields.tags as string[];
qs = {
query: keyword,
tags: tags ? tags.join() : '',
};
returnAll = this.getNodeParameter('returnAll', i) as boolean;
if (!returnAll) {
qs.hitsPerPage = this.getNodeParameter('limit', i) as number;
}
endpoint = 'search?';
} else {
throw new NodeOperationError(this.getNode(), `The operation '${operation}' is unknown!`);
}
} else if (resource === 'article') {
if (operation === 'get') {
endpoint = `items/${this.getNodeParameter('articleId', i)}`;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
includeComments = additionalFields.includeComments as boolean;
} else {
throw new NodeOperationError(this.getNode(), `The operation '${operation}' is unknown!`);
}
} else if (resource === 'user') {
if (operation === 'get') {
endpoint = `users/${this.getNodeParameter('username', i)}`;
} else {
throw new NodeOperationError(this.getNode(), `The operation '${operation}' is unknown!`);
}
} else {
throw new NodeOperationError(this.getNode(), `The resource '${resource}' is unknown!`);
}
let responseData;
if (returnAll === true) {
responseData = await hackerNewsApiRequestAllItems.call(this, 'GET', endpoint, qs);
} else {
responseData = await hackerNewsApiRequest.call(this, 'GET', endpoint, qs);
if (resource === 'all' && operation === 'getAll') {
responseData = responseData.hits;
}
}
if (resource === 'article' && operation === 'get' && !includeComments) {
delete responseData.children;
}
if (Array.isArray(responseData)) {
returnData.push.apply(returnData, responseData as IDataObject[]);
} else {
returnData.push(responseData as IDataObject);
}
} catch (error) {
if (this.continueOnFail()) {
returnData.push({ error: error.message });
continue;
}
throw error;
}
}
return [this.helpers.returnJsonArray(returnData)];
}
}