http-snapshotter 0.2.0 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +38 -12
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -42,13 +42,14 @@ let snapshotDirectory = null;
|
|
|
42
42
|
|
|
43
43
|
/**
|
|
44
44
|
* @typedef SnapshotText
|
|
45
|
-
* @property {'text'} responseType
|
|
46
45
|
* @property {string} fileSuffixKey
|
|
46
|
+
* @property {'json'|'text'} requestType
|
|
47
47
|
* @property {object} request
|
|
48
48
|
* @property {string} request.method
|
|
49
49
|
* @property {string} request.url
|
|
50
50
|
* @property {string[][]} request.headers
|
|
51
|
-
* @property {string|undefined} request.body
|
|
51
|
+
* @property {string|object|undefined} request.body
|
|
52
|
+
* @property {'text'} responseType
|
|
52
53
|
* @property {object} response
|
|
53
54
|
* @property {number} response.status
|
|
54
55
|
* @property {string} response.statusText
|
|
@@ -57,13 +58,14 @@ let snapshotDirectory = null;
|
|
|
57
58
|
*/
|
|
58
59
|
/**
|
|
59
60
|
* @typedef SnapshotJson
|
|
60
|
-
* @property {'json'} responseType
|
|
61
61
|
* @property {string} fileSuffixKey
|
|
62
|
+
* @property {'json'|'text'} requestType
|
|
62
63
|
* @property {object} request
|
|
63
64
|
* @property {string} request.method
|
|
64
65
|
* @property {string} request.url
|
|
65
66
|
* @property {string[][]} request.headers
|
|
66
|
-
* @property {string|undefined} request.body
|
|
67
|
+
* @property {string|object|undefined} request.body
|
|
68
|
+
* @property {'json'} responseType
|
|
67
69
|
* @property {object} response
|
|
68
70
|
* @property {number} response.status
|
|
69
71
|
* @property {string} response.statusText
|
|
@@ -166,31 +168,55 @@ async function saveSnapshot(request, response) {
|
|
|
166
168
|
|
|
167
169
|
/** @returns {ReadSnapshotReturnType} */
|
|
168
170
|
const saveFreshSnapshot = async () => {
|
|
169
|
-
let
|
|
171
|
+
let requestBody;
|
|
172
|
+
let responseBody;
|
|
173
|
+
|
|
174
|
+
/** @type {'text' | 'json'} */
|
|
175
|
+
let requestType;
|
|
176
|
+
const reqContentType = request.headers.get('content-type') || '';
|
|
177
|
+
if (reqContentType.includes('application/json') || reqContentType.includes('application/x-amz-json-1.0')) {
|
|
178
|
+
try {
|
|
179
|
+
requestBody = await request.clone().json();
|
|
180
|
+
requestType = 'json';
|
|
181
|
+
} catch (err) {
|
|
182
|
+
requestBody = await request.clone().text();
|
|
183
|
+
requestType = 'text';
|
|
184
|
+
}
|
|
185
|
+
} else {
|
|
186
|
+
requestBody = await request.clone().text();
|
|
187
|
+
requestType = 'text';
|
|
188
|
+
}
|
|
189
|
+
|
|
170
190
|
/** @type {'text' | 'json'} */
|
|
171
191
|
let responseType;
|
|
172
|
-
const
|
|
173
|
-
if (
|
|
174
|
-
|
|
175
|
-
|
|
192
|
+
const resContentType = response.headers.get('content-type') || '';
|
|
193
|
+
if (resContentType.includes('application/json') || resContentType.includes('application/x-amz-json-1.0')) {
|
|
194
|
+
try {
|
|
195
|
+
responseBody = await response.clone().json();
|
|
196
|
+
responseType = 'json';
|
|
197
|
+
} catch (err) {
|
|
198
|
+
responseBody = await response.clone().text();
|
|
199
|
+
responseType = 'text';
|
|
200
|
+
}
|
|
176
201
|
} else {
|
|
202
|
+
responseBody = await response.clone().text();
|
|
177
203
|
responseType = 'text';
|
|
178
|
-
body = await response.clone().text();
|
|
179
204
|
}
|
|
180
205
|
/** @type {Snapshot} */
|
|
181
206
|
const snapshot = {
|
|
207
|
+
requestType,
|
|
182
208
|
request: {
|
|
183
209
|
method: request.method,
|
|
184
210
|
url: request.url,
|
|
185
211
|
headers: [...request.headers.entries()],
|
|
186
|
-
body:
|
|
212
|
+
body: requestBody,
|
|
187
213
|
},
|
|
188
214
|
responseType,
|
|
189
215
|
response: {
|
|
190
216
|
status: response.status,
|
|
191
217
|
statusText: response.statusText,
|
|
192
218
|
headers: [...response.headers.entries()],
|
|
193
|
-
body,
|
|
219
|
+
body: responseBody,
|
|
194
220
|
},
|
|
195
221
|
fileSuffixKey,
|
|
196
222
|
};
|