UNPKG

@dgac/nmb2b-client

Version:

EUROCONTROL Network Manager B2B SOAP client

1 lines 6.84 kB
{"version":3,"file":"config.mjs","names":[],"sources":["../src/config.ts"],"sourcesContent":["import type { Security } from './security.js';\nimport { assertValidSecurity } from './security.js';\nimport type { B2BFlavour } from './constants.js';\nimport { B2B_VERSION, B2BFlavours } from './constants.js';\nimport { assert } from './utils/assert.js';\nimport type { SoapQueryHook } from './utils/hooks/hooks.js';\n\nexport interface Config {\n /**\n * The endpoint used to perform B2B queries.\n *\n * If not specified, uses the public NM B2B endpoints.\n */\n endpoint?: string;\n\n /**\n * Where to fetch the WSDL/XSD files.\n *\n * If not specified, uses the public NM B2B endpoints.\n */\n xsdEndpoint?: string;\n\n /**\n * If true, will redownload the WSDL/XSD files.\n */\n ignoreWSDLCache?: boolean;\n\n /**\n * The security configuration.\n *\n */\n security: Security;\n\n /**\n * Either 'OPS' or 'PREOPS'\n */\n flavour: B2BFlavour;\n\n /**\n * Where the WSDL/XSD files should be stored on the disk.\n */\n XSD_PATH: string;\n\n /**\n * Soap query hooks.\n *\n * @see {@link SoapQueryHook}\n */\n hooks: Array<SoapQueryHook>;\n}\n\n/**\n * @deprecated Use {@link assertValidConfig} instead.\n */\nexport function isConfigValid(args: unknown): args is Config {\n assertValidConfig(args);\n return true;\n}\n\n/**\n * Type guard to validate a {@link Config} object.\n * Checks for required fields and validity of nested objects like {@link Security}.\n *\n * @param args - The config object to validate.\n */\nexport function assertValidConfig(args: unknown): asserts args is Config {\n assert(!!args && typeof args === 'object', 'Invalid config');\n\n assert(\n 'security' in args && typeof args.security === 'object' && !!args.security,\n 'Please provide a valid security option',\n );\n\n assertValidSecurity(args.security);\n\n assert(\n 'flavour' in args &&\n typeof args.flavour === 'string' &&\n B2BFlavours.includes(args.flavour as B2BFlavour),\n `Invalid config.flavour. Supported flavours: ${B2BFlavours.join(', ')}`,\n );\n\n if ('apiKeyId' in args.security) {\n assert(\n 'endpoint' in args && !!args.endpoint,\n `When using an config.security.apiKeyId, config.endpoint must be defined`,\n );\n\n assert(\n 'xsdEndpoint' in args && !!args.xsdEndpoint,\n `When using an config.security.apiKeyId, config.xsdEndpoint must be defined`,\n );\n }\n}\n\nconst B2B_ROOTS = {\n OPS: 'https://www.b2b.nm.eurocontrol.int',\n PREOPS: 'https://www.b2b.preops.nm.eurocontrol.int',\n};\n\n/**\n * Constructs the full URL for the B2B SOAP Gateway (Specification Endpoint).\n *\n * The URL is built using the correct context (`B2B_OPS` or `B2B_PREOPS`) and version.\n *\n * @param config - Configuration object.\n * @param config.endpoint - Optional base URL override.\n * @param config.flavour - Target environment ('OPS' or 'PREOPS').\n * @returns The full SOAP Gateway URL (e.g. `https://www.b2b.nm.eurocontrol.int/B2B_OPS/gateway/spec/27.0.0`).\n */\nexport function getSoapEndpoint(\n config: { endpoint?: string; flavour?: B2BFlavour } = {},\n): string {\n const { endpoint, flavour } = config;\n const isPreops = flavour === 'PREOPS';\n\n const root = endpoint ?? (isPreops ? B2B_ROOTS.PREOPS : B2B_ROOTS.OPS);\n const context = isPreops ? 'B2B_PREOPS' : 'B2B_OPS';\n\n // Ensure we don't have double slashes when concatenating the path\n const normalizedRoot = root.replace(/\\/$/, '');\n\n return `${normalizedRoot}/${context}/gateway/spec/${B2B_VERSION}`;\n}\n\n/**\n * @deprecated Use {@link getSoapEndpoint} instead.\n */\nexport function getEndpoint(\n config: { endpoint?: string; flavour?: B2BFlavour } = {},\n): string {\n return getSoapEndpoint(config);\n}\n\n/**\n * @internal\n * @deprecated Use {@link getFileUrl} instead.\n */\nexport function getFileEndpoint(\n config: { endpoint?: string; flavour?: B2BFlavour } = {},\n): string {\n const { endpoint, flavour } = config;\n\n if (flavour && flavour === 'PREOPS') {\n return `${endpoint ?? B2B_ROOTS.PREOPS}/FILE_PREOPS/gateway/spec`;\n }\n\n return `${endpoint ?? B2B_ROOTS.OPS}/FILE_OPS/gateway/spec`;\n}\n\n/**\n * Constructs the absolute URL to download a specific file from the B2B Gateway.\n * Handles different environments (OPS/PREOPS) and custom endpoints.\n *\n * @param path - The relative file path (usually returned by a SOAP response).\n * @param config - Configuration object.\n * @returns The complete, absolute URL to the file.\n */\nexport function getFileUrl(\n path: string,\n config: { flavour?: B2BFlavour; endpoint?: string } = {},\n): string {\n assert(\n config.endpoint === undefined,\n 'File download URL is not supported when config.endpoint is overriden',\n );\n\n const baseUrl =\n config.flavour === 'PREOPS'\n ? `${B2B_ROOTS.PREOPS}/FILE_PREOPS/gateway/spec`\n : `${B2B_ROOTS.OPS}/FILE_OPS/gateway/spec`;\n\n return baseUrl + (path[0] && path.startsWith('/') ? '' : '/') + path;\n}\n\n/**\n * Creates a safe copy of the configuration object for logging purposes.\n * Masks all sensitive security credentials (passwords, keys, secrets) with 'xxxxxxxxxxxxxxxx'.\n *\n * @param config - The configuration object to obfuscate.\n * @returns A new configuration object with sensitive data masked.\n */\nexport function obfuscate(config: Config) {\n return {\n ...config,\n security: Object.fromEntries(\n Object.entries(config.security).map(([key]) => [key, 'xxxxxxxxxxxxxxxx']),\n ),\n };\n}\n"],"mappings":";;;;;;;;AAsDA,SAAgB,cAAc,MAA+B;AAC3D,mBAAkB,KAAK;AACvB,QAAO;;;;;;;;AAST,SAAgB,kBAAkB,MAAuC;AACvE,QAAO,CAAC,CAAC,QAAQ,OAAO,SAAS,UAAU,iBAAiB;AAE5D,QACE,cAAc,QAAQ,OAAO,KAAK,aAAa,YAAY,CAAC,CAAC,KAAK,UAClE,yCACD;AAED,qBAAoB,KAAK,SAAS;AAElC,QACE,aAAa,QACX,OAAO,KAAK,YAAY,YACxB,YAAY,SAAS,KAAK,QAAsB,EAClD,+CAA+C,YAAY,KAAK,KAAK,GACtE;AAED,KAAI,cAAc,KAAK,UAAU;AAC/B,SACE,cAAc,QAAQ,CAAC,CAAC,KAAK,UAC7B,0EACD;AAED,SACE,iBAAiB,QAAQ,CAAC,CAAC,KAAK,aAChC,6EACD;;;AAIL,MAAM,YAAY;CAChB,KAAK;CACL,QAAQ;CACT;;;;;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