2023-01-27 03:22:44 -08:00
|
|
|
import type { OptionsWithUri } from 'request';
|
2022-03-12 01:56:34 -08:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IExecuteFunctions,
|
2022-03-12 01:56:34 -08:00
|
|
|
ICredentialsDecrypted,
|
|
|
|
ICredentialTestFunctions,
|
2020-11-11 00:44:53 -08:00
|
|
|
IDataObject,
|
2022-03-12 01:56:34 -08:00
|
|
|
INodeCredentialTestResult,
|
2020-11-11 00:44:53 -08:00
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeOperationError } from 'n8n-workflow';
|
2020-11-11 00:44:53 -08:00
|
|
|
|
|
|
|
import {
|
|
|
|
getToken,
|
|
|
|
strapiApiRequest,
|
|
|
|
strapiApiRequestAllItems,
|
|
|
|
validateJSON,
|
|
|
|
} from './GenericFunctions';
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { entryFields, entryOperations } from './EntryDescription';
|
2020-11-11 00:44:53 -08:00
|
|
|
|
|
|
|
export class Strapi implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Strapi',
|
|
|
|
name: 'strapi',
|
|
|
|
icon: 'file:strapi.svg',
|
|
|
|
group: ['input'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
2021-07-03 05:40:16 -07:00
|
|
|
description: 'Consume Strapi API',
|
2020-11-11 00:44:53 -08:00
|
|
|
defaults: {
|
|
|
|
name: 'Strapi',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'strapiApi',
|
|
|
|
required: true,
|
2022-03-12 01:56:34 -08:00
|
|
|
testedBy: 'strapiApiTest',
|
2020-11-11 00:44:53 -08:00
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
2022-03-12 01:56:34 -08:00
|
|
|
noDataExpression: true,
|
2020-11-11 00:44:53 -08:00
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Entry',
|
|
|
|
value: 'entry',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'entry',
|
|
|
|
},
|
|
|
|
...entryOperations,
|
|
|
|
...entryFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2022-03-12 01:56:34 -08:00
|
|
|
methods = {
|
|
|
|
credentialTest: {
|
2022-08-17 08:50:24 -07:00
|
|
|
async strapiApiTest(
|
|
|
|
this: ICredentialTestFunctions,
|
|
|
|
credential: ICredentialsDecrypted,
|
|
|
|
): Promise<INodeCredentialTestResult> {
|
2022-12-02 12:54:28 -08:00
|
|
|
const credentials = credential.data as IDataObject;
|
2022-03-12 01:56:34 -08:00
|
|
|
let options = {} as OptionsWithUri;
|
|
|
|
|
|
|
|
options = {
|
|
|
|
headers: {
|
2022-12-29 03:20:43 -08:00
|
|
|
'content-type': 'application/json',
|
2022-03-12 01:56:34 -08:00
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
body: {
|
|
|
|
identifier: credentials.email,
|
|
|
|
password: credentials.password,
|
|
|
|
},
|
2022-08-17 08:50:24 -07:00
|
|
|
uri:
|
|
|
|
credentials.apiVersion === 'v4'
|
|
|
|
? `${credentials.url}/api/auth/local`
|
|
|
|
: `${credentials.url}/auth/local`,
|
2022-03-12 01:56:34 -08:00
|
|
|
json: true,
|
|
|
|
};
|
2022-08-17 08:50:24 -07:00
|
|
|
try {
|
|
|
|
await this.helpers.request(options);
|
|
|
|
return {
|
|
|
|
status: 'OK',
|
|
|
|
message: 'Authentication successful',
|
|
|
|
};
|
|
|
|
} catch (error) {
|
|
|
|
return {
|
|
|
|
status: 'Error',
|
|
|
|
message: `Auth settings are not valid: ${error}`,
|
|
|
|
};
|
|
|
|
}
|
2022-03-12 01:56:34 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-11-11 00:44:53 -08:00
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
2022-08-30 08:55:33 -07:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2022-04-22 09:29:51 -07:00
|
|
|
const length = items.length;
|
2020-11-11 00:44:53 -08:00
|
|
|
const qs: IDataObject = {};
|
2020-11-11 00:52:05 -08:00
|
|
|
const headers: IDataObject = {};
|
2020-11-11 00:44:53 -08:00
|
|
|
let responseData;
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2020-11-11 00:44:53 -08:00
|
|
|
|
2022-04-14 23:00:47 -07:00
|
|
|
const { apiVersion } = await this.getCredentials('strapiApi');
|
2020-11-11 00:44:53 -08:00
|
|
|
const { jwt } = await getToken.call(this);
|
|
|
|
|
2020-11-11 00:52:05 -08:00
|
|
|
headers.Authorization = `Bearer ${jwt}`;
|
2021-07-19 23:58:54 -07:00
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
try {
|
|
|
|
if (resource === 'entry') {
|
|
|
|
if (operation === 'create') {
|
|
|
|
const body: IDataObject = {};
|
2020-11-11 00:44:53 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const contentType = this.getNodeParameter('contentType', i) as string;
|
2020-11-11 00:44:53 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const columns = this.getNodeParameter('columns', i) as string;
|
2020-11-11 00:44:53 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const columnList = columns.split(',').map((column) => column.trim());
|
2020-11-11 00:44:53 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
for (const key of Object.keys(items[i].json)) {
|
|
|
|
if (columnList.includes(key)) {
|
2022-08-17 08:50:24 -07:00
|
|
|
apiVersion === 'v4'
|
|
|
|
? (body.data = items[i].json)
|
|
|
|
: (body[key] = items[i].json[key]);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-11-11 00:44:53 -08:00
|
|
|
}
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await strapiApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
`/${contentType}`,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
undefined,
|
|
|
|
headers,
|
|
|
|
);
|
2020-11-11 00:45:50 -08:00
|
|
|
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2023-02-27 19:39:43 -08:00
|
|
|
this.helpers.returnJsonArray(responseData as IDataObject),
|
2022-08-30 08:55:33 -07:00
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-11-11 00:44:53 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'delete') {
|
|
|
|
const contentType = this.getNodeParameter('contentType', i) as string;
|
2020-11-11 00:44:53 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const entryId = this.getNodeParameter('entryId', i) as string;
|
2020-11-11 00:45:50 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await strapiApiRequest.call(
|
|
|
|
this,
|
|
|
|
'DELETE',
|
|
|
|
`/${contentType}/${entryId}`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
undefined,
|
|
|
|
headers,
|
|
|
|
);
|
2020-11-11 00:44:53 -08:00
|
|
|
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2023-02-27 19:39:43 -08:00
|
|
|
this.helpers.returnJsonArray(responseData as IDataObject),
|
2022-08-30 08:55:33 -07:00
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-11-11 00:44:53 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'getAll') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2020-11-11 00:44:53 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const contentType = this.getNodeParameter('contentType', i) as string;
|
2020-11-11 00:44:53 -08:00
|
|
|
|
2022-11-18 07:29:44 -08:00
|
|
|
const options = this.getNodeParameter('options', i);
|
2020-11-11 00:44:53 -08:00
|
|
|
|
2022-03-12 01:56:34 -08:00
|
|
|
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 {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'Query must be a valid JSON', {
|
|
|
|
itemIndex: i,
|
|
|
|
});
|
2022-03-12 01:56:34 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Publication Option
|
|
|
|
if (options.publicationState) {
|
|
|
|
qs.publicationState = options.publicationState as string;
|
|
|
|
}
|
|
|
|
// Limit Option
|
|
|
|
if (returnAll) {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await strapiApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/${contentType}`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
headers,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
qs['pagination[pageSize]'] = this.getNodeParameter('limit', i);
|
2022-08-17 08:50:24 -07:00
|
|
|
({ data: responseData } = await strapiApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/${contentType}`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
undefined,
|
|
|
|
headers,
|
|
|
|
));
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
} else {
|
2022-03-12 01:56:34 -08:00
|
|
|
// 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 {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'Query must be a valid JSON', {
|
|
|
|
itemIndex: i,
|
|
|
|
});
|
2022-03-12 01:56:34 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Publication Option
|
|
|
|
if (options.publicationState) {
|
|
|
|
qs._publicationState = options.publicationState as string;
|
|
|
|
}
|
|
|
|
// Limit Option
|
|
|
|
if (returnAll) {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await strapiApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/${contentType}`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
headers,
|
|
|
|
);
|
2022-03-12 01:56:34 -08:00
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
qs._limit = this.getNodeParameter('limit', i);
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await strapiApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/${contentType}`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
undefined,
|
|
|
|
headers,
|
|
|
|
);
|
2022-03-12 01:56:34 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
|
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2023-02-27 19:39:43 -08:00
|
|
|
this.helpers.returnJsonArray(responseData as IDataObject),
|
2022-08-30 08:55:33 -07:00
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
|
|
|
|
returnData.push(...executionData);
|
2020-11-11 00:44:53 -08:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'get') {
|
|
|
|
const contentType = this.getNodeParameter('contentType', i) as string;
|
2020-11-11 00:44:53 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const entryId = this.getNodeParameter('entryId', i) as string;
|
2020-11-11 00:44:53 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await strapiApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/${contentType}/${entryId}`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
undefined,
|
|
|
|
headers,
|
|
|
|
);
|
2022-08-30 08:55:33 -07:00
|
|
|
|
|
|
|
if (apiVersion === 'v4') {
|
|
|
|
responseData = responseData.data;
|
|
|
|
}
|
|
|
|
|
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2023-02-27 19:39:43 -08:00
|
|
|
this.helpers.returnJsonArray(responseData as IDataObject),
|
2022-08-30 08:55:33 -07:00
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-11-11 00:44:53 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'update') {
|
|
|
|
const body: IDataObject = {};
|
2020-11-11 00:44:53 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const contentType = this.getNodeParameter('contentType', i) as string;
|
2020-11-11 00:44:53 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const columns = this.getNodeParameter('columns', i) as string;
|
2020-11-11 00:44:53 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const updateKey = this.getNodeParameter('updateKey', i) as string;
|
2020-11-11 00:44:53 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const columnList = columns.split(',').map((column) => column.trim());
|
2020-11-11 00:44:53 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const entryId = items[i].json[updateKey];
|
2020-11-11 00:44:53 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
for (const key of Object.keys(items[i].json)) {
|
|
|
|
if (columnList.includes(key)) {
|
2022-08-17 08:50:24 -07:00
|
|
|
apiVersion === 'v4'
|
|
|
|
? (body.data = items[i].json)
|
|
|
|
: (body[key] = items[i].json[key]);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-11-11 00:44:53 -08:00
|
|
|
}
|
2022-03-12 01:56:34 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await strapiApiRequest.call(
|
|
|
|
this,
|
|
|
|
'PUT',
|
|
|
|
`/${contentType}/${entryId}`,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
undefined,
|
|
|
|
headers,
|
|
|
|
);
|
2022-08-30 08:55:33 -07:00
|
|
|
|
|
|
|
if (apiVersion === 'v4') {
|
|
|
|
responseData = responseData.data;
|
|
|
|
}
|
|
|
|
|
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2023-02-27 19:39:43 -08:00
|
|
|
this.helpers.returnJsonArray(responseData as IDataObject),
|
2022-08-30 08:55:33 -07:00
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionErrorData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionErrorData);
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
2020-11-11 00:44:53 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
throw error;
|
2020-11-11 00:44:53 -08:00
|
|
|
}
|
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
return this.prepareOutputData(returnData);
|
2020-11-11 00:44:53 -08:00
|
|
|
}
|
|
|
|
}
|