@hyper-fetch/elysia 0.0.0-stage → 9.9.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 +133 -2
- package/__bun__/edge.test.ts +124 -0
- package/__bun__/sdk.test.ts +108 -0
- package/__bun__/tsconfig.json +10 -0
- package/__tests__/features/schema/schema.edge.sdk.spec.ts +359 -0
- package/__tests__/features/schema/schema.edge.types.spec.ts +345 -0
- package/__tests__/features/schema/schema.sdk.spec.ts +148 -0
- package/__tests__/features/schema/schema.types.spec.ts +213 -0
- package/__tests__/tsconfig.json +11 -0
- package/__tests__/utils/app.ts +49 -0
- package/__tests__/utils/edge-app.ts +109 -0
- package/dist/elysia.types.d.ts +136 -0
- package/dist/elysia.types.d.ts.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.mjs +0 -0
- package/package.json +66 -4
- package/project.json +4 -0
- package/src/elysia.types.ts +226 -0
- package/src/index.ts +1 -0
- package/tsconfig.json +7 -0
- package/vite.config.ts +36 -0
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import type { ClientInstance, Request } from "@hyper-fetch/core";
|
|
2
|
+
|
|
3
|
+
type Prettify<T> = {
|
|
4
|
+
[K in keyof T]: T[K];
|
|
5
|
+
} & {};
|
|
6
|
+
|
|
7
|
+
/* -------------------------------------------------------------------------------------------------
|
|
8
|
+
* Route entries
|
|
9
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The entry Elysia stores under a method key of its route tree. `response` maps every status the
|
|
13
|
+
* handler can return to the body it returns with that status.
|
|
14
|
+
*/
|
|
15
|
+
type ElysiaRoute = {
|
|
16
|
+
body: unknown;
|
|
17
|
+
params: unknown;
|
|
18
|
+
query: unknown;
|
|
19
|
+
headers: unknown;
|
|
20
|
+
response: Record<number, unknown>;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Elysia stores WebSocket routes under `subscribe`, and `.all()` routes under an index signature.
|
|
25
|
+
* Neither maps to a single HTTP method, so they are not part of the schema.
|
|
26
|
+
*/
|
|
27
|
+
type MethodKeys<Node> = {
|
|
28
|
+
[K in keyof Node & string]: string extends K
|
|
29
|
+
? never
|
|
30
|
+
: K extends "subscribe"
|
|
31
|
+
? never
|
|
32
|
+
: Node[K] extends ElysiaRoute
|
|
33
|
+
? K
|
|
34
|
+
: never;
|
|
35
|
+
}[keyof Node & string];
|
|
36
|
+
|
|
37
|
+
type SuccessStatus<Responses> = {
|
|
38
|
+
[S in keyof Responses]: `${S & number}` extends `2${string}` ? S : never;
|
|
39
|
+
}[keyof Responses];
|
|
40
|
+
|
|
41
|
+
type ErrorStatus<Responses> = Exclude<keyof Responses, SuccessStatus<Responses>>;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Elysia types what the handler returns, while Hyper Fetch receives what was sent over HTTP. `Date`
|
|
45
|
+
* values arrive as ISO strings. Streams, generators, `Response` objects, files and `form()` bodies
|
|
46
|
+
* are not JSON, so their content is `unknown`.
|
|
47
|
+
*/
|
|
48
|
+
type Serialized<T> = T extends Date
|
|
49
|
+
? string
|
|
50
|
+
: T extends Response | Blob | FormData | ReadableStream | Generator | AsyncGenerator
|
|
51
|
+
? unknown
|
|
52
|
+
: T extends object
|
|
53
|
+
? { [K in keyof T]: Serialized<T[K]> }
|
|
54
|
+
: T;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Elysia sends `null`, `undefined` and `void` as an empty body, and Hyper Fetch reads an empty body
|
|
58
|
+
* as `""`. Nested values are JSON, so a `null` field stays `null`.
|
|
59
|
+
*/
|
|
60
|
+
type ResponseBody<T> = T extends null | undefined | void ? "" : Serialized<T>;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* `fetch` resolves a 2xx status as a success, so only 2xx bodies are the response. A route that only
|
|
64
|
+
* returns other statuses gets `undefined`, because a `never` response breaks the `Extract*` helpers
|
|
65
|
+
* of core. Elysia leaves the status map empty both for a handler without a return value and for one
|
|
66
|
+
* that returns `any`, so the response of an empty map is `unknown`.
|
|
67
|
+
*/
|
|
68
|
+
type RouteResponse<Route extends ElysiaRoute> = [keyof Route["response"]] extends [never]
|
|
69
|
+
? unknown
|
|
70
|
+
: [SuccessStatus<Route["response"]>] extends [never]
|
|
71
|
+
? undefined
|
|
72
|
+
: ResponseBody<Route["response"][SuccessStatus<Route["response"]>]>;
|
|
73
|
+
|
|
74
|
+
/** Bodies of every other status, including the `422` Elysia sends when a schema rejects the input. */
|
|
75
|
+
type RouteError<Route extends ElysiaRoute> = [ErrorStatus<Route["response"]>] extends [never]
|
|
76
|
+
? undefined
|
|
77
|
+
: ResponseBody<Route["response"][ErrorStatus<Route["response"]>]>;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Elysia types a route without a `body` or `query` schema as `unknown`, and as `{}` when the route
|
|
81
|
+
* uses `status()`, a macro or the HEAD method. Neither has anything to send.
|
|
82
|
+
*/
|
|
83
|
+
type DefinedOrUndefined<T> = unknown extends T ? undefined : IsEmptyObject<T> extends true ? undefined : T;
|
|
84
|
+
|
|
85
|
+
/** Only `{}` itself. Unions without common keys and objects with only optional keys keep their type. */
|
|
86
|
+
type IsEmptyObject<T> = [T] extends [{}]
|
|
87
|
+
? [{}] extends [T]
|
|
88
|
+
? [keyof T] extends [never]
|
|
89
|
+
? true
|
|
90
|
+
: false
|
|
91
|
+
: false
|
|
92
|
+
: false;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* A body with a `t.File()` or `t.Files()` field can only be sent as multipart form data, so the
|
|
96
|
+
* payload is a `FormData` that Hyper Fetch sends as it is.
|
|
97
|
+
*/
|
|
98
|
+
type RoutePayload<Body> = true extends {
|
|
99
|
+
[K in keyof Body]: Body[K] extends Blob | Blob[] | undefined ? true : false;
|
|
100
|
+
}[keyof Body]
|
|
101
|
+
? FormData
|
|
102
|
+
: DefinedOrUndefined<Body>;
|
|
103
|
+
|
|
104
|
+
type Endpoint<Path extends string> = Path extends "" ? "/" : Path;
|
|
105
|
+
|
|
106
|
+
type ConvertMethods<Node, Path extends string, Client extends ClientInstance> = {
|
|
107
|
+
[K in MethodKeys<Node> as `$${Lowercase<K>}`]: Node[K] extends ElysiaRoute
|
|
108
|
+
? Request<
|
|
109
|
+
RouteResponse<Node[K]>,
|
|
110
|
+
RoutePayload<Node[K]["body"]>,
|
|
111
|
+
DefinedOrUndefined<Node[K]["query"]>,
|
|
112
|
+
RouteError<Node[K]>,
|
|
113
|
+
Endpoint<Path>,
|
|
114
|
+
Client
|
|
115
|
+
>
|
|
116
|
+
: never;
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
/* -------------------------------------------------------------------------------------------------
|
|
120
|
+
* Path segments
|
|
121
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
122
|
+
|
|
123
|
+
type CamelCase<S extends string> = S extends `${infer H}-${infer T}` ? `${H}${Capitalize<CamelCase<T>>}` : S;
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Keys of the route tree that are path segments. Wildcard segments are left out, because Hyper
|
|
127
|
+
* Fetch has no way to fill them, and so are optional params, which `ConvertOptionalParams` handles.
|
|
128
|
+
*/
|
|
129
|
+
type SegmentKeys<Node> = {
|
|
130
|
+
[K in keyof Node & string]: string extends K
|
|
131
|
+
? never
|
|
132
|
+
: K extends "*" | `:${string}?`
|
|
133
|
+
? never
|
|
134
|
+
: Node[K] extends ElysiaRoute
|
|
135
|
+
? never
|
|
136
|
+
: K;
|
|
137
|
+
}[keyof Node & string];
|
|
138
|
+
|
|
139
|
+
/** `:userId` becomes `$userId` and `google-login` becomes `googleLogin`, as `createSdk` expects. */
|
|
140
|
+
type NormalizeSegment<S extends string> = S extends `:${infer P}` ? `$${P}` : CamelCase<S>;
|
|
141
|
+
|
|
142
|
+
/** A segment that only holds left out routes, like `/live` with a WebSocket, is left out as well. */
|
|
143
|
+
type OmitEmpty<T> = {
|
|
144
|
+
[K in keyof T as keyof T[K] extends never ? never : K]: T[K];
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
type ConvertSegments<Node, Path extends string, Client extends ClientInstance, Depth extends unknown[]> = OmitEmpty<{
|
|
148
|
+
[K in SegmentKeys<Node> as NormalizeSegment<K>]: ConvertNode<Node[K], `${Path}/${K}`, Client, [...Depth, unknown]>;
|
|
149
|
+
}>;
|
|
150
|
+
|
|
151
|
+
type UnionToIntersection<U> = (U extends any ? (arg: U) => void : never) extends (arg: infer I) => void ? I : never;
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Elysia matches `/files/:id?` with and without the param. Hyper Fetch params are always required,
|
|
155
|
+
* so the routes below an optional param are added twice: on the current level without the param,
|
|
156
|
+
* and under `$id` with it.
|
|
157
|
+
*/
|
|
158
|
+
type ConvertOptionalParams<
|
|
159
|
+
Node,
|
|
160
|
+
Path extends string,
|
|
161
|
+
Client extends ClientInstance,
|
|
162
|
+
Depth extends unknown[],
|
|
163
|
+
> = UnionToIntersection<
|
|
164
|
+
{
|
|
165
|
+
[K in keyof Node & string]: K extends `:${infer P}?`
|
|
166
|
+
? ConvertNode<Node[K], Path, Client, [...Depth, unknown]> & {
|
|
167
|
+
[Key in `$${P}`]: ConvertNode<Node[K], `${Path}/:${P}`, Client, [...Depth, unknown]>;
|
|
168
|
+
}
|
|
169
|
+
: never;
|
|
170
|
+
}[keyof Node & string]
|
|
171
|
+
>;
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* The depth guard matches the 10 levels `createSdk` injects its client into. Without optional
|
|
175
|
+
* params the last intersection resolves to `unknown`, which leaves the level unchanged.
|
|
176
|
+
*/
|
|
177
|
+
type ConvertNode<
|
|
178
|
+
Node,
|
|
179
|
+
Path extends string,
|
|
180
|
+
Client extends ClientInstance,
|
|
181
|
+
Depth extends unknown[] = [],
|
|
182
|
+
> = Depth["length"] extends 10
|
|
183
|
+
? {}
|
|
184
|
+
: Prettify<
|
|
185
|
+
ConvertMethods<Node, Path, Client> &
|
|
186
|
+
ConvertSegments<Node, Path, Client, Depth> &
|
|
187
|
+
ConvertOptionalParams<Node, Path, Client, Depth>
|
|
188
|
+
>;
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Converts the type of an Elysia app into a Hyper Fetch SDK schema for `createSdk`.
|
|
192
|
+
*
|
|
193
|
+
* Every route becomes a `Request`. The response is the union of the bodies the handler returns with
|
|
194
|
+
* a 2xx status, the local error is the union of the bodies of every other status, and the payload
|
|
195
|
+
* and query params come from the route's `body` and `query` schemas. Path segments become nested
|
|
196
|
+
* properties, `:param` segments become `$param` properties, and kebab-case segments become
|
|
197
|
+
* camelCase.
|
|
198
|
+
*
|
|
199
|
+
* Response and error types follow what arrives over HTTP: dates become strings, `null` becomes the
|
|
200
|
+
* empty body `""`, and streams, generators, `Response` objects and `form()` bodies are `unknown`. A
|
|
201
|
+
* body with file fields is typed as `FormData`.
|
|
202
|
+
*
|
|
203
|
+
* Routes below an optional `:param?` segment appear both without and with the param. WebSocket
|
|
204
|
+
* routes, `.all()` routes, wildcard `*` segments, mounted handlers and paths with ten or more
|
|
205
|
+
* segments are left out. Anything that is not an Elysia app resolves to `{}`.
|
|
206
|
+
*
|
|
207
|
+
* @typeParam App - The Elysia app type, usually `typeof app`.
|
|
208
|
+
* @typeParam Client - The client the requests belong to. `createSdk` injects its own client, so
|
|
209
|
+
* this is only needed when the schema is used outside of `createSdk`.
|
|
210
|
+
*
|
|
211
|
+
* @example
|
|
212
|
+
* ```ts
|
|
213
|
+
* // backend
|
|
214
|
+
* const app = new Elysia().get("/users/:userId", ({ params }) => ({ id: params.userId }));
|
|
215
|
+
* export type AppSchema = ElysiaAppToHyperFetch<typeof app>;
|
|
216
|
+
*
|
|
217
|
+
* // frontend
|
|
218
|
+
* const sdk = createSdk<typeof client, AppSchema>(client);
|
|
219
|
+
* const { data } = await sdk.users.$userId.$get.setParams({ userId: "1" }).send();
|
|
220
|
+
* ```
|
|
221
|
+
*/
|
|
222
|
+
export type ElysiaAppToHyperFetch<App, Client extends ClientInstance = ClientInstance> = App extends {
|
|
223
|
+
"~Routes": infer Routes;
|
|
224
|
+
}
|
|
225
|
+
? ConvertNode<Routes, "", Client>
|
|
226
|
+
: {};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type { ElysiaAppToHyperFetch } from "./elysia.types";
|
package/tsconfig.json
ADDED
package/vite.config.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/// <reference types="vitest/config" />
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { defineConfig } from "vite";
|
|
4
|
+
import dts from "vite-plugin-dts";
|
|
5
|
+
import tsconfigPaths from "vite-tsconfig-paths";
|
|
6
|
+
|
|
7
|
+
import { getRollupExternalsFromPackageJson } from "../../scripts/vite-lib-externals-from-package";
|
|
8
|
+
|
|
9
|
+
export default defineConfig({
|
|
10
|
+
build: {
|
|
11
|
+
lib: {
|
|
12
|
+
entry: "src/index.ts",
|
|
13
|
+
fileName: "index",
|
|
14
|
+
formats: ["es"],
|
|
15
|
+
},
|
|
16
|
+
minify: false,
|
|
17
|
+
rollupOptions: {
|
|
18
|
+
external: getRollupExternalsFromPackageJson(__dirname),
|
|
19
|
+
},
|
|
20
|
+
sourcemap: true,
|
|
21
|
+
},
|
|
22
|
+
plugins: [tsconfigPaths(), dts({ entryRoot: "src" })],
|
|
23
|
+
test: {
|
|
24
|
+
alias: {
|
|
25
|
+
"@hyper-fetch/core": path.resolve(__dirname, "../core/src/index.ts"),
|
|
26
|
+
},
|
|
27
|
+
coverage: {
|
|
28
|
+
include: ["src/**/*.ts"],
|
|
29
|
+
provider: "v8",
|
|
30
|
+
thresholds: { 100: true },
|
|
31
|
+
},
|
|
32
|
+
environment: "node",
|
|
33
|
+
globals: true,
|
|
34
|
+
include: ["__tests__/**/*.spec.{ts,tsx}", "src/**/*.spec.{ts,tsx}"],
|
|
35
|
+
},
|
|
36
|
+
});
|