Skip to content

Building Blocks ​

The building blocks routup uses internally, exported for plugin authors and integrations: type guards, plugin errors, the classes behind handlers and events, path matching, response conversion and constants. An application built from the Guide does not need them. Plugin Authoring shows how plugins use them.

Type Guards ​

isHandler ​

typescript
declare function isHandler(input: unknown): input is Handler;

Checks whether a value is a Handler, as returned by defineCoreHandler(), defineErrorHandler() and the adapters. It checks a marker, not instanceof, so a handler of another copy of routup also counts.

isHandlerOptions ​

typescript
declare function isHandlerOptions(input: unknown): input is HandlerOptions;

Checks whether a value is an object with a function fn and a string type, the shape app.use() accepts in place of a Handler.

isPlugin ​

typescript
declare function isPlugin(input: unknown): input is Plugin;

Checks whether a value is a plugin: an object whose install is a function declaring exactly one parameter, and whose name is a string or absent.

isPluginError ​

typescript
declare function isPluginError(input: unknown): input is PluginError;

Checks whether a value is an AppError whose code is one of the PluginErrorCode values. A PluginError created with another code does not count.

isWebHandlerProvider ​

typescript
declare function isWebHandlerProvider(input: unknown): input is WebHandlerProvider;

Checks whether a value is an object with a fetch function, which fromWebHandler() calls. An App counts.

Plugin Errors ​

PluginError ​

typescript
declare class PluginError extends AppError {
    constructor(input?: HTTPErrorInput);
}

The base class of the plugin errors. It takes the options of AppError; code defaults to 'PLUGIN' and the status to 500.

PluginNotInstalledError ​

typescript
declare class PluginNotInstalledError extends PluginError {
    readonly pluginName: string;
    readonly helperName: string;

    constructor(pluginName: string, helperName: string);
}

The error a plugin's helper throws when it is called on an App without the plugin; see Plugin Conventions. Its code is 'PLUGIN_NOT_INSTALLED'.

typescript
throw new PluginNotInstalledError('cookie', 'useRequestCookie');
// message: useRequestCookie() requires the "cookie" plugin. Register it with: router.use(cookie())

PluginInstallError ​

typescript
declare class PluginInstallError extends PluginError {
    readonly pluginName: string;

    constructor(pluginName: string, cause?: Error);
}

An error for a plugin whose installation failed, with the message Failed to install plugin "<name>". and the code 'PLUGIN_INSTALL'. Routup does not throw it itself: an error thrown by install() reaches the caller of app.use() unchanged.

PluginErrorCode ​

typescript
const PluginErrorCode = {
    PLUGIN: 'PLUGIN',
    NOT_INSTALLED: 'PLUGIN_NOT_INSTALLED',
    INSTALL: 'PLUGIN_INSTALL',
} as const;

The code values of the plugin errors.

Handlers and Events ​

Handler ​

typescript
declare class Handler {
    constructor(handler: HandlerOptions);

    readonly method: MethodName | undefined;
    get type(): HandlerType;
    get path(): Path | undefined;
}

The object the handler factories return and app.use() registers. Its constructor takes HandlerOptions, which is CoreHandlerOptions | ErrorHandlerOptions. The factories set type; the constructor adds a leading / to path and converts method to upper case.

HandlerType ​

typescript
const HandlerType = {
    CORE: 'core',
    ERROR: 'error',
} as const;

The type of a handler: 'core' runs while no error is pending, 'error' only while one is.

AppEvent ​

typescript
declare class AppEvent implements IAppEvent {
    constructor(context: AppEventContext);
}

The class of the event every handler receives. Routup creates one for each handler; constructing one yourself lets a helper run outside a request. The table lists the fields of AppEventContext; AppEventCreateContext is a deprecated alias of it.

