http-snapshotter 0.2.0 → 0.2.1

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.
Files changed (2) hide show
  1. package/index.js +9 -3
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -171,11 +171,17 @@ async function saveSnapshot(request, response) {
171
171
  let responseType;
172
172
  const contentType = response.headers.get('content-type') || '';
173
173
  if (contentType.includes('application/json') || contentType.includes('application/x-amz-json-1.0')) {
174
- responseType = 'json';
175
- body = await response.clone().json();
174
+ try {
175
+ // most common JSON parse failure is when body is empty.
176
+ body = await response.clone().json();
177
+ responseType = 'json';
178
+ } catch (err) {
179
+ body = await response.clone().text();
180
+ responseType = 'text';
181
+ }
176
182
  } else {
177
- responseType = 'text';
178
183
  body = await response.clone().text();
184
+ responseType = 'text';
179
185
  }
180
186
  /** @type {Snapshot} */
181
187
  const snapshot = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "http-snapshotter",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "Snapshot HTTP requests for tests (node.js)",
5
5
  "main": "index.cjs",
6
6
  "types": "index.d.ts",