fix(Strapi Node): Add support for Strapi v4

* 🐛 Fix get all operation for v4

* 🔨 Fix create operation

* 🔨 Fix update operation

* 🔨 Fix delete operation

* 🔨 Fix get operation

* 🔨 Fix Return All

* 👕 Fix nodelinter issues

*  Add Credential Test

* 🔨 Code improvement

* 👕 Fix lint issue

* Removed extra /api from Get All on v4

Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
Co-authored-by: Jonathan Bennetts <jonathan.bennetts@gmail.com>
This commit is contained in:
Harshil Agrawal 2022-03-12 10:56:34 +01:00 committed by GitHub
parent c11edb2521
commit 2a3cbf3fc8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 288 additions and 187 deletions

236
package-lock.json generated
View file

@ -13677,6 +13677,15 @@
"resolved": "https://registry.npmjs.org/@nodelib/fs.stat/-/fs.stat-1.1.3.tgz",
"integrity": "sha512-shAmDyaQC4H92APFoIaVDHCx5bStIocgvbwQyxPRrbUY20V1EYTbSDchWbuwlMG3V17cprZhA6+78JfB+3DTPw=="
},
"ansi-styles": {
"version": "4.3.0",
"resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz",
"integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==",
"optional": true,
"requires": {
"color-convert": "^2.0.1"
}
},
"array-union": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/array-union/-/array-union-1.0.2.tgz",
@ -13712,6 +13721,21 @@
}
}
},
"color-convert": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
"integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==",
"optional": true,
"requires": {
"color-name": "~1.1.4"
}
},
"color-name": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz",
"integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==",
"optional": true
},
"commander": {
"version": "2.20.3",
"resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz",
@ -13774,6 +13798,58 @@
"worker-rpc": "^0.1.0"
}
},
"fork-ts-checker-webpack-plugin-v5": {
"version": "npm:fork-ts-checker-webpack-plugin@5.2.1",
"resolved": "https://registry.npmjs.org/fork-ts-checker-webpack-plugin/-/fork-ts-checker-webpack-plugin-5.2.1.tgz",
"integrity": "sha512-SVi+ZAQOGbtAsUWrZvGzz38ga2YqjWvca1pXQFUArIVXqli0lLoDQ8uS0wg0kSpcwpZmaW5jVCZXQebkyUQSsw==",
"optional": true,
"requires": {
"@babel/code-frame": "^7.8.3",
"@types/json-schema": "^7.0.5",
"chalk": "^4.1.0",
"cosmiconfig": "^6.0.0",
"deepmerge": "^4.2.2",
"fs-extra": "^9.0.0",
"memfs": "^3.1.2",
"minimatch": "^3.0.4",
"schema-utils": "2.7.0",
"semver": "^7.3.2",
"tapable": "^1.0.0"
},
"dependencies": {
"chalk": {
"version": "4.1.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz",
"integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==",
"optional": true,
"requires": {
"ansi-styles": "^4.1.0",
"supports-color": "^7.1.0"
}
},
"semver": {
"version": "7.3.5",
"resolved": "https://registry.npmjs.org/semver/-/semver-7.3.5.tgz",
"integrity": "sha512-PoeGJYh8HK4BTO/a9Tf6ZG3veo/A7ZVsYrSA6J8ny9nb3B1VrpkuN+z9OE5wfE5p6H4LchYZsegiQgbJD94ZFQ==",
"optional": true,
"requires": {
"lru-cache": "^6.0.0"
}
}
}
},
"fs-extra": {
"version": "9.1.0",
"resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-9.1.0.tgz",
"integrity": "sha512-hcg3ZmepS30/7BSFqRvoo3DOMQu7IjqxO5nCDt+zM9XWjb33Wg7ziNT+Qvqbuc3+gWpzO02JubVyk2G4Zvo1OQ==",
"optional": true,
"requires": {
"at-least-node": "^1.0.0",
"graceful-fs": "^4.2.0",
"jsonfile": "^6.0.1",
"universalify": "^2.0.0"
}
},
"glob-parent": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-3.1.0.tgz",
@ -13808,6 +13884,12 @@
"slash": "^2.0.0"
}
},
"has-flag": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz",
"integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==",
"optional": true
},
"ignore": {
"version": "4.0.6",
"resolved": "https://registry.npmjs.org/ignore/-/ignore-4.0.6.tgz",
@ -13836,6 +13918,16 @@
"resolved": "https://registry.npmjs.org/isobject/-/isobject-3.0.1.tgz",
"integrity": "sha1-TkMekrEalzFjaqH5yNHMvP2reN8="
},
"jsonfile": {
"version": "6.1.0",
"resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.1.0.tgz",
"integrity": "sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==",
"optional": true,
"requires": {
"graceful-fs": "^4.1.6",
"universalify": "^2.0.0"
}
},
"micromatch": {
"version": "3.1.10",
"resolved": "https://registry.npmjs.org/micromatch/-/micromatch-3.1.10.tgz",
@ -13871,6 +13963,17 @@
}
}
},
"schema-utils": {
"version": "2.7.0",
"resolved": "https://registry.npmjs.org/schema-utils/-/schema-utils-2.7.0.tgz",
"integrity": "sha512-0ilKFI6QQF5nxDZLFn2dMjvc4hjg/Wkg7rHd3jK6/A4a1Hl9VFdQWvgB1UMGoU94pad1P/8N7fMcEnLnSiju8A==",
"optional": true,
"requires": {
"@types/json-schema": "^7.0.4",
"ajv": "^6.12.2",
"ajv-keywords": "^3.4.1"
}
},
"semver": {
"version": "5.7.1",
"resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz",
@ -13881,6 +13984,15 @@
"resolved": "https://registry.npmjs.org/slash/-/slash-2.0.0.tgz",
"integrity": "sha512-ZYKh3Wh2z1PpEXWr0MpSBZ0V6mZHAQfYevttO11c51CaWjGTaadiKZ+wVt1PbMlDV5qhMFslpZCemhwOK7C89A=="
},
"supports-color": {
"version": "7.2.0",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
"integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==",
"optional": true,
"requires": {
"has-flag": "^4.0.0"
}
},
"to-regex-range": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-2.1.1.tgz",
@ -13974,6 +14086,12 @@
"requires": {
"tslib": "^1.8.1"
}
},
"universalify": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.0.tgz",
"integrity": "sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==",
"optional": true
}
}
},
@ -23452,124 +23570,6 @@
}
}
},
"fork-ts-checker-webpack-plugin-v5": {
"version": "npm:fork-ts-checker-webpack-plugin@5.2.1",
"resolved": "https://registry.npmjs.org/fork-ts-checker-webpack-plugin/-/fork-ts-checker-webpack-plugin-5.2.1.tgz",
"integrity": "sha512-SVi+ZAQOGbtAsUWrZvGzz38ga2YqjWvca1pXQFUArIVXqli0lLoDQ8uS0wg0kSpcwpZmaW5jVCZXQebkyUQSsw==",
"optional": true,
"requires": {
"@babel/code-frame": "^7.8.3",
"@types/json-schema": "^7.0.5",
"chalk": "^4.1.0",
"cosmiconfig": "^6.0.0",
"deepmerge": "^4.2.2",
"fs-extra": "^9.0.0",
"memfs": "^3.1.2",
"minimatch": "^3.0.4",
"schema-utils": "2.7.0",
"semver": "^7.3.2",
"tapable": "^1.0.0"
},
"dependencies": {
"ansi-styles": {
"version": "4.3.0",
"resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz",
"integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==",
"optional": true,
"requires": {
"color-convert": "^2.0.1"
}
},
"chalk": {
"version": "4.1.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz",
"integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==",
"optional": true,
"requires": {
"ansi-styles": "^4.1.0",
"supports-color": "^7.1.0"
}
},
"color-convert": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
"integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==",
"optional": true,
"requires": {
"color-name": "~1.1.4"
}
},
"color-name": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz",
"integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==",
"optional": true
},
"fs-extra": {
"version": "9.1.0",
"resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-9.1.0.tgz",
"integrity": "sha512-hcg3ZmepS30/7BSFqRvoo3DOMQu7IjqxO5nCDt+zM9XWjb33Wg7ziNT+Qvqbuc3+gWpzO02JubVyk2G4Zvo1OQ==",
"optional": true,
"requires": {
"at-least-node": "^1.0.0",
"graceful-fs": "^4.2.0",
"jsonfile": "^6.0.1",
"universalify": "^2.0.0"
}
},
"has-flag": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz",
"integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==",
"optional": true
},
"jsonfile": {
"version": "6.1.0",
"resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.1.0.tgz",
"integrity": "sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==",
"optional": true,
"requires": {
"graceful-fs": "^4.1.6",
"universalify": "^2.0.0"
}
},
"schema-utils": {
"version": "2.7.0",
"resolved": "https://registry.npmjs.org/schema-utils/-/schema-utils-2.7.0.tgz",
"integrity": "sha512-0ilKFI6QQF5nxDZLFn2dMjvc4hjg/Wkg7rHd3jK6/A4a1Hl9VFdQWvgB1UMGoU94pad1P/8N7fMcEnLnSiju8A==",
"optional": true,
"requires": {
"@types/json-schema": "^7.0.4",
"ajv": "^6.12.2",
"ajv-keywords": "^3.4.1"
}
},
"semver": {
"version": "7.3.5",
"resolved": "https://registry.npmjs.org/semver/-/semver-7.3.5.tgz",
"integrity": "sha512-PoeGJYh8HK4BTO/a9Tf6ZG3veo/A7ZVsYrSA6J8ny9nb3B1VrpkuN+z9OE5wfE5p6H4LchYZsegiQgbJD94ZFQ==",
"optional": true,
"requires": {
"lru-cache": "^6.0.0"
}
},
"supports-color": {
"version": "7.2.0",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
"integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==",
"optional": true,
"requires": {
"has-flag": "^4.0.0"
}
},
"universalify": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.0.tgz",
"integrity": "sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==",
"optional": true
}
}
},
"form-data": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz",

