enlace-hono 0.0.1-beta.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/LICENSE +21 -0
- package/dist/index.d.mts +68 -0
- package/dist/index.d.ts +68 -0
- package/dist/index.js +18 -0
- package/dist/index.mjs +0 -0
- package/package.json +29 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Enlace
|
|
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.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { EndpointWithFormData, EndpointFull, EndpointWithQuery, Endpoint } from 'enlace-core';
|
|
2
|
+
export { Endpoint, EndpointFull, EndpointWithFormData, EndpointWithQuery } from 'enlace-core';
|
|
3
|
+
import { Hono } from 'hono';
|
|
4
|
+
import { HonoBase } from 'hono/hono-base';
|
|
5
|
+
|
|
6
|
+
type IsNever<T> = [T] extends [never] ? true : false;
|
|
7
|
+
type HonoSchemaMethod = "$get" | "$post" | "$put" | "$patch" | "$delete";
|
|
8
|
+
type ExtractHonoOutput<T> = T extends {
|
|
9
|
+
output: infer O;
|
|
10
|
+
} ? O : never;
|
|
11
|
+
type ExtractHonoBody<T> = T extends {
|
|
12
|
+
input: {
|
|
13
|
+
json: infer B;
|
|
14
|
+
};
|
|
15
|
+
} ? IsNever<B> extends true ? never : B : never;
|
|
16
|
+
type ExtractHonoQuery<T> = T extends {
|
|
17
|
+
input: {
|
|
18
|
+
query: infer Q;
|
|
19
|
+
};
|
|
20
|
+
} ? IsNever<Q> extends true ? never : Q : never;
|
|
21
|
+
type ExtractHonoFormData<T> = T extends {
|
|
22
|
+
input: {
|
|
23
|
+
form: infer F;
|
|
24
|
+
};
|
|
25
|
+
} ? IsNever<F> extends true ? never : F : never;
|
|
26
|
+
type HonoEndpointToEnlace<T> = IsNever<ExtractHonoFormData<T>> extends false ? EndpointWithFormData<ExtractHonoOutput<T>, ExtractHonoFormData<T>> : IsNever<ExtractHonoQuery<T>> extends false ? IsNever<ExtractHonoBody<T>> extends false ? EndpointFull<{
|
|
27
|
+
data: ExtractHonoOutput<T>;
|
|
28
|
+
body: ExtractHonoBody<T>;
|
|
29
|
+
query: ExtractHonoQuery<T>;
|
|
30
|
+
}> : EndpointWithQuery<ExtractHonoOutput<T>, ExtractHonoQuery<T>> : IsNever<ExtractHonoBody<T>> extends false ? Endpoint<ExtractHonoOutput<T>, ExtractHonoBody<T>> : ExtractHonoOutput<T>;
|
|
31
|
+
type TransformMethods<T> = {
|
|
32
|
+
[K in keyof T as K extends HonoSchemaMethod ? K : never]: HonoEndpointToEnlace<T[K]>;
|
|
33
|
+
};
|
|
34
|
+
type TransformSegment<S extends string> = S extends `:${string}` ? "_" : S;
|
|
35
|
+
type PathToEnlace<Path extends string, S, Original extends string = Path> = Path extends `/${infer P}` ? PathToEnlace<P, S, Original> : Path extends `${infer Head}/${infer Rest}` ? {
|
|
36
|
+
[K in TransformSegment<Head>]: PathToEnlace<Rest, S, Original>;
|
|
37
|
+
} : {
|
|
38
|
+
[K in TransformSegment<Path extends "" ? "index" : Path>]: TransformMethods<S extends Record<Original, infer V> ? V : never>;
|
|
39
|
+
};
|
|
40
|
+
type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends (k: infer I) => void ? I : unknown;
|
|
41
|
+
type ExtractSchemaFromHono<T> = T extends HonoBase<any, infer S, any, any> ? S : T extends Hono<any, infer S, any> ? S : never;
|
|
42
|
+
type SchemaToEnlace<S> = S extends Record<infer K, unknown> ? K extends string ? PathToEnlace<K, S> : never : never;
|
|
43
|
+
/**
|
|
44
|
+
* Transforms Hono's AppType (from `typeof app`) into Enlace's ApiSchema format.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```typescript
|
|
48
|
+
* // Server (Hono)
|
|
49
|
+
* const app = new Hono()
|
|
50
|
+
* .basePath('/api')
|
|
51
|
+
* .get('/posts', (c) => c.json([{ id: 1, title: 'Hello' }]))
|
|
52
|
+
* .post('/posts', zValidator('json', schema), (c) => c.json({ id: 1 }))
|
|
53
|
+
* .get('/posts/:id', (c) => c.json({ id: c.req.param('id') }));
|
|
54
|
+
*
|
|
55
|
+
* export type AppType = typeof app;
|
|
56
|
+
*
|
|
57
|
+
* // Client (Enlace)
|
|
58
|
+
* import type { HonoToEnlace } from 'enlace-hono';
|
|
59
|
+
* type ApiSchema = HonoToEnlace<AppType>;
|
|
60
|
+
*
|
|
61
|
+
* const client = enlace<ApiSchema['api']>('http://localhost:3000/api');
|
|
62
|
+
* const posts = await client.posts.get(); // typed as { id, title }[]
|
|
63
|
+
* const post = await client.posts['123'].get(); // typed as { id }
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
type HonoToEnlace<T> = UnionToIntersection<SchemaToEnlace<ExtractSchemaFromHono<T>>>;
|
|
67
|
+
|
|
68
|
+
export type { HonoToEnlace };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { EndpointWithFormData, EndpointFull, EndpointWithQuery, Endpoint } from 'enlace-core';
|
|
2
|
+
export { Endpoint, EndpointFull, EndpointWithFormData, EndpointWithQuery } from 'enlace-core';
|
|
3
|
+
import { Hono } from 'hono';
|
|
4
|
+
import { HonoBase } from 'hono/hono-base';
|
|
5
|
+
|
|
6
|
+
type IsNever<T> = [T] extends [never] ? true : false;
|
|
7
|
+
type HonoSchemaMethod = "$get" | "$post" | "$put" | "$patch" | "$delete";
|
|
8
|
+
type ExtractHonoOutput<T> = T extends {
|
|
9
|
+
output: infer O;
|
|
10
|
+
} ? O : never;
|
|
11
|
+
type ExtractHonoBody<T> = T extends {
|
|
12
|
+
input: {
|
|
13
|
+
json: infer B;
|
|
14
|
+
};
|
|
15
|
+
} ? IsNever<B> extends true ? never : B : never;
|
|
16
|
+
type ExtractHonoQuery<T> = T extends {
|
|
17
|
+
input: {
|
|
18
|
+
query: infer Q;
|
|
19
|
+
};
|
|
20
|
+
} ? IsNever<Q> extends true ? never : Q : never;
|
|
21
|
+
type ExtractHonoFormData<T> = T extends {
|
|
22
|
+
input: {
|
|
23
|
+
form: infer F;
|
|
24
|
+
};
|
|
25
|
+
} ? IsNever<F> extends true ? never : F : never;
|
|
26
|
+
type HonoEndpointToEnlace<T> = IsNever<ExtractHonoFormData<T>> extends false ? EndpointWithFormData<ExtractHonoOutput<T>, ExtractHonoFormData<T>> : IsNever<ExtractHonoQuery<T>> extends false ? IsNever<ExtractHonoBody<T>> extends false ? EndpointFull<{
|
|
27
|
+
data: ExtractHonoOutput<T>;
|
|
28
|
+
body: ExtractHonoBody<T>;
|
|
29
|
+
query: ExtractHonoQuery<T>;
|
|
30
|
+
}> : EndpointWithQuery<ExtractHonoOutput<T>, ExtractHonoQuery<T>> : IsNever<ExtractHonoBody<T>> extends false ? Endpoint<ExtractHonoOutput<T>, ExtractHonoBody<T>> : ExtractHonoOutput<T>;
|
|
31
|
+
type TransformMethods<T> = {
|
|
32
|
+
[K in keyof T as K extends HonoSchemaMethod ? K : never]: HonoEndpointToEnlace<T[K]>;
|
|
33
|
+
};
|
|
34
|
+
type TransformSegment<S extends string> = S extends `:${string}` ? "_" : S;
|
|
35
|
+
type PathToEnlace<Path extends string, S, Original extends string = Path> = Path extends `/${infer P}` ? PathToEnlace<P, S, Original> : Path extends `${infer Head}/${infer Rest}` ? {
|
|
36
|
+
[K in TransformSegment<Head>]: PathToEnlace<Rest, S, Original>;
|
|
37
|
+
} : {
|
|
38
|
+
[K in TransformSegment<Path extends "" ? "index" : Path>]: TransformMethods<S extends Record<Original, infer V> ? V : never>;
|
|
39
|
+
};
|
|
40
|
+
type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends (k: infer I) => void ? I : unknown;
|
|
41
|
+
type ExtractSchemaFromHono<T> = T extends HonoBase<any, infer S, any, any> ? S : T extends Hono<any, infer S, any> ? S : never;
|
|
42
|
+
type SchemaToEnlace<S> = S extends Record<infer K, unknown> ? K extends string ? PathToEnlace<K, S> : never : never;
|
|
43
|
+
/**
|
|
44
|
+
* Transforms Hono's AppType (from `typeof app`) into Enlace's ApiSchema format.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```typescript
|
|
48
|
+
* // Server (Hono)
|
|
49
|
+
* const app = new Hono()
|
|
50
|
+
* .basePath('/api')
|
|
51
|
+
* .get('/posts', (c) => c.json([{ id: 1, title: 'Hello' }]))
|
|
52
|
+
* .post('/posts', zValidator('json', schema), (c) => c.json({ id: 1 }))
|
|
53
|
+
* .get('/posts/:id', (c) => c.json({ id: c.req.param('id') }));
|
|
54
|
+
*
|
|
55
|
+
* export type AppType = typeof app;
|
|
56
|
+
*
|
|
57
|
+
* // Client (Enlace)
|
|
58
|
+
* import type { HonoToEnlace } from 'enlace-hono';
|
|
59
|
+
* type ApiSchema = HonoToEnlace<AppType>;
|
|
60
|
+
*
|
|
61
|
+
* const client = enlace<ApiSchema['api']>('http://localhost:3000/api');
|
|
62
|
+
* const posts = await client.posts.get(); // typed as { id, title }[]
|
|
63
|
+
* const post = await client.posts['123'].get(); // typed as { id }
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
type HonoToEnlace<T> = UnionToIntersection<SchemaToEnlace<ExtractSchemaFromHono<T>>>;
|
|
67
|
+
|
|
68
|
+
export type { HonoToEnlace };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
+
|
|
16
|
+
// src/index.ts
|
|
17
|
+
var src_exports = {};
|
|
18
|
+
module.exports = __toCommonJS(src_exports);
|
package/dist/index.mjs
ADDED
|
File without changes
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "enlace-hono",
|
|
3
|
+
"version": "0.0.1-beta.1",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"files": [
|
|
6
|
+
"dist"
|
|
7
|
+
],
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"require": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"peerDependencies": {
|
|
16
|
+
"enlace-core": ">=0.0.1-beta.1",
|
|
17
|
+
"hono": ">=4.0.0"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"hono": "^4.10.6",
|
|
21
|
+
"enlace-core": "0.0.1-beta.9"
|
|
22
|
+
},
|
|
23
|
+
"scripts": {
|
|
24
|
+
"dev": "tsup --watch",
|
|
25
|
+
"build": "tsup",
|
|
26
|
+
"typecheck": "tsc --noEmit",
|
|
27
|
+
"lint": "eslint src --max-warnings 0"
|
|
28
|
+
}
|
|
29
|
+
}
|