Skip to content

Commit de32893

Browse files
committed
bug fixes and path params changes
- prismaField error - OPTIONS route not found for index - catch handler bug - keyReplacer for key paths - named capture groups for path params
1 parent 9885d99 commit de32893

File tree

14 files changed

+63
-27
lines changed

14 files changed

+63
-27
lines changed

package-lock.json

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/mws/src/managers/admin-utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export function admin<T extends zod.ZodTypeAny, R extends JsonValue>(
1111
return zodRoute({
1212
method: ["POST"],
1313
path: "/admin/$key",
14+
keyReplacer: "$key",
1415
zodPathParams: z => ({}),
1516
zodQueryParams: z => ({}),
1617
bodyFormat: "json",
@@ -37,4 +38,4 @@ export function admin<T extends zod.ZodTypeAny, R extends JsonValue>(
3738
return state.$transaction(async (prisma) => await inner(state, prisma));
3839
}
3940
});
40-
}
41+
}

packages/mws/src/managers/wiki-index.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,12 @@ serverEvents.on("mws.routes", (root, config) => {
7070

7171
// the wiki index route
7272
root.defineRoute({
73-
method: ["GET", "HEAD"],
74-
path: /^\/wiki\/(.*)$/,
75-
pathParams: ["recipe_name"],
73+
method: ["GET", "HEAD", "OPTIONS"],
74+
path: /^\/wiki\/(?<recipe_name>.*)$/,
7675
bodyFormat: "ignore",
7776
}, async (state) => {
77+
if (state.method === "OPTIONS") throw state.sendEmpty(405, {});
78+
7879
const timekey = `handler ${state.bodyFormat} ${state.method} ${state.urlInfo.pathname} ${Date.now()}`;
7980

8081
checkPath(state, z => ({
@@ -102,7 +103,11 @@ serverEvents.on("mws.routes", (root, config) => {
102103
throw STREAM_ENDED;
103104
}, async (state, e) => {
104105
if (e instanceof SendError) {
105-
await state.sendAdmin(e.status, { sendError: e });
106+
if (state.headersSent) {
107+
console.log(e + "\n" + new Error("").stack?.split("\n").slice(2).join("\n"));
108+
} else {
109+
await state.sendAdmin(e.status, { sendError: e });
110+
}
106111
} else {
107112
console.log("Unexpected error in wiki index route", e);
108113
const error = new SendError("INTERNAL_SERVER_ERROR", 500, {

packages/mws/src/managers/wiki-recipe.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export class WikiRecipeRoutes {
4545
rpcLoadRecipeTiddlerList = zodRoute({
4646
method: ["PUT"],
4747
path: RECIPE_PREFIX + "/:recipe_name/rpc/$key",
48+
keyReplacer: "$key",
4849
bodyFormat: "json",
4950
zodPathParams: z => ({
5051
recipe_name: z.prismaField("Recipes", "recipe_name", "string"),
@@ -152,6 +153,7 @@ export class WikiRecipeRoutes {
152153
rpcSaveRecipeTiddlerList = zodRoute({
153154
method: ["PUT"],
154155
path: RECIPE_PREFIX + "/:recipe_name/rpc/$key",
156+
keyReplacer: "$key",
155157
bodyFormat: "json",
156158
zodPathParams: z => ({
157159
recipe_name: z.prismaField("Recipes", "recipe_name", "string"),
@@ -237,6 +239,7 @@ export class WikiRecipeRoutes {
237239
rpcDeleteRecipeTiddlerList = zodRoute({
238240
method: ["PUT"],
239241
path: RECIPE_PREFIX + "/:recipe_name/rpc/$key",
242+
keyReplacer: "$key",
240243
bodyFormat: "json",
241244
zodPathParams: z => ({
242245
recipe_name: z.prismaField("Recipes", "recipe_name", "string"),

packages/mws/src/registerRequest.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ declare module "@tiddlywiki/server" {
4242
}
4343
}
4444

45+
Router.allowedRequestedWithHeaders.TiddlyWiki = true;
4546

4647
serverEvents.on("listen.router.init", async (listen, router) => {
47-
router.allowedRequestedWithHeaders.push("TiddlyWiki");
48+
4849
router.config = listen.config;
4950
router.sendAdmin = await setupDevServer(listen.config);
5051
router.createServerRequest = <B extends BodyFormat>(

packages/mws/src/services/cache.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,8 @@ export async function startupCache($tw: TW, cachePath: string) {
5050
serverEvents.on("mws.routes", (root, config) => {
5151
root.defineRoute({
5252
method: ["GET", "HEAD"],
53-
path: /^\/\$cache\/(.*)\/plugin\.js$/,
53+
path: /^\/\$cache\/(?<plugin>.*)\/plugin\.js$/,
5454
bodyFormat: "ignore",
55-
pathParams: ["plugin"]
5655
}, async (state: ServerRequest<BodyFormat, string, unknown>) => {
5756

5857
checkPath(state, z => ({ plugin: z.string() }), new Error());

packages/mws/src/services/sessions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ export function zodSession<P extends string, T extends zod.ZodTypeAny, R extends
4646
): ZodSessionRoute<P, T, R> {
4747
return {
4848
...zodRoute({
49-
method: ["POST"], path,
49+
method: ["POST"],
50+
path,
5051
bodyFormat: "json",
5152
zodPathParams: z => ({}),
5253
zodQueryParams: z => ({}),

packages/mws/src/services/tw-routes.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ serverEvents.on("mws.routes", (rootRoute, config) => {
1414
method: ["OPTIONS", "GET", "HEAD", "POST", "PUT", "DELETE"],
1515
bodyFormat: "stream",
1616
path: new RegExp("^" + mountPath + "(/|$)"),
17-
pathParams: []
1817
}, TW5Route({
1918
mountPath,
2019
singleFile: true,

packages/mws/src/zodAssert.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ declare module "@tiddlywiki/server" {
4242

4343

4444

45-
export function prismaField(table: any, field: any, fieldtype: ExtraFieldType, nullable?: boolean): any {
45+
Z2.prismaField = function prismaField(table: any, field: any, fieldtype: ExtraFieldType, nullable?: boolean): any {
4646
const z = zod;
4747
const check = (() => {
4848
switch (fieldtype) {
@@ -67,6 +67,3 @@ export function prismaField(table: any, field: any, fieldtype: ExtraFieldType, n
6767

6868
}
6969

70-
serverEvents.on("zod.make", (zod: Z2<any>) => {
71-
zod.prismaField = prismaField;
72-
});

packages/server/src/router.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ export interface AllowedRequestedWithHeaderKeys {
1818
}
1919

2020
export class Router {
21-
static allowedRequestedWithHeaders: AllowedRequestedWithHeaderKeys = {
21+
static allowedRequestedWithHeaders: Partial<AllowedRequestedWithHeaderKeys> = {
2222
fetch: true,
2323
XMLHttpRequest: true,
24-
}
24+
};
2525
constructor(
2626
public rootRoute: ServerRoute
2727
) {
@@ -185,6 +185,7 @@ export class Router {
185185
await Promise.resolve().then(() => {
186186
return match.route.handler(result);
187187
}).catch(e => {
188+
if (e === STREAM_ENDED) throw e;
188189
if (match.route.catchHandler) {
189190
return match.route.catchHandler(result, e);
190191
} else {

0 commit comments

Comments
 (0)