View file

@ -30,5 +30,24 @@ export class StrapiApi implements ICredentialType {
default: '',
placeholder: 'https://api.example.com',
},
{
displayName: 'API Version',
name: 'apiVersion',
default: 'v3',
type: 'options',
description: 'The version of api to be used',
options: [
{
name: 'Version 4',
value: 'v4',
description: 'API version supported by Strapi 4',
},
{
name: 'Version 3',
value: 'v3',
description: 'API version supported by Strapi 3',
},
],
},
];
}

View file

@ -6,6 +6,7 @@ export const entryOperations: INodeProperties[] = [
{
displayName: 'Operation',
name: 'operation',
noDataExpression: true,
type: 'options',
displayOptions: {
show: {
@ -42,7 +43,7 @@ export const entryOperations: INodeProperties[] = [
},
],
default: 'get',
description: 'The operation to perform.',
description: 'The operation to perform',
},
];
@ -66,7 +67,7 @@ export const entryFields: INodeProperties[] = [
],
},
},
description: 'Name of the content type.',
description: 'Name of the content type',
},
{
displayName: 'Columns',
@ -84,7 +85,7 @@ export const entryFields: INodeProperties[] = [
},
default: '',
placeholder: 'id,name,description',
description: 'Comma separated list of the properties which should used as columns for the new rows.',
description: 'Comma separated list of the properties which should used as columns for the new rows',
},
/* -------------------------------------------------------------------------- */
@ -106,7 +107,7 @@ export const entryFields: INodeProperties[] = [
],
},
},
description: 'Name of the content type.',
description: 'Name of the content type',
},
{
displayName: 'Entry ID',
@ -124,7 +125,7 @@ export const entryFields: INodeProperties[] = [
],
},
},
description: 'The ID of the entry to delete.',
description: 'The ID of the entry to delete',
},
/* -------------------------------------------------------------------------- */
@ -146,7 +147,7 @@ export const entryFields: INodeProperties[] = [
],
},
},
description: 'Name of the content type.',
description: 'Name of the content type',
},
{
displayName: 'Entry ID',
@ -164,7 +165,7 @@ export const entryFields: INodeProperties[] = [
],
},
},
description: 'The ID of the entry to get.',
description: 'The ID of the entry to get',
},
/* -------------------------------------------------------------------------- */
@ -203,7 +204,7 @@ export const entryFields: INodeProperties[] = [
},
},
default: false,
description: 'Returns a list of your user contacts.',
description: 'Whether to return all results or only up to a given limit',
},
{
displayName: 'Limit',
@ -227,7 +228,7 @@ export const entryFields: INodeProperties[] = [
maxValue: 100,
},
default: 50,
description: 'How many results to return.',
description: 'Max number of results to return',
},
{
displayName: 'Options',
@ -261,7 +262,7 @@ export const entryFields: INodeProperties[] = [
},
],
default: '',
description: 'Only select entries matching the publication state provided.',
description: 'Only select entries matching the publication state provided',
},
{
displayName: 'Sort Fields',
@ -273,7 +274,7 @@ export const entryFields: INodeProperties[] = [
},
default: '',
placeholder: 'name:asc',
description: `Name of the fields to sort the data by. By default will be sorted ascendingly. To modify that behavior, you have to add the sort direction after the name of sort field preceded by a colon. For example: <code>name:asc</code>`,
description: `Name of the fields to sort the data by. By default will be sorted ascendingly. To modify that behavior, you have to add the sort direction after the name of sort field preceded by a colon. For example: <code>name:asc</code>.`,
},
{
displayName: 'Where (JSON)',
@ -307,7 +308,7 @@ export const entryFields: INodeProperties[] = [
],
},
},
description: 'Name of the content type.',
description: 'Name of the content type',
},
{
displayName: 'Update Key',
@ -343,6 +344,6 @@ export const entryFields: INodeProperties[] = [
},
default: '',
placeholder: 'id,name,description',
description: 'Comma separated list of the properties which should used as columns for the new rows.',
description: 'Comma separated list of the properties which should used as columns for the new rows',
},
];

