Repository navigation
Expand file tree
/
Copy pathharHttpRequest.ts
More file actions
61 lines (53 loc) · 1.88 KB
/
Copy pathharHttpRequest.ts
File metadata and controls
61 lines (53 loc) · 1.88 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
import { ParsedUrlQuery } from 'querystring';
import { parse as urlParse } from 'url';
export interface HARNameValue {
name: string;
value: string;
}
export class HARHeader implements HARNameValue {
public constructor(public name: string, public value: string) {
}
}
export class HARCookie implements HARNameValue {
public constructor(public name: string, public value: string) {
}
}
export class HARParam implements HARNameValue {
public constructor(public name: string, public value: string) {
}
}
export class HARPostData {
public params: HARParam[];
public constructor(public mimeType: string, public text: string) {
if (mimeType === 'application/x-www-form-urlencoded') {
if (text) {
text = decodeURIComponent(text.replace(/\+/g, '%20'));
this.params = [];
const pairs = text.split('&');
pairs.forEach(pair => {
const [key, ...values] = pair.split('=');
this.params.push(new HARParam(key, values.join('=')));
});
}
}
}
}
export class HARHttpRequest {
public queryString: HARParam[];
public constructor(public method: string, public url: string, public headers: HARHeader[], public cookies: HARCookie[], public postData?: HARPostData) {
const queryObj = urlParse(url, true).query;
this.queryString = this.flatten(queryObj);
}
private flatten(queryObj: ParsedUrlQuery): HARParam[] {
const queryParams: HARParam[] = [];
Object.keys(queryObj).forEach(name => {
const value = queryObj[name];
if (Array.isArray(value)) {
queryParams.push(...value.map(v => new HARParam(name, v)));
} else {
queryParams.push(new HARParam(name, value));
}
});
return queryParams;
}
}