From 0cb190401187fa88afe082e616ab3eddf330d304 Mon Sep 17 00:00:00 2001 From: Jan Oberhauser Date: Fri, 3 Jan 2020 21:47:19 -0600 Subject: [PATCH] :zap: Improve some things on Wordpress-Node --- .../credentials/WordpressApi.credentials.ts | 5 +- .../nodes/Wordpress/GenericFunctions.ts | 33 ++-- .../nodes/Wordpress/PostDescription.ts | 142 +++++++------- .../nodes/Wordpress/UserDescription.ts | 64 +++---- .../nodes/Wordpress/Wordpress.node.ts | 175 ++++++------------ 5 files changed, 176 insertions(+), 243 deletions(-) diff --git a/packages/nodes-base/credentials/WordpressApi.credentials.ts b/packages/nodes-base/credentials/WordpressApi.credentials.ts index abe75d55d..25829ffbd 100644 --- a/packages/nodes-base/credentials/WordpressApi.credentials.ts +++ b/packages/nodes-base/credentials/WordpressApi.credentials.ts @@ -20,10 +20,11 @@ export class WordpressApi implements ICredentialType { default: '', }, { - displayName: 'Domain', - name: 'domain', + displayName: 'Wordpress URL', + name: 'url', type: 'string' as NodePropertyTypes, default: '', + placeholder: 'https://example.com', }, ]; } diff --git a/packages/nodes-base/nodes/Wordpress/GenericFunctions.ts b/packages/nodes-base/nodes/Wordpress/GenericFunctions.ts index 7229766c4..e526017b5 100644 --- a/packages/nodes-base/nodes/Wordpress/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Wordpress/GenericFunctions.ts @@ -1,9 +1,8 @@ import { OptionsWithUri } from 'request'; import { IExecuteFunctions, - ILoadOptionsFunctions, IExecuteSingleFunctions, - BINARY_ENCODING, + ILoadOptionsFunctions, } from 'n8n-core'; import { IDataObject } from 'n8n-workflow'; @@ -12,16 +11,20 @@ export async function wordpressApiRequest(this: IExecuteFunctions | IExecuteSing if (credentials === undefined) { throw new Error('No credentials got returned!'); } - const data = Buffer.from(`${credentials!.username}:${credentials!.password}`).toString(BINARY_ENCODING); - const headerWithAuthentication = Object.assign({}, - { Authorization: `Basic ${data}`, Accept: 'application/json', 'Content-Type': 'application/json' }); let options: OptionsWithUri = { - headers: headerWithAuthentication, + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + }, + auth: { + user: credentials!.username as string, + password: credentials!.password as string, + }, method, qs, body, - uri: uri ||`${credentials!.domain}/wp-json/wp/v2${resource}`, + uri: uri ||`${credentials!.url}/wp-json/wp/v2${resource}`, json: true }; options = Object.assign({}, options, option); @@ -36,7 +39,7 @@ export async function wordpressApiRequest(this: IExecuteFunctions | IExecuteSing errorMessage = error.response.body.message || error.response.body.Message || error.message; } - throw new Error(errorMessage); + throw new Error('Wordpress Error: ' + errorMessage); } } @@ -49,11 +52,9 @@ export async function wordpressApiRequestAllItems(this: IExecuteFunctions | ILoa query.per_page = 10; query.page = 0; - let uri: string | undefined; - do { query.page++; - responseData = await wordpressApiRequest.call(this, method, endpoint, body, query, uri, { resolveWithFullResponse: true }); + responseData = await wordpressApiRequest.call(this, method, endpoint, body, query, undefined, { resolveWithFullResponse: true }); returnData.push.apply(returnData, responseData.body); } while ( responseData.headers['x-wp-totalpages'] !== undefined && @@ -62,13 +63,3 @@ export async function wordpressApiRequestAllItems(this: IExecuteFunctions | ILoa return returnData; } - -export function validateJSON(json: string | undefined): any { // tslint:disable-line:no-any - let result; - try { - result = JSON.parse(json!); - } catch (exception) { - result = undefined; - } - return result; -} diff --git a/packages/nodes-base/nodes/Wordpress/PostDescription.ts b/packages/nodes-base/nodes/Wordpress/PostDescription.ts index 752667748..d14f3b329 100644 --- a/packages/nodes-base/nodes/Wordpress/PostDescription.ts +++ b/packages/nodes-base/nodes/Wordpress/PostDescription.ts @@ -18,11 +18,11 @@ export const postOperations = [ value: 'create', description: 'Create a post', }, - { - name: 'Update', - value: 'update', - description: 'Update a post', - }, + // { + // name: 'Delete', + // value: 'delete', + // description: 'Delete a post', + // }, { name: 'Get', value: 'get', @@ -34,9 +34,9 @@ export const postOperations = [ description: 'Get all posts', }, { - name: 'Delete', - value: 'delete', - description: 'Delete a post', + name: 'Update', + value: 'update', + description: 'Update a post', }, ], default: 'create', @@ -114,27 +114,27 @@ export const postFields = [ type: 'options', options: [ { - name: 'Publish', - value: 'publish' + name: 'Draft', + value: 'draft', }, { name: 'Future', - value: 'future' - }, - { - name: 'Draft', - value: 'draft' + value: 'future', }, { name: 'Pending', - value: 'pending' + value: 'pending', }, { name: 'Private', - value: 'private' + value: 'private', + }, + { + name: 'Publish', + value: 'publish', }, ], - default: '', + default: 'draft', description: 'A named status for the post.', }, { @@ -144,14 +144,14 @@ export const postFields = [ options: [ { name: 'Open', - value: 'open' + value: 'open', }, { name: 'Close', - value: 'closed' + value: 'closed', }, ], - default: '', + default: 'open', description: 'Whether or not comments are open on the post.', }, { @@ -161,15 +161,15 @@ export const postFields = [ options: [ { name: 'Open', - value: 'open' + value: 'open', }, { name: 'Close', - value: 'closed' + value: 'closed', }, ], - default: '', - description: 'Whether or not comments are open on the post.', + default: 'open', + description: 'If the a message should be send to announce the post.', }, { displayName: 'Format', @@ -178,46 +178,46 @@ export const postFields = [ options: [ { name: 'Standard', - value: 'standard' + value: 'standard', }, { name: 'Aside', - value: 'aside' + value: 'aside', }, { name: 'Chat', - value: 'chat' + value: 'chat', }, { name: 'Gallery', - value: 'gallery' + value: 'gallery', }, { name: 'Link', - value: 'link' + value: 'link', }, { name: 'Image', - value: 'image' + value: 'image', }, { name: 'Quote', - value: 'quote' + value: 'quote', }, { name: 'Status', - value: 'status' + value: 'status', }, { name: 'Video', - value: 'video' + value: 'video', }, { name: 'Audio', - value: 'audio' + value: 'audio', }, ], - default: '', + default: 'standard', description: 'Whether or not comments are open on the post.', }, { @@ -325,26 +325,26 @@ export const postFields = [ options: [ { name: 'Publish', - value: 'publish' + value: 'publish', }, { name: 'Future', - value: 'future' + value: 'future', }, { name: 'Draft', - value: 'draft' + value: 'draft', }, { name: 'Pending', - value: 'pending' + value: 'pending', }, { name: 'Private', - value: 'private' + value: 'private', }, ], - default: '', + default: 'draft', description: 'A named status for the post.', }, { @@ -354,14 +354,14 @@ export const postFields = [ options: [ { name: 'Open', - value: 'open' + value: 'open', }, { name: 'Close', - value: 'closed' + value: 'closed', }, ], - default: '', + default: 'open', description: 'Whether or not comments are open on the post.', }, { @@ -371,14 +371,14 @@ export const postFields = [ options: [ { name: 'Open', - value: 'open' + value: 'open', }, { name: 'Close', - value: 'closed' + value: 'closed', }, ], - default: '', + default: 'open', description: 'Whether or not comments are open on the post.', }, { @@ -388,47 +388,47 @@ export const postFields = [ options: [ { name: 'Standard', - value: 'standard' + value: 'standard', }, { name: 'Aside', - value: 'aside' + value: 'aside', }, { name: 'Chat', - value: 'chat' + value: 'chat', }, { name: 'Gallery', - value: 'gallery' + value: 'gallery', }, { name: 'Link', - value: 'link' + value: 'link', }, { name: 'Image', - value: 'image' + value: 'image', }, { name: 'Quote', - value: 'quote' + value: 'quote', }, { name: 'Status', - value: 'status' + value: 'status', }, { name: 'Video', - value: 'video' + value: 'video', }, { name: 'Audio', - value: 'audio' + value: 'audio', }, ], - default: '', - description: 'Whether or not comments are open on the post.', + default: 'standard', + description: 'The format of the post.', }, { displayName: 'Sticky', @@ -572,10 +572,10 @@ export const postFields = [ description: 'How many results to return.', }, { - displayName: 'Filters', - name: 'filters', + displayName: 'Options', + name: 'options', type: 'collection', - placeholder: 'Add Filter', + placeholder: 'Add Option', default: {}, displayOptions: { show: { @@ -606,7 +606,7 @@ export const postFields = [ value: 'edit', }, ], - default: [], + default: 'view', description: 'Scope under which the request is made; determines fields present in response.', }, { @@ -626,10 +626,6 @@ export const postFields = [ name: 'ID', value: 'id', }, - { - name: 'ID', - value: 'id', - }, { name: 'Include', value: 'include', @@ -659,7 +655,7 @@ export const postFields = [ value: 'title', }, ], - default: [], + default: 'id', description: 'Sort collection by object attribute.', }, { @@ -668,12 +664,12 @@ export const postFields = [ type: 'options', options: [ { - name: 'Desc', - value: 'desc', + name: 'ASC', + value: 'asc', }, { - name: 'Asc', - value: 'asc', + name: 'DESC', + value: 'desc', }, ], default: 'desc', diff --git a/packages/nodes-base/nodes/Wordpress/UserDescription.ts b/packages/nodes-base/nodes/Wordpress/UserDescription.ts index 62b1f1b2b..a9d413bf1 100644 --- a/packages/nodes-base/nodes/Wordpress/UserDescription.ts +++ b/packages/nodes-base/nodes/Wordpress/UserDescription.ts @@ -18,11 +18,11 @@ export const userOperations = [ value: 'create', description: 'Create a user', }, - { - name: 'Update', - value: 'update', - description: 'Update a user', - }, + // { + // name: 'Delete', + // value: 'delete', + // description: 'Delete a user', + // }, { name: 'Get', value: 'get', @@ -34,9 +34,9 @@ export const userOperations = [ description: 'Get all users', }, { - name: 'Delete', - value: 'delete', - description: 'Delete a user', + name: 'Update', + value: 'update', + description: 'Update a user', }, ], default: 'create', @@ -86,7 +86,7 @@ export const userFields = [ description: 'Display name for the user.', }, { - displayName: 'Fistname', + displayName: 'First Name', name: 'firstName', type: 'string', required: true, @@ -104,7 +104,7 @@ export const userFields = [ description: 'First name for the user.', }, { - displayName: 'Lastname', + displayName: 'Last Name', name: 'lastName', type: 'string', required: true, @@ -260,14 +260,14 @@ export const userFields = [ description: 'Display name for the user.', }, { - displayName: 'Fistname', + displayName: 'First Name', name: 'firstName', type: 'string', default: '', description: 'First name for the user.', }, { - displayName: 'Lastname', + displayName: 'Last Name', name: 'lastName', type: 'string', default: '', @@ -283,7 +283,7 @@ export const userFields = [ { displayName: 'Password', name: 'password', - type: 'password', + type: 'string', default: '', description: 'Password for the user (never included)', }, @@ -426,10 +426,10 @@ export const userFields = [ description: 'How many results to return.', }, { - displayName: 'Filters', - name: 'filters', + displayName: 'Options', + name: 'options', type: 'collection', - placeholder: 'Add Filter', + placeholder: 'Add Option', default: {}, displayOptions: { show: { @@ -460,7 +460,7 @@ export const userFields = [ value: 'edit', }, ], - default: [], + default: 'view', description: 'Scope under which the request is made; determines fields present in response.', }, { @@ -469,8 +469,8 @@ export const userFields = [ type: 'options', options: [ { - name: 'Name', - value: 'name', + name: 'Email', + value: 'email', }, { name: 'ID', @@ -480,6 +480,14 @@ export const userFields = [ name: 'Include', value: 'include', }, + { + name: 'Include Slugs', + value: 'include_slugs', + }, + { + name: 'Name', + value: 'name', + }, { name: 'Registered Date', value: 'registered_date', @@ -488,20 +496,12 @@ export const userFields = [ name: 'Slug', value: 'slug', }, - { - name: 'Include Slugs', - value: 'include_slugs', - }, - { - name: 'Email', - value: 'email', - }, { name: 'URL', value: 'url', }, ], - default: [], + default: 'id', description: 'Sort collection by object attribute.', }, { @@ -510,12 +510,12 @@ export const userFields = [ type: 'options', options: [ { - name: 'Desc', - value: 'desc', + name: 'ASC', + value: 'asc', }, { - name: 'Asc', - value: 'asc', + name: 'DESC', + value: 'desc', }, ], default: 'desc', diff --git a/packages/nodes-base/nodes/Wordpress/Wordpress.node.ts b/packages/nodes-base/nodes/Wordpress/Wordpress.node.ts index 03735a561..93d4df9de 100644 --- a/packages/nodes-base/nodes/Wordpress/Wordpress.node.ts +++ b/packages/nodes-base/nodes/Wordpress/Wordpress.node.ts @@ -3,10 +3,10 @@ import { } from 'n8n-core'; import { IDataObject, + ILoadOptionsFunctions, INodeTypeDescription, INodeExecutionData, INodeType, - ILoadOptionsFunctions, INodePropertyOptions, } from 'n8n-workflow'; import { @@ -14,12 +14,12 @@ import { wordpressApiRequestAllItems, } from './GenericFunctions'; import { - postOperations, postFields, + postOperations, } from './PostDescription'; import { - userOperations, userFields, + userOperations, } from './UserDescription'; import { IPost, @@ -31,7 +31,7 @@ import { export class Wordpress implements INodeType { description: INodeTypeDescription = { displayName: 'Wordpress', - name: 'Wordpress', + name: 'wordpress', icon: 'file:wordpress.png', group: ['output'], version: 1, @@ -39,7 +39,7 @@ export class Wordpress implements INodeType { description: 'Consume Wordpress API', defaults: { name: 'Wordpress', - color: '#c02428', + color: '#016087', }, inputs: ['main'], outputs: ['main'], @@ -82,12 +82,7 @@ export class Wordpress implements INodeType { // select them easily async getCategories(this: ILoadOptionsFunctions): Promise { const returnData: INodePropertyOptions[] = []; - let categories; - try { - categories = await wordpressApiRequestAllItems.call(this, 'GET', '/categories', {}); - } catch (err) { - throw new Error(`Wordpress Error: ${err}`); - } + const categories = await wordpressApiRequestAllItems.call(this, 'GET', '/categories', {}); for (const category of categories) { const categoryName = category.name; const categoryId = category.id; @@ -103,12 +98,7 @@ export class Wordpress implements INodeType { // select them easily async getTags(this: ILoadOptionsFunctions): Promise { const returnData: INodePropertyOptions[] = []; - let tags; - try { - tags = await wordpressApiRequestAllItems.call(this, 'GET', '/tags', {}); - } catch (err) { - throw new Error(`Wordpress Error: ${err}`); - } + const tags = await wordpressApiRequestAllItems.call(this, 'GET', '/tags', {}); for (const tag of tags) { const tagName = tag.name; const tagId = tag.id; @@ -124,12 +114,7 @@ export class Wordpress implements INodeType { // select them easily async getAuthors(this: ILoadOptionsFunctions): Promise { const returnData: INodePropertyOptions[] = []; - let authors; - try { - authors = await wordpressApiRequestAllItems.call(this, 'GET', '/users', {}, { who: 'authors' }); - } catch (err) { - throw new Error(`Wordpress Error: ${err}`); - } + const authors = await wordpressApiRequestAllItems.call(this, 'GET', '/users', {}, { who: 'authors' }); for (const author of authors) { const authorName = author.name; const authorId = author.id; @@ -192,11 +177,7 @@ export class Wordpress implements INodeType { if (additionalFields.format) { body.format = additionalFields.format as string; } - try{ - responseData = await wordpressApiRequest.call(this, 'POST', '/posts', body); - } catch (err) { - throw new Error(`Wordpress Error: ${err.message}`); - } + responseData = await wordpressApiRequest.call(this, 'POST', '/posts', body); } //https://developer.wordpress.org/rest-api/reference/posts/#update-a-post if (operation === 'update') { @@ -238,11 +219,7 @@ export class Wordpress implements INodeType { if (updateFields.format) { body.format = updateFields.format as string; } - try { - responseData = await wordpressApiRequest.call(this, 'POST', `/posts/${postId}`, body); - } catch (err) { - throw new Error(`Wordpress Error: ${err.message}`); - } + responseData = await wordpressApiRequest.call(this, 'POST', `/posts/${postId}`, body); } //https://developer.wordpress.org/rest-api/reference/posts/#retrieve-a-post if (operation === 'get') { @@ -254,58 +231,50 @@ export class Wordpress implements INodeType { if (options.context) { qs.context = options.context as string; } - try { - responseData = await wordpressApiRequest.call(this,'GET', `/posts/${postId}`, {}, qs); - } catch (err) { - throw new Error(`Wordpress Error: ${err.message}`); - } + responseData = await wordpressApiRequest.call(this,'GET', `/posts/${postId}`, {}, qs); } //https://developer.wordpress.org/rest-api/reference/posts/#list-posts if (operation === 'getAll') { const returnAll = this.getNodeParameter('returnAll', i) as boolean; - const filters = this.getNodeParameter('filters', i) as IDataObject; - if (filters.context) { - qs.context = filters.context as string; + const options = this.getNodeParameter('options', i) as IDataObject; + if (options.context) { + qs.context = options.context as string; } - if (filters.orderBy) { - qs.orderby = filters.orderBy as string; + if (options.orderBy) { + qs.orderby = options.orderBy as string; } - if (filters.order) { - qs.order = filters.order as string; + if (options.order) { + qs.order = options.order as string; } - if (filters.search) { - qs.search = filters.search as string; + if (options.search) { + qs.search = options.search as string; } - if (filters.after) { - qs.after = filters.after as string; + if (options.after) { + qs.after = options.after as string; } - if (filters.author) { - qs.author = filters.author as number[]; + if (options.author) { + qs.author = options.author as number[]; } - if (filters.categories) { - qs.categories = filters.categories as number[]; + if (options.categories) { + qs.categories = options.categories as number[]; } - if (filters.excludedCategories) { - qs.categories_exclude = filters.excludedCategories as number[]; + if (options.excludedCategories) { + qs.categories_exclude = options.excludedCategories as number[]; } - if (filters.tags) { - qs.tags = filters.tags as number[]; + if (options.tags) { + qs.tags = options.tags as number[]; } - if (filters.excludedTags) { - qs.tags_exclude = filters.excludedTags as number[]; + if (options.excludedTags) { + qs.tags_exclude = options.excludedTags as number[]; } - if (filters.sticky) { - qs.sticky = filters.sticky as boolean; + if (options.sticky) { + qs.sticky = options.sticky as boolean; } - try { - if (returnAll === true) { - responseData = await wordpressApiRequestAllItems.call(this, 'GET', '/posts', {}, qs); - } else { - qs.per_page = this.getNodeParameter('limit', i) as number; - responseData = await wordpressApiRequest.call(this, 'GET', '/posts', {}, qs); - } - } catch (err) { - throw new Error(`Wordpress Error: ${err.message}`); + if (returnAll === true) { + responseData = await wordpressApiRequestAllItems.call(this, 'GET', '/posts', {}, qs); + } else { + qs.per_page = this.getNodeParameter('limit', i) as number; + responseData = await wordpressApiRequest.call(this, 'GET', '/posts', {}, qs); } } //https://developer.wordpress.org/rest-api/reference/posts/#delete-a-post @@ -315,11 +284,7 @@ export class Wordpress implements INodeType { if (options.force) { qs.force = options.force as boolean; } - try { - responseData = await wordpressApiRequest.call(this, 'DELETE', `/posts/${postId}`, {}, qs); - } catch (err) { - throw new Error(`Wordpress Error: ${err.message}`); - } + responseData = await wordpressApiRequest.call(this, 'DELETE', `/posts/${postId}`, {}, qs); } } if (resource === 'user') { @@ -352,11 +317,7 @@ export class Wordpress implements INodeType { if (additionalFields.slug) { body.slug = additionalFields.slug as string; } - try{ - responseData = await wordpressApiRequest.call(this, 'POST', '/users', body); - } catch (err) { - throw new Error(`Wordpress Error: ${err.message}`); - } + responseData = await wordpressApiRequest.call(this, 'POST', '/users', body); } //https://developer.wordpress.org/rest-api/reference/users/#update-a-user if (operation === 'update') { @@ -395,11 +356,7 @@ export class Wordpress implements INodeType { if (updateFields.slug) { body.slug = updateFields.slug as string; } - try{ - responseData = await wordpressApiRequest.call(this, 'POST', `/users/${userId}`, body); - } catch (err) { - throw new Error(`Wordpress Error: ${err.message}`); - } + responseData = await wordpressApiRequest.call(this, 'POST', `/users/${userId}`, body); } //https://developer.wordpress.org/rest-api/reference/users/#retrieve-a-user if (operation === 'get') { @@ -408,40 +365,32 @@ export class Wordpress implements INodeType { if (options.context) { qs.context = options.context as string; } - try { - responseData = await wordpressApiRequest.call(this,'GET', `/users/${userId}`, {}, qs); - } catch (err) { - throw new Error(`Wordpress Error: ${err.message}`); - } + responseData = await wordpressApiRequest.call(this,'GET', `/users/${userId}`, {}, qs); } //https://developer.wordpress.org/rest-api/reference/users/#list-users if (operation === 'getAll') { const returnAll = this.getNodeParameter('returnAll', i) as boolean; - const filters = this.getNodeParameter('filters', i) as IDataObject; - if (filters.context) { - qs.context = filters.context as string; + const options = this.getNodeParameter('options', i) as IDataObject; + if (options.context) { + qs.context = options.context as string; } - if (filters.orderBy) { - qs.orderby = filters.orderBy as string; + if (options.orderBy) { + qs.orderby = options.orderBy as string; } - if (filters.order) { - qs.order = filters.order as string; + if (options.order) { + qs.order = options.order as string; } - if (filters.search) { - qs.search = filters.search as string; + if (options.search) { + qs.search = options.search as string; } - if (filters.who) { - qs.who = filters.who as string; + if (options.who) { + qs.who = options.who as string; } - try { - if (returnAll === true) { - responseData = await wordpressApiRequestAllItems.call(this, 'GET', '/users', {}, qs); - } else { - qs.per_page = this.getNodeParameter('limit', i) as number; - responseData = await wordpressApiRequest.call(this, 'GET', '/users', {}, qs); - } - } catch (err) { - throw new Error(`Wordpress Error: ${err.message}`); + if (returnAll === true) { + responseData = await wordpressApiRequestAllItems.call(this, 'GET', '/users', {}, qs); + } else { + qs.per_page = this.getNodeParameter('limit', i) as number; + responseData = await wordpressApiRequest.call(this, 'GET', '/users', {}, qs); } } //https://developer.wordpress.org/rest-api/reference/users/#delete-a-user @@ -449,11 +398,7 @@ export class Wordpress implements INodeType { const reassign = this.getNodeParameter('reassign', i) as string; qs.reassign = reassign; qs.force = true; - try { - responseData = await wordpressApiRequest.call(this, 'DELETE', `/users/me`, {}, qs); - } catch (err) { - throw new Error(`Wordpress Error: ${err.message}`); - } + responseData = await wordpressApiRequest.call(this, 'DELETE', `/users/me`, {}, qs); } } if (Array.isArray(responseData)) {