@deessejs/server-hono 0.0.0 → 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/CHANGELOG.md +12 -0
- package/LICENSE +21 -0
- package/dist/createHonoHandler.d.ts.map +1 -1
- package/dist/createHonoHandler.js +18 -5
- package/dist/createHonoHandler.js.map +1 -1
- package/eslint.config.js +27 -0
- package/package.json +39 -37
- package/src/createHonoHandler.ts +27 -5
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Nesalia Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createHonoHandler.d.ts","sourceRoot":"","sources":["../src/createHonoHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAC5B,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"createHonoHandler.d.ts","sourceRoot":"","sources":["../src/createHonoHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAC5B,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAyB7C;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,UAAU,GAAG,IAAI,CAiD1D"}
|
|
@@ -18,15 +18,20 @@ function isMutationMethod(method) {
|
|
|
18
18
|
export function createHonoHandler(client) {
|
|
19
19
|
const app = new Hono();
|
|
20
20
|
// Register route for all procedures
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
// The route captures the path after /api/ prefix
|
|
22
|
+
app.all("/api/:path{.*}", async (c) => {
|
|
23
|
+
// Get the path without the /api/ prefix
|
|
24
|
+
const rawPath = c.req.param("path") || "";
|
|
25
|
+
const path = normalizePath(rawPath);
|
|
23
26
|
const method = c.req.method;
|
|
24
27
|
// Determine args based on method
|
|
25
28
|
let args = {};
|
|
26
29
|
if (isMutationMethod(method)) {
|
|
27
30
|
// For mutations, parse JSON body
|
|
28
31
|
try {
|
|
29
|
-
|
|
32
|
+
const body = await c.req.json();
|
|
33
|
+
// Unwrap args if client wrapped them as { args: {...} }
|
|
34
|
+
args = body.args ?? body;
|
|
30
35
|
}
|
|
31
36
|
catch {
|
|
32
37
|
args = {};
|
|
@@ -37,12 +42,20 @@ export function createHonoHandler(client) {
|
|
|
37
42
|
const queryParams = c.req.queries();
|
|
38
43
|
args = queryParams;
|
|
39
44
|
}
|
|
40
|
-
const
|
|
45
|
+
const requestInfo = {
|
|
46
|
+
headers: c.req.header(),
|
|
47
|
+
method: c.req.method,
|
|
48
|
+
url: c.req.url,
|
|
49
|
+
};
|
|
50
|
+
// Cast to any to avoid TypeScript confusion with intersection of APIInstance & RouterProxy
|
|
51
|
+
// The execute method signature is correctly defined in APIInstance
|
|
52
|
+
const result = await client.execute(path, args, requestInfo);
|
|
41
53
|
if (result.ok) {
|
|
42
54
|
return c.json(result);
|
|
43
55
|
}
|
|
44
56
|
// Map error code to HTTP status
|
|
45
|
-
const
|
|
57
|
+
const error = result.error;
|
|
58
|
+
const status = getHTTPStatus(error?.name);
|
|
46
59
|
return c.json(result, status);
|
|
47
60
|
});
|
|
48
61
|
return app;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createHonoHandler.js","sourceRoot":"","sources":["../src/createHonoHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAE5B,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"createHonoHandler.js","sourceRoot":"","sources":["../src/createHonoHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAE5B,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAU5C;;GAEG;AACH,SAAS,aAAa,CAAC,IAAY;IACjC,OAAO,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;AAClC,CAAC;AAED;;GAEG;AACH,SAAS,gBAAgB,CAAC,MAAc;IACtC,OAAO,MAAM,KAAK,MAAM,IAAI,MAAM,KAAK,KAAK,IAAI,MAAM,KAAK,OAAO,IAAI,MAAM,KAAK,QAAQ,CAAC;AAC5F,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,iBAAiB,CAAC,MAAkB;IAClD,MAAM,GAAG,GAAG,IAAI,IAAI,EAAE,CAAC;IAEvB,oCAAoC;IACpC,iDAAiD;IACjD,GAAG,CAAC,GAAG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AAM,CAAC,KAA0B,CAAC;QAChD,MAAM,MAAM,GAAG,aAAa,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;QAC1C,OAAO,CAAC,CAAC,IAAI,CAAC,MAAM,EAAE,MAA2C,CAAC,CAAC;IACrE,CAAC,CAAC,CAAC;IAEH,OAAO,GAAG,CAAC;AACb,CAAC"}
|
package/eslint.config.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import eslint from "@eslint/js";
|
|
2
|
+
import tseslint from "typescript-eslint";
|
|
3
|
+
|
|
4
|
+
export default tseslint.config(
|
|
5
|
+
eslint.configs.recommended,
|
|
6
|
+
...tseslint.configs.recommended,
|
|
7
|
+
{
|
|
8
|
+
ignores: ["node_modules", "dist", "build", ".turbo"],
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
files: ["**/*.ts", "**/*.tsx"],
|
|
12
|
+
languageOptions: {
|
|
13
|
+
parser: tseslint.parser,
|
|
14
|
+
parserOptions: {
|
|
15
|
+
project: "./tsconfig.json",
|
|
16
|
+
ecmaVersion: "latest",
|
|
17
|
+
sourceType: "module",
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
rules: {
|
|
21
|
+
"@typescript-eslint/no-unused-vars": [
|
|
22
|
+
"error",
|
|
23
|
+
{ argsIgnorePattern: "^_" },
|
|
24
|
+
],
|
|
25
|
+
},
|
|
26
|
+
}
|
|
27
|
+
);
|
package/package.json
CHANGED
|
@@ -1,37 +1,39 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@deessejs/server-hono",
|
|
3
|
-
"version": "
|
|
4
|
-
"type": "module",
|
|
5
|
-
"description": "Hono HTTP adapter for @deessejs/server",
|
|
6
|
-
"license": "MIT",
|
|
7
|
-
"main": "./dist/index.js",
|
|
8
|
-
"types": "./dist/index.d.ts",
|
|
9
|
-
"exports": {
|
|
10
|
-
".": {
|
|
11
|
-
"import": "./dist/index.js",
|
|
12
|
-
"types": "./dist/index.d.ts"
|
|
13
|
-
}
|
|
14
|
-
},
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
"
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
"@
|
|
25
|
-
"
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
"
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
|
|
37
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@deessejs/server-hono",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Hono HTTP adapter for @deessejs/server",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"hono": "^4.0.0",
|
|
17
|
+
"@deessejs/server": "1.0.1"
|
|
18
|
+
},
|
|
19
|
+
"peerDependencies": {
|
|
20
|
+
"@deessejs/server": "*"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@deessejs/fp": "^3.0.0",
|
|
24
|
+
"@eslint/js": "^9.17.0",
|
|
25
|
+
"typescript": "^5.0.0",
|
|
26
|
+
"typescript-eslint": "^8.15.0",
|
|
27
|
+
"vitest": "^2.0.0",
|
|
28
|
+
"zod": "^3.0.0",
|
|
29
|
+
"@deessejs/server": "1.0.1"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsc",
|
|
33
|
+
"dev": "tsc --watch",
|
|
34
|
+
"lint": "eslint src --ext .ts,.tsx",
|
|
35
|
+
"typecheck": "tsc --noEmit",
|
|
36
|
+
"test": "vitest run",
|
|
37
|
+
"clean": "rm -rf dist"
|
|
38
|
+
}
|
|
39
|
+
}
|
package/src/createHonoHandler.ts
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
import { Hono } from "hono";
|
|
2
2
|
import type { HTTPClient } from "./types.js";
|
|
3
3
|
import { getHTTPStatus } from "./errors.js";
|
|
4
|
+
import type { Error } from "@deessejs/fp";
|
|
5
|
+
|
|
6
|
+
interface RequestInfo {
|
|
7
|
+
headers?: Record<string, string>;
|
|
8
|
+
method?: string;
|
|
9
|
+
url?: string;
|
|
10
|
+
[key: string]: unknown;
|
|
11
|
+
}
|
|
4
12
|
|
|
5
13
|
/**
|
|
6
14
|
* Converts a path like "users/get" to "users.get" for procedure lookup
|
|
@@ -23,8 +31,11 @@ export function createHonoHandler(client: HTTPClient): Hono {
|
|
|
23
31
|
const app = new Hono();
|
|
24
32
|
|
|
25
33
|
// Register route for all procedures
|
|
26
|
-
|
|
27
|
-
|
|
34
|
+
// The route captures the path after /api/ prefix
|
|
35
|
+
app.all("/api/:path{.*}", async (c) => {
|
|
36
|
+
// Get the path without the /api/ prefix
|
|
37
|
+
const rawPath = c.req.param("path") || "";
|
|
38
|
+
const path = normalizePath(rawPath);
|
|
28
39
|
const method = c.req.method;
|
|
29
40
|
|
|
30
41
|
// Determine args based on method
|
|
@@ -32,7 +43,9 @@ export function createHonoHandler(client: HTTPClient): Hono {
|
|
|
32
43
|
if (isMutationMethod(method)) {
|
|
33
44
|
// For mutations, parse JSON body
|
|
34
45
|
try {
|
|
35
|
-
|
|
46
|
+
const body = await c.req.json();
|
|
47
|
+
// Unwrap args if client wrapped them as { args: {...} }
|
|
48
|
+
args = body.args ?? body;
|
|
36
49
|
} catch {
|
|
37
50
|
args = {};
|
|
38
51
|
}
|
|
@@ -42,14 +55,23 @@ export function createHonoHandler(client: HTTPClient): Hono {
|
|
|
42
55
|
args = queryParams as Record<string, unknown>;
|
|
43
56
|
}
|
|
44
57
|
|
|
45
|
-
const
|
|
58
|
+
const requestInfo: RequestInfo = {
|
|
59
|
+
headers: c.req.header(),
|
|
60
|
+
method: c.req.method,
|
|
61
|
+
url: c.req.url,
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
// Cast to any to avoid TypeScript confusion with intersection of APIInstance & RouterProxy
|
|
65
|
+
// The execute method signature is correctly defined in APIInstance
|
|
66
|
+
const result = await (client as { execute(route: string, args: unknown, requestInfo?: RequestInfo): Promise<{ ok: boolean; value?: unknown; error?: Error }> }).execute(path, args, requestInfo);
|
|
46
67
|
|
|
47
68
|
if (result.ok) {
|
|
48
69
|
return c.json(result);
|
|
49
70
|
}
|
|
50
71
|
|
|
51
72
|
// Map error code to HTTP status
|
|
52
|
-
const
|
|
73
|
+
const error = result.error as Error | undefined;
|
|
74
|
+
const status = getHTTPStatus(error?.name);
|
|
53
75
|
return c.json(result, status as 400 | 401 | 403 | 404 | 409 | 500);
|
|
54
76
|
});
|
|
55
77
|
|