View file

@ -23,7 +23,7 @@ export async function strapiApiRequest(this: IExecuteFunctions | ILoadOptionsFun
method,
body,
qs,
uri: uri || `${credentials.url}${resource}`,
uri: uri || credentials.apiVersion === 'v4' ? `${credentials.url}/api${resource}` : `${credentials.url}${resource}`,
json: true,
qsStringifyOptions: {
arrayFormat: 'indice',
@ -45,41 +45,50 @@ export async function strapiApiRequest(this: IExecuteFunctions | ILoadOptionsFun
export async function getToken(this: IExecuteFunctions | ILoadOptionsFunctions | IHookFunctions | IWebhookFunctions): Promise<any> { // tslint:disable-line:no-any
const credentials = await 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,
};
let options = {} as OptionsWithUri;
options = {
headers: {
'content-type': 'application/json',
},
method: 'POST',
body: {
identifier: credentials.email,
password: credentials.password,
},
uri: credentials.apiVersion === 'v4' ? `${credentials.url}/api/auth/local`:`${credentials.url}/auth/local`,
json: true,
};
return this.helpers.request!(options);
}
export async function strapiApiRequestAllItems(this: IHookFunctions | ILoadOptionsFunctions | IExecuteFunctions, method: string, resource: string, body: any = {}, query: IDataObject = {}, headers: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const returnData: IDataObject[] = [];
const {apiVersion} = await this.getCredentials('strapiApi') as IDataObject;
let responseData;
if (apiVersion === 'v4') {
query['pagination[pageSize]'] = 20;
query['pagination[page]'] = 0;
do {
({data:responseData} = await strapiApiRequest.call(this, method, resource, body, query, undefined, headers));
query['pagination[page]'] += query['pagination[pageSize]'];
returnData.push.apply(returnData, responseData);
} while (
responseData.length !== 0
);
query._limit = 20;
query._start = 0;
do {
responseData = await strapiApiRequest.call(this, method, resource, body, query, undefined, headers);
query._start += query._limit;
returnData.push.apply(returnData, responseData);
} while (
responseData.length !== 0
);
} else {
query._limit = 20;
query._start = 0;
do {
responseData = await strapiApiRequest.call(this, method, resource, body, query, undefined, headers);
query._start += query._limit;
returnData.push.apply(returnData, responseData);
} while (
responseData.length !== 0
);
}
return returnData;
}

View file

@ -3,7 +3,14 @@ import {
} from 'n8n-core';
import {
OptionsWithUri,
} from 'request';
import {
ICredentialsDecrypted,
ICredentialTestFunctions,
IDataObject,
INodeCredentialTestResult,
INodeExecutionData,
INodeType,
INodeTypeDescription,
@ -40,12 +47,14 @@ export class Strapi implements INodeType {
{
name: 'strapiApi',
required: true,
testedBy: 'strapiApiTest',
},
],
properties: [
{
displayName: 'Resource',
name: 'resource',
noDataExpression: true,
type: 'options',
options: [
{
@ -54,13 +63,47 @@ export class Strapi implements INodeType {
},
],
default: 'entry',
description: 'The resource to operate on.',
description: 'The resource to operate on',
},
...entryOperations,
...entryFields,
],
};
methods = {
credentialTest: {
async strapiApiTest(this: ICredentialTestFunctions, credential: ICredentialsDecrypted): Promise<INodeCredentialTestResult> {
const credentials = await credential.data as IDataObject;
let options = {} as OptionsWithUri;
options = {
headers: {
'content-type': `application/json`,
},
method: 'POST',
body: {
identifier: credentials.email,
password: credentials.password,
},
uri: credentials.apiVersion === 'v4' ? `${credentials.url}/api/auth/local`:`${credentials.url}/auth/local`,
json: true,
};
try {
await this.helpers.request(options);
return {
status: 'OK',
message: 'Authentication successful',
};
} catch (error) {
return {
status: 'Error',
message: `Auth settings are not valid: ${error}`,
};
}
},
},
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: IDataObject[] = [];
@ -71,6 +114,7 @@ export class Strapi implements INodeType {
const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
const { apiVersion } = await this.getCredentials('strapiApi') as IDataObject;
const { jwt } = await getToken.call(this);
headers.Authorization = `Bearer ${jwt}`;
@ -89,7 +133,7 @@ export class Strapi implements INodeType {
for (const key of Object.keys(items[i].json)) {
if (columnList.includes(key)) {
body[key] = items[i].json[key];
apiVersion === 'v4'? body.data = items[i].json: body[key] = items[i].json[key];
}
}
responseData = await strapiApiRequest.call(this, 'POST', `/${contentType}`, body, qs, undefined, headers);
@ -115,30 +159,58 @@ export class Strapi implements INodeType {
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 NodeOperationError(this.getNode(), 'Query must be a valid JSON');
if (apiVersion === 'v4') {
// Sort Option
if (options.sort && (options.sort as string[]).length !== 0) {
const sortFields = options.sort as string[];
qs.sort = sortFields.join(',');
}
// Filter Option
if (options.where) {
const query = validateJSON(options.where as string);
if (query !== undefined) {
qs.filters = query;
} else {
throw new NodeOperationError(this.getNode(), 'Query must be a valid JSON');
}
}
// Publication Option
if (options.publicationState) {
qs.publicationState = options.publicationState as string;
}
// Limit Option
if (returnAll) {
responseData = await strapiApiRequestAllItems.call(this, 'GET', `/${contentType}`, {}, qs, headers);
} else {
qs['pagination[pageSize]'] = this.getNodeParameter('limit', i) as number;
({ data:responseData } = await strapiApiRequest.call(this, 'GET', `/${contentType}`, {}, qs, undefined, headers));
}
}
if (options.publicationState) {
qs._publicationState = options.publicationState as string;
}
if (returnAll) {
responseData = await strapiApiRequestAllItems.call(this, 'GET', `/${contentType}`, {}, qs, headers);
} else {
qs._limit = this.getNodeParameter('limit', i) as number;
responseData = await strapiApiRequest.call(this, 'GET', `/${contentType}`, {}, qs, undefined, headers);
// Sort Option
if (options.sort && (options.sort as string[]).length !== 0) {
const sortFields = options.sort as string[];
qs._sort = sortFields.join(',');
}
// Filter Option
if (options.where) {
const query = validateJSON(options.where as string);
if (query !== undefined) {
qs._where = query;
} else {
throw new NodeOperationError(this.getNode(), 'Query must be a valid JSON');
}
}
// Publication Option
if (options.publicationState) {
qs._publicationState = options.publicationState as string;
}
// Limit Option
if (returnAll) {
responseData = await strapiApiRequestAllItems.call(this, 'GET', `/${contentType}`, {}, qs, headers);
} else {
qs._limit = this.getNodeParameter('limit', i) as number;
responseData = await strapiApiRequest.call(this, 'GET', `/${contentType}`, {}, qs, undefined, headers);
}
}
returnData.push.apply(returnData, responseData);
}
@ -150,8 +222,7 @@ export class Strapi implements INodeType {
const entryId = this.getNodeParameter('entryId', i) as string;
responseData = await strapiApiRequest.call(this, 'GET', `/${contentType}/${entryId}`, {}, qs, undefined, headers);
returnData.push(responseData);
apiVersion === 'v4'? returnData.push(responseData.data): returnData.push(responseData);
}
if (operation === 'update') {
@ -170,12 +241,13 @@ export class Strapi implements INodeType {
for (const key of Object.keys(items[i].json)) {
if (columnList.includes(key)) {
body[key] = items[i].json[key];
apiVersion === 'v4'? body.data = items[i].json:body[key] = items[i].json[key];
}
}
responseData = await strapiApiRequest.call(this, 'PUT', `/${contentType}/${entryId}`, body, qs, undefined, headers);
returnData.push(responseData);
responseData = await strapiApiRequest.call(this, 'PUT', `/${contentType}/${entryId}`, body, qs, undefined, headers);
apiVersion === 'v4'? returnData.push(responseData.data): returnData.push(responseData);
}
}
} catch (error) {