2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IExecuteFunctions,
|
2021-09-18 13:12:20 -07:00
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodePropertyOptions,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
2024-08-29 06:55:53 -07:00
|
|
|
import { NodeConnectionType } from 'n8n-workflow';
|
2021-09-18 13:12:20 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { netlifyApiRequest, netlifyRequestAllItems } from './GenericFunctions';
|
2021-09-18 13:12:20 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { deployFields, deployOperations } from './DeployDescription';
|
2021-09-18 13:12:20 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { siteFields, siteOperations } from './SiteDescription';
|
2021-09-18 13:12:20 -07:00
|
|
|
|
|
|
|
export class Netlify implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Netlify',
|
|
|
|
name: 'netlify',
|
|
|
|
icon: 'file:netlify.svg',
|
|
|
|
group: ['transform'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
|
|
|
description: 'Consume Netlify API',
|
|
|
|
defaults: {
|
|
|
|
name: 'Netlify',
|
|
|
|
},
|
2024-08-29 06:55:53 -07:00
|
|
|
inputs: [NodeConnectionType.Main],
|
|
|
|
outputs: [NodeConnectionType.Main],
|
2021-09-18 13:12:20 -07:00
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'netlifyApi',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2021-09-18 13:12:20 -07:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Deploy',
|
|
|
|
value: 'deploy',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Site',
|
|
|
|
value: 'site',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'deploy',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
...deployOperations,
|
|
|
|
...deployFields,
|
|
|
|
...siteOperations,
|
|
|
|
...siteFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
|
|
|
async getSites(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2022-08-17 08:50:24 -07:00
|
|
|
const sites = await netlifyApiRequest.call(this, 'GET', '/sites');
|
2021-09-18 13:12:20 -07:00
|
|
|
for (const site of sites) {
|
|
|
|
returnData.push({
|
|
|
|
name: site.name,
|
|
|
|
value: site.site_id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
2022-04-22 09:29:51 -07:00
|
|
|
const length = items.length;
|
2021-09-18 13:12:20 -07:00
|
|
|
let responseData;
|
2022-08-30 08:55:33 -07:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2021-09-18 13:12:20 -07:00
|
|
|
const qs: IDataObject = {};
|
|
|
|
const body: IDataObject = {};
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2021-09-18 13:12:20 -07:00
|
|
|
|
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
try {
|
|
|
|
if (resource === 'deploy') {
|
|
|
|
if (operation === 'cancel') {
|
|
|
|
const deployId = this.getNodeParameter('deployId', i);
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await netlifyApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
`/deploys/${deployId}/cancel`,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
);
|
2021-09-18 13:12:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'create') {
|
|
|
|
const siteId = this.getNodeParameter('siteId', i);
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2021-09-18 13:12:20 -07:00
|
|
|
|
|
|
|
Object.assign(body, additionalFields);
|
2021-12-23 04:30:35 -08:00
|
|
|
|
2021-09-18 13:12:20 -07:00
|
|
|
if (body.title) {
|
|
|
|
qs.title = body.title;
|
|
|
|
delete body.title;
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await netlifyApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
`/sites/${siteId}/deploys`,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
);
|
2021-09-18 13:12:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'get') {
|
|
|
|
const siteId = this.getNodeParameter('siteId', i);
|
|
|
|
const deployId = this.getNodeParameter('deployId', i);
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await netlifyApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/sites/${siteId}/deploys/${deployId}`,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
);
|
2021-09-18 13:12:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'getAll') {
|
|
|
|
const siteId = this.getNodeParameter('siteId', i);
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2022-12-02 12:54:28 -08:00
|
|
|
if (returnAll) {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await netlifyRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/sites/${siteId}/deploys`,
|
|
|
|
);
|
2021-09-18 13:12:20 -07:00
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
const limit = this.getNodeParameter('limit', i);
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await netlifyApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/sites/${siteId}/deploys`,
|
|
|
|
{},
|
|
|
|
{ per_page: limit },
|
|
|
|
);
|
2021-09-18 13:12:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (resource === 'site') {
|
|
|
|
if (operation === 'delete') {
|
|
|
|
const siteId = this.getNodeParameter('siteId', i);
|
|
|
|
responseData = await netlifyApiRequest.call(this, 'DELETE', `/sites/${siteId}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'get') {
|
|
|
|
const siteId = this.getNodeParameter('siteId', i);
|
|
|
|
responseData = await netlifyApiRequest.call(this, 'GET', `/sites/${siteId}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'getAll') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2022-12-02 12:54:28 -08:00
|
|
|
if (returnAll) {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await netlifyRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
2022-12-29 03:20:43 -08:00
|
|
|
'/sites',
|
2022-08-17 08:50:24 -07:00
|
|
|
{},
|
|
|
|
{ filter: 'all' },
|
|
|
|
);
|
2021-09-18 13:12:20 -07:00
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
const limit = this.getNodeParameter('limit', i);
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await netlifyApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
2022-12-29 03:20:43 -08:00
|
|
|
'/sites',
|
2022-08-17 08:50:24 -07:00
|
|
|
{},
|
|
|
|
{ filter: 'all', per_page: limit },
|
|
|
|
);
|
2021-09-18 13:12:20 -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);
|
2021-09-18 13:12:20 -07:00
|
|
|
} catch (error) {
|
2024-06-19 22:45:00 -07:00
|
|
|
if (this.continueOnFail(error)) {
|
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-09-18 13:12:20 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-05 03:59:02 -07:00
|
|
|
return [returnData];
|
2021-09-18 13:12:20 -07:00
|
|
|
}
|
2022-08-17 08:50:24 -07:00
|
|
|
}
|