Add Strapi-Node (#1143)

*  Strapi-Node

*  Small improvements

*  Small improvement
This commit is contained in:
Ricardo Espinoza 2020-11-11 03:44:53 -05:00 committed by GitHub
parent 702a8bf3bf
commit a5ecf3d58d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 746 additions and 0 deletions

View file

@ -0,0 +1,29 @@
import {
ICredentialType,
NodePropertyTypes,
} from 'n8n-workflow';
export class StrapiApi implements ICredentialType {
name = 'strapiApi';
displayName = 'Strapi API';
properties = [
{
displayName: 'Email',
name: 'email',
type: 'string' as NodePropertyTypes,
default: '',
},
{
displayName: 'Password',
name: 'password',
type: 'string' as NodePropertyTypes,
default: '',
},
{
displayName: 'URL',
name: 'url',
type: 'string' as NodePropertyTypes,
default: '',
},
];
}

View file

@ -0,0 +1,346 @@
import {
INodeProperties,
} from 'n8n-workflow';
export const entryOperations = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'entry',
],
},
},
options: [
{
name: 'Create',
value: 'create',
description: 'Create an entry',
},
{
name: 'Delete',
value: 'delete',
description: 'Delete an entry',
},
{
name: 'Get',
value: 'get',
description: 'Get an entry',
},
{
name: 'Get All',
value: 'getAll',
description: 'Get all entries',
},
{
name: 'Update',
value: 'update',
description: 'Update an entry',
},
],
default: 'get',
description: 'The operation to perform.',
},
] as INodeProperties[];
export const entryFields = [
/* -------------------------------------------------------------------------- */
/* entry:create */
/* -------------------------------------------------------------------------- */
{
displayName: 'Content Type',
name: 'contentType',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
resource: [
'entry',
],
operation: [
'create',
],
},
},
description: 'Name of the content type',
},
{
displayName: 'Columns',
name: 'columns',
type: 'string',
displayOptions: {
show: {
resource: [
'entry',
],
operation: [
'create',
],
},
},
default: '',
placeholder: 'id,name,description',
description: 'Comma separated list of the properties which should used as columns for the new rows.',
},
/* -------------------------------------------------------------------------- */
/* entry:delete */
/* -------------------------------------------------------------------------- */
{
displayName: 'Content Type',
name: 'contentType',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
resource: [
'entry',
],
operation: [
'delete',
],
},
},
description: 'Name of the content type',
},
{
displayName: 'Entry ID',
name: 'entryId',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
resource: [
'entry',
],
operation: [
'delete',
],
},
},
description: 'The ID of the entry to get.',
},
/* -------------------------------------------------------------------------- */
/* entry:get */
/* -------------------------------------------------------------------------- */
{
displayName: 'Content Type',
name: 'contentType',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
resource: [
'entry',
],
operation: [
'get',
],
},
},
description: 'Name of the content type',
},
{
displayName: 'Entry ID',
name: 'entryId',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
resource: [
'entry',
],
operation: [
'get',
],
},
},
description: 'The ID of the entry to get.',
},
/* -------------------------------------------------------------------------- */
/* entry:getAll */
/* -------------------------------------------------------------------------- */
{
displayName: 'Content Type',
name: 'contentType',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
resource: [
'entry',
],
operation: [
'getAll',
],
},
},
description: 'Name of the content type',
},
{
displayName: 'Return All',
name: 'returnAll',
type: 'boolean',
displayOptions: {
show: {
resource: [
'entry',
],
operation: [
'getAll',
],
},
},
default: false,
description: 'Returns a list of your user contacts.',
},
{
displayName: 'Limit',
name: 'limit',
type: 'number',
displayOptions: {
show: {
resource: [
'entry',
],
operation: [
'getAll',
],
returnAll: [
false,
],
},
},
typeOptions: {
minValue: 1,
maxValue: 100,
},
default: 50,
description: 'How many results to return.',
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
resource: [
'entry',
],
operation: [
'getAll',
],
},
},
options: [
{
displayName: 'Publication State',
name: 'publicationState',
type: 'options',
options: [
{
name: 'Live',
value: 'live',
},
{
name: 'Preview',
value: 'preview',
},
],
default: '',
description: 'Only select entries matching the publication state provided.',
},
{
displayName: 'Sort Fields',
name: 'sort',
type: 'string',
typeOptions: {
multipleValues: true,
multipleValueButtonText: 'Add Sort Field',
},
default: '',
placeholder: 'name:asc',
description: `Name of the fields to sort the data by. By default will be sorted ascendingly.<br>
To modify that behavior, you have to add the sort direction after the name of sort field preceded by a colon.
For example: name:asc`,
},
{
displayName: 'Where (JSON)',
name: 'where',
type: 'string',
typeOptions: {
alwaysOpenEditWindow: true,
},
default: '',
description: 'JSON query to filter the data.<a href="https://strapi.io/documentation/v3.x/content-api/parameters.html#filters" target="_blank"> Info</a>',
},
],
},
/* -------------------------------------------------------------------------- */
/* entry:update */
/* -------------------------------------------------------------------------- */
{
displayName: 'Content Type',
name: 'contentType',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
resource: [
'entry',
],
operation: [
'update',
],
},
},
description: 'Name of the content type',
},
{
displayName: 'Update Key',
name: 'updateKey',
type: 'string',
displayOptions: {
show: {
resource: [
'entry',
],
operation: [
'update',
],
},
},
default: 'id',
required: true,
description: 'Name of the property which decides which rows in the database should be updated. Normally that would be "id".',
},
{
displayName: 'Columns',
name: 'columns',
type: 'string',
displayOptions: {
show: {
resource: [
'entry',
],
operation: [
'update',
],
},
},
default: '',
placeholder: 'id,name,description',
description: 'Comma separated list of the properties which should used as columns for the new rows.',
},
] as INodeProperties[];

