freight-club 0.0.3 → 0.0.4

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/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.0.4
4
+
5
+ - Check the content type before the status. 0.0.3 only checked a 200, so an HTML error page served at a 404 still reached `HttpError` and came back as `err.text`.
6
+
3
7
  ## 0.0.3
4
8
 
5
9
  - Refuse a 200 that is not JSON, naming the content type and the URL that answered. Error pages arrive with a 200, and a whole marketing page was once printed as the reason there were no freight quotes.
package/index.js CHANGED
@@ -10,15 +10,15 @@ const HttpError = require('@stores.com/http-error');
10
10
  * @throws {HttpError} If the response status is not 200.
11
11
  */
12
12
  async function parseResponse(res) {
13
- if (res.status !== 200) {
14
- throw await HttpError.from(res);
15
- }
16
-
17
- // Error pages, WAF challenges and marketing sites all arrive with a 200, and res.json() on one throws a parse error naming a character offset
13
+ // Before the status, or HttpError puts a whole error page in err.text. Freight Club's own 401s declare no type and belong to HttpError.
18
14
  const contentType = res.headers.get('content-type');
19
15
 
20
- if (!contentType?.includes('json')) {
21
- throw new Error(`Freight Club answered ${res.url} with ${contentType || 'no content type'} rather than JSON.`);
16
+ if (contentType && !contentType.includes('json')) {
17
+ throw new Error(`Freight Club answered with ${contentType} rather than JSON.`);
18
+ }
19
+
20
+ if (res.status !== 200) {
21
+ throw await HttpError.from(res);
22
22
  }
23
23
 
24
24
  return await res.json();
package/package.json CHANGED
@@ -32,5 +32,5 @@
32
32
  "test": "node --test --test-force-exit test",
33
33
  "test:only": "node --test --test-force-exit --test-only test"
34
34
  },
35
- "version": "0.0.3"
35
+ "version": "0.0.4"
36
36
  }