convex
Version:
Client for the Convex Cloud
102 lines • 3.41 kB
TypeScript
import { PublicHttpEndpoint } from "./registration.js";
declare const routableMethods: readonly ["GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"];
declare type RoutableMethod = typeof routableMethods[number];
/**
* Return a new {@link HttpRouter} object.
*
* @public
*/
export declare const httpRouter: () => HttpRouter;
declare type RouteSpec = {
path: string;
method: RoutableMethod;
handler: PublicHttpEndpoint<any>;
} | {
pathPrefix: string;
method: RoutableMethod;
handler: PublicHttpEndpoint<any>;
};
/**
* HTTP router for specifying the paths and methods of {@link httpEndpointGeneric}s
*
* An example `convex/http.js` file might look like this.
*
* ```js
* import { httpRouter } from "./_generated/server";
* import { getMessagesByAuthor } from "./getMessagesByAuthor";
* import { httpEndpoint } from "./_generated/server";
*
* const http = httpRouter();
*
* // HTTP endpoints can be defined inline...
* http.route({
* path: "/message",
* method: "POST",
* handler: httpEndpoint(async ({ runMutation }, request) => {
* const { author, body } = await request.json();
*
* await runMutation("sendMessage", body, author);
* return new Response(null, {
* status: 200,
* });
* })
* });
*
* // ...or they can be imported from other files.
* http.route({
* path: "/getMessagesByAuthor",
* method: "GET",
* handler: getMessagesByAuthor,
* });
*
* // Convex expects the router to be the default export of `convex/http.js`.
* export default http;
* ```
*
* @public
*/
export declare class HttpRouter {
exactRoutes: Map<string, Map<RoutableMethod, PublicHttpEndpoint<any>>>;
prefixRoutes: Map<RoutableMethod, Map<string, PublicHttpEndpoint<any>>>;
isRouter: boolean;
/**
* Specify an HttpEndpoint to be used to respond to requests
* for an HTTP method (e.g. "GET") and a path or pathPrefix.
*
* Paths must begin with a slash. Path prefixes must also end in a slash.
*
* ```js
* // matches `/profile` (but not `/profile/`)
* http.route({ path: "/profile", method: "GET", handler: getProfile})
*
* // matches `/profiles/`, `/profiles/abc`, and `/profiles/a/c/b` (but not `/profile`)
* http.route({ pathPrefix: "/profile/", method: "GET", handler: getProfile})
* ```
* @returns - an array of [path, route, endpoints] tuples.
*/
route: (spec: RouteSpec) => void;
/**
* Returns a list of routed HTTP endpoints.
*
* @returns - an array of [path, route, endpoints] tuples.
*/
getRoutes: () => (readonly [string, "POST" | "GET" | "PUT" | "DELETE" | "OPTIONS" | "PATCH", PublicHttpEndpoint<any>])[];
/**
* Returns the appropriate HTTP endpoint for a request path and method.
*
* @returns - a {@link PublicHttpEndpoint} or null.
*/
lookup: (path: string, method: RoutableMethod | "HEAD") => [PublicHttpEndpoint<any>, RoutableMethod, string] | null;
/**
* Given a JSON string representation of a Request object, return a Response
* by routing the request and running the appropriate endpoint or returning
* a 404 Response.
*
* @param argsStr - a JSON string representing a Request object.
*
* @returns - a Response object.
*/
runRequest: (argsStr: string) => Promise<string>;
}
export {};
//# sourceMappingURL=router.d.ts.map