View file

@ -0,0 +1,106 @@
import {
OptionsWithUri,
} from 'request';
import {
IExecuteFunctions,
IHookFunctions,
ILoadOptionsFunctions,
IWebhookFunctions,
} from 'n8n-core';
import {
IDataObject,
} from 'n8n-workflow';
export async function strapiApiRequest(this: IExecuteFunctions | ILoadOptionsFunctions | IHookFunctions | IWebhookFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, headers: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('strapiApi') as IDataObject;
try {
const options: OptionsWithUri = {
headers: {
'Authorization': `Bearer ${qs.jwt}`,
},
method,
body,
qs,
uri: uri || `${credentials.url}${resource}`,
json: true,
};
if (Object.keys(headers).length !== 0) {
options.headers = Object.assign({}, options.headers, headers);
}
if (Object.keys(body).length === 0) {
delete options.body;
}
delete qs.jwt;
//@ts-ignore
return await this.helpers?.request(options);
} catch (error) {
if (error.response && error.response.body && error.response.body.message) {
let messages = error.response.body.message;
if (Array.isArray(error.response.body.message)) {
messages = messages[0].messages.map((e: IDataObject) => e.message).join('|');
}
// Try to return the error prettier
throw new Error(
`Strapi error response [${error.statusCode}]: ${messages}`,
);
}
throw error;
}
}
export async function getToken(this: IExecuteFunctions | ILoadOptionsFunctions | IHookFunctions | IWebhookFunctions): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('strapiApi') as IDataObject;
const options: OptionsWithUri = {
headers: {
'content-type': `application/json`,
},
method: 'POST',
uri: `${credentials.url}/auth/local`,
body: {
identifier: credentials.email,
password: credentials.password,
},
json: true,
};
return this.helpers.request!(options);
}
export async function strapiApiRequestAllItems(this: IHookFunctions | ILoadOptionsFunctions | IExecuteFunctions, method: string, resource: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const returnData: IDataObject[] = [];
let responseData;
query._limit = 20;
query._start = 0;
do {
responseData = await strapiApiRequest.call(this, method, resource, body, query);
query._start += query._limit;
returnData.push.apply(returnData, responseData);
} while (
responseData.length !== 0
);
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;
}

View file

