UNPKG

3.36 kBJavaScriptView Raw
1import _objectSpread from "@babel/runtime-corejs2/helpers/objectSpread2";
2import assign from 'lodash/assign';
3import startsWith from 'lodash/startsWith';
4import Url from 'url';
5import Http, { makeHttp, serializeRes, serializeHeaders } from './http';
6import Resolver, { clearCache } from './resolver';
7import resolveSubtree from './subtree-resolver';
8import { makeApisTagOperation } from './interfaces';
9import { execute, buildRequest, baseUrl } from './execute';
10import { opId } from './helpers';
11Swagger.http = Http;
12Swagger.makeHttp = makeHttp.bind(null, Swagger.http);
13Swagger.resolve = Resolver;
14Swagger.resolveSubtree = resolveSubtree;
15Swagger.execute = execute;
16Swagger.serializeRes = serializeRes;
17Swagger.serializeHeaders = serializeHeaders;
18Swagger.clearCache = clearCache;
19Swagger.makeApisTagOperation = makeApisTagOperation;
20Swagger.buildRequest = buildRequest;
21Swagger.helpers = {
22 opId: opId
23};
24Swagger.getBaseUrl = baseUrl;
25
26function Swagger(url) {
27 var _this = this;
28
29 var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
30
31 // Allow url as a separate argument
32 if (typeof url === 'string') {
33 opts.url = url;
34 } else {
35 opts = url;
36 }
37
38 if (!(this instanceof Swagger)) {
39 return new Swagger(opts);
40 }
41
42 assign(this, opts);
43 var prom = this.resolve().then(function () {
44 if (!_this.disableInterfaces) {
45 assign(_this, Swagger.makeApisTagOperation(_this));
46 }
47
48 return _this;
49 }); // Expose this instance on the promise that gets returned
50
51 prom.client = this;
52 return prom;
53}
54
55Swagger.prototype = {
56 http: Http,
57 execute: function execute(options) {
58 this.applyDefaults();
59 return Swagger.execute(_objectSpread({
60 spec: this.spec,
61 http: this.http,
62 securities: {
63 authorized: this.authorizations
64 },
65 contextUrl: typeof this.url === 'string' ? this.url : undefined,
66 requestInterceptor: this.requestInterceptor || null,
67 responseInterceptor: this.responseInterceptor || null
68 }, options));
69 },
70 resolve: function resolve() {
71 var _this2 = this;
72
73 var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
74 return Swagger.resolve(_objectSpread({
75 spec: this.spec,
76 url: this.url,
77 http: this.http || this.fetch,
78 allowMetaPatches: this.allowMetaPatches,
79 useCircularStructures: this.useCircularStructures,
80 requestInterceptor: this.requestInterceptor || null,
81 responseInterceptor: this.responseInterceptor || null
82 }, options)).then(function (obj) {
83 _this2.originalSpec = _this2.spec;
84 _this2.spec = obj.spec;
85 _this2.errors = obj.errors;
86 return _this2;
87 });
88 }
89};
90
91Swagger.prototype.applyDefaults = function applyDefaults() {
92 var spec = this.spec;
93 var specUrl = this.url; // TODO: OAS3: support servers here
94
95 if (specUrl && startsWith(specUrl, 'http')) {
96 var parsed = Url.parse(specUrl);
97
98 if (!spec.host) {
99 spec.host = parsed.host;
100 }
101
102 if (!spec.schemes) {
103 spec.schemes = [parsed.protocol.replace(':', '')];
104 }
105
106 if (!spec.basePath) {
107 spec.basePath = '/';
108 }
109 }
110}; // add backwards compatibility with older versions of swagger-ui
111// Refs https://github.com/swagger-api/swagger-ui/issues/6210
112
113
114var helpers = Swagger.helpers;
115export { helpers };
116export default Swagger;
\No newline at end of file