UNPKG

1.06 kBJavaScriptView Raw
1'use strict'
2
3/**
4 * Store and maintain the set of registered Swagger API Specs.
5 *
6 * Exposes a setter to update the host of all registered apis.
7 */
8class SwaggerApis {
9 constructor() {
10 this._apis = []
11 }
12
13 add(api) {
14 this._apis.push(api)
15 this._updateHost(api)
16 return this
17 }
18
19 atIndex(index) {
20 return this._apis[index]
21 }
22
23 last() {
24 return this._apis[this._apis.length - 1]
25 }
26
27 get size() {
28 return this._apis.length
29 }
30
31 get host() {
32 return this._host || process.env.API_HOST
33 }
34
35 set host(value) {
36 this._host = value
37 this.updateApis()
38 }
39
40 get schemes() {
41 return this._schemes
42 }
43
44 set schemes(value) {
45 if (typeof value === 'string') value = [ value ]
46 this._schemes = value
47 this.updateApis()
48 }
49
50 updateApis() {
51 for (let api of this._apis) {
52 this._updateHost(api)
53 this._updateSchemes(api)
54 }
55 }
56
57 _updateHost(api) {
58 api.host = this.host || api.host
59 }
60
61 _updateSchemes(api) {
62 api.schemes = this.schemes || api.schemes
63 }
64}
65
66module.exports = SwaggerApis