aramex-service-api
Version:
An Aramex client package for shipment operations using TypeScript and OOP.
248 lines (233 loc) • 7.61 kB
text/typescript
// src/AramexClient.ts
import moment from 'moment';
import path from 'path';
import { AramexClientConfig, ShipperDetails } from './types';
import {
dispatchSoapRequest,
generateConsignee,
generateShipmentDetails,
generateThirdParty
} from './utils/helper';
import getCountryCodeByCurrency from './utils/countryCodeFinder';
export class AramexClient {
private clientInfo: {
UserName: string;
Password: string;
Version: string;
AccountNumber: string;
AccountPin: string;
AccountEntity: string;
AccountCountryCode: string;
};
private shipper: ShipperDetails;
constructor(config: AramexClientConfig) {
this.clientInfo = {
UserName: config.username,
Password: config.password,
Version: 'v1.0',
AccountNumber: config.accountNumber,
AccountPin: config.accountPin,
AccountEntity: config.accountEntity,
AccountCountryCode: config.accountCountryCode
};
this.shipper = config.shipper;
}
/**
* Update the shipper details dynamically.
* @param shipper New shipper details.
*/
public setShipper(shipper: ShipperDetails): void {
this.shipper = shipper;
}
/**
* Create a shipment.
* @param order Order data.
* @returns The response from the Aramex API.
*/
public async createShipment(order: any): Promise<any> {
// Update shipper details dynamically (for example, set Reference1 to order.id)
const updatedShipper = { ...this.shipper, Reference1: `${order.id}` };
// Use the live WSDL URL for shipping
const wsdlPath = 'https://ws.aramex.net/ShippingAPI.V2/Shipping/Service_1_0.svc?wsdl';
const consignee = generateConsignee(order);
const shipmentDetails = generateShipmentDetails(order);
const thirdParty = generateThirdParty(order);
const payload = {
ShipmentCreationRequest: {
ClientInfo: this.clientInfo,
Shipments: {
Shipment: {
Shipper: updatedShipper,
Consignee: consignee,
ThirdParty: thirdParty,
Reference1: `${order.id}`,
Reference2: '',
Reference3: '',
ForeignHAWB: '',
TransportType: 0,
ShippingDateTime: moment().format(),
PickupLocation: 'Reception',
PickupGUID: '',
Comments: '',
AccountingInstrcutions: '',
OperationsInstructions: '',
Details: shipmentDetails
}
},
Transaction: null
}
};
return await dispatchSoapRequest(wsdlPath, 'CreateShipments', payload);
}
/**
* Track shipments.
* @param shipments A string identifier (or comma-separated IDs) for shipments.
* @returns The tracking response.
*/
public async trackShipments(shipments: string): Promise<any> {
const wsdlPath = path.resolve(__dirname, 'wsdl/tracking.wsdl');
const payload = {
ShipmentTrackingRequest: {
ClientInfo: this.clientInfo,
Shipments: { string: shipments }
}
};
return await dispatchSoapRequest(wsdlPath, 'TrackShipments', payload);
}
/**
* Print a shipment label.
* @param shipmentNumber The shipment number.
* @returns The label printing response.
*/
public async printShipmentLabel(shipmentNumber: string): Promise<any> {
try {
const wsdlPath = path.resolve(__dirname, 'wsdl/shipping.wsdl');
const payload = {
LabelPrintingRequest: {
ClientInfo: this.clientInfo,
ShipmentNumber: shipmentNumber,
LabelInfo: {
ReportID: '9729',
ReportType: 'URL'
}
}
};
return await dispatchSoapRequest(wsdlPath, 'PrintLabel', payload);
} catch (error) {
console.error('Error printing shipment label:', error);
throw error;
}
}
/**
* Validate an address.
* @param order Order data.
* @returns The address validation response.
*/
public async validateAddress(order: any): Promise<any> {
const address = {
Line1: order?.shipping?.address?.street || '',
Line2: order?.shipping?.address?.district || '',
Line3: order?.shipping?.address?.formatted_address || '',
City: order?.shipping?.address?.city?.name || '',
StateOrProvinceCode: order?.shipping?.address?.state || '',
PostCode: order?.shipping?.address?.postal_code || '00000',
CountryCode: getCountryCodeByCurrency(order?.currency_code) || 'SA'
};
try {
// Use the live WSDL URL for location services
const wsdlPath = 'https://ws.aramex.net/ShippingAPI/Location/Service_1_0.svc?wsdl';
const payload = {
AddressValidationRequest: {
ClientInfo: this.clientInfo,
Address: address,
Transaction: null
}
};
return await dispatchSoapRequest(wsdlPath, 'ValidateAddress', payload);
} catch (error) {
console.error('Error validating address:', error);
throw error;
}
}
/**
* Fetch all countries.
* @returns The countries fetching response.
*/
public async fetchAllCountries(): Promise<any> {
try {
const wsdlPath = path.resolve(__dirname, 'wsdl/location.wsdl');
const payload = {
CountriesFetchingRequest: {
ClientInfo: this.clientInfo,
Transaction: {
Reference1: 'FetchCountries',
Reference2: '',
Reference3: '',
Reference4: '',
Reference5: ''
}
}
};
return await dispatchSoapRequest(wsdlPath, 'FetchCountries', payload);
} catch (error) {
console.error('Error fetching countries:', error);
throw error;
}
}
/**
* Fetch cities by country code.
* @param countryCode The country code.
* @returns The cities fetching response.
*/
public async fetchCitiesByCountry(countryCode: string): Promise<any> {
try {
const wsdlPath = path.resolve(__dirname, 'wsdl/location.wsdl');
const payload = {
CitiesFetchingRequest: {
ClientInfo: this.clientInfo,
Transaction: {
Reference1: 'FetchCities',
Reference2: '',
Reference3: '',
Reference4: '',
Reference5: ''
},
CountryCode: countryCode,
State: '',
NameStartsWith: ''
}
};
return await dispatchSoapRequest(wsdlPath, 'FetchCities', payload);
} catch (error) {
console.error('Error fetching cities:', error);
throw error;
}
}
/**
* Fetch offices by country code.
* @param countryCode The country code.
* @returns The offices fetching response.
*/
public async fetchOfficesByCountry(countryCode: string): Promise<any> {
try {
const wsdlPath = path.resolve(__dirname, 'wsdl/location.wsdl');
const payload = {
OfficesFetchingRequest: {
ClientInfo: this.clientInfo,
Transaction: {
Reference1: 'FetchOffices',
Reference2: '',
Reference3: '',
Reference4: '',
Reference5: ''
},
CountryCode: countryCode
}
};
return await dispatchSoapRequest(wsdlPath, 'FetchOffices', payload);
} catch (error) {
console.error('Error fetching offices:', error);
throw error;
}
}
}