-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathimport.js
93 lines (77 loc) · 1.96 KB
/
import.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
import UUID from 'uuid-js';
import { formDataToFormString } from './requestUtils';
const isTest = process.env.NODE_ENV === 'test';
function stringHeadersToObject(string) {
const result = [];
string.split('\n').forEach(line => {
if (!line) {
return;
}
const header = line.split(': ');
result.push({
name: header[0],
value: header[1],
});
});
return result;
}
/**
* The postman import complies with the format
* the Chrome extension postman uses for its
* "download collection" feature. This is to
* provide an easy migration path for users
* and help teams cooperate across extensions.
*/
export function fromPostman(json) {
if (!json) {
return {};
}
const entries = json.requests;
return entries.map(request => {
const result = request;
result.headers = stringHeadersToObject(result.headers);
if (Array.isArray(result.data)) {
result.data = formDataToFormString(
result.data.map(data => ({
name: data.key,
value: data.value,
})),
);
}
// Trim away fluff
return {
id: isTest ? 'UUID' : UUID.create().toString(),
method: result.method,
url: result.url,
headers: result.headers,
data: result.data,
};
});
}
/**
* The HAR import complies with the HAR 1.2
* spec that can be found here:
* http://www.softwareishard.com/blog/har-12-spec
*
* We don't use everything, as there is a
* fair bit of fluff that is not needed to
* archive a request to a collection.
*/
export function fromHAR(har) {
if (!har) {
return {};
}
const entries = har.log.entries;
const result = [];
entries.forEach(entry => {
// Trim away fluff
result.push({
id: isTest ? 'UUID' : UUID.create().toString(),
method: entry.request.method,
url: entry.request.url,
headers: entry.request.headers,
data: entry.request.postData ? entry.request.postData.text : undefined,
});
});
return result;
}