-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathbruno-request.js
More file actions
305 lines (265 loc) · 7.65 KB
/
Copy pathbruno-request.js
File metadata and controls
305 lines (265 loc) · 7.65 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
const HeaderList = require('./header-list');
class BrunoRequest {
/**
* The following properties are available as shorthand:
* - req.url
* - req.method
* - req.headers (raw headers object)
* - req.headerList (PropertyList API for headers)
* - req.timeout
* - req.body
*
* Above shorthands are useful for accessing the request properties directly in the scripts
* It must be noted that the user cannot set these properties directly.
* They should use the respective setter methods to set these properties.
*/
constructor(req, { interpolate } = {}) {
this.req = req;
this.__interpolate = interpolate;
this.url = req.url;
this.method = req.method;
this.headers = req.headers;
this.timeout = req.timeout;
this.name = req.name;
this.pathParams = req.pathParams;
this.tags = req.tags || [];
this.headerList = new HeaderList(this.req);
/**
* We automatically parse the JSON body if the content type is JSON
* This is to make it easier for the user to access the body directly
*
* It must be noted that the request data is always a string and is what gets sent over the network
* If the user wants to access the raw data, they can use getBody({raw: true}) method
*/
const isJson = this.hasJSONContentType(this.req.headers);
if (isJson) {
this.body = this.__safeParseJSON(req.data);
}
}
getUrl() {
return this.req.url;
}
setUrl(url) {
this.url = url;
this.req.url = url;
}
getHost() {
try {
const url = new URL(this.__getInterpolatedUrl());
return url.host;
} catch (e) {
return '';
}
}
getPath() {
try {
const url = new URL(this.__getInterpolatedUrl());
let pathname = url.pathname;
// If path params exist, interpolate them into the pathname
if (this.req.pathParams && Array.isArray(this.req.pathParams)) {
pathname = pathname
.split('/')
.map((segment) => {
if (segment.startsWith(':')) {
const paramName = segment.slice(1);
const pathParam = this.req.pathParams.find((param) => param.name === paramName);
if (
pathParam
&& pathParam.enabled !== false
&& pathParam.value !== null
&& pathParam.value !== undefined
&& (typeof pathParam.value !== 'string' || pathParam.value.trim() !== '')
) {
return pathParam.value;
}
}
return segment;
})
.join('/');
}
return pathname;
} catch (e) {
return '';
}
}
getQueryString() {
try {
const url = new URL(this.__getInterpolatedUrl());
// Return query string without the leading '?'
return url.search ? url.search.substring(1) : '';
} catch (e) {
return '';
}
}
getMethod() {
return this.req.method;
}
getAuthMode() {
const headers = this.req.headers;
if (this.req?.oauth2) {
return 'oauth2';
} else if (this.req?.oauth1config) {
return 'oauth1';
} else if (headers?.['Authorization']?.startsWith('Bearer')) {
return 'bearer';
} else if (headers?.['Authorization']?.startsWith('Basic') || this.req?.auth?.username) {
return 'basic';
} else if (this.req?.apiKeyAuthValueForQueryParams) {
return 'apikey';
} else if (this.req?.apiKeyHeaderName && this.headers?.[this.req.apiKeyHeaderName] !== undefined) {
return 'apikey';
} else if (this.req?.awsv4) {
return 'awsv4';
} else if (this.req?.digestConfig) {
return 'digest';
} else if (headers?.['X-WSSE'] || this.req?.auth?.username) {
return 'wsse';
} else {
return 'none';
}
}
setMethod(method) {
this.method = method;
this.req.method = method;
}
getHeaders() {
return this.req.headers;
}
/**
* Replaces the whole header set, dropping headers set at collection/folder level.
* TODO: make this upsert instead, since setHeaders is the bulk form of setHeader.
*/
setHeaders(headers) {
this.req.headers = headers;
}
deleteHeaders(headers) {
headers.forEach((name) => this.deleteHeader(name));
}
getHeader(name) {
return this.req.headers[name];
}
setHeader(name, value) {
this.req.headers[name] = value;
}
deleteHeader(name) {
delete this.req.headers[name];
/**
Store header name to be applied in the axios request interceptor.
Default headers (user-agent, accept, accept-encoding, etc.) are added after
the pre-request script runs, so we track them here and delete them later.
*/
if (!this.req.__headersToDelete) {
this.req.__headersToDelete = [];
}
if (!this.req.__headersToDelete.includes(name)) {
this.req.__headersToDelete.push(name);
}
}
hasJSONContentType(headers) {
const contentType = headers?.['Content-Type'] || headers?.['content-type'] || '';
return contentType.includes('json');
}
/**
* Get the body of the request
*
* We automatically parse and return the JSON body if the content type is JSON
* If the user wants the raw body, they can pass the raw option as true
*/
getBody(options = {}) {
if (options.raw) {
return this.req.data;
}
const isJson = this.hasJSONContentType(this.req.headers);
if (isJson) {
return this.__safeParseJSON(this.req.data);
}
return this.req.data;
}
/**
* If the content type is JSON and if the data is an object
* - We set the body property as the object itself
* - We set the request data as the stringified JSON as it is what gets sent over the network
* Otherwise
* - We set the request data as the data itself
* - We set the body property as the data itself
*
* If the user wants to override this behavior, they can pass the raw option as true
*/
setBody(data, options = {}) {
if (options.raw) {
this.req.data = data;
this.body = data;
return;
}
const isJson = this.hasJSONContentType(this.req.headers);
if (isJson && this.__isObject(data)) {
this.body = data;
this.req.data = this.__safeStringifyJSON(data);
return;
}
this.req.data = data;
this.body = data;
}
setMaxRedirects(maxRedirects) {
this.req.maxRedirects = maxRedirects;
}
getTimeout() {
return this.req.timeout;
}
setTimeout(timeout) {
this.timeout = timeout;
this.req.timeout = timeout;
}
onFail(callback) {
if (typeof callback === 'function') {
this.req.onFailHandler = callback;
} else if (callback) {
throw new Error(`${callback} is not a function`);
}
}
__getInterpolatedUrl() {
return this.__interpolate ? this.__interpolate(this.req.url) : this.req.url;
}
__safeParseJSON(str) {
try {
return JSON.parse(str);
} catch (e) {
return str;
}
}
__safeStringifyJSON(obj) {
try {
return JSON.stringify(obj);
} catch (e) {
return obj;
}
}
__isObject(obj) {
return obj !== null && typeof obj === 'object';
}
disableParsingResponseJson() {
this.req.__brunoDisableParsingResponseJson = true;
}
getExecutionMode() {
return this.req.__bruno__executionMode;
}
getName() {
return this.req.name;
}
getPathParams() {
const params = Array.isArray(this.req.pathParams) ? this.req.pathParams : [];
return params.map((param) => ({
name: param.name,
value: param.value,
type: param.type
}));
}
/**
* Get the tags associated with this request
* @returns {Array<string>} Array of tag strings
*/
getTags() {
return this.req.tags || [];
}
}
module.exports = BrunoRequest;