Add author field to post resource on WordPress Node (#958)

This commit is contained in:
Ricardo Espinoza 2020-09-16 02:33:59 -04:00 committed by GitHub
parent 181ba3c4e2
commit 615a44726d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 3 deletions

View file

@ -1,10 +1,16 @@
import { OptionsWithUri } from 'request'; import {
OptionsWithUri,
} from 'request';
import { import {
IExecuteFunctions, IExecuteFunctions,
IExecuteSingleFunctions, IExecuteSingleFunctions,
ILoadOptionsFunctions, ILoadOptionsFunctions,
} from 'n8n-core'; } from 'n8n-core';
import { IDataObject } from 'n8n-workflow';
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<any> { // tslint:disable-line:no-any export async function wordpressApiRequest(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('wordpressApi'); const credentials = this.getCredentials('wordpressApi');

View file

@ -84,6 +84,16 @@ export const postFields = [
}, },
}, },
options: [ options: [
{
displayName: 'Author ID',
name: 'authorId',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getAuthors',
},
default: '',
description: 'The ID for the author of the object.',
},
{ {
displayName: 'Content', displayName: 'Content',
name: 'content', name: 'content',
@ -287,6 +297,16 @@ export const postFields = [
}, },
}, },
options: [ options: [
{
displayName: 'Author ID',
name: 'authorId',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getAuthors',
},
default: '',
description: 'The ID for the author of the object.',
},
{ {
displayName: 'Title', displayName: 'Title',
name: 'title', name: 'title',

View file

@ -1,5 +1,6 @@
export interface IPost { export interface IPost {
author?: number;
id?: number; id?: number;
title?: string; title?: string;
content?: string; content?: string;

View file

@ -47,7 +47,7 @@ export class Wordpress implements INodeType {
{ {
name: 'wordpressApi', name: 'wordpressApi',
required: true, required: true,
} },
], ],
properties: [ properties: [
{ {
@ -115,6 +115,7 @@ export class Wordpress implements INodeType {
async getAuthors(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> { async getAuthors(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = []; const returnData: INodePropertyOptions[] = [];
const authors = await wordpressApiRequestAllItems.call(this, 'GET', '/users', {}, { who: 'authors' }); const authors = await wordpressApiRequestAllItems.call(this, 'GET', '/users', {}, { who: 'authors' });
console.log(authors);
for (const author of authors) { for (const author of authors) {
const authorName = author.name; const authorName = author.name;
const authorId = author.id; const authorId = author.id;
@ -147,6 +148,9 @@ export class Wordpress implements INodeType {
const body: IPost = { const body: IPost = {
title, title,
}; };
if (additionalFields.authorId) {
body.author = additionalFields.authorId as number;
}
if (additionalFields.content) { if (additionalFields.content) {
body.content = additionalFields.content as string; body.content = additionalFields.content as string;
} }
@ -186,6 +190,9 @@ export class Wordpress implements INodeType {
const body: IPost = { const body: IPost = {
id: parseInt(postId, 10), id: parseInt(postId, 10),
}; };
if (updateFields.authorId) {
body.author = updateFields.authorId as number;
}
if (updateFields.title) { if (updateFields.title) {
body.title = updateFields.title as string; body.title = updateFields.title as string;
} }