-
Notifications
You must be signed in to change notification settings - Fork 758
/
index.js
103 lines (90 loc) · 2.52 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import cloneDeep from 'lodash/cloneDeep'
import assign from 'lodash/assign'
import startsWith from 'lodash/startsWith'
import Url from 'url'
import Http, {makeHttp, serializeRes, serializeHeaders} from './http'
import Resolver, {clearCache} from './resolver'
import resolveSubtree from './subtree-resolver'
import {makeApisTagOperation} from './interfaces'
import {execute, buildRequest} from './execute'
import {opId} from './helpers'
Swagger.http = Http
Swagger.makeHttp = makeHttp.bind(null, Swagger.http)
Swagger.resolve = Resolver
Swagger.resolveSubtree = resolveSubtree
Swagger.execute = execute
Swagger.serializeRes = serializeRes
Swagger.serializeHeaders = serializeHeaders
Swagger.clearCache = clearCache
Swagger.makeApisTagOperation = makeApisTagOperation
Swagger.buildRequest = buildRequest
Swagger.helpers = {opId}
function Swagger(url, opts = {}) {
// Allow url as a separate argument
if (typeof url === 'string') {
opts.url = url
}
else {
opts = url
}
if (!(this instanceof Swagger)) {
return new Swagger(opts)
}
assign(this, opts)
const prom = this.resolve()
.then(() => {
if (!this.disableInterfaces) {
assign(this, Swagger.makeApisTagOperation(this))
}
return this
})
// Expose this instance on the promise that gets returned
prom.client = this
return prom
}
Swagger.prototype = {
http: Http,
execute(argHash) {
this.applyDefaults()
return Swagger.execute({
spec: this.spec,
http: this.http,
securities: {authorized: this.authorizations},
contextUrl: typeof this.url === 'string' ? this.url : undefined,
...argHash
})
},
resolve() {
return Swagger.resolve({
spec: this.spec,
url: this.url,
allowMetaPatches: this.allowMetaPatches,
useCircularStructures: this.useCircularStructures,
requestInterceptor: this.requestInterceptor || null,
responseInterceptor: this.responseInterceptor || null
}).then((obj) => {
this.originalSpec = this.spec
this.spec = obj.spec
this.errors = obj.errors
return this
})
}
}
Swagger.prototype.applyDefaults = function () {
const spec = this.spec
const specUrl = this.url
// TODO: OAS3: support servers here
if (specUrl && startsWith(specUrl, 'http')) {
const parsed = Url.parse(specUrl)
if (!spec.host) {
spec.host = parsed.host
}
if (!spec.schemes) {
spec.schemes = [parsed.protocol.replace(':', '')]
}
if (!spec.basePath) {
spec.basePath = '/'
}
}
}
export default Swagger