freight-club 0.0.1 → 0.0.3

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,13 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.0.3
4
+
5
+ - 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.
6
+
7
+ ## 0.0.2
8
+
9
+ - No functional changes; first release published from CI
10
+
3
11
  ## 0.0.1
4
12
 
5
13
  - Initial release, covering Freight Club API Documentation version 2.4: `bookShipment`, `cancelShipment`, `downloadBol`, `exportOrders`, `getBol`, `getLabel`, `getOrderStatus`, `getRate`, `getRates` and `getShipmentTracking`
package/index.js CHANGED
@@ -1,11 +1,12 @@
1
1
  const HttpError = require('@stores.com/http-error');
2
2
 
3
3
  /**
4
- * Throws an HttpError for any non-200 response and returns the parsed JSON body otherwise.
4
+ * Throws for anything that is not a JSON 200 and returns the parsed JSON body otherwise.
5
5
  *
6
6
  * @private
7
7
  * @param {Response} res - A fetch Response.
8
8
  * @returns {Promise.<Object>} The parsed JSON body.
9
+ * @throws {Error} If the response is not JSON.
9
10
  * @throws {HttpError} If the response status is not 200.
10
11
  */
11
12
  async function parseResponse(res) {
@@ -13,6 +14,13 @@ async function parseResponse(res) {
13
14
  throw await HttpError.from(res);
14
15
  }
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
18
+ const contentType = res.headers.get('content-type');
19
+
20
+ if (!contentType?.includes('json')) {
21
+ throw new Error(`Freight Club answered ${res.url} with ${contentType || 'no content type'} rather than JSON.`);
22
+ }
23
+
16
24
  return await res.json();
17
25
  }
18
26
 
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.1"
35
+ "version": "0.0.3"
36
36
  }