http-snapshotter 0.2.1 → 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.
Files changed (2) hide show
  1. package/index.js +33 -13
  2. 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,37 +168,55 @@ async function saveSnapshot(request, response) {
166
168
 
167
169
  /** @returns {ReadSnapshotReturnType} */
168
170
  const saveFreshSnapshot = async () => {
169
- let body;
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 contentType = response.headers.get('content-type') || '';
173
- if (contentType.includes('application/json') || contentType.includes('application/x-amz-json-1.0')) {
192
+ const resContentType = response.headers.get('content-type') || '';
193
+ if (resContentType.includes('application/json') || resContentType.includes('application/x-amz-json-1.0')) {
174
194
  try {
175
- // most common JSON parse failure is when body is empty.
176
- body = await response.clone().json();
195
+ responseBody = await response.clone().json();
177
196
  responseType = 'json';
178
197
  } catch (err) {
179
- body = await response.clone().text();
198
+ responseBody = await response.clone().text();
180
199
  responseType = 'text';
181
200
  }
182
201
  } else {
183
- body = await response.clone().text();
202
+ responseBody = await response.clone().text();
184
203
  responseType = 'text';
185
204
  }
186
205
  /** @type {Snapshot} */
187
206
  const snapshot = {
207
+ requestType,
188
208
  request: {
189
209
  method: request.method,
190
210
  url: request.url,
191
211
  headers: [...request.headers.entries()],
192
- body: await request.clone().text(),
212
+ body: requestBody,
193
213
  },
194
214
  responseType,
195
215
  response: {
196
216
  status: response.status,
197
217
  statusText: response.statusText,
198
218
  headers: [...response.headers.entries()],
199
- body,
219
+ body: responseBody,
200
220
  },
201
221
  fileSuffixKey,
202
222
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "http-snapshotter",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "Snapshot HTTP requests for tests (node.js)",
5
5
  "main": "index.cjs",
6
6
  "types": "index.d.ts",