/
service.js.template
280 lines (244 loc) · 8.19 KB
/
service.js.template
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Auto-generated, edits will be overwritten
import spec from './spec';;
export class ServiceError extends Error {}
let options = {};;
export function init(serviceOptions) {
options = serviceOptions;;
}
export function request(op, parameters, attempt) {
if (!attempt) attempt = 1;
return acquireRights(op, spec, options)
.then(rights => {
parameters = parameters || {};;
const baseUrl = getBaseUrl(spec);;
let reqInfo = { parameters, baseUrl };;
if (options.processRequest) {
reqInfo = options.processRequest(op, reqInfo);;
}
const req = buildRequest(op, reqInfo.baseUrl, reqInfo.parameters, rights);;
return makeFetchRequest(req)
.then(res => processResponse(req, res, attempt, options), e => processError(req, e))
.then(outcome => outcome.retry ? request(op, parameters, attempt + 1) : outcome.res);;
});;
}
function acquireRights(op, spec, options) {
if (op.security && options.getAuthorization) {
return op.security.reduce((promise, security) => {
return promise.then(rights => {
const securityDefinition = spec.securityDefinitions[security.id];;
return options.getAuthorization(security, securityDefinition, op)
.then(auth => {
rights[security.id] = auth;;
return rights;;
});;
});;
}, Promise.resolve({}));;
}
return Promise.resolve({});;
}
function makeFetchRequest(req) {
let fetchOptions = {
compress: true,
method: (req.method || 'get').toUpperCase(),
headers: req.headers,
body: req.body ? JSON.stringify(req.body) : undefined
};;
if (options.fetchOptions) {
const opts = options.fetchOptions;;
const headers = opts.headers
? Object.assign(fetchOptions.headers, opts.headers)
: fetchOptions.headers;;
fetchOptions = Object.assign({}, fetchOptions, opts);;
fetchOptions.headers = headers;;
}
let promise = fetch(req.url, fetchOptions);;
return promise;;
}
function buildRequest(op, baseUrl, parameters, rights) {
let paramGroups = groupParams(op, parameters);;
paramGroups = applyAuthorization(paramGroups, rights, spec);;
const url = buildUrl(op, baseUrl, paramGroups, spec);;
const headers = buildHeaders(op, paramGroups);;
const body = buildBody(parameters.body);;
return {
method: op.method,
url,
headers,
body
};;
}
function groupParams(op, parameters) {
const groups = ['header', 'path', 'query', 'formData'].reduce((groups, name) => {
groups[name] = formatParamsGroup(groups[name]);;
return groups;;
}, parameters);;
if (!groups.header) groups.header = {};;
return groups;;
}
function formatParamsGroup(groups) {
return Object.keys(groups || {}).reduce((g, name) => {
const param = groups[name];;
if (param !== undefined) {
g[name] = formatParam(param);;
}
return g;;
}, {});;
}
function formatParam(param) {
if (param === undefined || param === null) return '';;
else if (param instanceof Date) return param.toJSON();;
else if (Array.isArray(param)) return param;;
else return param.toString();;
}
function buildUrl(op, baseUrl, parameters, spec) {
let url = `${baseUrl}${op.path}`;;
if (parameters.path) {
url = Object.keys(parameters.path)
.reduce((url, name) => url.replace(`{${name}}`, parameters.path[name]), url);;
}
const query = createQueryString(parameters.query);;
return url + query;;
}
function getBaseUrl(spec) {
return options.url || `${spec.schemes[0] || 'https'}://${spec.host}${spec.basePath}`;;
}
function createQueryParam(name, value) {
const v = formatParam(value);;
if (v && typeof v === 'string') return `${name}=${encodeURIComponent(v)}`;;
return name;
}
function createQueryString(query) {
const names = Object.keys(query || {});;
if (!names.length) return '';;
const params = names.map(name => ({name, value: query[name]}))
.reduce((acc, value) => {
if (Array.isArray(value.value)) {
return acc.concat(value.value);;
} else {
acc.push(createQueryParam(value.name, value.value));;
return acc;;
}
}, []);;
return '?' + params.sort().join('&');;
}
function buildHeaders(op, parameters) {
const headers = {};;
let accepts;;
if (op.accepts && op.accepts.length) accepts = op.accepts;;
else if (spec.accepts && spec.accepts.length) accepts = spec.accepts;;
else accepts = [ 'application/json' ];;
headers.Accept = accepts.join(', ');;
let contentType;;
if (op.contentTypes && op.contentTypes[0]) contentType = op.contentTypes[0];;
else if (spec.contentTypes && spec.contentTypes[0]) contentType = spec.contentTypes[0];;
if (contentType) headers['Content-Type'] = contentType;;
return Object.assign(headers, parameters.header);;
}
function buildBody(bodyParams) {
if (bodyParams) {
if (bodyParams.body) return bodyParams.body;;
const key = Object.keys(bodyParams)[0];;
if (key) return bodyParams[key];;
}
return undefined;;
}
function resolveAuthHeaderName(headerName){
if (options.authorizationHeader && headerName.toLowerCase() === 'authorization') {
return options.authorizationHeader;;
} else {
return headerName;;
}
}
function applyAuthorization(req, rights, spec) {
Object.keys(rights).forEach(name => {
const rightsInfo = rights[name];;
const definition = spec.securityDefinitions[name];;
switch (definition.type) {
case 'basic':
const creds = `${rightsInfo.username}:${rightsInfo.password}`;;
const token = (typeof window !== 'undefined' && window.btoa)
? window.btoa(creds)
: new Buffer(creds).toString('base64');;
req.header[resolveAuthHeaderName('Authorization')] = `Basic ${token}`;;
break;;
case 'oauth2':
req.header[resolveAuthHeaderName('Authorization')] = `Bearer ${rightsInfo.token}`;;
break;;
case 'apiKey':
if (definition.in === 'header') {
req.header[resolveAuthHeaderName(definition.name)] = rightsInfo.apiKey;;
} else if (definition.in === 'query') {
req.query[definition.name] = rightsInfo.apiKey;;
} else {
throw new Error(`Api key must be in header or query not '${definition.in}'`);;
}
break;;
default:
throw new Error(`Security definition type '${definition.type}' not supported`);;
}
});;
return req;;
}
function processResponse(req, response, attempt, options) {
const format = response.ok ? formatResponse : formatServiceError;;
const contentType = response.headers.get('content-type') || '';;
let parse;;
if (response.status === 204) {
parse = Promise.resolve();;
} else if (~contentType.indexOf('json')) {
parse = response.json();;
} else if (~contentType.indexOf('octet-stream')) {
parse = response.blob();;
} else if (~contentType.indexOf('text')) {
parse = response.text();;
} else {
parse = Promise.resolve();;
}
return parse
.then(data => format(response, data, options))
.then(res => {
if (options.processResponse) return options.processResponse(req, res, attempt);;
else return Promise.resolve({ res });;
});;
}
function formatResponse(response, data, options) {
return { raw: response, data };;
}
function formatServiceError(response, data, options) {
if (options.formatServiceError) {
data = options.formatServiceError(response, data);;
} else {
const serviceError = new ServiceError();;
if (data) {
if (typeof data === 'string') serviceError.message = data;;
else {
if (data.message) serviceError.message = data.message;;
if (data.body) serviceError.body = data.body;;
else serviceError.body = data;;
}
if (data.code) serviceError.code = data.code;;
} else {
serviceError.message = response.statusText;;
}
serviceError.status = response.status;;
data = serviceError;;
}
return { raw: response, data, error: true };;
}
function processError(req, error) {
const { processError } = options;;
const res = { res: { raw: {}, data: error, error: true } };;
return Promise.resolve(processError ? processError(req, res) : res);;
}
const COLLECTION_DELIM = { csv: ',', multi: '&', pipes: '|', ssv: ' ', tsv: '\t' };;
export function formatArrayParam(array, format, name) {
if (!array) return;;
if (format === 'multi') return array.map(value => createQueryParam(name, value));;
const delim = COLLECTION_DELIM[format];;
if (!delim) throw new Error(`Invalid collection format '${format}'`);;
return array.map(formatParam).join(delim);;
}
export function formatDate(date, format) {
const str = date.toISOString();
return (format === 'date') ? str.split('T')[0] : str;
}