n8n/packages/nodes-base/nodes/Dhl/Dhl.node.ts
Iván Ovejero 63b6c9f128
refactor: Apply more eslint-plugin-n8n-nodes-base autofixable rules (#3243)
* ✏️ Alphabetize rules

* 🔖 Update version

*  Update lintfix command

*  Run baseline lintfix

* 📦 Update package-lock.json

* 👕 Apply `node-param-description-untrimmed` (#3200)

* Removing unneeded backticks (#3249)

* 👕 Apply node-param-description-wrong-for-return-all (#3253)

* 👕 Apply node-param-description-missing-limit (#3252)

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

* 👕 Apply node-param-description-excess-final-period (#3250)

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

* 👕 Apply node-param-description-unencoded-angle-brackets (#3256)

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

* 👕 Apply node-param-description-url-missing-protocol (#3258)

* 👕 Apply `node-param-description-miscased-id` (#3254)

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

* 👕 Apply node-param-description-wrong-for-limit (#3257)

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

* 👕 Apply node-param-description-wrong-for-ignore-ssl-issues (#3261)

* 👕 Apply rule

*  Restore lintfix script

*  Restore lintfix script

Co-authored-by: agobrech <45268029+agobrech@users.noreply.github.com>
Co-authored-by: Omar Ajoue <krynble@gmail.com>
Co-authored-by: Michael Kret <88898367+michael-radency@users.noreply.github.com>
2022-05-06 23:01:25 +02:00

162 lines
3.5 KiB
TypeScript

import {
IExecuteFunctions,
} from 'n8n-core';
import {
ICredentialDataDecryptedObject,
ICredentialsDecrypted,
ICredentialTestFunctions,
IDataObject,
INodeCredentialTestResult,
INodeExecutionData,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import {
dhlApiRequest,
validateCrendetials,
} from './GenericFunctions';
export class Dhl implements INodeType {
description: INodeTypeDescription = {
displayName: 'DHL',
name: 'dhl',
icon: 'file:dhl.svg',
group: ['input'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Consume DHL API',
defaults: {
name: 'DHL',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'dhlApi',
required: true,
testedBy: 'dhlApiCredentialTest',
},
],
properties: [
{
displayName: 'Resource',
name: 'resource',
noDataExpression: true,
type: 'hidden',
options: [
{
name: 'Shipment',
value: 'shipment',
},
],
default: 'shipment',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: [
'shipment',
],
},
},
options: [
{
name: 'Get Tracking Details',
value: 'get',
},
],
default: 'get',
},
{
displayName: 'Tracking Number',
name: 'trackingNumber',
type: 'string',
required: true,
default: '',
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add Option',
default: {},
options: [
{
displayName: `Recipient's Postal Code`,
name: 'recipientPostalCode',
type: 'string',
default: '',
description: 'DHL will return more detailed information on the shipment when you provide the Recipient\'s Postal Code - it acts as a verification step',
},
],
},
],
};
methods = {
credentialTest: {
async dhlApiCredentialTest(this: ICredentialTestFunctions, credential: ICredentialsDecrypted): Promise<INodeCredentialTestResult> {
try {
await validateCrendetials.call(this, credential.data as ICredentialDataDecryptedObject);
} catch (error) {
if (error.statusCode === 401) {
return {
status: 'Error',
message: 'The API Key included in the request is invalid',
};
}
}
return {
status: 'OK',
message: 'Connection successful!',
};
},
},
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: IDataObject[] = [];
let qs: IDataObject = {};
let responseData;
const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
for (let i = 0; i < items.length; i++) {
try {
if (resource === 'shipment') {
if (operation === 'get') {
const trackingNumber = this.getNodeParameter('trackingNumber', i) as string;
const options = this.getNodeParameter('options', i) as IDataObject;
qs = {
trackingNumber,
};
Object.assign(qs, options);
responseData = await dhlApiRequest.call(this, 'GET', `/track/shipments`, {}, qs);
returnData.push(...responseData.shipments);
}
}
} catch (error) {
if (this.continueOnFail()) {
returnData.push({ error: error.description });
continue;
}
throw error;
}
}
return [this.helpers.returnJsonArray(returnData)];
}
}