@ -0,0 +1,191 @@
import {
IExecuteFunctions,
} from 'n8n-core';
import {
IDataObject,
INodeExecutionData,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import {
getToken,
strapiApiRequest,
strapiApiRequestAllItems,
validateJSON,
} from './GenericFunctions';
import {
entryFields,
entryOperations,
} from './EntryDescription';
export class Strapi implements INodeType {
description: INodeTypeDescription = {
displayName: 'Strapi',
name: 'strapi',
icon: 'file:strapi.svg',
group: ['input'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Consume Strapi API.',
defaults: {
name: 'Strapi',
color: '#725ed8',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'strapiApi',
required: true,
},
],
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
options: [
{
name: 'Entry',
value: 'entry',
},
],
default: 'entry',
description: 'The resource to operate on.',
},
...entryOperations,
...entryFields,
],
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: IDataObject[] = [];
const length = (items.length as unknown) as number;
const qs: IDataObject = {};
let responseData;
const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
const { jwt } = await getToken.call(this);
qs.jwt = jwt;
if (resource === 'entry') {
if (operation === 'create') {
for (let i = 0; i < length; i++) {
const body: IDataObject = {};
const contentType = this.getNodeParameter('contentType', i) as string;
const columns = this.getNodeParameter('columns', i) as string;
const columnList = columns.split(',').map(column => column.trim());
for (const key of Object.keys(items[i].json)) {
if (columnList.includes(key)) {
body[key] = items[i].json[key];
}
}
responseData = await strapiApiRequest.call(this, 'POST', `/${contentType}`, body, qs);
returnData.push(responseData);
}
}
if (operation === 'delete') {
for (let i = 0; i < length; i++) {
const contentType = this.getNodeParameter('contentType', i) as string;
const entryId = this.getNodeParameter('entryId', i) as string;
responseData = await strapiApiRequest.call(this, 'DELETE', `/${contentType}/${entryId}`, {}, qs);
returnData.push(responseData);
}
}
if (operation === 'getAll') {
for (let i = 0; i < length; i++) {
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
const contentType = this.getNodeParameter('contentType', i) as string;
const options = this.getNodeParameter('options', i) as IDataObject;
if (options.sort && (options.sort as string[]).length !== 0) {
const sortFields = options.sort as string[];
qs._sort = sortFields.join(',');
}
if (options.where) {
const query = validateJSON(options.where as string);
if (query !== undefined) {
qs._where = query;
} else {
throw new Error('Query must be a valid JSON');
}
}
if (options.publicationState) {
qs._publicationState = options.publicationState as string;
}
if (returnAll) {
responseData = await strapiApiRequestAllItems.call(this, 'GET', `/${contentType}`, {}, qs);
} else {
qs._limit = this.getNodeParameter('limit', i) as number;
responseData = await strapiApiRequest.call(this, 'GET', `/${contentType}`, {}, qs);
}
returnData.push.apply(returnData, responseData);
}
}
if (operation === 'get') {
for (let i = 0; i < length; i++) {
const contentType = this.getNodeParameter('contentType', i) as string;
const entryId = this.getNodeParameter('entryId', i) as string;
responseData = await strapiApiRequest.call(this, 'GET', `/${contentType}/${entryId}`, {}, qs);
returnData.push(responseData);
}
}
if (operation === 'update') {
for (let i = 0; i < length; i++) {
const body: IDataObject = {};
const contentType = this.getNodeParameter('contentType', i) as string;
const columns = this.getNodeParameter('columns', i) as string;
const updateKey = this.getNodeParameter('updateKey', i) as string;
const columnList = columns.split(',').map(column => column.trim());
const entryId = items[i].json[updateKey];
for (const key of Object.keys(items[i].json)) {
if (columnList.includes(key)) {
body[key] = items[i].json[key];
}
}
responseData = await strapiApiRequest.call(this, 'PUT', `/${contentType}/${entryId}`, body, qs);
returnData.push(responseData);
}
}
}
return [this.helpers.returnJsonArray(returnData)];
}
}

View file

