Skip to content

Commit 526bddb

Browse files
pashabitzConvex, Inc.
authored andcommitted
[CX-5445] document http routing with pathPrefix (#24832)
Modifying our `http` demo to include an http action that fetches messages by author, determining the author id using the **request path suffix**, and a new route to it, to use as an example of using `pathPrefix` in routing. Also adding some typedocs to include documentation of `RouteSpec` in docs. GitOrigin-RevId: a4fb00fe2efa83bb5fdc82079015d47839ad7a39
1 parent 30e650b commit 526bddb

File tree

2 files changed

+62
-13
lines changed

2 files changed

+62
-13
lines changed

npm-packages/convex/src/server/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,12 @@ export type {
131131
IndexTiebreakerField,
132132
} from "./system_fields.js";
133133
export { httpRouter, HttpRouter, ROUTABLE_HTTP_METHODS } from "./router.js";
134-
export type { RoutableMethod } from "./router.js";
134+
export type {
135+
RoutableMethod,
136+
RouteSpec,
137+
RouteSpecWithPath,
138+
RouteSpecWithPathPrefix,
139+
} from "./router.js";
135140
export {
136141
anyApi,
137142
getFunctionName,

npm-packages/convex/src/server/router.ts

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,59 @@ export function normalizeMethod(
4646
*/
4747
export const httpRouter = () => new HttpRouter();
4848

49-
type RouteSpec =
50-
| {
51-
path: string;
52-
method: RoutableMethod;
53-
handler: PublicHttpAction;
54-
}
55-
| {
56-
pathPrefix: string;
57-
method: RoutableMethod;
58-
handler: PublicHttpAction;
59-
};
49+
/**
50+
* A type representing a route to an HTTP action using an exact request URL path match.
51+
*
52+
* Used by {@link HttpRouter} to route requests to HTTP actions.
53+
*
54+
* @public
55+
*/
56+
export type RouteSpecWithPath = {
57+
/**
58+
* Exact HTTP request path to route.
59+
*/
60+
path: string;
61+
/**
62+
* HTTP method ("GET", "POST", ...) to route.
63+
*/
64+
method: RoutableMethod;
65+
/**
66+
* The HTTP action to execute.
67+
*/
68+
handler: PublicHttpAction;
69+
};
70+
71+
/**
72+
* A type representing a route to an HTTP action using a request URL path prefix match.
73+
*
74+
* Used by {@link HttpRouter} to route requests to HTTP actions.
75+
*
76+
* @public
77+
*/
78+
export type RouteSpecWithPathPrefix = {
79+
/**
80+
* An HTTP request path prefix to route. Requests with a path starting with this value
81+
* will be routed to the HTTP action.
82+
*/
83+
pathPrefix: string;
84+
/**
85+
* HTTP method ("GET", "POST", ...) to route.
86+
*/
87+
method: RoutableMethod;
88+
/**
89+
* The HTTP action to execute.
90+
*/
91+
handler: PublicHttpAction;
92+
};
93+
94+
/**
95+
* A type representing a route to an HTTP action.
96+
*
97+
* Used by {@link HttpRouter} to route requests to HTTP actions.
98+
*
99+
* @public
100+
*/
101+
export type RouteSpec = RouteSpecWithPath | RouteSpecWithPathPrefix;
60102

61103
/**
62104
* HTTP router for specifying the paths and methods of {@link httpActionGeneric}s
@@ -152,7 +194,9 @@ export class HttpRouter {
152194
this.exactRoutes.set(spec.path, methods);
153195
} else if ("pathPrefix" in spec) {
154196
if (!spec.pathPrefix.startsWith("/")) {
155-
throw new Error(`path '${spec.pathPrefix}' does not start with a /`);
197+
throw new Error(
198+
`pathPrefix '${spec.pathPrefix}' does not start with a /`,
199+
);
156200
}
157201
if (!spec.pathPrefix.endsWith("/")) {
158202
throw new Error(`pathPrefix ${spec.pathPrefix} must end with a /`);

0 commit comments

Comments
 (0)