NameTypeMeaning
requestAppRequestThe request
paramsRecord<string, string | undefined>Route parameters
pathstringRequest path
methodMethodNameLikeRequest method
mountPathstringPath prefix the handler's route consumed
headersHeadersRequest headers
searchParamsURLSearchParamsOptional; the query, parsed
searchstringOptional; the query string, parsed on the first read of searchParams when searchParams is omitted
responseAppResponseStatus and headers of the response
storeRecord<string | symbol, unknown>Per-request data
signalAbortSignalAbort signal of the handler
appOptionsReadonly<AppOptions>Options of the App
route() => AppEventRoute | undefinedOptional; read on every access of event.route
next(event: IAppEvent, error?: Error) => Promise<Response | undefined>Called by the first event.next(); later calls return its promise

Paths ​

PathMatcher ​

typescript
declare class PathMatcher implements IPathMatcher {
    constructor(path: Path, options?: PathMatcherOptions);

    test(path: string): boolean;
    exec(path: string): PathMatcherExecResult | undefined;
}

type PathMatcherExecResult = {
    path: string,
    params: Record<string, any>,
};

Matches request paths against a route path with path-to-regexp; the syntax is on Paths. The options, PathMatcherOptions, are those of path-to-regexp (end, trailing, sensitive, delimiter, encodePath); end: false matches a prefix. sensitive defaults to true, so static segments match case-sensitively; pass sensitive: false to opt out.

typescript
import { PathMatcher } from 'routup';

const matcher = new PathMatcher('/users/:id');

matcher.exec('/users/a%20b'); // { path: '/users/a%20b', params: { id: 'a b' } }
matcher.exec('/users');       // undefined

IPathMatcher is the interface of its exec() method, the type buildRoutePathMatcher() returns.

Responses ​

toResponse ​

typescript
declare function toResponse(
    value: unknown,
    event: IAppEvent,
): Response | undefined | Promise<Response | undefined>;

Converts a handler's return value into a Response with the status and headers of event.response, following the return value rules. Routup calls it for every handler.

  • undefined gives undefined.
  • It returns a promise when it computes an ETag, which it does for string and JSON bodies of a 2xx GET or HEAD unless etag is off.
  • It also returns a promise for a generator or async iterable, which it streams (see sendIterable).

serializeEventStreamMessage ​

typescript
declare function serializeEventStreamMessage(message: EventStreamMessage): string;

Formats a message in the server-sent events format, as createEventStream() writes it.

typescript
serializeEventStreamMessage({ id: '1', event: 'tick', data: 'a\nb' });
// 'id: 1\nevent: tick\ndata: a\ndata: b\n\n'
  • Each line of data becomes a data: line. Line breaks in id and event are removed.
  • retry is written only when it is a safe integer.

serializeEventStreamComment ​

typescript
declare function serializeEventStreamComment(comment: string): string;

Formats a comment in the server-sent events format: each line prefixed with : , followed by an empty line. serializeEventStreamComment('ping') gives ': ping\n\n'.

Requests ​

useRequestNegotiator ​

typescript
declare function useRequestNegotiator(event: IAppEvent): Negotiator;

Returns a negotiator instance for the request headers, created once per request and kept in event.store. The getRequestAcceptable* request helpers use it.

Constants ​

MethodName ​

typescript
const MethodName = {
    GET: 'GET',
    POST: 'POST',
    PUT: 'PUT',
    PATCH: 'PATCH',
    DELETE: 'DELETE',
    OPTIONS: 'OPTIONS',
    HEAD: 'HEAD',
} as const;

The HTTP methods the App has shortcuts for. The type MethodNameLike is MethodName or any other string.

HeaderName ​

typescript
const HeaderName = {
    ACCEPT: 'accept',
    CONTENT_TYPE: 'content-type',
    ETag: 'etag',
    X_FORWARDED_FOR: 'x-forwarded-for',
    // ...
} as const;

Lower-case names of the HTTP headers routup reads or writes, from ACCEPT to X_FORWARDED_PROTO. The full list is in src/constants.ts.