shelving 1.154.0 → 1.154.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.
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "state-management",
12
12
  "query-builder"
13
13
  ],
14
- "version": "1.154.0",
14
+ "version": "1.154.1",
15
15
  "repository": "https://github.com/dhoulb/shelving",
16
16
  "author": "Dave Houlbrooke <dave@shax.com>",
17
17
  "license": "0BSD",
package/util/http.d.ts CHANGED
@@ -9,41 +9,25 @@ export declare function _getMessageContent(message: Request | Response, MessageE
9
9
  /**
10
10
  * Get the body content of an HTTP `Request` based on its content type, or throw `RequestError` if the content could not be parsed.
11
11
  *
12
+ * @returns undefined If the request method is `GET` or `HEAD` (these request methods have no body).
12
13
  * @returns unknown If content type is `application/json` and has valid JSON (including `undefined` if the content is empty).
13
14
  * @returns unknown If content type is `multipart/form-data` then convert it to a simple `Data` object.
14
15
  * @returns string If content type is `text/plain` or anything else (including `""` empty string if it's empty).
15
16
  *
16
17
  * @throws RequestError if the content is not `text/plain`, or `application/json` with valid JSON.
17
18
  */
18
- export declare function getRequestContent(message: Request, caller?: AnyCaller): Promise<unknown>;
19
+ export declare function getRequestContent(request: Request, caller?: AnyCaller): Promise<unknown>;
19
20
  /**
20
21
  * Get the body content of an HTTP `Response` based on its content type, or throw `ResponseError` if the content could not be parsed.
21
22
  *
23
+ * @returns undefined If the request status is `204 No Content` (this response has no body).
22
24
  * @returns unknown If content type is `application/json` and has valid JSON (including `undefined` if the content is empty).
23
25
  * @returns unknown If content type is `multipart/form-data` then convert it to a simple `Data` object.
24
26
  * @returns string If content type is `text/plain` or anything else (including `""` empty string if it's empty).
25
27
  *
26
28
  * @throws RequestError if the content is not `text/plain` or `application/json` with valid JSON.
27
29
  */
28
- export declare function getResponseContent(message: Response, caller?: AnyCaller): Promise<unknown>;
29
- /**
30
- * Get the body content of an HTTP `Request` as JSON, or throw `RequestError` if the content could not be parsed.
31
- * - Doesn't check the `Content-Type` header, so it can be used for any request.
32
- *
33
- * @returns unknown The parsed JSON content of the request body, or `undefined` if the body is empty.
34
- *
35
- * @throws RequestError if the content is not valid JSON.
36
- */
37
- export declare function getRequestJSON(message: Request, caller?: AnyCaller): Promise<unknown>;
38
- /**
39
- * Get the body content of an HTTP `Response` as JSON, or throw `ResponseError` if the content could not be parsed.
40
- * - Doesn't check the `Content-Type` header, so it can be used for any response.
41
- *
42
- * @returns unknown The parsed JSON content of the response body, or `undefined` if the body is empty.
43
- *
44
- * @throws RequestError if the content is not valid JSON.
45
- */
46
- export declare function getResponseJSON(message: Response, caller?: AnyCaller): Promise<unknown>;
30
+ export declare function getResponseContent(response: Response, caller?: AnyCaller): Promise<unknown>;
47
31
  /**
48
32
  * Get an HTTP `Response` for an unknown value.
49
33
  *
package/util/http.js CHANGED
@@ -24,59 +24,47 @@ export async function _getMessageFormData(message, MessageError, caller) {
24
24
  }
25
25
  export function _getMessageContent(message, MessageError, caller) {
26
26
  const type = message.headers.get("Content-Type");
27
+ if (!type || type?.startsWith("text/"))
28
+ return message.text();
27
29
  if (type?.startsWith("application/json"))
28
30
  return _getMessageJSON(message, MessageError, caller);
29
31
  if (type?.startsWith("multipart/form-data"))
30
32
  return _getMessageFormData(message, MessageError, caller);
31
- return message.text();
33
+ throw new MessageError("Content-Type must be text/*, application/json, or multipart/form-data", { received: type, caller });
32
34
  }
33
35
  /**
34
36
  * Get the body content of an HTTP `Request` based on its content type, or throw `RequestError` if the content could not be parsed.
35
37
  *
38
+ * @returns undefined If the request method is `GET` or `HEAD` (these request methods have no body).
36
39
  * @returns unknown If content type is `application/json` and has valid JSON (including `undefined` if the content is empty).
37
40
  * @returns unknown If content type is `multipart/form-data` then convert it to a simple `Data` object.
38
41
  * @returns string If content type is `text/plain` or anything else (including `""` empty string if it's empty).
39
42
  *
40
43
  * @throws RequestError if the content is not `text/plain`, or `application/json` with valid JSON.
41
44
  */
42
- export function getRequestContent(message, caller = getRequestContent) {
43
- if (message.method === "GET" || message.method === "HEAD")
45
+ export function getRequestContent(request, caller = getRequestContent) {
46
+ const { method } = request;
47
+ // The HTTP/1.1 RFC 7231 does not forbid sending a body in GET or HEAD requests, but it is uncommon and not recommended because many servers, proxies, and caches may ignore or mishandle it.
48
+ if (method === "GET" || method === "HEAD")
44
49
  return Promise.resolve(undefined);
45
- return _getMessageContent(message, RequestError, caller);
50
+ return _getMessageContent(request, RequestError, caller);
46
51
  }
47
52
  /**
48
53
  * Get the body content of an HTTP `Response` based on its content type, or throw `ResponseError` if the content could not be parsed.
49
54
  *
55
+ * @returns undefined If the request status is `204 No Content` (this response has no body).
50
56
  * @returns unknown If content type is `application/json` and has valid JSON (including `undefined` if the content is empty).
51
57
  * @returns unknown If content type is `multipart/form-data` then convert it to a simple `Data` object.
52
58
  * @returns string If content type is `text/plain` or anything else (including `""` empty string if it's empty).
53
59
  *
54
60
  * @throws RequestError if the content is not `text/plain` or `application/json` with valid JSON.
55
61
  */
56
- export function getResponseContent(message, caller = getResponseContent) {
57
- return _getMessageContent(message, ResponseError, caller);
58
- }
59
- /**
60
- * Get the body content of an HTTP `Request` as JSON, or throw `RequestError` if the content could not be parsed.
61
- * - Doesn't check the `Content-Type` header, so it can be used for any request.
62
- *
63
- * @returns unknown The parsed JSON content of the request body, or `undefined` if the body is empty.
64
- *
65
- * @throws RequestError if the content is not valid JSON.
66
- */
67
- export function getRequestJSON(message, caller = getRequestJSON) {
68
- return _getMessageJSON(message, RequestError, caller);
69
- }
70
- /**
71
- * Get the body content of an HTTP `Response` as JSON, or throw `ResponseError` if the content could not be parsed.
72
- * - Doesn't check the `Content-Type` header, so it can be used for any response.
73
- *
74
- * @returns unknown The parsed JSON content of the response body, or `undefined` if the body is empty.
75
- *
76
- * @throws RequestError if the content is not valid JSON.
77
- */
78
- export function getResponseJSON(message, caller = getResponseJSON) {
79
- return _getMessageJSON(message, ResponseError, caller);
62
+ export function getResponseContent(response, caller = getResponseContent) {
63
+ const { status } = response;
64
+ // RFC 7230 Section 3.3.3: A server MUST NOT send a Content-Length header field in any response with a status code of 1xx (Informational), 204 (No Content), or 304 (Not Modified).
65
+ if ((status >= 100 && status < 200) || status === 204 || status === 304)
66
+ return Promise.resolve(undefined);
67
+ return _getMessageContent(response, ResponseError, caller);
80
68
  }
81
69
  /**
82
70
  * Get an HTTP `Response` for an unknown value.