UNPKG

@mft/moneyhub-api-client

Version:
140 lines 5.39 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.addVersionToUrl = void 0; const got_1 = __importDefault(require("got")); const query_string_1 = __importDefault(require("query-string")); const R = __importStar(require("ramda")); const DEFAULT_API_VERSION = "v3"; const DEFAULT_MAX_RETRY_AFTER = 5000; const DEFAULT_RETRY_LIMIT = 2; const DEFAULT_RETRY_METHODS = [ "GET", "HEAD", "PUT", "DELETE", "OPTIONS", "TRACE", ]; const DEFAULT_RETRY_STATUS_CODES = [ 408, 413, 429, 500, 502, 503, 504, 521, 522, 524, // A Timeout Occurred ]; const getResponseBody = (err) => { let body = {}; try { const { code, message, details } = JSON.parse(R.pathOr("{}", ["response", "body"], err)); body = { code, message, details: typeof details === "object" ? JSON.stringify(details) : details }; } catch (e) { body = {}; } return body; }; const attachErrorDetails = (err) => { const { code, message, details } = getResponseBody(err); err.error = code; err.error_description = message; err.error_details = details; throw err; }; const addVersionToUrl = (url, apiVersioning, version = DEFAULT_API_VERSION) => { if (!apiVersioning || url.includes("identity") || /\/v.+/g.test(url)) return url; const urlWithVersion = R.pipe(R.split("/"), // split url [ "https:", "", "test.com", "path", "path2" ] R.insert(3, String(version)), // insert and stringify version after domain R.join("/"))(url); return urlWithVersion; }; exports.addVersionToUrl = addVersionToUrl; const getRetryOptions = (retry, requestOptions = {}) => { var _a, _b, _c, _d; return { limit: ((_a = requestOptions.retry) === null || _a === void 0 ? void 0 : _a.limit) || retry.limit || DEFAULT_RETRY_LIMIT, methods: ((_b = requestOptions.retry) === null || _b === void 0 ? void 0 : _b.methods) || retry.methods || DEFAULT_RETRY_METHODS, statusCodes: ((_c = requestOptions.retry) === null || _c === void 0 ? void 0 : _c.statusCodes) || retry.statusCodes || DEFAULT_RETRY_STATUS_CODES, maxRetryAfter: ((_d = requestOptions.retry) === null || _d === void 0 ? void 0 : _d.maxRetryAfter) || retry.maxRetryAfter || DEFAULT_MAX_RETRY_AFTER, }; }; exports.default = ({ client, options: { timeout, apiVersioning, mTLS, retry = {} }, }) => async (url, opts = {}) => { var _a, _b, _c, _d; const retryOptions = getRetryOptions(retry, opts.options); const gotOpts = { method: opts.method || "GET", headers: opts.headers || {}, searchParams: query_string_1.default.stringify(opts.searchParams), timeout, retry: retryOptions, }; const formattedUrl = (0, exports.addVersionToUrl)(url, apiVersioning, (_a = opts.options) === null || _a === void 0 ? void 0 : _a.version); if ((_b = opts.options) === null || _b === void 0 ? void 0 : _b.token) { gotOpts.headers = R.assoc("Authorization", `Bearer ${opts.options.token}`, gotOpts.headers); } if ((_c = opts.options) === null || _c === void 0 ? void 0 : _c.headers) { gotOpts.headers = R.mergeDeepRight(gotOpts.headers || {}, opts.options.headers); } if (!((_d = gotOpts.headers) === null || _d === void 0 ? void 0 : _d.Authorization) && opts.cc) { const { access_token } = await client.grant({ grant_type: "client_credentials", scope: opts.cc.scope, sub: opts.cc.sub, }); gotOpts.headers = R.assoc("Authorization", `Bearer ${access_token}`, gotOpts.headers); } if (opts.body) { gotOpts.json = opts.body; } if (opts.form) { gotOpts.form = opts.form; } if (opts.formData) { gotOpts.body = opts.formData; } if (mTLS) { gotOpts.https = { certificate: mTLS.cert, key: mTLS.key, }; } const req = (0, got_1.default)(formattedUrl, gotOpts); if (opts.returnStatus) { return req.then((res) => res.statusCode) .catch(attachErrorDetails); } return req.json() .catch(attachErrorDetails); }; //# sourceMappingURL=request.js.map