-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
185 lines (162 loc) · 6.26 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// npm package : swag-wrap
import SwaggerClient from 'swagger-client'
class SwagCli {
constructor (options, store) {
this.store = store
this.storeModuleName = options.storeModuleName
this.store && this.store.commit(`${this.storeModuleName}/setOptions`, options)
// retrieve spec from options
this.spec = options.swaggerUrl
console.log('>>> SwagCli > init > this.spec : ', this.spec)
// instantiate client
this.resetCli(options)
// set up security definitions
// this.security = undefined
// this._setSecurity()
}
buildSpecAndAuth (options) {
// create swagger client from spec
const specAndAuth = { url: this.spec }
// add auth to spec if necessary
// TO DO : simplify this part...
if (options.apiKey) {
specAndAuth.authorizationHeader = {
'X-API-KEY': options.apiKey
}
specAndAuth.credentials = 'include'
}
if (options.bearerAuth) {
specAndAuth.authorizationHeader = {
// Authorization: `Basic ${options.clientId}:${options.clientSecret}`,
// Authorization: encodeURIComponent(`Bearer ${options.bearerAuth}`),
Authorization: `Bearer ${options.bearerAuth}`
// Authorization: `Bearer Token ${options.bearerAuth}`,
// Authorization: `Basic ${options.bearerAuth}`,
// Authorization: `${options.bearerAuth}`,
// Accept: 'application/json',
// Accept: '*/*',
// 'Content-Type': 'application/json'
}
specAndAuth.credentials = 'include'
}
// if (options.userPassword) {
// specAndAuth.authorizationHeader = { BasicAuth: { username: options.userName, password: options.userPassword } }
// }
// if (options.token) {
// specAndAuth.authorizationHeader = { oAuth2: { token: { access_token: options.token } } }
// }
// if (options.customAuthHeader && options.apiKey) {
// const customHeader = {}
// customHeader[options.customAuthHeader] = options.apiKey
// specAndAuth.authorizationHeader = customHeader
// }
// if (options.rawHeader) {
// specAndAuth.authorizationHeader = options.rawHeader
// }
return specAndAuth
}
resetCli (authOptions) {
console.log('>>> SwagCli > resetCli > authOptions : ', authOptions)
this.specAndAuth = this.buildSpecAndAuth(authOptions)
console.log('>>> SwagCli > resetCli > this.specAndAuth : ', this.specAndAuth)
// Activate CORS or not
// if (authOptions.activateCORS) {
// SwaggerClient.http.withCredentials = true
// }
this.setSpecsInStore()
this.cli = new SwaggerClient(this.specAndAuth)
}
setSpecsInStore () {
console.log('>>> SwagCli > setSpecsInStore > this.specAndAuth : ', this.specAndAuth)
this.store && this.store.commit(`${this.storeModuleName}/setSpecs`, this.specAndAuth)
}
// _setSecurity () {
// // cf : https://github.com/swagger-api/swagger-js/blob/HEAD/docs/usage/http-client.md
// // console.log('>>> SwagCli > _setSecurity ...')
// return this.cli.then(
// client => {
// // console.log('>>> SwagCli > _setSecurity >> client.spec.securityDefinitions : ', client.spec.securityDefinitions)
// this.security = client.spec.securityDefinitions
// }
// )
// }
_requestInterceptor (req, needAuth) {
// swagger client request interceptor
const authHeader = this.specAndAuth.authorizationHeader
console.log('>>> SwagCli > requestInterceptor >> authHeader : ', authHeader)
// update request's headers => append authorization header if needAuth == true
req.headers = needAuth ? { ...req.headers, ...authHeader } : req.headers
// update request's mode
// req.mode = needAuth ? 'cors' : 'same-origin'
// req.mode = 'cors'
// update request's credentials
// req.credentials = needAuth ? 'include' : 'same-origin' // include | same-origin | omit
// req.credentials = this.specAndAuth.credentials || 'same-origin'
console.log('>>> SwagCli > requestInterceptor >> req : ', req)
return req
}
_request (operationId, { params, body, needAuth } = {}) {
// main request function
// arg :: operationId : string / endpoint's operation ID
return this.cli.then(
// once client is ready trigger the api's path
client => {
console.log('- - - NEW REQUEST vvv', '- '.repeat(20))
// get endpoint by resolving endpoint's path in client.apis
console.log('>>> SwagCli > _request >> client : ', client)
console.log('>>> SwagCli > _request >> operationId : ', operationId)
params && console.log('>>> SwagCli > _request >> params : ', params)
body && console.log('>>> SwagCli > _request >> body : ', body)
// build request
let request = {
operationId: operationId,
requestInterceptor: req => this._requestInterceptor(req, needAuth)
}
request = params ? { ...request, parameters: params } : request
request = body ? { ...request, body: body } : request
console.log('>>> SwagCli > _request >> request : ', request)
// execute request
const endpoint = client.execute(request)
console.log('>>> SwagCli > _request >> endpoint (Promise): ', endpoint)
return endpoint
},
reason => console.error('>>> SwagCli > _request >> failed to execute the request : ' + reason)
)
}
}
// vue store module within plugin just for auth
export const moduleApiClient = {
namespaced: true,
state: () => ({
options: undefined,
specs: undefined
}),
getters: {},
mutations: {
setOptions (state, options) {
state.options = options
},
setSpecs (state, specs) {
state.specs = specs
}
},
actions: {},
modules: {}
}
const APIcli = {
install (Vue, options, store) {
// register namespaced store if necessary
const moduleName = options.storeModuleName ? options.storeModuleName : 'swagwrap'
options.storeModuleName = moduleName
if (options.registerApiStore && store) {
store.registerModule(moduleName, moduleApiClient)
}
// declare client as a global prototype in Vue
Vue.prototype.$APIcli = new SwagCli(options, store)
}
}
export default APIcli
// // Automatic installation if Vue has been added to the global scope.
// if (typeof window !== 'undefined' && window.Vue) {
// window.Vue.use(APIcli)
// }