Some improvements on Disqus-Node

This commit is contained in:
Jan Oberhauser 2020-01-23 14:50:22 -08:00
parent cd04d603ee
commit 5813ec7c3f
2 changed files with 235 additions and 112 deletions

View file

@ -1,5 +1,4 @@
import { import {
BINARY_ENCODING,
IExecuteFunctions, IExecuteFunctions,
} from 'n8n-core'; } from 'n8n-core';
import { import {
@ -42,7 +41,7 @@ export class Disqus implements INodeType {
{ {
name: 'Forum', name: 'Forum',
value: 'forum', value: 'forum',
} },
], ],
default: 'forum', default: 'forum',
description: 'The resource to operate on.', description: 'The resource to operate on.',
@ -127,18 +126,62 @@ export class Disqus implements INodeType {
default: {}, default: {},
options: [ options: [
{ {
displayName: 'attach', displayName: 'Attach',
name: 'attach', name: 'attach',
type: 'string', type: 'multiOptions',
default: '[]', options: [
description: 'Choices: followsForum, forumCanDisableAds, forumForumCategory, counters, forumDaysAlive, forumFeatures, forumIntegration, forumNewPolicy, forumPermissions', {
name: 'counters',
value: 'counters',
},
{
name: 'followsForum',
value: 'followsForum',
},
{
name: 'forumCanDisableAds',
value: 'forumCanDisableAds',
},
{
name: 'forumDaysAlive',
value: 'forumDaysAlive',
},
{
name: 'forumFeatures',
value: 'forumFeatures',
},
{
name: 'forumForumCategory',
value: 'forumForumCategory',
},
{
name: 'forumIntegration',
value: 'forumIntegration',
},
{
name: 'forumNewPolicy',
value: 'forumNewPolicy',
},
{
name: 'forumPermissions',
value: 'forumPermissions',
},
],
default: [],
description: 'The resource to operate on.',
}, },
{ {
displayName: 'related', displayName: 'Related',
name: 'related', name: 'related',
type: 'string', type: 'multiOptions',
default: false, options: [
description: 'You may specify relations to include with your response. Choices `author`', {
name: 'author',
value: 'author',
},
],
default: [],
description: 'You may specify relations to include with your response',
}, },
], ],
}, },
@ -222,61 +265,115 @@ export class Disqus implements INodeType {
}, },
default: {}, default: {},
options: [ options: [
{
displayName: 'Since',
name: 'since',
type: 'string',
default: `[]`,
description: 'Unix timestamp (or ISO datetime standard)',
},
{
displayName: 'Related',
name: 'related',
type: 'string',
default: '[]',
description: 'You may specify relations to include with your response. Choices `author`',
},
{
displayName: 'Cursor',
name: 'cursor',
type: 'string',
default: '',
description: 'You may specify cursor for your response.',
},
{
displayName: 'Limit',
name: 'limit',
type: 'string',
default: 25,
description: 'You may specify relations maximum number of posts to return. Maximum value is 100',
},
{ {
displayName: 'Filters', displayName: 'Filters',
name: 'filters', name: 'filters',
type: 'string', type: 'multiOptions',
default: '[]', options: [
description: 'You may specify filters for your response. Choices: `Is_Anonymous`, `Has_Link`, `Has_Low_Rep_Author`, `Has_Bad_Word`, `Is_Flagged`, `No_Issue`, `Is_Toxic`, `Modified_By_Rule`, `Shadow_Banned`, `Has_Media`, `Is_At_Flag_Limit`', {
name: 'Has_Bad_Word',
value: 'Has_Bad_Word',
},
{
name: 'Has_Link',
value: 'Has_Link',
},
{
name: 'Has_Low_Rep_Author',
value: 'Has_Low_Rep_Author',
},
{
name: 'Has_Media',
value: 'Has_Media',
},
{
name: 'Is_Anonymous',
value: 'Is_Anonymous',
},
{
name: 'Is_Flagged',
value: 'Is_Flagged',
},
{
name: 'No_Issue',
value: 'No_Issue',
},
{
name: 'Is_At_Flag_Limit',
value: 'Is_At_Flag_Limit',
},
{
name: 'Is_Toxic',
value: 'Is_Toxic',
},
{
name: 'Modified_By_Rule',
value: 'Modified_By_Rule',
},
{
name: 'Shadow_Banned',
value: 'Shadow_Banned',
},
],
default: [],
description: 'You may specify filters for your response. Choices: , ``, ``, ``',
},
{
displayName: 'Include',
name: 'include',
type: 'multiOptions',
options: [
{
name: 'author',
value: 'author',
},
],
default: [],
description: 'You may specify relations to include with your response.',
},
{
displayName: 'Order',
name: 'order',
type: 'options',
options: [
{
name: 'ASC',
value: 'asc',
},
{
name: 'DESC',
value: 'desc',
}
],
default: 'asc',
description: 'You may specify order to sort your response.',
}, },
{ {
displayName: 'Query', displayName: 'Query',
name: 'query', name: 'query',
type: 'string', type: 'string',
default: '', default: '',
description: 'You may specify query for your response.', description: 'You may specify query forChoices: asc, desc your response.',
}, },
{ {
displayName: 'Include', displayName: 'Related',
name: 'include', name: 'related',
type: 'string', type: 'multiOptions',
default: false, options: [
description: 'You may specify relations to include with your response. Choices `author`', {
name: 'thread',
value: 'thread',
},
],
default: [],
description: 'You may specify relations to include with your response',
}, },
{ {
displayName: 'Order', displayName: 'Since',
name: 'order', name: 'since',
type: 'string', type: 'dateTime',
default: 'asc', default: '',
description: 'You may specify order to sort your response.Choices: asc, desc', description: 'Unix timestamp (or ISO datetime standard)',
}, },
], ],
}, },
@ -360,26 +457,22 @@ export class Disqus implements INodeType {
}, },
default: {}, default: {},
options: [ options: [
{
displayName: 'Since ID',
name: 'sinceId',
type: 'string',
default: `[]`,
description: 'You may specify cursor since_id for your response.',
},
{
displayName: 'Cursor',
name: 'cursor',
type: 'string',
default: '',
description: 'You may specify cursor for your response.',
},
{ {
displayName: 'Order', displayName: 'Order',
name: 'order', name: 'order',
type: 'string', type: 'options',
options: [
{
name: 'ASC',
value: 'asc',
},
{
name: 'DESC',
value: 'desc',
}
],
default: 'asc', default: 'asc',
description: 'You may specify order to sort your response.Choices: asc, desc', description: 'You may specify order to sort your response.',
}, },
], ],
}, },
@ -463,54 +556,74 @@ export class Disqus implements INodeType {
}, },
default: {}, default: {},
options: [ options: [
{
displayName: 'Thread',
name: 'threadId',
type: 'string',
default: '',
description: 'Looks up a thread by ID. You may pass us the "ident" query type instead of an ID by including "forum". You may pass us the "link" query type to filter by URL. You must pass the "forum" if you do not have the Pro API Access addon.',
},
{
displayName: 'Since',
name: 'since',
type: 'string',
default: '',
description: 'Unix timestamp (or ISO datetime standard)',
},
{ {
displayName: 'Related', displayName: 'Related',
name: 'related', name: 'related',
type: 'string', type: 'multiOptions',
default: '[]', options: [
description: 'You may specify relations to include with your response. Choices `author`', {
}, name: 'author',
{ value: 'author',
displayName: 'Cursor', },
name: 'cursor', {
type: 'string', name: 'forum',
default: '', value: 'forum',
description: 'You may specify cursor for your response.', },
}, ],
{ default: [],
displayName: 'Limit', description: 'You may specify relations to include with your response',
name: 'limit',
type: 'string',
default: 25,
description: 'You may specify relations maximum number of posts to return. Maximum value is 100',
}, },
{ {
displayName: 'Include', displayName: 'Include',
name: 'include', name: 'include',
type: 'string', type: 'multiOptions',
default: '', options: [
description: 'You may specify relations to include with your response. Choices: open, closed, killed', {
name: 'closed',
value: 'closed',
},
{
name: 'open',
value: 'open',
},
{
name: 'killed',
value: 'killed',
},
],
default: [],
description: 'You may specify relations to include with your response.',
}, },
{ {
displayName: 'Order', displayName: 'Order',
name: 'order', name: 'order',
type: 'options',
options: [
{
name: 'ASC',
value: 'asc',
},
{
name: 'DESC',
value: 'desc',
}
],
default: 'asc',
description: 'You may specify order to sort your response.',
},
{
displayName: 'Since',
name: 'since',
type: 'dateTime',
default: '',
description: 'Unix timestamp (or ISO datetime standard)',
},
{
displayName: 'Thread',
name: 'threadId',
type: 'string', type: 'string',
default: 'desc', default: '',
description: 'You may specify order to sort your response.Choices: asc, desc', description: 'Looks up a thread by ID. You may pass us the "ident"<br />query type instead of an ID by including "forum". You may<br />pass us the "link" query type to filter by URL. You must pass<br />the "forum" if you do not have the Pro API Access addon.',
}, },
], ],
} }

