aramex-service-api
Version:
An Aramex client package for shipment operations using TypeScript and OOP.
231 lines • 9.88 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AramexClient = void 0;
// src/AramexClient.ts
const moment_1 = __importDefault(require("moment"));
const path_1 = __importDefault(require("path"));
const helper_1 = require("./utils/helper");
const countryCodeFinder_1 = __importDefault(require("./utils/countryCodeFinder"));
class AramexClient {
constructor(config) {
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.
*/
setShipper(shipper) {
this.shipper = shipper;
}
/**
* Create a shipment.
* @param order Order data.
* @returns The response from the Aramex API.
*/
async createShipment(order) {
// Update shipper details dynamically (for example, set Reference1 to order.id)
const updatedShipper = Object.assign(Object.assign({}, 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 = (0, helper_1.generateConsignee)(order);
const shipmentDetails = (0, helper_1.generateShipmentDetails)(order);
const thirdParty = (0, helper_1.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: (0, moment_1.default)().format(),
PickupLocation: 'Reception',
PickupGUID: '',
Comments: '',
AccountingInstrcutions: '',
OperationsInstructions: '',
Details: shipmentDetails
}
},
Transaction: null
}
};
return await (0, helper_1.dispatchSoapRequest)(wsdlPath, 'CreateShipments', payload);
}
/**
* Track shipments.
* @param shipments A string identifier (or comma-separated IDs) for shipments.
* @returns The tracking response.
*/
async trackShipments(shipments) {
const wsdlPath = path_1.default.resolve(__dirname, 'wsdl/tracking.wsdl');
const payload = {
ShipmentTrackingRequest: {
ClientInfo: this.clientInfo,
Shipments: { string: shipments }
}
};
return await (0, helper_1.dispatchSoapRequest)(wsdlPath, 'TrackShipments', payload);
}
/**
* Print a shipment label.
* @param shipmentNumber The shipment number.
* @returns The label printing response.
*/
async printShipmentLabel(shipmentNumber) {
try {
const wsdlPath = path_1.default.resolve(__dirname, 'wsdl/shipping.wsdl');
const payload = {
LabelPrintingRequest: {
ClientInfo: this.clientInfo,
ShipmentNumber: shipmentNumber,
LabelInfo: {
ReportID: '9729',
ReportType: 'URL'
}
}
};
return await (0, helper_1.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.
*/
async validateAddress(order) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
const address = {
Line1: ((_b = (_a = order === null || order === void 0 ? void 0 : order.shipping) === null || _a === void 0 ? void 0 : _a.address) === null || _b === void 0 ? void 0 : _b.street) || '',
Line2: ((_d = (_c = order === null || order === void 0 ? void 0 : order.shipping) === null || _c === void 0 ? void 0 : _c.address) === null || _d === void 0 ? void 0 : _d.district) || '',
Line3: ((_f = (_e = order === null || order === void 0 ? void 0 : order.shipping) === null || _e === void 0 ? void 0 : _e.address) === null || _f === void 0 ? void 0 : _f.formatted_address) || '',
City: ((_j = (_h = (_g = order === null || order === void 0 ? void 0 : order.shipping) === null || _g === void 0 ? void 0 : _g.address) === null || _h === void 0 ? void 0 : _h.city) === null || _j === void 0 ? void 0 : _j.name) || '',
StateOrProvinceCode: ((_l = (_k = order === null || order === void 0 ? void 0 : order.shipping) === null || _k === void 0 ? void 0 : _k.address) === null || _l === void 0 ? void 0 : _l.state) || '',
PostCode: ((_o = (_m = order === null || order === void 0 ? void 0 : order.shipping) === null || _m === void 0 ? void 0 : _m.address) === null || _o === void 0 ? void 0 : _o.postal_code) || '00000',
CountryCode: (0, countryCodeFinder_1.default)(order === null || order === void 0 ? void 0 : 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 (0, helper_1.dispatchSoapRequest)(wsdlPath, 'ValidateAddress', payload);
}
catch (error) {
console.error('Error validating address:', error);
throw error;
}
}
/**
* Fetch all countries.
* @returns The countries fetching response.
*/
async fetchAllCountries() {
try {
const wsdlPath = path_1.default.resolve(__dirname, 'wsdl/location.wsdl');
const payload = {
CountriesFetchingRequest: {
ClientInfo: this.clientInfo,
Transaction: {
Reference1: 'FetchCountries',
Reference2: '',
Reference3: '',
Reference4: '',
Reference5: ''
}
}
};
return await (0, helper_1.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.
*/
async fetchCitiesByCountry(countryCode) {
try {
const wsdlPath = path_1.default.resolve(__dirname, 'wsdl/location.wsdl');
const payload = {
CitiesFetchingRequest: {
ClientInfo: this.clientInfo,
Transaction: {
Reference1: 'FetchCities',
Reference2: '',
Reference3: '',
Reference4: '',
Reference5: ''
},
CountryCode: countryCode,
State: '',
NameStartsWith: ''
}
};
return await (0, helper_1.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.
*/
async fetchOfficesByCountry(countryCode) {
try {
const wsdlPath = path_1.default.resolve(__dirname, 'wsdl/location.wsdl');
const payload = {
OfficesFetchingRequest: {
ClientInfo: this.clientInfo,
Transaction: {
Reference1: 'FetchOffices',
Reference2: '',
Reference3: '',
Reference4: '',
Reference5: ''
},
CountryCode: countryCode
}
};
return await (0, helper_1.dispatchSoapRequest)(wsdlPath, 'FetchOffices', payload);
}
catch (error) {
console.error('Error fetching offices:', error);
throw error;
}
}
}
exports.AramexClient = AramexClient;
//# sourceMappingURL=AramexClient.js.map