@ -0,0 +1,72 @@
<svg width="1025" height="1032" viewBox="0 0 1025 1032" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M341.328 0V344.061H682.661V688.121H1023.99V0H341.328Z" fill="url(#paint0_linear)"/>
<path d="M683 343.725H343V688.457H683V343.725Z" fill="url(#paint1_linear)"/>
<path d="M341.333 344.061H0L341.333 0V344.061Z" fill="url(#paint2_linear)"/>
<path d="M682.367 1031.18V687.457H1024.37L682.367 1031.18Z" fill="url(#paint3_linear)"/>
<circle r="1.7963" transform="matrix(1 8.74228e-08 8.74228e-08 -1 365.162 314.663)" fill="#956FFF"/>
<circle r="1.7963" transform="matrix(1 8.74228e-08 8.74228e-08 -1 374.502 314.663)" fill="#956FFF"/>
<circle r="1.7963" transform="matrix(1 8.74228e-08 8.74228e-08 -1 383.843 314.663)" fill="#956FFF"/>
<circle r="1.7963" transform="matrix(1 8.74228e-08 8.74228e-08 -1 393.184 314.663)" fill="#956FFF"/>
<circle r="1.7963" transform="matrix(1 8.74228e-08 8.74228e-08 -1 402.525 314.663)" fill="#956FFF"/>
<circle r="1.7963" transform="matrix(1 8.74228e-08 8.74228e-08 -1 411.866 314.663)" fill="#956FFF"/>
<circle r="1.7963" transform="matrix(1 8.74228e-08 8.74228e-08 -1 421.206 314.663)" fill="#956FFF"/>
<circle r="1.7963" transform="matrix(1 8.74228e-08 8.74228e-08 -1 430.546 314.663)" fill="#956FFF"/>
<circle r="1.7963" transform="matrix(1 8.74228e-08 8.74228e-08 -1 439.887 314.663)" fill="#956FFF"/>
<circle r="1.7963" transform="matrix(1 8.74228e-08 8.74228e-08 -1 449.228 314.663)" fill="#956FFF"/>
<circle r="1.7963" transform="matrix(1 8.74228e-08 8.74228e-08 -1 458.569 314.663)" fill="#956FFF"/>
<circle r="1.7963" transform="matrix(1 8.74228e-08 8.74228e-08 -1 365.162 306.041)" fill="#956FFF"/>
<circle r="1.7963" transform="matrix(1 8.74228e-08 8.74228e-08 -1 374.502 306.041)" fill="#956FFF"/>
<circle r="1.7963" transform="matrix(1 8.74228e-08 8.74228e-08 -1 383.843 306.041)" fill="#956FFF"/>
<circle r="1.7963" transform="matrix(1 8.74228e-08 8.74228e-08 -1 393.184 306.041)" fill="#956FFF"/>
<circle r="1.7963" transform="matrix(1 8.74228e-08 8.74228e-08 -1 402.525 306.041)" fill="#956FFF"/>
<circle r="1.7963" transform="matrix(1 8.74228e-08 8.74228e-08 -1 411.866 306.041)" fill="#956FFF"/>
<circle r="1.7963" transform="matrix(1 8.74228e-08 8.74228e-08 -1 421.206 306.041)" fill="#956FFF"/>
<circle r="1.7963" transform="matrix(1 8.74228e-08 8.74228e-08 -1 430.546 306.041)" fill="#956FFF"/>
<circle r="1.7963" transform="matrix(1 8.74228e-08 8.74228e-08 -1 365.162 297.419)" fill="#956FFF"/>
<circle r="1.7963" transform="matrix(1 8.74228e-08 8.74228e-08 -1 374.502 297.419)" fill="#956FFF"/>
<circle r="1.7963" transform="matrix(1 8.74228e-08 8.74228e-08 -1 383.843 297.419)" fill="#956FFF"/>
<circle r="1.7963" transform="matrix(1 8.74228e-08 8.74228e-08 -1 393.184 297.419)" fill="#956FFF"/>
<circle r="1.7963" transform="matrix(1 8.74228e-08 8.74228e-08 -1 402.525 297.419)" fill="#956FFF"/>
<circle r="1.7963" transform="matrix(1 8.74228e-08 8.74228e-08 -1 365.162 288.796)" fill="#956FFF"/>
<circle r="1.7963" transform="matrix(1 8.74228e-08 8.74228e-08 -1 374.502 288.797)" fill="#956FFF"/>
<circle r="1.7963" transform="matrix(1 8.74228e-08 8.74228e-08 -1 383.843 288.797)" fill="#956FFF"/>
<circle cx="716.162" cy="638.796" r="1.7963" fill="#956FFF"/>
<circle cx="725.502" cy="638.796" r="1.7963" fill="#956FFF"/>
<circle cx="734.843" cy="638.796" r="1.7963" fill="#956FFF"/>
<circle cx="744.184" cy="638.796" r="1.7963" fill="#956FFF"/>
<circle cx="753.525" cy="638.796" r="1.7963" fill="#956FFF"/>
<circle cx="762.866" cy="638.796" r="1.7963" fill="#956FFF"/>
<circle cx="772.206" cy="638.796" r="1.7963" fill="#956FFF"/>
<circle cx="716.162" cy="647.419" r="1.7963" fill="#956FFF"/>
<circle cx="725.502" cy="647.419" r="1.7963" fill="#956FFF"/>
<circle cx="734.843" cy="647.419" r="1.7963" fill="#956FFF"/>
<circle cx="744.184" cy="647.419" r="1.7963" fill="#956FFF"/>
<circle cx="753.525" cy="647.419" r="1.7963" fill="#956FFF"/>
<circle cx="762.866" cy="647.419" r="1.7963" fill="#956FFF"/>
<circle cx="716.162" cy="656.041" r="1.7963" fill="#956FFF"/>
<circle cx="725.502" cy="656.04" r="1.7963" fill="#956FFF"/>
<circle cx="734.843" cy="656.04" r="1.7963" fill="#956FFF"/>
<circle cx="744.184" cy="656.04" r="1.7963" fill="#956FFF"/>
<circle cx="753.525" cy="656.04" r="1.7963" fill="#956FFF"/>
<circle cx="716.162" cy="664.664" r="1.7963" fill="#956FFF"/>
<circle cx="725.502" cy="664.663" r="1.7963" fill="#956FFF"/>
<circle cx="734.843" cy="664.663" r="1.7963" fill="#956FFF"/>
<defs>
<linearGradient id="paint0_linear" x1="1072.36" y1="-51.4075" x2="553.031" y2="446.945" gradientUnits="userSpaceOnUse">
<stop offset="0.0208333" stop-color="#956FFF"/>
<stop offset="1" stop-color="#1C1B7E"/>
</linearGradient>
<linearGradient id="paint1_linear" x1="634.365" y1="396.5" x2="277.177" y2="712.901" gradientUnits="userSpaceOnUse">
<stop stop-color="#956FFF"/>
<stop offset="1" stop-color="#1A2670"/>
</linearGradient>
<linearGradient id="paint2_linear" x1="170.667" y1="0" x2="170.667" y2="344.061" gradientUnits="userSpaceOnUse">
<stop offset="0.0208333" stop-color="#956FFF"/>
<stop offset="1" stop-color="#1C1B7E"/>
</linearGradient>
<linearGradient id="paint3_linear" x1="1085.18" y1="556.22" x2="695.654" y2="967.042" gradientUnits="userSpaceOnUse">
<stop stop-color="#956FFF"/>
<stop offset="0.838542" stop-color="#1C1B7E"/>
</linearGradient>
</defs>
</svg>

After

Width:  |  Height:  |  Size: 5.2 KiB

View file

@ -180,6 +180,7 @@
"dist/credentials/SpotifyOAuth2Api.credentials.js",
"dist/credentials/StoryblokContentApi.credentials.js",
"dist/credentials/StoryblokManagementApi.credentials.js",
"dist/credentials/StrapiApi.credentials.js",
"dist/credentials/SurveyMonkeyApi.credentials.js",
"dist/credentials/SurveyMonkeyOAuth2Api.credentials.js",
"dist/credentials/TaigaCloudApi.credentials.js",
@ -387,6 +388,7 @@
"dist/nodes/SseTrigger.node.js",
"dist/nodes/Start.node.js",
"dist/nodes/Storyblok/Storyblok.node.js",
"dist/nodes/Strapi/Strapi.node.js",
"dist/nodes/Strava/Strava.node.js",
"dist/nodes/Strava/StravaTrigger.node.js",
"dist/nodes/Stripe/StripeTrigger.node.js",