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>
ParamType
basePossibleURL
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
handlersEndpointHandlers<C>
The iterable of EndpointHandler objects to match the request against, in order. required
requestRequest
The input request to handle. required
contextC
The additional context value passed through to the matched handler's callback. required
callerAnyCaller
The function to attribute thrown errors to (defaults to this function).
basePossibleURL
Values that can be converted to a URL instance. required
handlersEndpointHandlers<void>
A collection of endpoint handlers that can be matched and invoked by handleEndpoints(). required
requestRequest
required
contextundefined
callerAnyCaller
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 Request object 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)