1 | /* eslint-disable camelcase */
|
---|
2 | import { DEFAULT_OPENAPI_3_SERVER } from './constants.js';
|
---|
3 | import Http, { makeHttp, serializeRes, serializeHeaders } from './http/index.js';
|
---|
4 | import { makeResolve } from './resolver/index.js';
|
---|
5 | import { makeResolveSubtree } from './subtree-resolver/index.js';
|
---|
6 | import genericResolveStrategy from './resolver/strategies/generic/index.js';
|
---|
7 | import openApi2ResolveStrategy, { clearCache } from './resolver/strategies/openapi-2/index.js';
|
---|
8 | import openApi30ResolveStrategy from './resolver/strategies/openapi-3-0/index.js';
|
---|
9 | import openApi31ApiDOMResolveStrategy from './resolver/strategies/openapi-3-1-apidom/index.js';
|
---|
10 | import { makeApisTagOperation } from './interfaces.js';
|
---|
11 | import { execute, buildRequest, baseUrl } from './execute/index.js';
|
---|
12 | import { opId, isHttpUrl } from './helpers/index.js';
|
---|
13 | import { isOpenAPI2, isOpenAPI3 } from './helpers/openapi-predicates.js';
|
---|
14 | import HttpResolverSwaggerClient from './resolver/apidom/reference/resolve/resolvers/http-swagger-client/index.js';
|
---|
15 | import JsonParser from './resolver/apidom/reference/parse/parsers/json/index.js';
|
---|
16 | import YamlParser from './resolver/apidom/reference/parse/parsers/yaml-1-2/index.js';
|
---|
17 | import OpenApiJson3_1Parser from './resolver/apidom/reference/parse/parsers/openapi-json-3-1/index.js';
|
---|
18 | import OpenApiYaml3_1Parser from './resolver/apidom/reference/parse/parsers/openapi-yaml-3-1/index.js';
|
---|
19 | import OpenApi3_1SwaggerClientDereferenceStrategy from './resolver/apidom/reference/dereference/strategies/openapi-3-1-swagger-client/index.js';
|
---|
20 | Swagger.http = Http;
|
---|
21 | Swagger.makeHttp = makeHttp.bind(null, Swagger.http);
|
---|
22 | Swagger.resolveStrategies = {
|
---|
23 | 'openapi-3-1-apidom': openApi31ApiDOMResolveStrategy,
|
---|
24 | 'openapi-3-0': openApi30ResolveStrategy,
|
---|
25 | 'openapi-2-0': openApi2ResolveStrategy,
|
---|
26 | generic: genericResolveStrategy
|
---|
27 | };
|
---|
28 | Swagger.resolve = makeResolve({
|
---|
29 | strategies: [Swagger.resolveStrategies['openapi-3-1-apidom'], Swagger.resolveStrategies['openapi-3-0'], Swagger.resolveStrategies['openapi-2-0'], Swagger.resolveStrategies.generic]
|
---|
30 | });
|
---|
31 | Swagger.resolveSubtree = makeResolveSubtree({
|
---|
32 | strategies: [Swagger.resolveStrategies['openapi-3-1-apidom'], Swagger.resolveStrategies['openapi-3-0'], Swagger.resolveStrategies['openapi-2-0'], Swagger.resolveStrategies.generic]
|
---|
33 | });
|
---|
34 | Swagger.execute = execute;
|
---|
35 | Swagger.serializeRes = serializeRes;
|
---|
36 | Swagger.serializeHeaders = serializeHeaders;
|
---|
37 | Swagger.clearCache = clearCache;
|
---|
38 | Swagger.makeApisTagOperation = makeApisTagOperation;
|
---|
39 | Swagger.buildRequest = buildRequest;
|
---|
40 | Swagger.helpers = {
|
---|
41 | opId
|
---|
42 | };
|
---|
43 | Swagger.getBaseUrl = baseUrl;
|
---|
44 | Swagger.apidom = {
|
---|
45 | resolve: {
|
---|
46 | resolvers: {
|
---|
47 | HttpResolverSwaggerClient
|
---|
48 | }
|
---|
49 | },
|
---|
50 | parse: {
|
---|
51 | parsers: {
|
---|
52 | JsonParser,
|
---|
53 | YamlParser,
|
---|
54 | OpenApiJson3_1Parser,
|
---|
55 | OpenApiYaml3_1Parser
|
---|
56 | }
|
---|
57 | },
|
---|
58 | dereference: {
|
---|
59 | strategies: {
|
---|
60 | OpenApi3_1SwaggerClientDereferenceStrategy
|
---|
61 | }
|
---|
62 | }
|
---|
63 | };
|
---|
64 | function Swagger(url, opts = {}) {
|
---|
65 | // Allow url as a separate argument
|
---|
66 | if (typeof url === 'string') {
|
---|
67 | opts.url = url;
|
---|
68 | } else {
|
---|
69 | opts = url;
|
---|
70 | }
|
---|
71 | if (!(this instanceof Swagger)) {
|
---|
72 | return new Swagger(opts);
|
---|
73 | }
|
---|
74 | Object.assign(this, opts);
|
---|
75 | const prom = this.resolve().then(() => {
|
---|
76 | if (!this.disableInterfaces) {
|
---|
77 | Object.assign(this, Swagger.makeApisTagOperation(this));
|
---|
78 | }
|
---|
79 | return this;
|
---|
80 | });
|
---|
81 |
|
---|
82 | // Expose this instance on the promise that gets returned
|
---|
83 | prom.client = this;
|
---|
84 | return prom;
|
---|
85 | }
|
---|
86 | Swagger.prototype = {
|
---|
87 | http: Http,
|
---|
88 | execute(options) {
|
---|
89 | this.applyDefaults();
|
---|
90 | return Swagger.execute({
|
---|
91 | spec: this.spec,
|
---|
92 | http: this.http,
|
---|
93 | securities: {
|
---|
94 | authorized: this.authorizations
|
---|
95 | },
|
---|
96 | contextUrl: typeof this.url === 'string' ? this.url : undefined,
|
---|
97 | requestInterceptor: this.requestInterceptor || null,
|
---|
98 | responseInterceptor: this.responseInterceptor || null,
|
---|
99 | ...options
|
---|
100 | });
|
---|
101 | },
|
---|
102 | resolve(options = {}) {
|
---|
103 | return Swagger.resolve({
|
---|
104 | spec: this.spec,
|
---|
105 | url: this.url,
|
---|
106 | http: this.http || this.fetch,
|
---|
107 | allowMetaPatches: this.allowMetaPatches,
|
---|
108 | useCircularStructures: this.useCircularStructures,
|
---|
109 | requestInterceptor: this.requestInterceptor || null,
|
---|
110 | responseInterceptor: this.responseInterceptor || null,
|
---|
111 | skipNormalization: this.skipNormalization || false,
|
---|
112 | ...options
|
---|
113 | }).then(obj => {
|
---|
114 | this.originalSpec = this.spec;
|
---|
115 | this.spec = obj.spec;
|
---|
116 | this.errors = obj.errors;
|
---|
117 | return this;
|
---|
118 | });
|
---|
119 | }
|
---|
120 | };
|
---|
121 | Swagger.prototype.applyDefaults = function applyDefaults() {
|
---|
122 | const {
|
---|
123 | spec
|
---|
124 | } = this;
|
---|
125 | const specUrl = this.url;
|
---|
126 | if (isOpenAPI2(spec) && isHttpUrl(specUrl)) {
|
---|
127 | const parsed = new URL(specUrl);
|
---|
128 | if (!spec.host) {
|
---|
129 | spec.host = parsed.host;
|
---|
130 | }
|
---|
131 | if (!spec.schemes) {
|
---|
132 | spec.schemes = [parsed.protocol.replace(':', '')];
|
---|
133 | }
|
---|
134 | if (!spec.basePath) {
|
---|
135 | spec.basePath = '/';
|
---|
136 | }
|
---|
137 | } else if (isOpenAPI3(spec)) {
|
---|
138 | const isEmptyServerList = Array.isArray(spec.servers) && spec.servers.length === 0;
|
---|
139 | if (!spec.servers || isEmptyServerList) {
|
---|
140 | spec.servers = [DEFAULT_OPENAPI_3_SERVER];
|
---|
141 | }
|
---|
142 | }
|
---|
143 | };
|
---|
144 |
|
---|
145 | // add backwards compatibility with older versions of swagger-ui
|
---|
146 | // Refs https://github.com/swagger-api/swagger-ui/issues/6210
|
---|
147 | export const {
|
---|
148 | helpers
|
---|
149 | } = Swagger;
|
---|
150 | export default Swagger;
|
---|
151 | /* eslint-enable camelcase */ |
---|