1 | import _objectSpread from "@babel/runtime-corejs2/helpers/objectSpread2";
|
2 | import assign from 'lodash/assign';
|
3 | import startsWith from 'lodash/startsWith';
|
4 | import Url from 'url';
|
5 | import Http, { makeHttp, serializeRes, serializeHeaders } from './http';
|
6 | import Resolver, { clearCache } from './resolver';
|
7 | import resolveSubtree from './subtree-resolver';
|
8 | import { makeApisTagOperation } from './interfaces';
|
9 | import { execute, buildRequest, baseUrl } from './execute';
|
10 | import { opId } from './helpers';
|
11 | Swagger.http = Http;
|
12 | Swagger.makeHttp = makeHttp.bind(null, Swagger.http);
|
13 | Swagger.resolve = Resolver;
|
14 | Swagger.resolveSubtree = resolveSubtree;
|
15 | Swagger.execute = execute;
|
16 | Swagger.serializeRes = serializeRes;
|
17 | Swagger.serializeHeaders = serializeHeaders;
|
18 | Swagger.clearCache = clearCache;
|
19 | Swagger.makeApisTagOperation = makeApisTagOperation;
|
20 | Swagger.buildRequest = buildRequest;
|
21 | Swagger.helpers = {
|
22 | opId: opId
|
23 | };
|
24 | Swagger.getBaseUrl = baseUrl;
|
25 |
|
26 | function Swagger(url) {
|
27 | var _this = this;
|
28 |
|
29 | var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
30 |
|
31 |
|
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 | });
|
50 |
|
51 | prom.client = this;
|
52 | return prom;
|
53 | }
|
54 |
|
55 | Swagger.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 |
|
91 | Swagger.prototype.applyDefaults = function applyDefaults() {
|
92 | var spec = this.spec;
|
93 | var specUrl = this.url;
|
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 | };
|
111 |
|
112 |
|
113 |
|
114 | var helpers = Swagger.helpers;
|
115 | export { helpers };
|
116 | export default Swagger; |
\ | No newline at end of file |