handleEndpoints()function
handleEndpoints(base: PossibleURL, handlers: EndpointHandlers<C>, request: Request, context: C, caller?: AnyCaller): Promise<Response>
handleEndpoints(base: PossibleURL, handlers: EndpointHandlers<void>, request: Request, context?: undefined, caller?: AnyCaller): Promise<Response>
| Param | Type | |
|---|---|---|
base | PossibleURL | The base URL for the API, e.g. https://myapi.com/a/b- pathname of this URL gets trimmed from request.path to form the target path when matching against endpoints, e.g. /a/b/c/d will produce /c/d for matching. required |
handlers | EndpointHandlers<C> | The iterable of EndpointHandler objects to match the request against, in order. required |
request | Request | The input request to handle. required |
context | C | The additional context value passed through to the matched handler's callback. required |
caller | AnyCaller | The function to attribute thrown errors to (defaults to this function). |
base | PossibleURL | Values that can be converted to a URL instance. required |
handlers | EndpointHandlers<void> | A collection of endpoint handlers that can be matched and invoked by handleEndpoints(). required |
request | Request | required |
context | undefined | |
caller | AnyCaller | Any calling function or constructor, usually referring to something that can call in the current scope that can appear in a stack trace. |
| Return | |
|---|---|
Promise<Response> | A promise resolving to the Response produced by the matching handler. |
Promise<Response> |
| Throws | |
|---|---|
MethodNotAllowedError | if the request method is not a supported HTTP method. |
NotFoundError | if no base path matches, or no endpoint matches the target path. |
Handle a Request with the first matching endpoint handler after stripping any base-path prefix from the request pathname.
- The original
Requestobject is passed through to the callback unchanged. - Path params and query params are merged before payload validation.
Examples
await handleEndpoints("https://myapi.com", [getUser.handler(loadUser)], request, undefined)