11import { OpenAPIHono } from '@hono/zod-openapi' ;
2+ import { Env , Hono , Schema } from 'hono' ;
23import { ClientRequest , hc } from 'hono/client' ;
3- import { UnionToIntersection } from 'hono/utils/types' ;
4- import { cookies , headers } from 'next/headers' ;
5-
6- import { CONFIG } from './config' ;
7- import { Env , Schema } from 'hono' ;
84import { HonoBase } from 'hono/hono-base' ;
9-
10- /**
11- * Maps an API path string to a nested object structure for type-safe client usage.
12- * Example: '/foo/bar' -> { foo: { bar: ... } }
13- * This is intentionally shallow to avoid deep recursion for TS perf.
14- */
15- type PathToChain <
16- Path extends string ,
17- E extends Schema ,
18- Orig extends string = Path ,
19- > = Path extends `/${infer P } `
20- ? PathToChain < P , E , Path >
21- : Path extends `${infer P } /${infer R } `
22- ? { [ K in P ] : PathToChain < R , E , Orig > }
23- : Record <
24- Path extends '' ? 'index' : Path ,
25- ClientRequest < E extends Record < string , unknown > ? E [ Orig ] : never >
26- > ;
27-
28- /**
29- * Type-safe client for a Hono API schema.
30- */
31- export type Client < T > =
32- T extends HonoBase < Env , infer S , string >
33- ? S extends Record < infer K , Schema >
34- ? K extends string
35- ? PathToChain < K , S >
36- : never
37- : never
38- : never ;
5+ import { UnionToIntersection } from 'hono/utils/types' ;
396
407/**
418 * Create a type-safe client for a VitNode API module.
@@ -51,31 +18,4 @@ export async function fetcherNew<T extends OpenAPIHono>({
5118 module : string ;
5219 options ?: Omit < RequestInit , 'body' > ;
5320 plugin : string ;
54- } ) : Promise < UnionToIntersection < Client < T > > > {
55- const url = new URL ( `/api/${ plugin } /${ module } ` , CONFIG . backend . origin ) ;
56- const [ nextInternalHeaders , cookie ] = await Promise . all ( [
57- headers ( ) ,
58- cookies ( ) ,
59- ] ) ;
60-
61- const client = hc < T > ( url . href , {
62- fetch : async ( input , requestInit ) => {
63- const headers = new Headers ( {
64- 'Content-Type' : 'application/json' ,
65- Cookie : cookie . toString ( ) ,
66- [ 'user-agent' ] : nextInternalHeaders . get ( 'user-agent' ) ?? 'node' ,
67- [ 'x-forwarded-for' ] :
68- nextInternalHeaders . get ( 'x-forwarded-for' ) ?? '0.0.0.0' ,
69- ...options ?. headers ,
70- } ) ;
71-
72- return await fetch ( input , {
73- ...requestInit ,
74- ...options ,
75- headers,
76- } ) ;
77- } ,
78- } ) ;
79-
80- return client as unknown as UnionToIntersection < Client < T > > ;
81- }
21+ } ) { }
0 commit comments