mirror of
https://github.com/n8n-io/n8n.git
synced 2025-01-11 04:47:29 -08:00
✨ Add DHL node (#2385)
* ✨ DHL node * ⚡ Add credentials verfication * 👕 Nodelinter pass * ⚡ Improvements * ⚡ Fix node name Co-authored-by: Iván Ovejero <ivov.src@gmail.com> Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
This commit is contained in:
parent
5fd3f8a244
commit
a58c251a28
18
packages/nodes-base/credentials/DhlApi.credentials.ts
Normal file
18
packages/nodes-base/credentials/DhlApi.credentials.ts
Normal file
|
@ -0,0 +1,18 @@
|
|||
import {
|
||||
ICredentialType,
|
||||
INodeProperties,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
export class DhlApi implements ICredentialType {
|
||||
name = 'dhlApi';
|
||||
displayName = 'DHL API';
|
||||
documentationUrl = 'dhl';
|
||||
properties: INodeProperties[] = [
|
||||
{
|
||||
displayName: 'API Key',
|
||||
name: 'apiKey',
|
||||
type: 'string',
|
||||
default: '',
|
||||
},
|
||||
];
|
||||
}
|
162
packages/nodes-base/nodes/Dhl/Dhl.node.ts
Normal file
162
packages/nodes-base/nodes/Dhl/Dhl.node.ts
Normal file
|
@ -0,0 +1,162 @@
|
|||
import {
|
||||
IExecuteFunctions,
|
||||
} from 'n8n-core';
|
||||
|
||||
import {
|
||||
ICredentialDataDecryptedObject,
|
||||
ICredentialsDecrypted,
|
||||
ICredentialTestFunctions,
|
||||
IDataObject,
|
||||
INodeExecutionData,
|
||||
INodeType,
|
||||
INodeTypeDescription,
|
||||
NodeCredentialTestResult,
|
||||
} 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',
|
||||
color: '#fecc00',
|
||||
},
|
||||
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<NodeCredentialTestResult> {
|
||||
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)];
|
||||
}
|
||||
}
|
65
packages/nodes-base/nodes/Dhl/GenericFunctions.ts
Normal file
65
packages/nodes-base/nodes/Dhl/GenericFunctions.ts
Normal file
|
@ -0,0 +1,65 @@
|
|||
import {
|
||||
OptionsWithUri,
|
||||
} from 'request';
|
||||
|
||||
import {
|
||||
IExecuteFunctions,
|
||||
IExecuteSingleFunctions,
|
||||
IHookFunctions,
|
||||
ILoadOptionsFunctions,
|
||||
} from 'n8n-core';
|
||||
|
||||
import {
|
||||
ICredentialDataDecryptedObject,
|
||||
ICredentialTestFunctions,
|
||||
IDataObject,
|
||||
NodeApiError,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
export async function dhlApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, path: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
||||
|
||||
const credentials = await this.getCredentials('dhlApi') as { apiKey: string };
|
||||
|
||||
let options: OptionsWithUri = {
|
||||
headers: {
|
||||
'DHL-API-Key': credentials.apiKey,
|
||||
},
|
||||
method,
|
||||
qs,
|
||||
body,
|
||||
uri: uri || `https://api-eu.dhl.com${path}`,
|
||||
json: true,
|
||||
};
|
||||
options = Object.assign({}, options, option);
|
||||
if (Object.keys(options.body).length === 0) {
|
||||
delete options.body;
|
||||
}
|
||||
|
||||
try {
|
||||
return await this.helpers.request!(options);
|
||||
} catch (error) {
|
||||
throw new NodeApiError(this.getNode(), error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function validateCrendetials(this: ICredentialTestFunctions, decryptedCredentials: ICredentialDataDecryptedObject): Promise<any> { // tslint:disable-line:no-any
|
||||
const credentials = decryptedCredentials;
|
||||
|
||||
const { apiKey } = credentials as {
|
||||
apiKey: string,
|
||||
};
|
||||
|
||||
const options: OptionsWithUri = {
|
||||
headers: {
|
||||
'DHL-API-Key': apiKey,
|
||||
},
|
||||
qs: {
|
||||
trackingNumber: 123,
|
||||
},
|
||||
method: 'GET',
|
||||
uri: `https://api-eu.dhl.com/track/shipments`,
|
||||
json: true,
|
||||
};
|
||||
|
||||
return this.helpers.request!(options);
|
||||
}
|
21
packages/nodes-base/nodes/Dhl/dhl.svg
Normal file
21
packages/nodes-base/nodes/Dhl/dhl.svg
Normal file
|
@ -0,0 +1,21 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- Generator: Adobe Illustrator 24.2.1, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
|
||||
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
|
||||
viewBox="0 0 60 60" style="enable-background:new 0 0 60 60;" xml:space="preserve">
|
||||
<style type="text/css">
|
||||
.st0{fill:#FFCC00;}
|
||||
.st1{fill:#D40511;}
|
||||
</style>
|
||||
<path class="st0" d="M56,60H4c-2.2,0-4-1.8-4-4V4c0-2.2,1.8-4,4-4h52c2.2,0,4,1.8,4,4v52C60,58.2,58.2,60,56,60z"/>
|
||||
<g>
|
||||
<g>
|
||||
<g>
|
||||
<path class="st1" d="M16.2,32.3H6v2h8.7L16.2,32.3z M18.9,28.6H6v2h11.4L18.9,28.6z M36.1,27.9c-0.5,0.7-1.3,1.8-1.8,2.5
|
||||
c-0.3,0.3-0.7,1,0.8,1h7.8c0,0,1.3-1.8,2.4-3.2c1.5-2,0.1-6.2-5.2-6.2H19.2l-3.6,4.9h20C36.6,26.9,36.6,27.3,36.1,27.9z M6,38h6
|
||||
l1.5-2H6V38z M40.2,38H54v-2H41.6L40.2,38z M47.1,28.6l-1.5,2H54v-2H47.1z M41.6,33.1h-8.9l-3.4,0c-1.5,0-1.1-0.6-0.8-1
|
||||
c0.5-0.7,1.4-1.8,1.8-2.5c0.5-0.7,0.8-1-0.2-1h-9L14.2,38h17.7C37.1,38,40.3,34.8,41.6,33.1z M42.9,34.3H54v-2h-9.6L42.9,34.3z"
|
||||
/>
|
||||
</g>
|
||||
</g>
|
||||
</g>
|
||||
</svg>
|
After Width: | Height: | Size: 1.1 KiB |
|
@ -71,6 +71,7 @@
|
|||
"dist/credentials/CustomerIoApi.credentials.js",
|
||||
"dist/credentials/DeepLApi.credentials.js",
|
||||
"dist/credentials/DemioApi.credentials.js",
|
||||
"dist/credentials/DhlApi.credentials.js",
|
||||
"dist/credentials/DiscourseApi.credentials.js",
|
||||
"dist/credentials/DisqusApi.credentials.js",
|
||||
"dist/credentials/DriftApi.credentials.js",
|
||||
|
@ -374,6 +375,7 @@
|
|||
"dist/nodes/DateTime/DateTime.node.js",
|
||||
"dist/nodes/DeepL/DeepL.node.js",
|
||||
"dist/nodes/Demio/Demio.node.js",
|
||||
"dist/nodes/Dhl/Dhl.node.js",
|
||||
"dist/nodes/Discord/Discord.node.js",
|
||||
"dist/nodes/Discourse/Discourse.node.js",
|
||||
"dist/nodes/Disqus/Disqus.node.js",
|
||||
|
|
Loading…
Reference in a new issue