@dgac/nmb2b-client
Version:
EUROCONTROL Network Manager B2B SOAP client
129 lines (125 loc) • 3.67 kB
JavaScript
// 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 isValidSecurity(obj) {
invariant(!!obj && typeof obj === "object", "Must be an object");
if ("apiKeyId" in obj) {
invariant(
!!obj.apiKeyId && typeof obj.apiKeyId === "string" && obj.apiKeyId.length > 0,
"security.apiKeyId must be a string with a length > 0"
);
invariant(
"apiSecretKey" in obj && typeof obj.apiSecretKey === "string" && obj.apiSecretKey.length > 0,
"security.apiSecretKey must be defined when using security.apiKeyId"
);
return true;
}
invariant(
"pfx" in obj && Buffer.isBuffer(obj.pfx) || "cert" in obj && Buffer.isBuffer(obj.cert),
"security.pfx or security.cert must be buffers"
);
if ("cert" in obj && obj.cert) {
invariant(
"key" in obj && obj.key && Buffer.isBuffer(obj.key),
"security.key must be a buffer if security.pem is defined"
);
}
return true;
}
// src/constants.ts
import path from "path";
var B2B_VERSION = "27.0.0";
var B2BFlavours = ["OPS", "PREOPS"];
// src/config.ts
import invariant2 from "invariant";
import { URL } from "url";
function isConfigValid(args) {
invariant2(!!args && typeof args === "object", "Invalid config");
invariant2(
"security" in args && isValidSecurity(args.security),
"Please provide a valid security option"
);
invariant2(
"flavour" in args && typeof args.flavour === "string",
`Invalid config.flavour. Supported flavours: ${B2BFlavours.join(", ")}`
);
invariant2(
B2BFlavours.includes(args.flavour),
`Invalid config.flavour. Supported flavours: ${B2BFlavours.join(", ")}`
);
if ("apiKeyId" in args.security) {
invariant2(
"endpoint" in args && !!args.endpoint,
`When using an config.security.apiKeyId, config.endpoint must be defined`
);
invariant2(
"xsdEndpoint" in args && !!args.xsdEndpoint,
`When using an config.security.apiKeyId, config.xsdEndpoint must be defined`
);
}
return true;
}
var B2B_ROOTS = {
OPS: "https://www.b2b.nm.eurocontrol.int",
PREOPS: "https://www.b2b.preops.nm.eurocontrol.int"
};
function getEndpoint(config = {}) {
const { endpoint, flavour } = config;
if (flavour && flavour === "PREOPS") {
return `${endpoint ?? B2B_ROOTS.PREOPS}/B2B_PREOPS/gateway/spec/${B2B_VERSION}`;
}
return `${endpoint ?? B2B_ROOTS.OPS}/B2B_OPS/gateway/spec/${B2B_VERSION}`;
}
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`;
}
function getFileUrl(path2, config = {}) {
if (config.endpoint) {
return new URL(
(path2[0] && path2.startsWith("/") ? "" : "/") + path2,
config.endpoint
).toString();
}
return getFileEndpoint(config) + (path2[0] && path2.startsWith("/") ? "" : "/") + path2;
}
function obfuscate(config) {
return {
...config,
security: Object.keys(config.security).reduce((prev, curr) => {
return {
...prev,
[curr]: "xxxxxxxxxxxxxxxx"
};
}, {})
};
}
export {
getEndpoint,
getFileEndpoint,
getFileUrl,
isConfigValid,
obfuscate
};
//# sourceMappingURL=config.js.map