-
Notifications
You must be signed in to change notification settings - Fork 762
/
build-request.js
160 lines (140 loc) · 5.1 KB
/
build-request.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
// This function runs after the common function,
// `src/execute/index.js#buildRequest`
import assign from 'lodash/assign';
import get from 'lodash/get';
import isPlainObject from 'lodash/isPlainObject';
import btoa from 'btoa';
export default function buildRequest(options, req) {
const { operation, requestBody, securities, spec, attachContentTypeForEmptyPayload } = options;
let { requestContentType } = options;
req = applySecurities({
request: req,
securities,
operation,
spec,
});
const requestBodyDef = operation.requestBody || {};
const requestBodyMediaTypes = Object.keys(requestBodyDef.content || {});
const isExplicitContentTypeValid =
requestContentType && requestBodyMediaTypes.indexOf(requestContentType) > -1;
// for OAS3: set the Content-Type
if (requestBody || attachContentTypeForEmptyPayload) {
// does the passed requestContentType appear in the requestBody definition?
if (requestContentType && isExplicitContentTypeValid) {
req.headers['Content-Type'] = requestContentType;
} else if (!requestContentType) {
const firstMediaType = requestBodyMediaTypes[0];
if (firstMediaType) {
req.headers['Content-Type'] = firstMediaType;
requestContentType = firstMediaType;
}
}
} else if (requestContentType && isExplicitContentTypeValid) {
req.headers['Content-Type'] = requestContentType;
}
if (!options.responseContentType && operation.responses) {
const mediaTypes = Object.entries(operation.responses)
.filter(([key, value]) => {
const code = parseInt(key, 10);
return code >= 200 && code < 300 && isPlainObject(value.content);
})
.reduce((acc, [, value]) => acc.concat(Object.keys(value.content)), []);
if (mediaTypes.length > 0) {
req.headers.accept = mediaTypes.join(', ');
}
}
// for OAS3: add requestBody to request
if (requestBody) {
if (requestContentType) {
if (requestBodyMediaTypes.indexOf(requestContentType) > -1) {
// only attach body if the requestBody has a definition for the
// contentType that has been explicitly set
if (
requestContentType === 'application/x-www-form-urlencoded' ||
requestContentType === 'multipart/form-data'
) {
if (typeof requestBody === 'object') {
const encoding = (requestBodyDef.content[requestContentType] || {}).encoding || {};
req.form = {};
Object.keys(requestBody).forEach((k) => {
req.form[k] = {
value: requestBody[k],
encoding: encoding[k] || {},
};
});
} else {
req.form = requestBody;
}
} else {
req.body = requestBody;
}
}
} else {
req.body = requestBody;
}
}
return req;
}
// Add security values, to operations - that declare their need on them
// Adapted from the Swagger2 implementation
export function applySecurities({ request, securities = {}, operation = {}, spec }) {
const result = assign({}, request);
const { authorized = {} } = securities;
const security = operation.security || spec.security || [];
const isAuthorized = authorized && !!Object.keys(authorized).length;
const securityDef = get(spec, ['components', 'securitySchemes']) || {};
result.headers = result.headers || {};
result.query = result.query || {};
if (
!Object.keys(securities).length ||
!isAuthorized ||
!security ||
(Array.isArray(operation.security) && !operation.security.length)
) {
return request;
}
security.forEach((securityObj) => {
Object.keys(securityObj).forEach((key) => {
const auth = authorized[key];
const schema = securityDef[key];
if (!auth) {
return;
}
const value = auth.value || auth;
const { type } = schema;
if (auth) {
if (type === 'apiKey') {
if (schema.in === 'query') {
result.query[schema.name] = value;
}
if (schema.in === 'header') {
result.headers[schema.name] = value;
}
if (schema.in === 'cookie') {
result.cookies[schema.name] = value;
}
} else if (type === 'http') {
if (/^basic$/i.test(schema.scheme)) {
const username = value.username || '';
const password = value.password || '';
const encoded = btoa(`${username}:${password}`);
result.headers.Authorization = `Basic ${encoded}`;
}
if (/^bearer$/i.test(schema.scheme)) {
result.headers.Authorization = `Bearer ${value}`;
}
} else if (type === 'oauth2' || type === 'openIdConnect') {
const token = auth.token || {};
const tokenName = schema['x-tokenName'] || 'access_token';
const tokenValue = token[tokenName];
let tokenType = token.token_type;
if (!tokenType || tokenType.toLowerCase() === 'bearer') {
tokenType = 'Bearer';
}
result.headers.Authorization = `${tokenType} ${tokenValue}`;
}
}
});
});
return result;
}