@resora/plugin-clear-router 0.1.0
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 +42 -0
- package/dist/index.cjs +60 -0
- package/dist/index.d.cts +33 -0
- package/dist/index.d.mts +33 -0
- package/dist/index.mjs +57 -0
- package/package.json +52 -0
package/README.md
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# @resora/plugin-clear-router
|
|
2
|
+
|
|
3
|
+
Opt-in bridge plugins for using `resora` resources inside `clear-router` routes and controller handlers without changing either core package.
|
|
4
|
+
|
|
5
|
+
## Why
|
|
6
|
+
|
|
7
|
+
`resora` resources are thenables, while `clear-router` resolves returned values from both inline handlers and controller actions. Registering these plugins lets Resora bind the active clear-router request context before the handler runs, so plain `return new Resource(...)` works for direct callbacks and controller methods.
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { registerPlugin } from 'resora';
|
|
13
|
+
import { clearRouterExpressPlugin } from '@resora/plugin-clear-router';
|
|
14
|
+
|
|
15
|
+
registerPlugin(clearRouterExpressPlugin);
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { Router, Controller } from 'clear-router';
|
|
20
|
+
import { Resource } from 'resora';
|
|
21
|
+
|
|
22
|
+
class UserController extends Controller {
|
|
23
|
+
index() {
|
|
24
|
+
return new Resource({ id: 1, name: 'Ada' });
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
Router.get('/users', [UserController, 'index']);
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import { registerPlugin } from 'resora';
|
|
33
|
+
import { clearRouterH3Plugin } from '@resora/plugin-clear-router';
|
|
34
|
+
|
|
35
|
+
registerPlugin(clearRouterH3Plugin);
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Exports
|
|
39
|
+
|
|
40
|
+
- `clearRouterExpressPlugin`
|
|
41
|
+
- `clearRouterH3Plugin`
|
|
42
|
+
- `clearRouterPlugin`
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
let clear_router_express = require("clear-router/express");
|
|
3
|
+
let clear_router_h3 = require("clear-router/h3");
|
|
4
|
+
let resora = require("resora");
|
|
5
|
+
|
|
6
|
+
//#region src/index.ts
|
|
7
|
+
const patchedKey = Symbol.for("resora:clear-router-plugin:patched");
|
|
8
|
+
/**
|
|
9
|
+
* Patch the given Clear Router router class to wrap route handlers with the provided
|
|
10
|
+
* runWithCtx function, enabling context-aware execution of route handlers.
|
|
11
|
+
*
|
|
12
|
+
* @param router The Clear Router router class to patch
|
|
13
|
+
* @param runWithCtx A function that wraps the route handler execution with a given context
|
|
14
|
+
* @returns void
|
|
15
|
+
*/
|
|
16
|
+
const patchRouter = (router, runWithCtx) => {
|
|
17
|
+
const target = router;
|
|
18
|
+
if (target[patchedKey]) return;
|
|
19
|
+
const resolveHandler = target.resolveHandler.bind(target);
|
|
20
|
+
target.resolveHandler = ((route) => {
|
|
21
|
+
const resolved = resolveHandler(route);
|
|
22
|
+
if (!resolved.handlerFunction) return resolved;
|
|
23
|
+
const handlerFunction = resolved.handlerFunction;
|
|
24
|
+
resolved.handlerFunction = ((ctx, req) => {
|
|
25
|
+
return runWithCtx(ctx, async () => {
|
|
26
|
+
return await Promise.resolve(handlerFunction(ctx, req));
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
return resolved;
|
|
30
|
+
});
|
|
31
|
+
target[patchedKey] = true;
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Clear Router plugin for Express framework. Patches Clear Router's Express router to wrap
|
|
35
|
+
* route handlers with the provided runWithCtx function, enabling context-aware execution of
|
|
36
|
+
* route handlers in Express applications.
|
|
37
|
+
*/
|
|
38
|
+
const clearRouterExpressPlugin = (0, resora.definePlugin)({
|
|
39
|
+
name: "clear-router-express",
|
|
40
|
+
setup: ({ runWithCtx }) => {
|
|
41
|
+
patchRouter(clear_router_express.Router, runWithCtx);
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
/**
|
|
45
|
+
* Clear Router plugin for H3 framework. Patches Clear Router's H3 router to wrap route
|
|
46
|
+
* handlers with the provided runWithCtx function, enabling context-aware execution of
|
|
47
|
+
* route handlers in H3 applications.
|
|
48
|
+
*/
|
|
49
|
+
const clearRouterH3Plugin = (0, resora.definePlugin)({
|
|
50
|
+
name: "clear-router-h3",
|
|
51
|
+
setup: ({ runWithCtx }) => {
|
|
52
|
+
patchRouter(clear_router_h3.Router, runWithCtx);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
const clearRouterPlugin = [clearRouterExpressPlugin, clearRouterH3Plugin];
|
|
56
|
+
|
|
57
|
+
//#endregion
|
|
58
|
+
exports.clearRouterExpressPlugin = clearRouterExpressPlugin;
|
|
59
|
+
exports.clearRouterH3Plugin = clearRouterH3Plugin;
|
|
60
|
+
exports.clearRouterPlugin = clearRouterPlugin;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import * as resora from "resora";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Clear Router plugin for Express framework. Patches Clear Router's Express router to wrap
|
|
6
|
+
* route handlers with the provided runWithCtx function, enabling context-aware execution of
|
|
7
|
+
* route handlers in Express applications.
|
|
8
|
+
*/
|
|
9
|
+
declare const clearRouterExpressPlugin: {
|
|
10
|
+
name: string;
|
|
11
|
+
setup: ({
|
|
12
|
+
runWithCtx
|
|
13
|
+
}: resora.ResoraPluginApi) => void;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Clear Router plugin for H3 framework. Patches Clear Router's H3 router to wrap route
|
|
17
|
+
* handlers with the provided runWithCtx function, enabling context-aware execution of
|
|
18
|
+
* route handlers in H3 applications.
|
|
19
|
+
*/
|
|
20
|
+
declare const clearRouterH3Plugin: {
|
|
21
|
+
name: string;
|
|
22
|
+
setup: ({
|
|
23
|
+
runWithCtx
|
|
24
|
+
}: resora.ResoraPluginApi) => void;
|
|
25
|
+
};
|
|
26
|
+
declare const clearRouterPlugin: {
|
|
27
|
+
name: string;
|
|
28
|
+
setup: ({
|
|
29
|
+
runWithCtx
|
|
30
|
+
}: resora.ResoraPluginApi) => void;
|
|
31
|
+
}[];
|
|
32
|
+
//#endregion
|
|
33
|
+
export { clearRouterExpressPlugin, clearRouterH3Plugin, clearRouterPlugin };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import * as resora from "resora";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Clear Router plugin for Express framework. Patches Clear Router's Express router to wrap
|
|
6
|
+
* route handlers with the provided runWithCtx function, enabling context-aware execution of
|
|
7
|
+
* route handlers in Express applications.
|
|
8
|
+
*/
|
|
9
|
+
declare const clearRouterExpressPlugin: {
|
|
10
|
+
name: string;
|
|
11
|
+
setup: ({
|
|
12
|
+
runWithCtx
|
|
13
|
+
}: resora.ResoraPluginApi) => void;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Clear Router plugin for H3 framework. Patches Clear Router's H3 router to wrap route
|
|
17
|
+
* handlers with the provided runWithCtx function, enabling context-aware execution of
|
|
18
|
+
* route handlers in H3 applications.
|
|
19
|
+
*/
|
|
20
|
+
declare const clearRouterH3Plugin: {
|
|
21
|
+
name: string;
|
|
22
|
+
setup: ({
|
|
23
|
+
runWithCtx
|
|
24
|
+
}: resora.ResoraPluginApi) => void;
|
|
25
|
+
};
|
|
26
|
+
declare const clearRouterPlugin: {
|
|
27
|
+
name: string;
|
|
28
|
+
setup: ({
|
|
29
|
+
runWithCtx
|
|
30
|
+
}: resora.ResoraPluginApi) => void;
|
|
31
|
+
}[];
|
|
32
|
+
//#endregion
|
|
33
|
+
export { clearRouterExpressPlugin, clearRouterH3Plugin, clearRouterPlugin };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { Router } from "clear-router/express";
|
|
2
|
+
import { Router as Router$1 } from "clear-router/h3";
|
|
3
|
+
import { definePlugin } from "resora";
|
|
4
|
+
|
|
5
|
+
//#region src/index.ts
|
|
6
|
+
const patchedKey = Symbol.for("resora:clear-router-plugin:patched");
|
|
7
|
+
/**
|
|
8
|
+
* Patch the given Clear Router router class to wrap route handlers with the provided
|
|
9
|
+
* runWithCtx function, enabling context-aware execution of route handlers.
|
|
10
|
+
*
|
|
11
|
+
* @param router The Clear Router router class to patch
|
|
12
|
+
* @param runWithCtx A function that wraps the route handler execution with a given context
|
|
13
|
+
* @returns void
|
|
14
|
+
*/
|
|
15
|
+
const patchRouter = (router, runWithCtx) => {
|
|
16
|
+
const target = router;
|
|
17
|
+
if (target[patchedKey]) return;
|
|
18
|
+
const resolveHandler = target.resolveHandler.bind(target);
|
|
19
|
+
target.resolveHandler = ((route) => {
|
|
20
|
+
const resolved = resolveHandler(route);
|
|
21
|
+
if (!resolved.handlerFunction) return resolved;
|
|
22
|
+
const handlerFunction = resolved.handlerFunction;
|
|
23
|
+
resolved.handlerFunction = ((ctx, req) => {
|
|
24
|
+
return runWithCtx(ctx, async () => {
|
|
25
|
+
return await Promise.resolve(handlerFunction(ctx, req));
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
return resolved;
|
|
29
|
+
});
|
|
30
|
+
target[patchedKey] = true;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Clear Router plugin for Express framework. Patches Clear Router's Express router to wrap
|
|
34
|
+
* route handlers with the provided runWithCtx function, enabling context-aware execution of
|
|
35
|
+
* route handlers in Express applications.
|
|
36
|
+
*/
|
|
37
|
+
const clearRouterExpressPlugin = definePlugin({
|
|
38
|
+
name: "clear-router-express",
|
|
39
|
+
setup: ({ runWithCtx }) => {
|
|
40
|
+
patchRouter(Router, runWithCtx);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
/**
|
|
44
|
+
* Clear Router plugin for H3 framework. Patches Clear Router's H3 router to wrap route
|
|
45
|
+
* handlers with the provided runWithCtx function, enabling context-aware execution of
|
|
46
|
+
* route handlers in H3 applications.
|
|
47
|
+
*/
|
|
48
|
+
const clearRouterH3Plugin = definePlugin({
|
|
49
|
+
name: "clear-router-h3",
|
|
50
|
+
setup: ({ runWithCtx }) => {
|
|
51
|
+
patchRouter(Router$1, runWithCtx);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
const clearRouterPlugin = [clearRouterExpressPlugin, clearRouterH3Plugin];
|
|
55
|
+
|
|
56
|
+
//#endregion
|
|
57
|
+
export { clearRouterExpressPlugin, clearRouterH3Plugin, clearRouterPlugin };
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@resora/plugin-clear-router",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Opt-in bridge plugin for using resora return values inside clear-router handlers.",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"keywords": [
|
|
9
|
+
"resora",
|
|
10
|
+
"clear-router",
|
|
11
|
+
"express",
|
|
12
|
+
"h3",
|
|
13
|
+
"plugin",
|
|
14
|
+
"typescript"
|
|
15
|
+
],
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"type": "module",
|
|
18
|
+
"main": "./dist/index.cjs",
|
|
19
|
+
"module": "./dist/index.mjs",
|
|
20
|
+
"types": "./dist/index.d.cts",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"import": "./dist/index.mjs",
|
|
24
|
+
"require": "./dist/index.cjs"
|
|
25
|
+
},
|
|
26
|
+
"./package.json": "./package.json"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist",
|
|
30
|
+
"README.md"
|
|
31
|
+
],
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"resora": "1.0.0"
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"clear-router": "^2.3.3"
|
|
37
|
+
},
|
|
38
|
+
"peerDependenciesMeta": {
|
|
39
|
+
"clear-router": {
|
|
40
|
+
"optional": false
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"clear-router": "^2.3.3"
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"test": "vitest run tests/**/*.spec.ts",
|
|
48
|
+
"build": "tsdown",
|
|
49
|
+
"lint": "eslint src tests --ext .ts",
|
|
50
|
+
"version:patch": "pnpm version patch"
|
|
51
|
+
}
|
|
52
|
+
}
|