forked from Kong/insomnia
/
request.js
391 lines (329 loc) 路 10.5 KB
/
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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// @flow
import type {BaseModel} from './index';
import {AUTH_BASIC, AUTH_DIGEST, AUTH_NONE, AUTH_NTLM, AUTH_OAUTH_1, AUTH_OAUTH_2, AUTH_HAWK, AUTH_AWS_IAM, AUTH_NETRC, CONTENT_TYPE_FILE, CONTENT_TYPE_FORM_DATA, CONTENT_TYPE_FORM_URLENCODED, CONTENT_TYPE_OTHER, getContentTypeFromHeaders, METHOD_GET, CONTENT_TYPE_GRAPHQL, CONTENT_TYPE_JSON, METHOD_POST, HAWK_ALGORITHM_SHA256} from '../common/constants';
import * as db from '../common/database';
import {getContentTypeHeader} from '../common/misc';
import {buildFromParams, deconstructToParams} from '../common/querystring';
import {GRANT_TYPE_AUTHORIZATION_CODE} from '../network/o-auth-2/constants';
import {SIGNATURE_METHOD_HMAC_SHA1} from '../network/o-auth-1/constants';
export const name = 'Request';
export const type = 'Request';
export const prefix = 'req';
export const canDuplicate = true;
export type RequestAuthentication = Object;
export type RequestHeader = {
name: string,
value: string,
disabled?: boolean
};
export type RequestParameter = {
name: string,
value: string,
disabled?: boolean,
id?: string,
fileName?: string
};
export type RequestBodyParameter = {
name: string,
value: string,
disabled?: boolean,
id?: string,
fileName?: string,
type?: string
};
export type RequestBody = {
mimeType?: string | null,
text?: string,
fileName?: string,
params?: Array<RequestBodyParameter>
};
type BaseRequest = {
url: string,
name: string,
description: string,
method: string,
body: RequestBody,
parameters: Array<RequestParameter>,
headers: Array<RequestHeader>,
authentication: RequestAuthentication,
metaSortKey: number,
// Settings
settingStoreCookies: boolean,
settingSendCookies: boolean,
settingDisableRenderRequestBody: boolean,
settingEncodeUrl: boolean
};
export type Request = BaseModel & BaseRequest;
export function init (): BaseRequest {
return {
url: '',
name: 'New Request',
description: '',
method: METHOD_GET,
body: {},
parameters: [],
headers: [],
authentication: {},
metaSortKey: -1 * Date.now(),
// Settings
settingStoreCookies: true,
settingSendCookies: true,
settingDisableRenderRequestBody: false,
settingEncodeUrl: true
};
}
export function newAuth (type: string, oldAuth: RequestAuthentication = {}): RequestAuthentication {
switch (type) {
// No Auth
case AUTH_NONE:
return {};
// HTTP Basic Authentication
case AUTH_BASIC:
case AUTH_DIGEST:
case AUTH_NTLM:
return {
type,
disabled: oldAuth.disabled || false,
username: oldAuth.username || '',
password: oldAuth.password || ''
};
case AUTH_OAUTH_1:
return {
type,
disabled: false,
signatureMethod: SIGNATURE_METHOD_HMAC_SHA1,
version: '1.0'
};
// OAuth 2.0
case AUTH_OAUTH_2:
return {type, grantType: GRANT_TYPE_AUTHORIZATION_CODE};
// Aws IAM
case AUTH_AWS_IAM:
return {
type,
disabled: oldAuth.disabled || false,
accessKeyId: oldAuth.accessKeyId || '',
secretAccessKey: oldAuth.secretAccessKey || ''
};
// netrc
case AUTH_NETRC:
return {type};
// hawk
case AUTH_HAWK:
return {type, algorithm: HAWK_ALGORITHM_SHA256};
// Types needing no defaults
default:
return {type};
}
}
export function newBodyNone (): RequestBody {
return {};
}
export function newBodyRaw (rawBody: string, contentType?: string): RequestBody {
if (typeof contentType !== 'string') {
return {text: rawBody};
}
const mimeType = contentType.split(';')[0];
return {mimeType, text: rawBody};
}
export function newBodyFormUrlEncoded (parameters: Array<RequestBodyParameter> | null): RequestBody {
// Remove any properties (eg. fileName) that might not fit
parameters = (parameters || []).map(parameter => {
const newParameter: RequestBodyParameter = {
name: parameter.name,
value: parameter.value
};
if (parameter.hasOwnProperty('id')) {
newParameter.id = parameter.id;
}
if (parameter.hasOwnProperty('disabled')) {
newParameter.disabled = parameter.disabled;
} else {
newParameter.disabled = false;
}
return newParameter;
});
return {
mimeType: CONTENT_TYPE_FORM_URLENCODED,
params: parameters
};
}
export function newBodyFile (path: string): RequestBody {
return {
mimeType: CONTENT_TYPE_FILE,
fileName: path
};
}
export function newBodyForm (parameters: Array<RequestBodyParameter>): RequestBody {
return {
mimeType: CONTENT_TYPE_FORM_DATA,
params: parameters || []
};
}
export function migrate (doc: Request): Request {
doc = migrateBody(doc);
doc = migrateWeirdUrls(doc);
doc = migrateAuthType(doc);
return doc;
}
export function create (patch: Object = {}): Promise<Request> {
if (!patch.parentId) {
throw new Error(`New Requests missing \`parentId\`: ${JSON.stringify(patch)}`);
}
return db.docCreate(type, patch);
}
export function getById (id: string): Promise<Request | null> {
return db.get(type, id);
}
export function findByParentId (parentId: string): Promise<Array<Request>> {
return db.find(type, {parentId: parentId});
}
export function update (request: Request, patch: Object): Promise<Request> {
return db.docUpdate(request, patch);
}
export function updateMimeType (
request: Request,
mimeType: string,
doCreate: boolean = false
): Promise<Request> {
let headers = request.headers ? [...request.headers] : [];
const contentTypeHeader = getContentTypeHeader(headers);
// GraphQL uses JSON content-type
const contentTypeHeaderValue = mimeType === CONTENT_TYPE_GRAPHQL
? CONTENT_TYPE_JSON
: mimeType;
// GraphQL must be POST
if (mimeType === CONTENT_TYPE_GRAPHQL) {
request.method = METHOD_POST;
}
// Check if we are converting to/from variants of XML or JSON
let leaveContentTypeAlone = false;
if (contentTypeHeader && mimeType) {
const current = contentTypeHeader.value;
if (current.includes('xml') && mimeType.includes('xml')) {
leaveContentTypeAlone = true;
} else if (current.includes('json') && mimeType.includes('json')) {
leaveContentTypeAlone = true;
}
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
// 1. Update Content-Type header //
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
const hasBody = typeof mimeType === 'string';
if (!hasBody || mimeType === CONTENT_TYPE_OTHER) {
headers = headers.filter(h => h !== contentTypeHeader);
} else if (mimeType && contentTypeHeader && !leaveContentTypeAlone) {
contentTypeHeader.value = contentTypeHeaderValue;
} else if (mimeType && !contentTypeHeader) {
headers.push({name: 'Content-Type', value: contentTypeHeaderValue});
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~ //
// 2. Make a new request body //
// ~~~~~~~~~~~~~~~~~~~~~~~~~~ //
let body;
if (mimeType === CONTENT_TYPE_FORM_URLENCODED) {
// Urlencoded
body = request.body.params
? newBodyFormUrlEncoded(request.body.params)
: newBodyFormUrlEncoded(deconstructToParams(request.body.text));
} else if (mimeType === CONTENT_TYPE_FORM_DATA) {
// Form Data
body = request.body.params
? newBodyForm(request.body.params)
: newBodyForm(deconstructToParams(request.body.text));
} else if (mimeType === CONTENT_TYPE_FILE) {
// File
body = newBodyFile('');
} else if (mimeType === CONTENT_TYPE_GRAPHQL) {
if (contentTypeHeader) {
contentTypeHeader.value = CONTENT_TYPE_JSON;
}
body = newBodyRaw(request.body.text || '', CONTENT_TYPE_GRAPHQL);
} else if (typeof mimeType !== 'string') {
// No body
body = newBodyNone();
} else {
// Raw Content-Type (ex: application/json)
body = request.body.params
? newBodyRaw(buildFromParams(request.body.params, false), mimeType)
: newBodyRaw(request.body.text || '', mimeType);
}
// ~~~~~~~~~~~~~~~~~~~~~~~~ //
// 2. create/update request //
// ~~~~~~~~~~~~~~~~~~~~~~~~ //
if (doCreate) {
const newRequest: Request = Object.assign({}, request, {headers, body});
return create(newRequest);
} else {
return update(request, {headers, body});
}
}
export async function duplicate (request: Request): Promise<Request> {
const name = `${request.name} (Copy)`;
// Get sort key of next request
const q = {metaSortKey: {$gt: request.metaSortKey}};
const [nextRequest] = await db.find(type, q, {metaSortKey: 1});
const nextSortKey = nextRequest ? nextRequest.metaSortKey : request.metaSortKey + 100;
// Calculate new sort key
const sortKeyIncrement = (nextSortKey - request.metaSortKey) / 2;
const metaSortKey = request.metaSortKey + sortKeyIncrement;
return db.duplicate(request, {name, metaSortKey});
}
export function remove (request: Request): Promise<void> {
return db.remove(request);
}
export function all () {
return db.all(type);
}
// ~~~~~~~~~~ //
// Migrations //
// ~~~~~~~~~~ //
/**
* Migrate old body (string) to new body (object)
* @param request
* @returns {*}
*/
function migrateBody (request: Request): Request {
if (request.body && typeof request.body === 'object') {
return request;
}
// Second, convert all existing urlencoded bodies to new format
const contentType = getContentTypeFromHeaders(request.headers) || '';
const wasFormUrlEncoded = !!contentType.match(/^application\/x-www-form-urlencoded/i);
if (wasFormUrlEncoded) {
// Convert old-style form-encoded request bodies to new style
const body = typeof request.body === 'string' ? request.body : '';
request.body = newBodyFormUrlEncoded(deconstructToParams(body, false));
} else if (!request.body && !contentType) {
request.body = {};
} else {
const body: string = typeof request.body === 'string' ? request.body : '';
request.body = newBodyRaw(body, contentType);
}
return request;
}
/**
* Fix some weird URLs that were caused by an old bug
* @param request
* @returns {*}
*/
function migrateWeirdUrls (request: Request): Request {
// Some people seem to have requests with URLs that don't have the indexOf
// function. This should clear that up. This can be removed at a later date.
if (typeof request.url !== 'string') {
request.url = '';
}
return request;
}
/**
* Ensure the request.authentication.type property is added
* @param request
* @returns {*}
*/
function migrateAuthType (request: Request): Request {
const isAuthSet = request.authentication && request.authentication.username;
if (isAuthSet && !request.authentication.type) {
request.authentication.type = AUTH_BASIC;
}
return request;
}