Improvements to HackerNews-Node

This commit is contained in:
ricardo 2020-06-24 14:02:17 -04:00
parent 81ef9c6801
commit 86a42f4747
2 changed files with 47 additions and 35 deletions

View file

@ -4,11 +4,12 @@ import {
} from 'n8n-core'; } from 'n8n-core';
import { import {
IDataObject, ILoadOptionsFunctions, IDataObject,
ILoadOptionsFunctions,
} from 'n8n-workflow'; } from 'n8n-workflow';
import { import {
OptionsWithUri OptionsWithUri,
} from 'request'; } from 'request';
@ -23,13 +24,23 @@ import {
*/ */
export async function hackerNewsApiRequest(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions, method: string, endpoint: string, qs: IDataObject): Promise<any> { // tslint:disable-line:no-any export async function hackerNewsApiRequest(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions, method: string, endpoint: string, qs: IDataObject): Promise<any> { // tslint:disable-line:no-any
const options: OptionsWithUri = { const options: OptionsWithUri = {
method: method, method,
qs, qs,
uri: `http://hn.algolia.com/api/v1/${endpoint}`, uri: `http://hn.algolia.com/api/v1/${endpoint}`,
json: true, json: true,
}; };
return await this.helpers.request!(options); try {
return await this.helpers.request!(options);
} catch (error) {
if (error.response && error.response.body && error.response.body.error) {
// Try to return the error prettier
throw new Error(`Hacker News error response [${error.statusCode}]: ${error.response.body.error}`);
}
throw error;
}
} }

View file

@ -1,5 +1,5 @@
import { import {
IExecuteFunctions IExecuteFunctions,
} from 'n8n-core'; } from 'n8n-core';
import { import {
@ -11,7 +11,7 @@ import {
import { import {
hackerNewsApiRequest, hackerNewsApiRequest,
hackerNewsApiRequestAllItems hackerNewsApiRequestAllItems,
} from './GenericFunctions'; } from './GenericFunctions';
export class HackerNews implements INodeType { export class HackerNews implements INodeType {
@ -40,12 +40,12 @@ export class HackerNews implements INodeType {
options: [ options: [
{ {
name: 'Article', name: 'Article',
value: 'article' value: 'article',
}, },
{ {
name: 'User', name: 'User',
value: 'user' value: 'user',
} },
], ],
default: 'article', default: 'article',
description: 'Resource to consume.', description: 'Resource to consume.',
@ -60,7 +60,7 @@ export class HackerNews implements INodeType {
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: [
'article' 'article',
], ],
}, },
}, },
@ -74,7 +74,7 @@ export class HackerNews implements INodeType {
name: 'Get All', name: 'Get All',
value: 'getAll', value: 'getAll',
description: 'Get all Hacker News articles', description: 'Get all Hacker News articles',
} },
], ],
default: 'get', default: 'get',
description: 'Operation to perform.', description: 'Operation to perform.',
@ -86,7 +86,7 @@ export class HackerNews implements INodeType {
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: [
'user' 'user',
], ],
}, },
}, },
@ -95,7 +95,7 @@ export class HackerNews implements INodeType {
name: 'Get', name: 'Get',
value: 'get', value: 'get',
description: 'Get a Hacker News user', description: 'Get a Hacker News user',
} },
], ],
default: 'get', default: 'get',
description: 'Operation to perform.', description: 'Operation to perform.',
@ -113,10 +113,10 @@ export class HackerNews implements INodeType {
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: [
'article' 'article',
], ],
operation: [ operation: [
'get' 'get',
], ],
}, },
}, },
@ -131,10 +131,10 @@ export class HackerNews implements INodeType {
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: [
'user' 'user',
], ],
operation: [ operation: [
'get' 'get',
], ],
}, },
}, },
@ -148,10 +148,10 @@ export class HackerNews implements INodeType {
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: [
'article' 'article',
], ],
operation: [ operation: [
'getAll' 'getAll',
], ],
}, },
}, },
@ -165,13 +165,13 @@ export class HackerNews implements INodeType {
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: [
'article' 'article',
], ],
operation: [ operation: [
'getAll' 'getAll',
], ],
returnAll: [ returnAll: [
false false,
], ],
}, },
}, },
@ -185,10 +185,10 @@ export class HackerNews implements INodeType {
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: [
'article' 'article',
], ],
operation: [ operation: [
'get' 'get',
], ],
}, },
}, },
@ -198,9 +198,9 @@ export class HackerNews implements INodeType {
name: 'includeComments', name: 'includeComments',
type: 'boolean', type: 'boolean',
default: false, default: false,
description: 'Whether to include all the comments in a Hacker News article.' description: 'Whether to include all the comments in a Hacker News article.',
}, },
] ],
}, },
{ {
displayName: 'Additional Fields', displayName: 'Additional Fields',
@ -211,10 +211,10 @@ export class HackerNews implements INodeType {
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: [
'article' 'article',
], ],
operation: [ operation: [
'getAll' 'getAll',
], ],
}, },
}, },
@ -260,14 +260,14 @@ export class HackerNews implements INodeType {
name: 'Front Page', name: 'Front Page',
value: 'front_page', // snake case per HN tags value: 'front_page', // snake case per HN tags
description: 'Returns query results filtered by Front Page tag', description: 'Returns query results filtered by Front Page tag',
} },
], ],
default: '', default: '',
description: 'Tags for filtering the results of the query.', description: 'Tags for filtering the results of the query.',
} },
] ],
} },
] ],
}; };
@ -335,8 +335,9 @@ export class HackerNews implements INodeType {
responseData = await hackerNewsApiRequestAllItems.call(this, 'GET', endpoint, qs); responseData = await hackerNewsApiRequestAllItems.call(this, 'GET', endpoint, qs);
} else { } else {
responseData = await hackerNewsApiRequest.call(this, 'GET', endpoint, qs); responseData = await hackerNewsApiRequest.call(this, 'GET', endpoint, qs);
if (resource === 'article' && operation === 'getAll') if (resource === 'article' && operation === 'getAll') {
responseData = responseData.hits; responseData = responseData.hits;
}
} }
if (resource === 'article' && operation === 'get' && !includeComments) { if (resource === 'article' && operation === 'get' && !includeComments) {