@resora/plugin-clear-router 0.1.6 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -38,5 +38,8 @@ registerPlugin(clearRouterH3Plugin);
38
38
  ## Exports
39
39
 
40
40
  - `clearRouterExpressPlugin`
41
+ - `clearRouterFastifyPlugin`
41
42
  - `clearRouterH3Plugin`
43
+ - `clearRouterHonoPlugin`
44
+ - `clearRouterKoaPlugin`
42
45
  - `clearRouterPlugin`
package/dist/index.cjs CHANGED
@@ -1,6 +1,9 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
2
  let clear_router_express = require("clear-router/express");
3
+ let clear_router_fastify = require("clear-router/fastify");
3
4
  let clear_router_h3 = require("clear-router/h3");
5
+ let clear_router_hono = require("clear-router/hono");
6
+ let clear_router_koa = require("clear-router/koa");
4
7
  let resora = require("resora");
5
8
 
6
9
  //#region src/index.ts
@@ -13,7 +16,7 @@ const patchedKey = Symbol.for("resora:clear-router-plugin:patched");
13
16
  * @param runWithCtx A function that wraps the route handler execution with a given context
14
17
  * @returns void
15
18
  */
16
- const patchRouter = (router, runWithCtx) => {
19
+ const patchRouter = (router, runWithCtx, resolveContext = (ctx) => ctx, resolveResult) => {
17
20
  const target = router;
18
21
  if (target[patchedKey]) return;
19
22
  const resolveHandler = target.resolveHandler.bind(target);
@@ -22,8 +25,10 @@ const patchRouter = (router, runWithCtx) => {
22
25
  if (!resolved.handlerFunction) return resolved;
23
26
  const handlerFunction = resolved.handlerFunction;
24
27
  resolved.handlerFunction = ((ctx, req) => {
25
- return runWithCtx(ctx, async () => {
26
- return await Promise.resolve(handlerFunction(ctx, req));
28
+ return runWithCtx(resolveContext(ctx), async () => {
29
+ const result = handlerFunction(ctx, req);
30
+ const resolvedResult = await Promise.resolve(result);
31
+ return resolveResult ? resolveResult(ctx, result, resolvedResult) : resolvedResult;
27
32
  });
28
33
  });
29
34
  return resolved;
@@ -42,6 +47,19 @@ const clearRouterExpressPlugin = (0, resora.definePlugin)({
42
47
  }
43
48
  });
44
49
  /**
50
+ * Clear Router plugin for Fastify framework. Patches Clear Router's Fastify router to wrap
51
+ * route handlers with a Fastify reply-aware context for resora response mutations.
52
+ */
53
+ const clearRouterFastifyPlugin = (0, resora.definePlugin)({
54
+ name: "clear-router-fastify",
55
+ setup: ({ runWithCtx }) => {
56
+ patchRouter(clear_router_fastify.Router, runWithCtx, (ctx) => ({
57
+ ...ctx,
58
+ res: ctx.reply
59
+ }));
60
+ }
61
+ });
62
+ /**
45
63
  * Clear Router plugin for H3 framework. Patches Clear Router's H3 router to wrap route
46
64
  * handlers with the provided runWithCtx function, enabling context-aware execution of
47
65
  * route handlers in H3 applications.
@@ -52,9 +70,41 @@ const clearRouterH3Plugin = (0, resora.definePlugin)({
52
70
  patchRouter(clear_router_h3.Router, runWithCtx);
53
71
  }
54
72
  });
55
- const clearRouterPlugin = [clearRouterExpressPlugin, clearRouterH3Plugin];
73
+ /**
74
+ * Clear Router plugin for Hono framework. Patches Clear Router's Hono router to wrap
75
+ * route handlers with the active Hono context for resora response mutations.
76
+ */
77
+ const clearRouterHonoPlugin = (0, resora.definePlugin)({
78
+ name: "clear-router-hono",
79
+ setup: ({ runWithCtx }) => {
80
+ patchRouter(clear_router_hono.Router, runWithCtx, (ctx) => ctx, (ctx, result, resolved) => {
81
+ if (result && typeof result === "object" && typeof result.then === "function" && typeof result.json === "function" && typeof ctx.json === "function") return ctx.json(resolved, ctx.__resoraStatus || ctx.res?.status || 200);
82
+ return resolved;
83
+ });
84
+ }
85
+ });
86
+ /**
87
+ * Clear Router plugin for Koa framework. Patches Clear Router's Koa router to wrap
88
+ * route handlers with the active Koa context for resora response mutations.
89
+ */
90
+ const clearRouterKoaPlugin = (0, resora.definePlugin)({
91
+ name: "clear-router-koa",
92
+ setup: ({ runWithCtx }) => {
93
+ patchRouter(clear_router_koa.Router, runWithCtx);
94
+ }
95
+ });
96
+ const clearRouterPlugin = [
97
+ clearRouterExpressPlugin,
98
+ clearRouterFastifyPlugin,
99
+ clearRouterH3Plugin,
100
+ clearRouterHonoPlugin,
101
+ clearRouterKoaPlugin
102
+ ];
56
103
 
57
104
  //#endregion
58
105
  exports.clearRouterExpressPlugin = clearRouterExpressPlugin;
106
+ exports.clearRouterFastifyPlugin = clearRouterFastifyPlugin;
59
107
  exports.clearRouterH3Plugin = clearRouterH3Plugin;
108
+ exports.clearRouterHonoPlugin = clearRouterHonoPlugin;
109
+ exports.clearRouterKoaPlugin = clearRouterKoaPlugin;
60
110
  exports.clearRouterPlugin = clearRouterPlugin;
package/dist/index.d.cts CHANGED
@@ -12,6 +12,16 @@ declare const clearRouterExpressPlugin: {
12
12
  runWithCtx
13
13
  }: resora.ResoraPluginApi) => void;
14
14
  };
15
+ /**
16
+ * Clear Router plugin for Fastify framework. Patches Clear Router's Fastify router to wrap
17
+ * route handlers with a Fastify reply-aware context for resora response mutations.
18
+ */
19
+ declare const clearRouterFastifyPlugin: {
20
+ name: string;
21
+ setup: ({
22
+ runWithCtx
23
+ }: resora.ResoraPluginApi) => void;
24
+ };
15
25
  /**
16
26
  * Clear Router plugin for H3 framework. Patches Clear Router's H3 router to wrap route
17
27
  * handlers with the provided runWithCtx function, enabling context-aware execution of
@@ -23,6 +33,26 @@ declare const clearRouterH3Plugin: {
23
33
  runWithCtx
24
34
  }: resora.ResoraPluginApi) => void;
25
35
  };
36
+ /**
37
+ * Clear Router plugin for Hono framework. Patches Clear Router's Hono router to wrap
38
+ * route handlers with the active Hono context for resora response mutations.
39
+ */
40
+ declare const clearRouterHonoPlugin: {
41
+ name: string;
42
+ setup: ({
43
+ runWithCtx
44
+ }: resora.ResoraPluginApi) => void;
45
+ };
46
+ /**
47
+ * Clear Router plugin for Koa framework. Patches Clear Router's Koa router to wrap
48
+ * route handlers with the active Koa context for resora response mutations.
49
+ */
50
+ declare const clearRouterKoaPlugin: {
51
+ name: string;
52
+ setup: ({
53
+ runWithCtx
54
+ }: resora.ResoraPluginApi) => void;
55
+ };
26
56
  declare const clearRouterPlugin: {
27
57
  name: string;
28
58
  setup: ({
@@ -30,4 +60,4 @@ declare const clearRouterPlugin: {
30
60
  }: resora.ResoraPluginApi) => void;
31
61
  }[];
32
62
  //#endregion
33
- export { clearRouterExpressPlugin, clearRouterH3Plugin, clearRouterPlugin };
63
+ export { clearRouterExpressPlugin, clearRouterFastifyPlugin, clearRouterH3Plugin, clearRouterHonoPlugin, clearRouterKoaPlugin, clearRouterPlugin };
package/dist/index.d.mts CHANGED
@@ -12,6 +12,16 @@ declare const clearRouterExpressPlugin: {
12
12
  runWithCtx
13
13
  }: resora.ResoraPluginApi) => void;
14
14
  };
15
+ /**
16
+ * Clear Router plugin for Fastify framework. Patches Clear Router's Fastify router to wrap
17
+ * route handlers with a Fastify reply-aware context for resora response mutations.
18
+ */
19
+ declare const clearRouterFastifyPlugin: {
20
+ name: string;
21
+ setup: ({
22
+ runWithCtx
23
+ }: resora.ResoraPluginApi) => void;
24
+ };
15
25
  /**
16
26
  * Clear Router plugin for H3 framework. Patches Clear Router's H3 router to wrap route
17
27
  * handlers with the provided runWithCtx function, enabling context-aware execution of
@@ -23,6 +33,26 @@ declare const clearRouterH3Plugin: {
23
33
  runWithCtx
24
34
  }: resora.ResoraPluginApi) => void;
25
35
  };
36
+ /**
37
+ * Clear Router plugin for Hono framework. Patches Clear Router's Hono router to wrap
38
+ * route handlers with the active Hono context for resora response mutations.
39
+ */
40
+ declare const clearRouterHonoPlugin: {
41
+ name: string;
42
+ setup: ({
43
+ runWithCtx
44
+ }: resora.ResoraPluginApi) => void;
45
+ };
46
+ /**
47
+ * Clear Router plugin for Koa framework. Patches Clear Router's Koa router to wrap
48
+ * route handlers with the active Koa context for resora response mutations.
49
+ */
50
+ declare const clearRouterKoaPlugin: {
51
+ name: string;
52
+ setup: ({
53
+ runWithCtx
54
+ }: resora.ResoraPluginApi) => void;
55
+ };
26
56
  declare const clearRouterPlugin: {
27
57
  name: string;
28
58
  setup: ({
@@ -30,4 +60,4 @@ declare const clearRouterPlugin: {
30
60
  }: resora.ResoraPluginApi) => void;
31
61
  }[];
32
62
  //#endregion
33
- export { clearRouterExpressPlugin, clearRouterH3Plugin, clearRouterPlugin };
63
+ export { clearRouterExpressPlugin, clearRouterFastifyPlugin, clearRouterH3Plugin, clearRouterHonoPlugin, clearRouterKoaPlugin, clearRouterPlugin };
package/dist/index.mjs CHANGED
@@ -1,5 +1,8 @@
1
1
  import { Router } from "clear-router/express";
2
- import { Router as Router$1 } from "clear-router/h3";
2
+ import { Router as Router$1 } from "clear-router/fastify";
3
+ import { Router as Router$2 } from "clear-router/h3";
4
+ import { Router as Router$3 } from "clear-router/hono";
5
+ import { Router as Router$4 } from "clear-router/koa";
3
6
  import { definePlugin } from "resora";
4
7
 
5
8
  //#region src/index.ts
@@ -12,7 +15,7 @@ const patchedKey = Symbol.for("resora:clear-router-plugin:patched");
12
15
  * @param runWithCtx A function that wraps the route handler execution with a given context
13
16
  * @returns void
14
17
  */
15
- const patchRouter = (router, runWithCtx) => {
18
+ const patchRouter = (router, runWithCtx, resolveContext = (ctx) => ctx, resolveResult) => {
16
19
  const target = router;
17
20
  if (target[patchedKey]) return;
18
21
  const resolveHandler = target.resolveHandler.bind(target);
@@ -21,8 +24,10 @@ const patchRouter = (router, runWithCtx) => {
21
24
  if (!resolved.handlerFunction) return resolved;
22
25
  const handlerFunction = resolved.handlerFunction;
23
26
  resolved.handlerFunction = ((ctx, req) => {
24
- return runWithCtx(ctx, async () => {
25
- return await Promise.resolve(handlerFunction(ctx, req));
27
+ return runWithCtx(resolveContext(ctx), async () => {
28
+ const result = handlerFunction(ctx, req);
29
+ const resolvedResult = await Promise.resolve(result);
30
+ return resolveResult ? resolveResult(ctx, result, resolvedResult) : resolvedResult;
26
31
  });
27
32
  });
28
33
  return resolved;
@@ -41,6 +46,19 @@ const clearRouterExpressPlugin = definePlugin({
41
46
  }
42
47
  });
43
48
  /**
49
+ * Clear Router plugin for Fastify framework. Patches Clear Router's Fastify router to wrap
50
+ * route handlers with a Fastify reply-aware context for resora response mutations.
51
+ */
52
+ const clearRouterFastifyPlugin = definePlugin({
53
+ name: "clear-router-fastify",
54
+ setup: ({ runWithCtx }) => {
55
+ patchRouter(Router$1, runWithCtx, (ctx) => ({
56
+ ...ctx,
57
+ res: ctx.reply
58
+ }));
59
+ }
60
+ });
61
+ /**
44
62
  * Clear Router plugin for H3 framework. Patches Clear Router's H3 router to wrap route
45
63
  * handlers with the provided runWithCtx function, enabling context-aware execution of
46
64
  * route handlers in H3 applications.
@@ -48,10 +66,39 @@ const clearRouterExpressPlugin = definePlugin({
48
66
  const clearRouterH3Plugin = definePlugin({
49
67
  name: "clear-router-h3",
50
68
  setup: ({ runWithCtx }) => {
51
- patchRouter(Router$1, runWithCtx);
69
+ patchRouter(Router$2, runWithCtx);
70
+ }
71
+ });
72
+ /**
73
+ * Clear Router plugin for Hono framework. Patches Clear Router's Hono router to wrap
74
+ * route handlers with the active Hono context for resora response mutations.
75
+ */
76
+ const clearRouterHonoPlugin = definePlugin({
77
+ name: "clear-router-hono",
78
+ setup: ({ runWithCtx }) => {
79
+ patchRouter(Router$3, runWithCtx, (ctx) => ctx, (ctx, result, resolved) => {
80
+ if (result && typeof result === "object" && typeof result.then === "function" && typeof result.json === "function" && typeof ctx.json === "function") return ctx.json(resolved, ctx.__resoraStatus || ctx.res?.status || 200);
81
+ return resolved;
82
+ });
83
+ }
84
+ });
85
+ /**
86
+ * Clear Router plugin for Koa framework. Patches Clear Router's Koa router to wrap
87
+ * route handlers with the active Koa context for resora response mutations.
88
+ */
89
+ const clearRouterKoaPlugin = definePlugin({
90
+ name: "clear-router-koa",
91
+ setup: ({ runWithCtx }) => {
92
+ patchRouter(Router$4, runWithCtx);
52
93
  }
53
94
  });
54
- const clearRouterPlugin = [clearRouterExpressPlugin, clearRouterH3Plugin];
95
+ const clearRouterPlugin = [
96
+ clearRouterExpressPlugin,
97
+ clearRouterFastifyPlugin,
98
+ clearRouterH3Plugin,
99
+ clearRouterHonoPlugin,
100
+ clearRouterKoaPlugin
101
+ ];
55
102
 
56
103
  //#endregion
57
- export { clearRouterExpressPlugin, clearRouterH3Plugin, clearRouterPlugin };
104
+ export { clearRouterExpressPlugin, clearRouterFastifyPlugin, clearRouterH3Plugin, clearRouterHonoPlugin, clearRouterKoaPlugin, clearRouterPlugin };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@resora/plugin-clear-router",
3
- "version": "0.1.6",
3
+ "version": "1.0.1",
4
4
  "description": "Opt-in bridge plugin for using resora return values inside clear-router handlers.",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -30,10 +30,10 @@
30
30
  "README.md"
31
31
  ],
32
32
  "dependencies": {
33
- "resora": "1.1.0"
33
+ "resora": "1.2.0"
34
34
  },
35
35
  "peerDependencies": {
36
- "clear-router": "^2.3.5"
36
+ "clear-router": "^2.5.1"
37
37
  },
38
38
  "peerDependenciesMeta": {
39
39
  "clear-router": {
@@ -41,7 +41,14 @@
41
41
  }
42
42
  },
43
43
  "devDependencies": {
44
- "clear-router": "^2.3.5"
44
+ "@koa/router": "^15.5.0",
45
+ "@types/koa": "^3.0.2",
46
+ "@types/koa__router": "^12.0.5",
47
+ "clear-router": "^2.5.1",
48
+ "fastify": "^5.8.5",
49
+ "hono": "^4.12.18",
50
+ "koa": "^3.2.0",
51
+ "parasito": "^0.1.6"
45
52
  },
46
53
  "scripts": {
47
54
  "test": "vitest run tests/*.spec.ts",