diff --git a/packages/nodes-base/nodes/Wordpress/GenericFunctions.ts b/packages/nodes-base/nodes/Wordpress/GenericFunctions.ts new file mode 100644 index 0000000000..46560b0bc2 --- /dev/null +++ b/packages/nodes-base/nodes/Wordpress/GenericFunctions.ts @@ -0,0 +1,51 @@ +import { OptionsWithUri } from 'request'; +import { + IExecuteFunctions, + ILoadOptionsFunctions, + IExecuteSingleFunctions, + BINARY_ENCODING, +} from 'n8n-core'; +import { IDataObject } from 'n8n-workflow'; + +export async function wordpressApiRequest(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise { // tslint:disable-line:no-any + const credentials = this.getCredentials('wordpressApi'); + 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, + method, + qs, + body, + uri: uri ||`${credentials!.domain}/wp-json/wp/v2${resource}`, + json: true + }; + options = Object.assign({}, options, option); + if (Object.keys(options.body).length === 0) { + delete options.body; + } + try { + return await this.helpers.request!(options); + } catch (error) { + let errorMessage = error.message; + if (error.response.body) { + errorMessage = error.response.body.message || error.response.body.Message || error.message; + } + + throw new Error(errorMessage); + } +} + +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 new file mode 100644 index 0000000000..b07840fd80 --- /dev/null +++ b/packages/nodes-base/nodes/Wordpress/PostDescription.ts @@ -0,0 +1,345 @@ +import { INodeProperties } from 'n8n-workflow'; + +export const postOperations = [ + { + displayName: 'Operation', + name: 'operation', + type: 'options', + displayOptions: { + show: { + resource: [ + 'post', + ], + }, + }, + options: [ + { + name: 'Create', + value: 'create', + description: ``, + }, + ], + default: 'create', + description: 'The operation to perform.', + }, +] as INodeProperties[]; + +export const postFields = [ + +/* -------------------------------------------------------------------------- */ +/* post:create */ +/* -------------------------------------------------------------------------- */ + + { + displayName: 'ID', + name: 'id', + type: 'string', + required: true, + default: '', + displayOptions: { + show: { + resource: [ + 'user', + ], + operation: [ + 'create', + ] + }, + }, + description: 'The unique identifier of the customer', + }, + { + displayName: 'JSON Parameters', + name: 'jsonParameters', + type: 'boolean', + default: false, + description: '', + displayOptions: { + show: { + resource: [ + 'user', + ], + operation: [ + 'create', + ] + }, + } + }, + { + displayName: 'Additional Fields', + name: 'additionalFields', + type: 'collection', + placeholder: 'Add Field', + default: {}, + displayOptions: { + show: { + resource: [ + 'user', + ], + operation: [ + 'create', + ], + }, + }, + options: [ + { + displayName: 'Email', + name: 'email', + type: 'string', + default: '', + description: 'The table to create the row in.', + }, + ] + }, + { + displayName: 'Data', + name: 'dataAttributesUi', + placeholder: 'Add Data', + description: 'key value pairs that represent the custom user properties you want to update', + type: 'fixedCollection', + typeOptions: { + multipleValues: true, + }, + default: {}, + displayOptions: { + show: { + resource: [ + 'user', + ], + operation: [ + 'create', + ], + jsonParameters: [ + false, + ], + }, + }, + options: [ + { + name: 'dataAttributesValues', + displayName: 'Data', + values: [ + { + displayName: 'Key', + name: 'key', + type: 'string', + default: '', + description: 'Name of the property to set.', + }, + { + displayName: 'Value', + name: 'value', + type: 'string', + default: '', + description: 'Value of the property to set.', + }, + ] + }, + ], + }, + { + displayName: 'Data', + name: 'dataAttributesJson', + type: 'json', + default: '', + required: false, + typeOptions: { + alwaysOpenEditWindow: true, + }, + description: 'key value pairs that represent the custom user properties you want to update', + displayOptions: { + show: { + resource: [ + 'user', + ], + operation: [ + 'create', + ], + jsonParameters: [ + true, + ], + }, + }, + }, + +/* -------------------------------------------------------------------------- */ +/* user:alias */ +/* -------------------------------------------------------------------------- */ + { + displayName: 'ID', + name: 'id', + type: 'string', + required: true, + default: '', + displayOptions: { + show: { + resource: [ + 'user', + ], + operation: [ + 'alias', + ] + }, + }, + description: 'The old unique identifier of the user', + }, + { + displayName: 'New ID', + name: 'newId', + type: 'string', + required: true, + default: '', + displayOptions: { + show: { + resource: [ + 'user', + ], + operation: [ + 'alias', + ] + }, + }, + description: 'The new unique identifier of the user', + }, +/* -------------------------------------------------------------------------- */ +/* user:unsubscribe */ +/* -------------------------------------------------------------------------- */ + { + displayName: 'ID', + name: 'id', + type: 'string', + required: true, + default: '', + displayOptions: { + show: { + resource: [ + 'user', + ], + operation: [ + 'unsubscribe', + ] + }, + }, + description: 'The unique identifier of the user', + }, +/* -------------------------------------------------------------------------- */ +/* user:resubscribe */ +/* -------------------------------------------------------------------------- */ + { + displayName: 'ID', + name: 'id', + type: 'string', + required: true, + default: '', + displayOptions: { + show: { + resource: [ + 'user', + ], + operation: [ + 'resubscribe', + ] + }, + }, + description: 'The unique identifier of the user', + }, +/* -------------------------------------------------------------------------- */ +/* user:delete */ +/* -------------------------------------------------------------------------- */ + { + displayName: 'ID', + name: 'id', + type: 'string', + required: true, + default: '', + displayOptions: { + show: { + resource: [ + 'user', + ], + operation: [ + 'delete', + ] + }, + }, + description: 'The unique identifier of the user', + }, +/* -------------------------------------------------------------------------- */ +/* user:addTags */ +/* -------------------------------------------------------------------------- */ + { + displayName: 'ID', + name: 'id', + type: 'string', + required: true, + default: '', + displayOptions: { + show: { + resource: [ + 'user', + ], + operation: [ + 'addTags', + ] + }, + }, + description: 'The unique identifier of the user', + }, + { + displayName: 'Tags', + name: 'tags', + type: 'string', + required: true, + default: '', + displayOptions: { + show: { + resource: [ + 'user', + ], + operation: [ + 'addTags', + ] + }, + }, + description: 'Tags to add separated by ","', + }, +/* -------------------------------------------------------------------------- */ +/* user:removeTags */ +/* -------------------------------------------------------------------------- */ + { + displayName: 'ID', + name: 'id', + type: 'string', + required: true, + default: '', + displayOptions: { + show: { + resource: [ + 'user', + ], + operation: [ + 'removeTags', + ] + }, + }, + description: 'The unique identifier of the user', + }, + { + displayName: 'Tags', + name: 'tags', + type: 'string', + required: true, + default: '', + displayOptions: { + show: { + resource: [ + 'user', + ], + operation: [ + 'removeTags', + ] + }, + }, + description: 'Tags to remove separated by ","', + }, +] as INodeProperties[]; diff --git a/packages/nodes-base/nodes/Wordpress/Wordpress.node.ts b/packages/nodes-base/nodes/Wordpress/Wordpress.node.ts index 4a5d5550a5..0c31f57724 100644 --- a/packages/nodes-base/nodes/Wordpress/Wordpress.node.ts +++ b/packages/nodes-base/nodes/Wordpress/Wordpress.node.ts @@ -7,17 +7,13 @@ import { INodeExecutionData, INodeType, } from 'n8n-workflow'; -// import { -// flowApiRequest, -// FlowApiRequestAllItems, -// } from './GenericFunctions'; -// import { -// taskOpeations, -// taskFields, -// } from './TaskDescription'; -// import { -// ITask, TaskInfo, -// } from './TaskInterface'; +import { + wordpressApiRequest, +} from './GenericFunctions'; +import { + postOperations, + postFields, +} from './PostDescription'; export class Wordpress implements INodeType { description: INodeTypeDescription = { @@ -49,16 +45,14 @@ export class Wordpress implements INodeType { { name: 'Post', value: 'post', - description: `The primary unit within Flow; tasks track units of work and can be assigned, sorted, nested, and tagged.
- Tasks can either be part of a List, or "private" (meaning "without a list", essentially).
- Through this endpoint you are able to do anything you wish to your tasks in Flow, including create new ones.`, + description: ``, }, ], - default: 'task', + default: 'post', description: 'Resource to consume.', }, - // ...taskOpeations, - // ...taskFields, + ...postOperations, + ...postFields, ], };