UNPKG

@affinidi/tools-openapi

Version:
96 lines 4.67 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createClient = exports.createThisData = exports.createClientMethods = void 0; const lodash_keyby_1 = __importDefault(require("lodash.keyby")); const platform_fetch_1 = require("@affinidi/platform-fetch"); const tools_common_1 = require("@affinidi/tools-common"); const headers_1 = require("./headers"); const mapFunctions_1 = require("./mapFunctions"); const httpMethodsLowercase = ['get', 'post', 'put', 'delete']; const parseSpec = (rawSpec) => { const basePath = rawSpec.servers[0].url === '/' ? '' : rawSpec.servers[0].url; const spec = Object.entries(rawSpec.paths).flatMap(([operationPath, operation]) => { const path = `${basePath}${operationPath}`; return httpMethodsLowercase.flatMap((method) => { const operationForMethod = operation[method]; if (!operationForMethod) { return []; } const { operationId } = operationForMethod; return [ { path, method, operationId, }, ]; }); }); return (0, lodash_keyby_1.default)(spec, 'operationId'); }; const executeByOptions = async (method, pathTemplate, { authorization, params, pathParams, queryParams, storageRegion }, clientOptions) => { if (!clientOptions.serviceUrl) { throw new Error('Service URL is empty'); } const headers = { ...(0, headers_1.createHeaders)(clientOptions), ...(0, headers_1.createAdditionalHeaders)({ authorization, storageRegion }), }; const fetchOptions = { headers, method: method.toUpperCase(), ...(!!params && { body: JSON.stringify(params, null, 2) }), }; // eslint-disable-next-line no-unused-vars const path = pathTemplate.replace(/\{(\w+)\}/g, (_match, p1) => pathParams === null || pathParams === void 0 ? void 0 : pathParams[p1]); const queryParamsString = Object.entries(queryParams !== null && queryParams !== void 0 ? queryParams : {}) .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(`${value}`)}`) .join('&'); const url = `${clientOptions.serviceUrl}${path}${queryParamsString !== '' ? `?${queryParamsString}` : ''}`; const response = await (0, platform_fetch_1.fetch)(url, fetchOptions); const { status } = response; const contentType = response.headers.get('content-type'); const isContentTypeJSON = contentType === null || contentType === void 0 ? void 0 : contentType.includes('application/json'); const isContentTypeHTML = contentType === null || contentType === void 0 ? void 0 : contentType.includes('text/html'); if (!status.toString().startsWith('2')) { if (isContentTypeJSON) { const error = await response.json(); const { code, message, context } = error; throw new tools_common_1.SdkError({ code, message }, context, Object.assign({}, error, { httpStatusCode: status })); } if (isContentTypeHTML) { const errorText = await response.text(); throw new tools_common_1.SdkError({ code: 'COR-0', message: errorText }, {}, Object.assign({}, { httpStatusCode: status, errorText })); } throw new tools_common_1.SdkError({ code: 'COR-0', message: 'Content type error.' }, {}, Object.assign({}, { httpStatusCode: status })); } if (!isContentTypeJSON) { return { body: {}, status }; } const jsonResponse = await response.json(); return { body: jsonResponse, status }; }; const createClientMethods = (rawSpec) => { const parsedSpec = parseSpec(rawSpec); const typedParsedSpec = parsedSpec; return (0, mapFunctions_1.mapFunctions)(typedParsedSpec, ({ path, method }) => { return (self, requestOptions) => executeByOptions(method, path, requestOptions, self); }); // to avoid TS2589 "Type instantiation is excessively deep and possibly infinite" }; exports.createClientMethods = createClientMethods; const createThisData = (serviceUrl, otherOptions) => { return { ...otherOptions, serviceUrl, }; }; exports.createThisData = createThisData; const createClient = (methods, serviceUrl, otherOptions) => { const thisData = (0, exports.createThisData)(serviceUrl, otherOptions); return Object.assign(Object.create(methods), thisData); }; exports.createClient = createClient; //# sourceMappingURL=client.js.map