UNPKG

@dgac/nmb2b-client

Version:

EUROCONTROL Network Manager B2B SOAP client

93 lines (91 loc) 3.86 kB
import { assert } from "./utils/assert.mjs"; import { assertValidSecurity } from "./security.mjs"; import { B2BFlavours, B2B_VERSION } from "./constants.mjs"; //#region src/config.ts /** * @deprecated Use {@link assertValidConfig} instead. */ function isConfigValid(args) { assertValidConfig(args); return true; } /** * Type guard to validate a {@link Config} object. * Checks for required fields and validity of nested objects like {@link Security}. * * @param args - The config object to validate. */ function assertValidConfig(args) { assert(!!args && typeof args === "object", "Invalid config"); assert("security" in args && typeof args.security === "object" && !!args.security, "Please provide a valid security option"); assertValidSecurity(args.security); assert("flavour" in args && typeof args.flavour === "string" && B2BFlavours.includes(args.flavour), `Invalid config.flavour. Supported flavours: ${B2BFlavours.join(", ")}`); if ("apiKeyId" in args.security) { assert("endpoint" in args && !!args.endpoint, `When using an config.security.apiKeyId, config.endpoint must be defined`); assert("xsdEndpoint" in args && !!args.xsdEndpoint, `When using an config.security.apiKeyId, config.xsdEndpoint must be defined`); } } const B2B_ROOTS = { OPS: "https://www.b2b.nm.eurocontrol.int", PREOPS: "https://www.b2b.preops.nm.eurocontrol.int" }; /** * Constructs the full URL for the B2B SOAP Gateway (Specification Endpoint). * * The URL is built using the correct context (`B2B_OPS` or `B2B_PREOPS`) and version. * * @param config - Configuration object. * @param config.endpoint - Optional base URL override. * @param config.flavour - Target environment ('OPS' or 'PREOPS'). * @returns The full SOAP Gateway URL (e.g. `https://www.b2b.nm.eurocontrol.int/B2B_OPS/gateway/spec/27.0.0`). */ function getSoapEndpoint(config = {}) { const { endpoint, flavour } = config; const isPreops = flavour === "PREOPS"; const root = endpoint ?? (isPreops ? B2B_ROOTS.PREOPS : B2B_ROOTS.OPS); const context = isPreops ? "B2B_PREOPS" : "B2B_OPS"; return `${root.replace(/\/$/, "")}/${context}/gateway/spec/${B2B_VERSION}`; } /** * @deprecated Use {@link getSoapEndpoint} instead. */ function getEndpoint(config = {}) { return getSoapEndpoint(config); } /** * @internal * @deprecated Use {@link getFileUrl} instead. */ function getFileEndpoint(config = {}) { const { endpoint, flavour } = config; if (flavour && flavour === "PREOPS") return `${endpoint ?? B2B_ROOTS.PREOPS}/FILE_PREOPS/gateway/spec`; return `${endpoint ?? B2B_ROOTS.OPS}/FILE_OPS/gateway/spec`; } /** * Constructs the absolute URL to download a specific file from the B2B Gateway. * Handles different environments (OPS/PREOPS) and custom endpoints. * * @param path - The relative file path (usually returned by a SOAP response). * @param config - Configuration object. * @returns The complete, absolute URL to the file. */ function getFileUrl(path, config = {}) { assert(config.endpoint === void 0, "File download URL is not supported when config.endpoint is overriden"); return (config.flavour === "PREOPS" ? `${B2B_ROOTS.PREOPS}/FILE_PREOPS/gateway/spec` : `${B2B_ROOTS.OPS}/FILE_OPS/gateway/spec`) + (path[0] && path.startsWith("/") ? "" : "/") + path; } /** * Creates a safe copy of the configuration object for logging purposes. * Masks all sensitive security credentials (passwords, keys, secrets) with 'xxxxxxxxxxxxxxxx'. * * @param config - The configuration object to obfuscate. * @returns A new configuration object with sensitive data masked. */ function obfuscate(config) { return { ...config, security: Object.fromEntries(Object.entries(config.security).map(([key]) => [key, "xxxxxxxxxxxxxxxx"])) }; } //#endregion export { assertValidConfig, getEndpoint, getFileEndpoint, getFileUrl, getSoapEndpoint, isConfigValid, obfuscate }; //# sourceMappingURL=config.mjs.map