cargowise-eadapter
Version:
CargoWise eAdapter client
60 lines (59 loc) • 2.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EAdapterClient = void 0;
const uuid_1 = require("uuid");
const compressMessage_1 = require("./compressMessage");
const setupSoap_1 = require("./setupSoap");
/**
* A simple eAdapter client
*/
class EAdapterClient {
constructor(config) {
this.config = config;
this.soapClient = setupSoap_1.setupSoap(config).then(({ client }) => client);
}
/**
* Simply ping the CW1 soap server to see if you are connected.
*
* @returns true indicating you are connected and authenticated.
*/
async ping() {
const client = await this.soapClient;
const [result] = await client.PingAsync({});
return result.PingResult;
}
/**
* This is how you send EDI messages into CW1
* CW1 doesn't respond to this. Instead you can use the tracking ids to look up in CW1 the responses.
*
* @param messages messages you wish to send
* @returns list of tracking ids. The order will be the same as your messages.
*/
async send(messages) {
const eHubMessages = await Promise.all(messages.map(async (msg) => {
const param = typeof msg === "string" ? { message: msg } : msg;
const compressedXml = await compressMessage_1.compressMessage(param.message);
return {
attributes: {
ClientID: param.clientId || this.config.clientId,
TrackingID: param.trackingId || uuid_1.v4(),
ApplicationCode: param.type === "native-xml" ? "NDM" : "UDM",
SchemaName: param.type === "native-xml"
? "http://www.cargowise.com/Schemas/Native#UniversalInterchange"
: "http://www.cargowise.com/Schemas/Universal/2011/11#UniversalInterchange",
SchemaType: "Xml",
},
$value: compressedXml,
};
}));
const trackingIds = eHubMessages.map((m) => m.attributes.TrackingID);
const client = await this.soapClient;
await client.SendStreamAsync({
Payload: {
Message: eHubMessages,
},
});
return trackingIds;
}
}
exports.EAdapterClient = EAdapterClient;