@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.
@@ -0,0 +1,359 @@
1
+ import { serve } from "@hono/node-server";
2
+ import type { ServerType } from "@hono/node-server";
3
+ import { createClient, createSdk } from "@hyper-fetch/core";
4
+ import type { AddressInfo } from "node:net";
5
+
6
+ import type { ElysiaAppToHyperFetch } from "../../../src";
7
+ import { edgeApp, rootParamApp } from "../../utils/edge-app";
8
+
9
+ type Schema = ElysiaAppToHyperFetch<typeof edgeApp>;
10
+ type RootSchema = ElysiaAppToHyperFetch<typeof rootParamApp>;
11
+
12
+ const startServer = (fetch: (request: Request) => Response | Promise<Response>) =>
13
+ new Promise<ServerType>((resolve) => {
14
+ const server = serve({ fetch, port: 0 }, () => resolve(server));
15
+ });
16
+
17
+ const stopServer = (server: ServerType) =>
18
+ new Promise<void>((resolve, reject) => {
19
+ server.close((error) => (error ? reject(error) : resolve()));
20
+ });
21
+
22
+ const getUrl = (server: ServerType) => `http://localhost:${(server.address() as AddressInfo).port}`;
23
+
24
+ describe("ElysiaAppToHyperFetch [ Edge cases against an Elysia server ]", () => {
25
+ let server: ServerType;
26
+ let url: string;
27
+
28
+ const createAppSdk = () => {
29
+ const client = createClient({ url });
30
+ return createSdk<typeof client, Schema>(client);
31
+ };
32
+
33
+ beforeAll(async () => {
34
+ server = await startServer(edgeApp.fetch);
35
+ url = getUrl(server);
36
+ });
37
+
38
+ afterAll(async () => {
39
+ await stopServer(server);
40
+ });
41
+
42
+ describe("When the path is unusual", () => {
43
+ it("should call a route registered on an empty path", async () => {
44
+ const { data } = await createAppSdk().$get.send();
45
+
46
+ expect(data).toBe("root");
47
+ });
48
+
49
+ it("should call the / route of a prefixed plugin and its nested routes", async () => {
50
+ const sdk = createAppSdk();
51
+
52
+ const index = await sdk.level.$get.send();
53
+ const nested = await sdk.level.level.$get.send();
54
+ const param = await sdk.level.$id.ok.$get.setParams({ id: "7" }).send();
55
+
56
+ expect(index.data).toBe("level-index");
57
+ expect(nested.data).toBe("level-level");
58
+ expect(param.data).toStrictEqual({ id: "7" });
59
+ });
60
+
61
+ it("should call a path with a segment named index", async () => {
62
+ const { data } = await createAppSdk().search.index.$indexId.stocks.$get.setParams({ indexId: "a" }).send();
63
+
64
+ expect(data).toStrictEqual({ indexId: "a" });
65
+ });
66
+
67
+ it("should call a group with an empty guard", async () => {
68
+ const { data } = await createAppSdk().nested.data.$get.send();
69
+
70
+ expect(data).toStrictEqual({ nested: true });
71
+ });
72
+
73
+ it("should call a deeply nested path", async () => {
74
+ const { data } = await createAppSdk().a.b.c.d.e.f.g.h.$get.send();
75
+
76
+ expect(data).toStrictEqual({ depth: 8 });
77
+ });
78
+
79
+ it("should send a number param that a params schema parses", async () => {
80
+ const { data } = await createAppSdk().params.$count.$get.setParams({ count: 5 }).send();
81
+
82
+ expect(data).toStrictEqual({ count: 5 });
83
+ });
84
+ });
85
+
86
+ describe("When the response is not plain JSON", () => {
87
+ it("should receive literal and primitive responses", async () => {
88
+ const sdk = createAppSdk();
89
+
90
+ const number = await sdk.number.$get.send();
91
+ const bool = await sdk.true.$get.send();
92
+
93
+ expect(number.data).toBe(1);
94
+ expect(bool.data).toBe(true);
95
+ });
96
+
97
+ it("should receive null and a handler without a return value as an empty body", async () => {
98
+ const sdk = createAppSdk();
99
+
100
+ const empty = await sdk.null.$get.send();
101
+ const nothing = await sdk.void.$get.send();
102
+
103
+ expect(empty.success).toBe(true);
104
+ expect(empty.data).toBe("");
105
+ expect(nothing.success).toBe(true);
106
+ expect(nothing.data).toBe("");
107
+ });
108
+
109
+ it("should receive dates as ISO strings", async () => {
110
+ const { data } = await createAppSdk().date.$get.send();
111
+
112
+ expect(data).toStrictEqual({ date: "2026-01-02T03:04:05.000Z", history: ["1970-01-01T00:00:00.000Z"] });
113
+ });
114
+
115
+ it("should resolve streamed and raw responses without failing", async () => {
116
+ const sdk = createAppSdk();
117
+
118
+ const responses = await Promise.all([
119
+ sdk.generator.$get.send(),
120
+ sdk.asyncGenerator.$get.send(),
121
+ sdk.sse.$get.send(),
122
+ sdk.response.$get.send(),
123
+ ]);
124
+
125
+ expect(responses.map(({ success }) => success)).toStrictEqual([true, true, true, true]);
126
+ expect(responses[3].data).toBe("raw text");
127
+ });
128
+
129
+ it("should read a streamed route as a stream with the streaming option", async () => {
130
+ const { data } = await createAppSdk().generator.$get.setOptions({ streaming: true }).send();
131
+
132
+ expect(data).toBeInstanceOf(ReadableStream);
133
+ expect(await new Response(data as ReadableStream).text()).toBe("ab");
134
+ });
135
+
136
+ it("should receive the JSON body of a handler that returns any", async () => {
137
+ const { data } = await createAppSdk().any.$get.send();
138
+
139
+ expect(data).toStrictEqual({ loose: true });
140
+ });
141
+
142
+ it("should follow a redirect to the target route", async () => {
143
+ const { data, status } = await createAppSdk().redirect.$get.send();
144
+
145
+ expect(status).toBe(200);
146
+ expect(data).toBe(1);
147
+ });
148
+ });
149
+
150
+ describe("When the route answers with an error status", () => {
151
+ it("should resolve a status passed by name", async () => {
152
+ const { data, error, status } = await createAppSdk().teapot.$get.send();
153
+
154
+ expect(status).toBe(418);
155
+ expect(data).toBeNull();
156
+ expect(error).toStrictEqual({ brewing: false });
157
+ });
158
+
159
+ it("should resolve a 3xx status as an error", async () => {
160
+ const { error, status, success } = await createAppSdk().multipleChoices.$get.send();
161
+
162
+ expect(success).toBe(false);
163
+ expect(status).toBe(300);
164
+ expect(error).toStrictEqual({ choices: ["a", "b"] });
165
+ });
166
+
167
+ it("should resolve the status a macro returns", async () => {
168
+ const sdk = createAppSdk();
169
+
170
+ const denied = await sdk.secret.$get.send();
171
+ const allowed = await sdk.secret.$get.setHeaders({ authorization: "Bearer token" }).send();
172
+
173
+ expect(denied.status).toBe(403);
174
+ expect(denied.error).toStrictEqual({ reason: "forbidden" });
175
+ expect(allowed.data).toStrictEqual({ userId: "1" });
176
+ });
177
+
178
+ it("should resolve an error a handler throws with its status", async () => {
179
+ const { error, status, success } = await createAppSdk().throws.$get.send();
180
+
181
+ expect(success).toBe(false);
182
+ expect(status).toBe(500);
183
+ expect(error).toBe("Unexpected failure");
184
+ });
185
+
186
+ it("should send the headers a headers schema requires with setHeaders", async () => {
187
+ const sdk = createAppSdk();
188
+
189
+ const missing = await sdk.headers.$get.send();
190
+ const sent = await sdk.headers.$get.setHeaders({ username: "ann" }).send();
191
+
192
+ expect(missing.status).toBe(422);
193
+ expect(sent.data).toStrictEqual({ username: "ann" });
194
+ });
195
+
196
+ it("should resolve a missing required query param as a validation error", async () => {
197
+ const client = createClient({ url });
198
+ const request = client.createRequest()({ endpoint: "/guarded" });
199
+
200
+ const { error, status } = await request.send();
201
+
202
+ expect(status).toBe(422);
203
+ expect(error).toMatchObject({ type: "validation", on: "query" });
204
+ });
205
+ });
206
+
207
+ describe("When the request sends a body", () => {
208
+ it("should send a body resolved from a model", async () => {
209
+ const { data } = await createAppSdk()
210
+ .deep.nested.mirror.$post.setPayload({ username: "ann", password: "secret" })
211
+ .send();
212
+
213
+ expect(data).toStrictEqual({ username: "ann", password: "secret" });
214
+ });
215
+
216
+ it("should send primitive and array bodies", async () => {
217
+ const sdk = createAppSdk();
218
+
219
+ const text = await sdk.string.$post.setPayload("hello").send();
220
+ const list = await sdk.array.$post.setPayload(["a", "b"]).send();
221
+
222
+ expect(text.data).toBe("hello");
223
+ expect(list.data).toStrictEqual(["a", "b"]);
224
+ });
225
+
226
+ it("should send an optional body with and without the payload", async () => {
227
+ const sdk = createAppSdk();
228
+
229
+ const withBody = await sdk.optionalBody.$post.setPayload({ note: "hi" }).send();
230
+ const withoutBody = await sdk.optionalBody.$post.setPayload(null).send();
231
+
232
+ expect(withBody.data).toStrictEqual({ received: { note: "hi" } });
233
+ expect(withoutBody.data).toStrictEqual({ received: null });
234
+ });
235
+
236
+ it("should send each member of a union body", async () => {
237
+ const sdk = createAppSdk();
238
+
239
+ const first = await sdk.unionBody.$post.setPayload({ a: "x" }).send();
240
+ const second = await sdk.unionBody.$post.setPayload({ b: 2 }).send();
241
+
242
+ expect(first.data).toStrictEqual({ a: "x" });
243
+ expect(second.data).toStrictEqual({ b: 2 });
244
+ });
245
+
246
+ it("should send a Date the body schema parses", async () => {
247
+ const { data } = await createAppSdk()
248
+ .dateBody.$post.setPayload({ date: new Date("2030-06-01T00:00:00.000Z") })
249
+ .send();
250
+
251
+ expect(data).toStrictEqual({ year: 2030 });
252
+ });
253
+
254
+ it("should upload files as FormData", async () => {
255
+ const sdk = createAppSdk();
256
+ const single = new FormData();
257
+ single.append("title", "report");
258
+ single.append("file", new File(["12345"], "report.txt"));
259
+ const multiple = new FormData();
260
+ multiple.append("files", new File(["a"], "a.txt"));
261
+ multiple.append("files", new File(["b"], "b.txt"));
262
+
263
+ const upload = await sdk.upload.$post.setPayload(single).send();
264
+ const uploads = await sdk.uploads.$post.setPayload(multiple).send();
265
+
266
+ expect(upload.data).toStrictEqual({ name: "report", size: 5 });
267
+ expect(uploads.data).toStrictEqual({ count: 2 });
268
+ });
269
+
270
+ it("should send a form without its optional file", async () => {
271
+ const { data } = await createAppSdk().optionalFile.$post.setPayload(new FormData()).send();
272
+
273
+ expect(data).toStrictEqual({ hasFile: false });
274
+ });
275
+
276
+ it("should send a DELETE without a body", async () => {
277
+ const { data } = await createAppSdk().empty.$delete.send();
278
+
279
+ expect(data).toStrictEqual({ deleted: true });
280
+ });
281
+ });
282
+
283
+ describe("When the request sends query params", () => {
284
+ it("should send a Date and a number the query schema parses", async () => {
285
+ const { data } = await createAppSdk()
286
+ .queryTypes.$get.setQueryParams({ day: new Date("2026-03-04T00:00:00.000Z"), count: 3 })
287
+ .send();
288
+
289
+ expect(data).toStrictEqual({ day: "2026-03-04T00:00:00.000Z", count: 3, note: null });
290
+ });
291
+
292
+ it("should fail the default bracket format for array query params", async () => {
293
+ const { status } = await createAppSdk()
294
+ .queryArray.$get.setQueryParams({ tags: ["a", "b"] })
295
+ .send();
296
+
297
+ expect(status).toBe(422);
298
+ });
299
+
300
+ it("should send an array query param with repeated keys", async () => {
301
+ const client = createClient({ url });
302
+ client.adapter.setQueryParamsMapperConfig({ arrayFormat: "none" });
303
+ const sdk = createSdk<typeof client, Schema>(client);
304
+
305
+ const { data } = await sdk.queryArray.$get.setQueryParams({ tags: ["a", "b"] }).send();
306
+
307
+ expect(data).toStrictEqual({ tags: ["a", "b"] });
308
+ });
309
+
310
+ it("should send optional query params with and without values", async () => {
311
+ const sdk = createAppSdk();
312
+
313
+ const withValue = await sdk.queryPartial.$get.setQueryParams({ page: "2" }).send();
314
+ const withoutValue = await sdk.queryPartial.$get.setQueryParams({}).send();
315
+
316
+ expect(withValue.data).toStrictEqual({ page: "2" });
317
+ expect(withoutValue.data).toStrictEqual({});
318
+ });
319
+
320
+ it("should send the query a guard requires", async () => {
321
+ const { data } = await createAppSdk().guarded.$get.setQueryParams({ token: "abc" }).send();
322
+
323
+ expect(data).toStrictEqual({ token: "abc" });
324
+ });
325
+ });
326
+
327
+ describe("When the method is not GET or POST", () => {
328
+ it("should send HEAD and OPTIONS requests", async () => {
329
+ const sdk = createAppSdk();
330
+
331
+ const head = await sdk.head.$head.send();
332
+ const options = await sdk.options.$options.send();
333
+
334
+ expect(head.status).toBe(200);
335
+ expect(options.data).toBe("options");
336
+ });
337
+ });
338
+ });
339
+
340
+ describe("ElysiaAppToHyperFetch [ Root param against an Elysia server ]", () => {
341
+ let server: ServerType;
342
+
343
+ beforeAll(async () => {
344
+ server = await startServer(rootParamApp.fetch);
345
+ });
346
+
347
+ afterAll(async () => {
348
+ await stopServer(server);
349
+ });
350
+
351
+ it("should fill a param at the root of the app", async () => {
352
+ const client = createClient({ url: getUrl(server) });
353
+ const sdk = createSdk<typeof client, RootSchema>(client);
354
+
355
+ const { data } = await sdk.$id.$get.setParams({ id: "42" }).send();
356
+
357
+ expect(data).toStrictEqual({ id: "42" });
358
+ });
359
+ });