View file

@ -12,8 +12,8 @@ export async function disqusApiRequest(
method: string, method: string,
qs: IDataObject = {}, qs: IDataObject = {},
uri?: string, uri?: string,
body: any = {}, body: IDataObject = {},
option: IDataObject = {} option: IDataObject = {},
): Promise<any> { // tslint:disable-line:no-any ): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('disqusApi') as IDataObject; const credentials = this.getCredentials('disqusApi') as IDataObject;
@ -22,11 +22,22 @@ export async function disqusApiRequest(
throw new Error('No credentials got returned!'); throw new Error('No credentials got returned!');
} }
// Convert to query string into a format the API can read
const queryStringElements: string[] = [];
for (const key of Object.keys(qs)) {
if (Array.isArray(qs[key])) {
(qs[key] as string[]).forEach(value => {
queryStringElements.push(`${key}=${value}`);
});
} else {
queryStringElements.push(`${key}=${qs[key]}`);
}
}
let options: OptionsWithUri = { let options: OptionsWithUri = {
method, method,
qs,
body, body,
uri: `https://disqus.com/api/3.0/${uri}`, uri: `https://disqus.com/api/3.0/${uri}?${queryStringElements.join('&')}`,
json: true json: true
}; };
@ -35,10 +46,9 @@ export async function disqusApiRequest(
delete options.body; delete options.body;
} }
try { try {
const result = await this.helpers.request!(options) const result = await this.helpers.request!(options);
return result; return result;
} catch (error) { } catch (error) {
console.log(error)
if (error.statusCode === 401) { if (error.statusCode === 401) {
// Return a clear error // Return a clear error
throw new Error('The Disqus credentials are not valid!'); throw new Error('The Disqus credentials are not valid!');
@ -63,8 +73,8 @@ export async function disqusApiRequestAllItems(
method: string, method: string,
qs: IDataObject = {}, qs: IDataObject = {},
uri?: string, uri?: string,
body: any = {}, body: IDataObject = {},
option: IDataObject = {} option: IDataObject = {},
): Promise<any> { // tslint:disable-line:no-any ): Promise<any> { // tslint:disable-line:no-any
const returnData: IDataObject[] = []; const returnData: IDataObject[] = [];