shelving 1.167.0 → 1.167.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/api/util.d.ts +2 -2
- package/api/util.js +4 -5
- package/package.json +1 -1
package/api/util.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { AnyCaller } from "../util/function.js";
|
|
1
2
|
import type { Endpoint } from "./Endpoint.js";
|
|
2
3
|
/**
|
|
3
4
|
* A function that handles a endpoint request, with a payload and returns a result.
|
|
@@ -32,9 +33,8 @@ export type EndpointHandlers = ReadonlyArray<AnyEndpointHandler>;
|
|
|
32
33
|
*
|
|
33
34
|
* 1. Define your `Endpoint` objects with a method, path, payload and result validators, e.g. `GET("/test/{id}", PAYLOAD, STRING)`
|
|
34
35
|
* 2. Make an array of `EndpointHandler` objects combining an `Endpoint` with a `callback` function
|
|
35
|
-
* -
|
|
36
36
|
*
|
|
37
37
|
* @returns The resulting `Response` from the first handler that matches the `Request`.
|
|
38
38
|
* @throws `NotFoundError` if no handler matches the `Request`.
|
|
39
39
|
*/
|
|
40
|
-
export declare function handleEndpoints(request: Request, endpoints: EndpointHandlers): Promise<Response>;
|
|
40
|
+
export declare function handleEndpoints(request: Request, endpoints: EndpointHandlers, caller?: AnyCaller): Promise<Response>;
|
package/api/util.js
CHANGED
|
@@ -9,17 +9,16 @@ import { getURL } from "../util/url.js";
|
|
|
9
9
|
*
|
|
10
10
|
* 1. Define your `Endpoint` objects with a method, path, payload and result validators, e.g. `GET("/test/{id}", PAYLOAD, STRING)`
|
|
11
11
|
* 2. Make an array of `EndpointHandler` objects combining an `Endpoint` with a `callback` function
|
|
12
|
-
* -
|
|
13
12
|
*
|
|
14
13
|
* @returns The resulting `Response` from the first handler that matches the `Request`.
|
|
15
14
|
* @throws `NotFoundError` if no handler matches the `Request`.
|
|
16
15
|
*/
|
|
17
|
-
export function handleEndpoints(request, endpoints) {
|
|
16
|
+
export function handleEndpoints(request, endpoints, caller = handleEndpoints) {
|
|
18
17
|
// Parse the URL of the request.
|
|
19
18
|
const requestUrl = request.url;
|
|
20
19
|
const url = getURL(requestUrl);
|
|
21
20
|
if (!url)
|
|
22
|
-
throw new RequestError("Invalid request URL", { received: requestUrl, caller
|
|
21
|
+
throw new RequestError("Invalid request URL", { received: requestUrl, caller });
|
|
23
22
|
const { origin, pathname, searchParams } = url;
|
|
24
23
|
// Iterate over the handlers and return the first one that matches the request.
|
|
25
24
|
for (const { endpoint, callback } of endpoints) {
|
|
@@ -28,7 +27,7 @@ export function handleEndpoints(request, endpoints) {
|
|
|
28
27
|
continue;
|
|
29
28
|
// Ensure the request URL e.g. `/user/123` matches the endpoint path e.g. `/user/{id}`
|
|
30
29
|
// Any `{placeholders}` in the endpoint path are matched against the request URL to extract parameters.
|
|
31
|
-
const pathParams = matchTemplate(endpoint.url, `${origin}${pathname}`,
|
|
30
|
+
const pathParams = matchTemplate(endpoint.url, `${origin}${pathname}`, caller);
|
|
32
31
|
if (!pathParams)
|
|
33
32
|
continue;
|
|
34
33
|
// Make a simple dictionary object from the `{placeholder}` path params and the `?a=123` query params from the URL.
|
|
@@ -37,7 +36,7 @@ export function handleEndpoints(request, endpoints) {
|
|
|
37
36
|
return handleEndpoint(endpoint, callback, combinedParams, request);
|
|
38
37
|
}
|
|
39
38
|
// No handler matched the request.
|
|
40
|
-
throw new NotFoundError("No matching endpoint", { received: requestUrl, caller
|
|
39
|
+
throw new NotFoundError("No matching endpoint", { received: requestUrl, caller });
|
|
41
40
|
}
|
|
42
41
|
/** Handle an individual call to an endpoint callback. */
|
|
43
42
|
async function handleEndpoint(endpoint, callback, params, request) {
|