UNPKG

@dgac/nmb2b-client

Version:

EUROCONTROL Network Manager B2B SOAP client

603 lines (579 loc) 16.8 kB
// src/Flow/index.ts import { createClient } from "soap"; // src/constants.ts import path from "path"; var B2B_VERSION = "27.0.0"; var getWSDLPath = ({ service, flavour, XSD_PATH }) => path.join( XSD_PATH, `${B2B_VERSION}/${service}_${flavour}_${B2B_VERSION}.wsdl` ); // src/security.ts import invariant from "invariant"; // src/utils/debug.ts import d from "debug"; var PREFIX = "@dgac/nmb2b-client"; var debug = d(PREFIX); function log(ns) { if (!ns) { return debug; } return debug.extend(ns); } var debug_default = log; // src/security.ts import { ClientSSLSecurity, ClientSSLSecurityPFX, BasicAuthSecurity } from "soap"; import fs from "fs"; var debug2 = debug_default("security"); function prepareSecurity(config) { const { security } = config; if ("apiKeyId" in security) { const { apiKeyId, apiSecretKey } = security; debug2("Using ApiGateway security"); return new BasicAuthSecurity(apiKeyId, apiSecretKey); } else if ("pfx" in security) { const { pfx, passphrase } = security; debug2("Using PFX certificates"); return new ClientSSLSecurityPFX(pfx, passphrase); } else if ("cert" in security) { debug2("Using PEM certificates"); const { key, cert, passphrase } = security; return new ClientSSLSecurity( key, cert, void 0, passphrase ? { passphrase } : null ); } throw new Error("Invalid security object"); } // src/utils/transformers/types.ts import { UTCDate } from "@date-fns/utc"; import { format } from "date-fns"; // src/utils/timeFormats.ts var timeFormat = "yyyy-MM-dd HH:mm"; var dateFormat = "yyyy-MM-dd"; var timeFormatWithSeconds = timeFormat + ":ss"; // src/utils/transformers/types.ts var outputBase = { integer: (text) => { return parseInt(text, 10); }, /** * * Parse a NMB2B date/datetime. * * All datetimes are assumed to be UTC. * * Per NM B2B documentation, we only need to support these formats: * - DateTimeMinute: YYYY-MM-DD hh:mm * - DateTimeSecond: YYYY-MM-DD hh:mm:ss * - DateYearMonthDay: YYYY-MM-DD * * All dates are * @param text NM B2B Date string * @returns Parsed Date instance */ date: (text) => { let [date, time] = text.split(" "); if (!time) { return new Date(text); } if (time.length === 5) { time += ":00"; } return /* @__PURE__ */ new Date(`${date}T${time}Z`); } }; var types = { FlightLevel_DataType: { input: null, output: outputBase.integer }, DurationHourMinute: { input: (d2) => { const totalMinutes = Math.floor(d2 / 60); const hours = Math.floor(totalMinutes / 60); const minutes = totalMinutes % 60; return `${hours}`.padStart(2, "0") + `${minutes}`.padStart(2, "0"); }, output: (s) => { const hours = parseInt(s.slice(0, 2), 10); const minutes = parseInt(s.slice(2), 10); return 60 * (60 * hours + minutes); } }, DurationHourMinuteSecond: { input: (d2) => { const totalMinutes = Math.floor(d2 / 60); const hours = Math.floor(totalMinutes / 60); const minutes = totalMinutes % 60; return `${hours}`.padStart(2, "0") + `${minutes}`.padStart(2, "0") + `${d2 % 60}`.padStart(2, "0"); }, output: (s) => { const hours = parseInt(s.slice(0, 2), 10); const minutes = parseInt(s.slice(2, 4), 10); const seconds = parseInt(s.slice(4), 10); return 3600 * hours + 60 * minutes + seconds; } }, DurationMinute: { input: (d2) => Math.floor(d2 / 60), output: (d2) => 60 * d2 }, CountsValue: { input: null, output: outputBase.integer }, DateTimeMinute: { input: (d2) => format(new UTCDate(d2), timeFormat), output: outputBase.date }, DateYearMonthDay: { input: (d2) => format(new UTCDate(d2), dateFormat), output: outputBase.date }, DateTimeSecond: { input: (d2) => format(new UTCDate(d2), timeFormatWithSeconds), output: outputBase.date }, DistanceNM: { input: null, output: outputBase.integer }, DistanceM: { input: null, output: outputBase.integer }, Bearing: { input: null, output: outputBase.integer }, OTMVThreshold: { input: null, output: outputBase.integer } }; // src/utils/transformers/serializer.ts import { piped, identity, evolve, map } from "remeda"; function prepareSerializer(schema) { const transformer = prepareTransformer(schema); return piped( reorderKeys(schema), transformer ? evolve(transformer) : identity // (obj) => { // console.log(JSON.stringify(obj, null, 2)); // return obj; // }, ); } function reduceXSDType(str) { return str.split("|")[0]; } function prepareTransformer(schema) { return Object.keys(schema).reduce((prev, curr) => { let key = curr; let isArray = false; if (curr.endsWith("[]")) { key = curr.slice(0, -2); isArray = true; } if (typeof schema[curr] === "string") { const type = reduceXSDType(schema[curr]); if (types[type]?.input) { const transformer = types[type].input; return { ...prev, [key]: isArray ? map(transformer) : transformer }; } } else if (typeof schema[curr] === "object") { const subItem = prepareTransformer(schema[curr]); if (subItem) { return { ...prev, [key]: isArray ? map(evolve(subItem)) : subItem }; } } return prev; }, null); } function reorderKeys(schema) { return (obj) => { return Object.keys(schema).reduce((prev, curr) => { const lookupKey = curr.replace(/\[\]$/, ""); const isArrayExpected = curr.endsWith("[]"); if (!(lookupKey in obj)) { return prev; } const currSchema = schema[curr]; if (typeof currSchema === "string") { prev[lookupKey] = obj[lookupKey]; return prev; } if (typeof currSchema === "object") { if (Object.keys(currSchema).filter( (k) => k !== "targetNSAlias" && k !== "targetNamespace" ).length) { prev[lookupKey] = isArrayExpected && obj[lookupKey] && Array.isArray(obj[lookupKey]) ? obj[lookupKey].map(reorderKeys(currSchema)) : reorderKeys(currSchema)(obj[lookupKey]); return prev; } prev[lookupKey] = obj[lookupKey]; return prev; } return prev; }, {}); }; } // src/utils/transformers/index.ts var deserializer = Object.entries(types).reduce((prev, [key, { output }]) => { if (output) { prev[key] = output; } return prev; }, {}); // src/utils/NMB2BError.ts var NMB2BError = class extends Error { constructor({ reply }) { super(); if (reply.requestId) { this.requestId = reply.requestId; } if (reply.requestReceptionTime) { this.requestReceptionTime = reply.requestReceptionTime; } if (reply.sendTime) { this.sendTime = reply.sendTime; } if (reply.inputValidationErrors) { this.inputValidationErrors = reply.inputValidationErrors; } if (reply.warnings) { this.warnings = reply.warnings; } if (reply.slaError) { this.slaError = reply.slaError; } if (reply.reason) { this.reason = reply.reason; } this.status = reply.status; this.message = this.status; if (this.reason) { this.message = `${this.message}: ${this.reason}`; } } }; // src/utils/internals.ts function injectSendTime(values) { const sendTime = /* @__PURE__ */ new Date(); if (!values || typeof values !== "object") { return { sendTime }; } return { sendTime, ...values }; } function responseStatusHandler(resolve, reject) { return (err, reply) => { if (err) { reject(err); return; } if (reply.status === "OK") { resolve(reply); return; } else { const err2 = new NMB2BError({ reply }); reject(err2); return; } }; } // src/utils/instrumentation/withLog.ts function withLog(namespace) { const debug3 = debug_default(namespace); return (fn) => (values, options) => { if (values) { debug3("Called with input %o", values); } else { debug3("Called"); } return fn(values, options).then( (res) => { debug3("Succeded"); return res; }, (err) => { debug3("Failed"); return Promise.reject( err instanceof Error ? err : new Error("Unknown error", { cause: err }) ); } ); }; } // src/utils/instrumentation/index.ts import { pipe } from "remeda"; function instrument({ service, query }) { return (fn) => pipe(fn, withLog(`${service}:${query}`)); } // src/Flow/queryHotspots.ts function prepareQueryHotspots(client) { const schema = ( // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access client.describe().TacticalUpdatesService.TacticalUpdatesPort.queryHotspots.input ); const serializer = prepareSerializer(schema); return instrument({ service: "Flow", query: "queryHotspots" })( (values, options) => new Promise((resolve, reject) => { client.queryHotspots( serializer(injectSendTime(values)), options, responseStatusHandler(resolve, reject) ); }) ); } // src/Flow/queryRegulations.ts function prepareQueryRegulations(client) { const schema = ( // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access client.describe().MeasuresService.MeasuresPort.queryRegulations.input ); const serializer = prepareSerializer(schema); return instrument({ service: "Flow", query: "queryRegulations" })( (values, options) => new Promise((resolve, reject) => { client.queryRegulations( serializer(injectSendTime(values)), options, responseStatusHandler(resolve, reject) ); }) ); } // src/Flow/queryTrafficCountsByAirspace.ts function prepareQueryTrafficCountsByAirspace(client) { const schema = ( // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access client.describe().TrafficCountsService.TrafficCountsPort.queryTrafficCountsByAirspace.input ); const serializer = prepareSerializer(schema); return instrument({ service: "Flow", query: "queryTrafficCountsByAirspace" })( (values, options) => new Promise((resolve, reject) => { client.queryTrafficCountsByAirspace( serializer(injectSendTime(values)), options, responseStatusHandler(resolve, reject) ); }) ); } // src/Flow/queryTrafficCountsByTrafficVolume.ts function prepareQueryTrafficCountsByTrafficVolume(client) { const schema = ( // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access client.describe().TrafficCountsService.TrafficCountsPort.queryTrafficCountsByTrafficVolume.input ); const serializer = prepareSerializer(schema); return instrument({ service: "Flow", query: "queryTrafficCountsByTrafficVolume" })( (values, options) => new Promise((resolve, reject) => { client.queryTrafficCountsByTrafficVolume( serializer(injectSendTime(values)), options, responseStatusHandler(resolve, reject) ); }) ); } // src/Flow/retrieveCapacityPlan.ts function prepareRetrieveCapacityPlan(client) { const schema = ( // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access client.describe().TacticalUpdatesService.TacticalUpdatesPort.retrieveCapacityPlan.input ); const serializer = prepareSerializer(schema); return instrument({ service: "Flow", query: "retrieveCapacityPlan" })( (values, options) => new Promise((resolve, reject) => { client.retrieveCapacityPlan( serializer(injectSendTime(values)), options, responseStatusHandler(resolve, reject) ); }) ); } // src/Flow/retrieveOTMVPlan.ts function prepareRetrieveOTMVPlan(client) { const schema = ( // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access client.describe().TacticalUpdatesService.TacticalUpdatesPort.retrieveOTMVPlan.input ); const serializer = prepareSerializer(schema); return instrument({ service: "Flow", query: "retrieveOTMVPlan" })( (values, options) => new Promise((resolve, reject) => { client.retrieveOTMVPlan( serializer(injectSendTime(values)), options, responseStatusHandler(resolve, reject) ); }) ); } // src/Flow/retrieveRunwayConfigurationPlan.ts function prepareRetrieveRunwayConfigurationPlan(client) { const schema = ( // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access client.describe().TacticalUpdatesService.TacticalUpdatesPort.retrieveRunwayConfigurationPlan.input ); const serializer = prepareSerializer(schema); return instrument({ service: "Flow", query: "retrieveRunwayConfigurationPlan" })( (values, options) => new Promise((resolve, reject) => { client.retrieveRunwayConfigurationPlan( serializer(injectSendTime(values)), options, responseStatusHandler(resolve, reject) ); }) ); } // src/Flow/retrieveSectorConfigurationPlan.ts function prepareRetrieveSectorConfigurationPlan(client) { const schema = ( // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access client.describe().TacticalUpdatesService.TacticalUpdatesPort.retrieveSectorConfigurationPlan.input ); const serializer = prepareSerializer(schema); return instrument({ service: "Flow", query: "retrieveSectorConfigurationPlan" })( (values, options) => new Promise((resolve, reject) => { client.retrieveSectorConfigurationPlan( serializer(injectSendTime(values)), options, responseStatusHandler(resolve, reject) ); }) ); } // src/Flow/updateCapacityPlan.ts function prepareUpdateCapacityPlan(client) { const schema = ( // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access client.describe().TacticalUpdatesService.TacticalUpdatesPort.updateCapacityPlan.input ); const serializer = prepareSerializer(schema); return instrument({ service: "Flow", query: "updateCapacityPlan" })( (values, options) => new Promise((resolve, reject) => { client.updateCapacityPlan( serializer(injectSendTime(values)), options, responseStatusHandler(resolve, reject) ); }) ); } // src/Flow/updateOTMVPlan.ts function prepareUpdateOTMVPlan(client) { const schema = ( // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access client.describe().TacticalUpdatesService.TacticalUpdatesPort.updateOTMVPlan.input ); const serializer = prepareSerializer(schema); return instrument({ service: "Flow", query: "updateOTMVPlan" })( (values, options) => new Promise((resolve, reject) => { console.log( JSON.stringify(serializer(injectSendTime(values)), null, 2) ); client.updateOTMVPlan( serializer(injectSendTime(values)), options, responseStatusHandler(resolve, reject) ); }) ); } // src/Flow/index.ts var getWSDL = ({ flavour, XSD_PATH }) => getWSDLPath({ service: "FlowServices", flavour, XSD_PATH }); function createFlowServices(config) { const WSDL = getWSDL(config); const security = prepareSecurity(config); return new Promise((resolve, reject) => { try { createClient(WSDL, { customDeserializer: deserializer }, (err, client) => { if (err) { reject( err instanceof Error ? err : new Error("Unknown error", { cause: err }) ); return; } client.setSecurity(security); resolve(client); }); } catch (err) { console.log(err); reject( err instanceof Error ? err : new Error("Unknown error", { cause: err }) ); return; } }); } function getFlowClient(config) { return createFlowServices(config).then((client) => ({ __soapClient: client, config, retrieveSectorConfigurationPlan: prepareRetrieveSectorConfigurationPlan(client), queryTrafficCountsByAirspace: prepareQueryTrafficCountsByAirspace(client), queryRegulations: prepareQueryRegulations(client), queryHotspots: prepareQueryHotspots(client), queryTrafficCountsByTrafficVolume: prepareQueryTrafficCountsByTrafficVolume(client), retrieveOTMVPlan: prepareRetrieveOTMVPlan(client), updateOTMVPlan: prepareUpdateOTMVPlan(client), retrieveCapacityPlan: prepareRetrieveCapacityPlan(client), updateCapacityPlan: prepareUpdateCapacityPlan(client), retrieveRunwayConfigurationPlan: prepareRetrieveRunwayConfigurationPlan(client) })); } export { getFlowClient }; //# sourceMappingURL=index.js.map