mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-10 14:44:05 -08:00
61e26804ba
* ⚡ enabled array-type * ⚡ await-thenable on * ⚡ ban-types on * ⚡ default-param-last on * ⚡ dot-notation on * ⚡ member-delimiter-style on * ⚡ no-duplicate-imports on * ⚡ no-empty-interface on * ⚡ no-floating-promises on * ⚡ no-for-in-array on * ⚡ no-invalid-void-type on * ⚡ no-loop-func on * ⚡ no-shadow on * ⚡ ban-ts-comment re enabled * ⚡ @typescript-eslint/lines-between-class-members on * address my own comment * @typescript-eslint/return-await on * @typescript-eslint/promise-function-async on * @typescript-eslint/no-unnecessary-boolean-literal-compare on * @typescript-eslint/no-unnecessary-type-assertion on * prefer-const on * @typescript-eslint/prefer-optional-chain on Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
179 lines
4.9 KiB
TypeScript
179 lines
4.9 KiB
TypeScript
import { IExecuteFunctions } from 'n8n-core';
|
|
|
|
import {
|
|
IDataObject,
|
|
ILoadOptionsFunctions,
|
|
INodeExecutionData,
|
|
INodePropertyOptions,
|
|
INodeType,
|
|
INodeTypeDescription,
|
|
} from 'n8n-workflow';
|
|
|
|
import { cloudflareApiRequest, cloudflareApiRequestAllItems } from './GenericFunctions';
|
|
|
|
import { zoneCertificateFields, zoneCertificateOperations } from './ZoneCertificateDescription';
|
|
|
|
export class Cloudflare implements INodeType {
|
|
description: INodeTypeDescription = {
|
|
displayName: 'Cloudflare',
|
|
name: 'cloudflare',
|
|
icon: 'file:cloudflare.svg',
|
|
group: ['input'],
|
|
version: 1,
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
|
description: 'Consume Cloudflare API',
|
|
defaults: {
|
|
name: 'Cloudflare',
|
|
},
|
|
inputs: ['main'],
|
|
outputs: ['main'],
|
|
credentials: [
|
|
{
|
|
name: 'cloudflareApi',
|
|
required: true,
|
|
},
|
|
],
|
|
properties: [
|
|
{
|
|
displayName: 'Resource',
|
|
name: 'resource',
|
|
type: 'options',
|
|
noDataExpression: true,
|
|
options: [
|
|
{
|
|
name: 'Zone Certificate',
|
|
value: 'zoneCertificate',
|
|
},
|
|
],
|
|
default: 'zoneCertificate',
|
|
},
|
|
...zoneCertificateOperations,
|
|
...zoneCertificateFields,
|
|
],
|
|
};
|
|
|
|
methods = {
|
|
loadOptions: {
|
|
async getZones(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
const returnData: INodePropertyOptions[] = [];
|
|
const { result: zones } = await cloudflareApiRequest.call(this, 'GET', '/zones');
|
|
for (const zone of zones) {
|
|
returnData.push({
|
|
name: zone.name,
|
|
value: zone.id,
|
|
});
|
|
}
|
|
return returnData;
|
|
},
|
|
},
|
|
};
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
const items = this.getInputData();
|
|
const returnData: IDataObject[] = [];
|
|
const length = items.length;
|
|
const qs: IDataObject = {};
|
|
let responseData;
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
const operation = this.getNodeParameter('operation', 0);
|
|
|
|
for (let i = 0; i < length; i++) {
|
|
try {
|
|
if (resource === 'zoneCertificate') {
|
|
//https://api.cloudflare.com/#zone-level-authenticated-origin-pulls-delete-certificate
|
|
if (operation === 'delete') {
|
|
const zoneId = this.getNodeParameter('zoneId', i) as string;
|
|
const certificateId = this.getNodeParameter('certificateId', i) as string;
|
|
|
|
responseData = await cloudflareApiRequest.call(
|
|
this,
|
|
'DELETE',
|
|
`/zones/${zoneId}/origin_tls_client_auth/${certificateId}`,
|
|
{},
|
|
);
|
|
responseData = responseData.result;
|
|
}
|
|
//https://api.cloudflare.com/#zone-level-authenticated-origin-pulls-get-certificate-details
|
|
if (operation === 'get') {
|
|
const zoneId = this.getNodeParameter('zoneId', i) as string;
|
|
const certificateId = this.getNodeParameter('certificateId', i) as string;
|
|
|
|
responseData = await cloudflareApiRequest.call(
|
|
this,
|
|
'GET',
|
|
`/zones/${zoneId}/origin_tls_client_auth/${certificateId}`,
|
|
{},
|
|
);
|
|
responseData = responseData.result;
|
|
}
|
|
//https://api.cloudflare.com/#zone-level-authenticated-origin-pulls-list-certificates
|
|
if (operation === 'getMany') {
|
|
const zoneId = this.getNodeParameter('zoneId', i) as string;
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
|
const filters = this.getNodeParameter('filters', i, {});
|
|
|
|
Object.assign(qs, filters);
|
|
|
|
if (returnAll) {
|
|
responseData = await cloudflareApiRequestAllItems.call(
|
|
this,
|
|
'result',
|
|
'GET',
|
|
`/zones/${zoneId}/origin_tls_client_auth`,
|
|
{},
|
|
qs,
|
|
);
|
|
} else {
|
|
const limit = this.getNodeParameter('limit', i);
|
|
Object.assign(qs, { per_page: limit });
|
|
responseData = await cloudflareApiRequest.call(
|
|
this,
|
|
'GET',
|
|
`/zones/${zoneId}/origin_tls_client_auth`,
|
|
{},
|
|
qs,
|
|
);
|
|
responseData = responseData.result;
|
|
}
|
|
}
|
|
//https://api.cloudflare.com/#zone-level-authenticated-origin-pulls-upload-certificate
|
|
if (operation === 'upload') {
|
|
const zoneId = this.getNodeParameter('zoneId', i) as string;
|
|
const certificate = this.getNodeParameter('certificate', i) as string;
|
|
const privateKey = this.getNodeParameter('privateKey', i) as string;
|
|
|
|
const body: IDataObject = {
|
|
certificate,
|
|
private_key: privateKey,
|
|
};
|
|
|
|
responseData = await cloudflareApiRequest.call(
|
|
this,
|
|
'POST',
|
|
`/zones/${zoneId}/origin_tls_client_auth`,
|
|
body,
|
|
qs,
|
|
);
|
|
|
|
responseData = responseData.result;
|
|
}
|
|
}
|
|
|
|
returnData.push(
|
|
...this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray(responseData), {
|
|
itemData: { item: i },
|
|
}),
|
|
);
|
|
} catch (error) {
|
|
if (this.continueOnFail()) {
|
|
returnData.push({ json: { error: error.message } });
|
|
continue;
|
|
}
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
return [returnData as INodeExecutionData[]];
|
|
}
|
|
}
|