2022-08-17 08:50:24 -07:00
|
|
|
import { IExecuteFunctions } from 'n8n-core';
|
2021-09-18 13:12:20 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodePropertyOptions,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
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',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
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 = {};
|
|
|
|
const resource = this.getNodeParameter('resource', 0) as string;
|
|
|
|
const operation = this.getNodeParameter('operation', 0) as string;
|
|
|
|
|
|
|
|
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);
|
2021-09-18 13:12:20 -07:00
|
|
|
if (returnAll === true) {
|
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);
|
2021-09-18 13:12:20 -07:00
|
|
|
if (returnAll === true) {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await netlifyRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/sites`,
|
|
|
|
{},
|
|
|
|
{ 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',
|
|
|
|
`/sites`,
|
|
|
|
{},
|
|
|
|
{ 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(
|
|
|
|
this.helpers.returnJsonArray(responseData),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
|
|
|
|
returnData.push(...executionData);
|
2021-09-18 13:12:20 -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-09-18 13:12:20 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-30 08:55:33 -07:00
|
|
|
return this.prepareOutputData(returnData);
|
2021-09-18 13:12:20 -07:00
|
|
|
}
|
2022-08-17 08:50:24 -07:00
|
|
|
}
|