2023-01-27 03:22:44 -08:00
|
|
|
import type { IExecuteFunctions } from 'n8n-core';
|
2021-11-25 09:48:52 -08:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2021-11-25 09:48:52 -08:00
|
|
|
ICredentialDataDecryptedObject,
|
|
|
|
ICredentialsDecrypted,
|
|
|
|
ICredentialTestFunctions,
|
|
|
|
IDataObject,
|
2022-02-05 13:55:43 -08:00
|
|
|
INodeCredentialTestResult,
|
2021-11-25 09:48:52 -08:00
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { dhlApiRequest, validateCredentials } from './GenericFunctions';
|
2021-11-25 09:48:52 -08:00
|
|
|
|
|
|
|
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: {
|
2022-08-01 13:47:55 -07:00
|
|
|
resource: ['shipment'],
|
2021-11-25 09:48:52 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Get Tracking Details',
|
|
|
|
value: 'get',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'Get tracking details for a shipment',
|
2021-11-25 09:48:52 -08:00
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'get',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Tracking Number',
|
|
|
|
name: 'trackingNumber',
|
|
|
|
type: 'string',
|
|
|
|
required: true,
|
|
|
|
default: '',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Options',
|
|
|
|
name: 'options',
|
|
|
|
type: 'collection',
|
|
|
|
placeholder: 'Add Option',
|
|
|
|
default: {},
|
|
|
|
options: [
|
|
|
|
{
|
2022-12-29 03:20:43 -08:00
|
|
|
displayName: "Recipient's Postal Code",
|
2021-11-25 09:48:52 -08:00
|
|
|
name: 'recipientPostalCode',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
2022-08-01 13:47:55 -07:00
|
|
|
description:
|
|
|
|
"DHL will return more detailed information on the shipment when you provide the Recipient's Postal Code - it acts as a verification step",
|
2021-11-25 09:48:52 -08:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
credentialTest: {
|
2022-08-01 13:47:55 -07:00
|
|
|
async dhlApiCredentialTest(
|
|
|
|
this: ICredentialTestFunctions,
|
|
|
|
credential: ICredentialsDecrypted,
|
|
|
|
): Promise<INodeCredentialTestResult> {
|
2021-11-25 09:48:52 -08:00
|
|
|
try {
|
2022-06-13 12:41:18 -07:00
|
|
|
await validateCredentials.call(this, credential.data as ICredentialDataDecryptedObject);
|
2021-11-25 09:48:52 -08:00
|
|
|
} 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;
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2021-11-25 09:48:52 -08:00
|
|
|
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
try {
|
|
|
|
if (resource === 'shipment') {
|
|
|
|
if (operation === 'get') {
|
|
|
|
const trackingNumber = this.getNodeParameter('trackingNumber', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const options = this.getNodeParameter('options', i);
|
2021-11-25 09:48:52 -08:00
|
|
|
|
|
|
|
qs = {
|
|
|
|
trackingNumber,
|
|
|
|
};
|
|
|
|
|
|
|
|
Object.assign(qs, options);
|
|
|
|
|
2022-12-29 03:20:43 -08:00
|
|
|
responseData = await dhlApiRequest.call(this, 'GET', '/track/shipments', {}, qs);
|
2021-11-25 09:48:52 -08:00
|
|
|
|
|
|
|
returnData.push(...responseData.shipments);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
|
|
|
returnData.push({ error: error.description });
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
|
|
}
|
|
|
|
}
|