@fragno-dev/core 0.1.7 → 0.1.9
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/.turbo/turbo-build.log +131 -64
- package/CHANGELOG.md +19 -0
- package/dist/api/api.d.ts +38 -2
- package/dist/api/api.d.ts.map +1 -0
- package/dist/api/api.js +9 -2
- package/dist/api/api.js.map +1 -0
- package/dist/api/bind-services.d.ts +6 -0
- package/dist/api/bind-services.d.ts.map +1 -0
- package/dist/api/bind-services.js +20 -0
- package/dist/api/bind-services.js.map +1 -0
- package/dist/api/error.d.ts +26 -0
- package/dist/api/error.d.ts.map +1 -0
- package/dist/{api-DngJDcmO.js → api/error.js} +2 -8
- package/dist/api/error.js.map +1 -0
- package/dist/api/fragment-definition-builder.d.ts +313 -0
- package/dist/api/fragment-definition-builder.d.ts.map +1 -0
- package/dist/api/fragment-definition-builder.js +326 -0
- package/dist/api/fragment-definition-builder.js.map +1 -0
- package/dist/api/fragment-instantiator.d.ts +216 -0
- package/dist/api/fragment-instantiator.d.ts.map +1 -0
- package/dist/api/fragment-instantiator.js +487 -0
- package/dist/api/fragment-instantiator.js.map +1 -0
- package/dist/api/fragno-response.d.ts +30 -0
- package/dist/api/fragno-response.d.ts.map +1 -0
- package/dist/api/fragno-response.js +73 -0
- package/dist/api/fragno-response.js.map +1 -0
- package/dist/api/internal/path.d.ts +50 -0
- package/dist/api/internal/path.d.ts.map +1 -0
- package/dist/api/internal/path.js +76 -0
- package/dist/api/internal/path.js.map +1 -0
- package/dist/api/internal/response-stream.d.ts +43 -0
- package/dist/api/internal/response-stream.d.ts.map +1 -0
- package/dist/api/internal/response-stream.js +81 -0
- package/dist/api/internal/response-stream.js.map +1 -0
- package/dist/api/internal/route.js +10 -0
- package/dist/api/internal/route.js.map +1 -0
- package/dist/api/mutable-request-state.d.ts +82 -0
- package/dist/api/mutable-request-state.d.ts.map +1 -0
- package/dist/api/mutable-request-state.js +97 -0
- package/dist/api/mutable-request-state.js.map +1 -0
- package/dist/api/request-context-storage.d.ts +42 -0
- package/dist/api/request-context-storage.d.ts.map +1 -0
- package/dist/api/request-context-storage.js +43 -0
- package/dist/api/request-context-storage.js.map +1 -0
- package/dist/api/request-input-context.d.ts +89 -0
- package/dist/api/request-input-context.d.ts.map +1 -0
- package/dist/api/request-input-context.js +118 -0
- package/dist/api/request-input-context.js.map +1 -0
- package/dist/api/request-middleware.d.ts +50 -0
- package/dist/api/request-middleware.d.ts.map +1 -0
- package/dist/api/request-middleware.js +83 -0
- package/dist/api/request-middleware.js.map +1 -0
- package/dist/api/request-output-context.d.ts +41 -0
- package/dist/api/request-output-context.d.ts.map +1 -0
- package/dist/api/request-output-context.js +119 -0
- package/dist/api/request-output-context.js.map +1 -0
- package/dist/api/route-handler-input-options.d.ts +21 -0
- package/dist/api/route-handler-input-options.d.ts.map +1 -0
- package/dist/api/route.d.ts +54 -3
- package/dist/api/route.d.ts.map +1 -0
- package/dist/api/route.js +29 -2
- package/dist/api/route.js.map +1 -0
- package/dist/api/shared-types.d.ts +47 -0
- package/dist/api/shared-types.d.ts.map +1 -0
- package/dist/api/shared-types.js +1 -0
- package/dist/client/client-error.d.ts +60 -0
- package/dist/client/client-error.d.ts.map +1 -0
- package/dist/client/client-error.js +92 -0
- package/dist/client/client-error.js.map +1 -0
- package/dist/client/client.d.ts +210 -4
- package/dist/client/client.d.ts.map +1 -0
- package/dist/client/client.js +397 -6
- package/dist/client/client.js.map +1 -0
- package/dist/client/client.svelte.d.ts +5 -3
- package/dist/client/client.svelte.d.ts.map +1 -1
- package/dist/client/client.svelte.js +1 -5
- package/dist/client/client.svelte.js.map +1 -1
- package/dist/client/internal/fetcher-merge.js +36 -0
- package/dist/client/internal/fetcher-merge.js.map +1 -0
- package/dist/client/internal/ndjson-streaming.js +139 -0
- package/dist/client/internal/ndjson-streaming.js.map +1 -0
- package/dist/client/react.d.ts +5 -3
- package/dist/client/react.d.ts.map +1 -1
- package/dist/client/react.js +3 -5
- package/dist/client/react.js.map +1 -1
- package/dist/client/solid.d.ts +5 -3
- package/dist/client/solid.d.ts.map +1 -1
- package/dist/client/solid.js +2 -5
- package/dist/client/solid.js.map +1 -1
- package/dist/client/vanilla.d.ts +5 -3
- package/dist/client/vanilla.d.ts.map +1 -1
- package/dist/client/vanilla.js +2 -43
- package/dist/client/vanilla.js.map +1 -1
- package/dist/client/vue.d.ts +5 -3
- package/dist/client/vue.d.ts.map +1 -1
- package/dist/client/vue.js +1 -5
- package/dist/client/vue.js.map +1 -1
- package/dist/http/http-status.d.ts +26 -0
- package/dist/http/http-status.d.ts.map +1 -0
- package/dist/integrations/react-ssr.js +1 -1
- package/dist/internal/symbols.d.ts +9 -0
- package/dist/internal/symbols.d.ts.map +1 -0
- package/dist/internal/symbols.js +10 -0
- package/dist/internal/symbols.js.map +1 -0
- package/dist/mod-client.d.ts +36 -0
- package/dist/mod-client.d.ts.map +1 -0
- package/dist/mod-client.js +21 -0
- package/dist/mod-client.js.map +1 -0
- package/dist/mod.d.ts +7 -4
- package/dist/mod.js +4 -6
- package/dist/request/request.d.ts +4 -0
- package/dist/request/request.js +5 -0
- package/dist/test/test.d.ts +62 -35
- package/dist/test/test.d.ts.map +1 -1
- package/dist/test/test.js +75 -40
- package/dist/test/test.js.map +1 -1
- package/dist/util/async.js +40 -0
- package/dist/util/async.js.map +1 -0
- package/dist/util/content-type.js +49 -0
- package/dist/util/content-type.js.map +1 -0
- package/dist/util/nanostores.js +31 -0
- package/dist/util/nanostores.js.map +1 -0
- package/dist/{ssr-BByDVfFD.js → util/ssr.js} +2 -2
- package/dist/util/ssr.js.map +1 -0
- package/dist/util/types-util.d.ts +8 -0
- package/dist/util/types-util.d.ts.map +1 -0
- package/package.json +19 -12
- package/src/api/api.ts +41 -6
- package/src/api/bind-services.ts +42 -0
- package/src/api/fragment-definition-builder.extend.test.ts +810 -0
- package/src/api/fragment-definition-builder.test.ts +499 -0
- package/src/api/fragment-definition-builder.ts +1088 -0
- package/src/api/fragment-instantiator.test.ts +1488 -0
- package/src/api/fragment-instantiator.ts +1053 -0
- package/src/api/fragment-services.test.ts +727 -0
- package/src/api/request-context-storage.ts +64 -0
- package/src/api/request-middleware.test.ts +301 -225
- package/src/api/route.test.ts +87 -1
- package/src/api/route.ts +345 -24
- package/src/api/shared-types.ts +43 -0
- package/src/client/client-builder.test.ts +23 -23
- package/src/client/client.ssr.test.ts +3 -3
- package/src/client/client.svelte.test.ts +15 -15
- package/src/client/client.test.ts +22 -22
- package/src/client/client.ts +72 -12
- package/src/client/internal/fetcher-merge.ts +1 -1
- package/src/client/react.test.ts +2 -2
- package/src/client/solid.test.ts +2 -2
- package/src/client/vanilla.test.ts +2 -2
- package/src/client/vue.test.ts +2 -2
- package/src/internal/symbols.ts +5 -0
- package/src/mod-client.ts +59 -0
- package/src/mod.ts +26 -9
- package/src/request/request.ts +8 -0
- package/src/test/test.test.ts +200 -381
- package/src/test/test.ts +190 -117
- package/tsdown.config.ts +8 -5
- package/dist/api/fragment-builder.d.ts +0 -4
- package/dist/api/fragment-builder.js +0 -3
- package/dist/api/fragment-instantiation.d.ts +0 -4
- package/dist/api/fragment-instantiation.js +0 -6
- package/dist/api-BWN97TOr.d.ts +0 -377
- package/dist/api-BWN97TOr.d.ts.map +0 -1
- package/dist/api-DngJDcmO.js.map +0 -1
- package/dist/client-C5LsYHEI.js +0 -782
- package/dist/client-C5LsYHEI.js.map +0 -1
- package/dist/fragment-builder-DOnCVBqc.js +0 -47
- package/dist/fragment-builder-DOnCVBqc.js.map +0 -1
- package/dist/fragment-builder-MGr68GNb.d.ts +0 -409
- package/dist/fragment-builder-MGr68GNb.d.ts.map +0 -1
- package/dist/fragment-instantiation-C4wvwl6V.js +0 -446
- package/dist/fragment-instantiation-C4wvwl6V.js.map +0 -1
- package/dist/request-output-context-CdIjwmEN.js +0 -320
- package/dist/request-output-context-CdIjwmEN.js.map +0 -1
- package/dist/route-Bl9Zr1Yv.d.ts +0 -26
- package/dist/route-Bl9Zr1Yv.d.ts.map +0 -1
- package/dist/route-C5Uryylh.js +0 -21
- package/dist/route-C5Uryylh.js.map +0 -1
- package/dist/ssr-BByDVfFD.js.map +0 -1
- package/src/api/fragment-builder.ts +0 -80
- package/src/api/fragment-instantiation.test.ts +0 -460
- package/src/api/fragment-instantiation.ts +0 -499
- package/src/api/fragment.test.ts +0 -537
package/src/api/fragment.test.ts
DELETED
|
@@ -1,537 +0,0 @@
|
|
|
1
|
-
import { test, expect, describe, expectTypeOf } from "vitest";
|
|
2
|
-
import { defineFragment, type FragmentBuilder } from "./fragment-builder";
|
|
3
|
-
import { createFragment } from "./fragment-instantiation";
|
|
4
|
-
import { defineRoute, defineRoutes, type RouteFactory, resolveRouteFactories } from "./route";
|
|
5
|
-
import { z } from "zod";
|
|
6
|
-
import type { InferOr } from "../util/types-util";
|
|
7
|
-
|
|
8
|
-
type Empty = Record<never, never>;
|
|
9
|
-
|
|
10
|
-
describe("new-fragment API", () => {
|
|
11
|
-
describe("Type inference", () => {
|
|
12
|
-
test("defineFragment infers config type correctly", () => {
|
|
13
|
-
const _config = {
|
|
14
|
-
apiKey: "test-key",
|
|
15
|
-
maxRetries: 3,
|
|
16
|
-
debug: false,
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
const _fragment = defineFragment<typeof _config>("test");
|
|
20
|
-
|
|
21
|
-
expectTypeOf<typeof _fragment>().toEqualTypeOf<
|
|
22
|
-
FragmentBuilder<
|
|
23
|
-
{
|
|
24
|
-
apiKey: string;
|
|
25
|
-
maxRetries: number;
|
|
26
|
-
debug: boolean;
|
|
27
|
-
},
|
|
28
|
-
Empty,
|
|
29
|
-
Empty,
|
|
30
|
-
Empty
|
|
31
|
-
>
|
|
32
|
-
>();
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
test("withDependencies correctly transforms dependency types", () => {
|
|
36
|
-
const _config = {
|
|
37
|
-
apiKey: "test-key",
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
const _fragment = defineFragment<typeof _config>("test").withDependencies(({ config }) => {
|
|
41
|
-
expectTypeOf(config).toEqualTypeOf<typeof _config>();
|
|
42
|
-
return {
|
|
43
|
-
httpClient: { fetch: () => Promise.resolve(new Response()) },
|
|
44
|
-
logger: { log: (msg: string) => console.log(msg) },
|
|
45
|
-
};
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
expectTypeOf<typeof _fragment>().toEqualTypeOf<
|
|
49
|
-
FragmentBuilder<
|
|
50
|
-
typeof _config,
|
|
51
|
-
{
|
|
52
|
-
httpClient: { fetch: () => Promise<Response> };
|
|
53
|
-
logger: { log: (msg: string) => void };
|
|
54
|
-
},
|
|
55
|
-
Empty,
|
|
56
|
-
Empty
|
|
57
|
-
>
|
|
58
|
-
>();
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
test("withServices has access to dependencies and config", () => {
|
|
62
|
-
const _config = {
|
|
63
|
-
apiKey: "test-key",
|
|
64
|
-
baseUrl: "https://api.example.com",
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
const _fragment = defineFragment<typeof _config>("test")
|
|
68
|
-
.withDependencies(({ config }) => {
|
|
69
|
-
expectTypeOf(config).toEqualTypeOf<{
|
|
70
|
-
apiKey: string;
|
|
71
|
-
baseUrl: string;
|
|
72
|
-
}>();
|
|
73
|
-
|
|
74
|
-
return { httpClient: { baseUrl: config.baseUrl } };
|
|
75
|
-
})
|
|
76
|
-
.withServices(({ config, deps }) => {
|
|
77
|
-
expectTypeOf(config).toEqualTypeOf<typeof _config>();
|
|
78
|
-
expectTypeOf(deps).toEqualTypeOf<{ httpClient: { baseUrl: string } }>();
|
|
79
|
-
|
|
80
|
-
return {
|
|
81
|
-
userService: {
|
|
82
|
-
getUser: async (id: string) => ({ id, name: "Test User" }),
|
|
83
|
-
},
|
|
84
|
-
cacheService: {
|
|
85
|
-
get: (_key: string): string => crypto.randomUUID(),
|
|
86
|
-
set: (_key: string, _value: string) => {},
|
|
87
|
-
},
|
|
88
|
-
};
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
expectTypeOf<typeof _fragment>().toEqualTypeOf<
|
|
92
|
-
FragmentBuilder<
|
|
93
|
-
typeof _config,
|
|
94
|
-
{ httpClient: { baseUrl: string } },
|
|
95
|
-
{
|
|
96
|
-
userService: {
|
|
97
|
-
getUser: (id: string) => Promise<{ id: string; name: string }>;
|
|
98
|
-
};
|
|
99
|
-
cacheService: {
|
|
100
|
-
get: (key: string) => string;
|
|
101
|
-
set: (key: string, value: string) => void;
|
|
102
|
-
};
|
|
103
|
-
},
|
|
104
|
-
Empty
|
|
105
|
-
>
|
|
106
|
-
>();
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
test("defineRoutes receives correct context types", () => {
|
|
110
|
-
type Config = {
|
|
111
|
-
apiKey: string;
|
|
112
|
-
model: "gpt-3" | "gpt-4";
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
type Deps = {
|
|
116
|
-
openai: { complete: (prompt: string) => Promise<string> };
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
type Services = {
|
|
120
|
-
cache: Map<string, unknown>;
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
const _routeFactory = defineRoutes<Config, Deps, Services>().create(
|
|
124
|
-
({ config, deps, services }) => {
|
|
125
|
-
expectTypeOf(config).toEqualTypeOf<Config>();
|
|
126
|
-
expectTypeOf(deps).toEqualTypeOf<Deps>();
|
|
127
|
-
expectTypeOf(services).toEqualTypeOf<Services>();
|
|
128
|
-
|
|
129
|
-
return [
|
|
130
|
-
defineRoute({
|
|
131
|
-
method: "POST",
|
|
132
|
-
path: "/complete",
|
|
133
|
-
inputSchema: z.object({ prompt: z.string() }),
|
|
134
|
-
outputSchema: z.object({ result: z.string() }),
|
|
135
|
-
handler: async ({ input }, { json }) => {
|
|
136
|
-
const { prompt } = await input.valid();
|
|
137
|
-
expectTypeOf(prompt).toEqualTypeOf<string>();
|
|
138
|
-
expectTypeOf<Parameters<typeof json>[0]>().toEqualTypeOf<{ result: string }>();
|
|
139
|
-
|
|
140
|
-
const result = await deps.openai.complete(prompt);
|
|
141
|
-
services.cache.set(prompt, result);
|
|
142
|
-
return json({ result });
|
|
143
|
-
},
|
|
144
|
-
}),
|
|
145
|
-
];
|
|
146
|
-
},
|
|
147
|
-
);
|
|
148
|
-
|
|
149
|
-
expectTypeOf<Parameters<typeof _routeFactory>[0]>().toEqualTypeOf<{
|
|
150
|
-
config: Config;
|
|
151
|
-
deps: Deps;
|
|
152
|
-
services: Services;
|
|
153
|
-
}>();
|
|
154
|
-
});
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
describe("Builder pattern", () => {
|
|
158
|
-
test("Builder methods return new instances", () => {
|
|
159
|
-
const _config = { test: true };
|
|
160
|
-
|
|
161
|
-
const lib1 = defineFragment<typeof _config>("test");
|
|
162
|
-
expectTypeOf(lib1).toEqualTypeOf<FragmentBuilder<typeof _config, Empty, Empty, Empty>>();
|
|
163
|
-
|
|
164
|
-
const lib2 = lib1.withDependencies(() => ({ dep1: "value1" }));
|
|
165
|
-
expectTypeOf(lib2).toEqualTypeOf<
|
|
166
|
-
FragmentBuilder<typeof _config, { dep1: string }, Empty, Empty>
|
|
167
|
-
>();
|
|
168
|
-
const lib3 = lib2.withServices(() => ({ service1: "value1" }));
|
|
169
|
-
expectTypeOf(lib3).toEqualTypeOf<
|
|
170
|
-
FragmentBuilder<typeof _config, { dep1: string }, { service1: string }, Empty>
|
|
171
|
-
>();
|
|
172
|
-
|
|
173
|
-
expect(lib1).not.toBe(lib2);
|
|
174
|
-
expect(lib2).not.toBe(lib3);
|
|
175
|
-
expect(lib1).not.toBe(lib3);
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
test("Each builder step preserves previous configuration", () => {
|
|
179
|
-
const _config = { apiKey: "test" };
|
|
180
|
-
|
|
181
|
-
const fragment = defineFragment<typeof _config>("my-lib")
|
|
182
|
-
.withDependencies(({ config }) => ({
|
|
183
|
-
client: `Client for ${config.apiKey}`,
|
|
184
|
-
}))
|
|
185
|
-
.withServices(({ deps }) => ({
|
|
186
|
-
service: `Service using ${deps.client}`,
|
|
187
|
-
}));
|
|
188
|
-
|
|
189
|
-
expect(fragment.definition.name).toBe("my-lib");
|
|
190
|
-
expect(fragment.definition.dependencies).toBeDefined();
|
|
191
|
-
expect(fragment.definition.services).toBeDefined();
|
|
192
|
-
});
|
|
193
|
-
});
|
|
194
|
-
|
|
195
|
-
describe("Fragment creation", () => {
|
|
196
|
-
test("createFragment instantiates fragment with config", async () => {
|
|
197
|
-
const InputSchema = z.object({ name: z.string() });
|
|
198
|
-
const OutputSchema = z.object({ greeting: z.string() });
|
|
199
|
-
|
|
200
|
-
const routeFactory = defineRoutes<
|
|
201
|
-
{ prefix: string },
|
|
202
|
-
{ formatter: (s: string) => string },
|
|
203
|
-
{ logger: { log: (s: string) => void } }
|
|
204
|
-
>().create(({ config, deps, services }) => [
|
|
205
|
-
defineRoute({
|
|
206
|
-
method: "POST",
|
|
207
|
-
path: "/greet",
|
|
208
|
-
inputSchema: InputSchema,
|
|
209
|
-
outputSchema: OutputSchema,
|
|
210
|
-
handler: async ({ input }, { json }) => {
|
|
211
|
-
const { name } = await input.valid();
|
|
212
|
-
const greeting = deps.formatter(`${config.prefix} ${name}`);
|
|
213
|
-
services.logger.log(greeting);
|
|
214
|
-
return json({ greeting });
|
|
215
|
-
},
|
|
216
|
-
}),
|
|
217
|
-
]);
|
|
218
|
-
|
|
219
|
-
const fragmentDef = defineFragment("greeting")
|
|
220
|
-
.withDependencies(() => ({
|
|
221
|
-
formatter: (s: string) => s.toUpperCase(),
|
|
222
|
-
}))
|
|
223
|
-
.withServices(() => ({
|
|
224
|
-
logger: { log: (s: string) => console.log(s) },
|
|
225
|
-
}));
|
|
226
|
-
|
|
227
|
-
const fragment = createFragment(fragmentDef, { prefix: "Hello" }, [routeFactory], {});
|
|
228
|
-
|
|
229
|
-
expect(fragment.mountRoute).toBe("/api/greeting");
|
|
230
|
-
expect(fragment.config.name).toBe("greeting");
|
|
231
|
-
expect(fragment.services).toHaveProperty("logger");
|
|
232
|
-
expect(fragment.handler).toBeInstanceOf(Function);
|
|
233
|
-
|
|
234
|
-
const request = new Request("http://localhost/api/greeting/greet", {
|
|
235
|
-
method: "POST",
|
|
236
|
-
body: JSON.stringify({ name: "World" }),
|
|
237
|
-
headers: { "Content-Type": "application/json" },
|
|
238
|
-
});
|
|
239
|
-
|
|
240
|
-
const response = await fragment.handler(request);
|
|
241
|
-
expect(response.status).toBe(200);
|
|
242
|
-
|
|
243
|
-
const data = await response.json();
|
|
244
|
-
expect(data).toEqual({ greeting: "HELLO WORLD" });
|
|
245
|
-
});
|
|
246
|
-
|
|
247
|
-
test("Wildcard path", async () => {
|
|
248
|
-
const route = defineRoute({
|
|
249
|
-
method: "GET",
|
|
250
|
-
path: "/thing/:id/**:path",
|
|
251
|
-
handler: async ({ pathParams: _pathParams }, outputCtx) => {
|
|
252
|
-
expectTypeOf<typeof _pathParams>().toEqualTypeOf<{ id: string; path: string }>();
|
|
253
|
-
return outputCtx.json({ message: "Hello, World!" });
|
|
254
|
-
},
|
|
255
|
-
});
|
|
256
|
-
|
|
257
|
-
const fragmentDef = defineFragment("test-fragment");
|
|
258
|
-
const fragment = createFragment(fragmentDef, {}, [route], {
|
|
259
|
-
mountRoute: "/api",
|
|
260
|
-
});
|
|
261
|
-
|
|
262
|
-
// Create a test request
|
|
263
|
-
const request = new Request("http://localhost:3000/api/thing/123/foo/bar", {
|
|
264
|
-
method: "GET",
|
|
265
|
-
});
|
|
266
|
-
|
|
267
|
-
// Call the handler
|
|
268
|
-
const response = await fragment.handler(request);
|
|
269
|
-
|
|
270
|
-
// Verify the response
|
|
271
|
-
expect(response.status).toBe(200);
|
|
272
|
-
const data = await response.json();
|
|
273
|
-
expect(data).toEqual({ message: "Hello, World!" });
|
|
274
|
-
});
|
|
275
|
-
|
|
276
|
-
test("Routes receive correct context from fragment definition", async () => {
|
|
277
|
-
let capturedConfig;
|
|
278
|
-
let capturedDeps;
|
|
279
|
-
let capturedServices;
|
|
280
|
-
|
|
281
|
-
const routeFactory = defineRoutes<
|
|
282
|
-
{ setting: string },
|
|
283
|
-
{ tool: string },
|
|
284
|
-
{ storage: string }
|
|
285
|
-
>().create(({ config, deps, services }) => {
|
|
286
|
-
capturedConfig = config;
|
|
287
|
-
capturedDeps = deps;
|
|
288
|
-
capturedServices = services;
|
|
289
|
-
return [
|
|
290
|
-
defineRoute({
|
|
291
|
-
method: "GET",
|
|
292
|
-
path: "/test",
|
|
293
|
-
handler: async (_, { json }) => json({ ok: true }),
|
|
294
|
-
}),
|
|
295
|
-
];
|
|
296
|
-
});
|
|
297
|
-
|
|
298
|
-
const fragmentDef = defineFragment("test")
|
|
299
|
-
.withDependencies(() => ({ tool: "hammer" }))
|
|
300
|
-
.withServices(() => ({ storage: "memory" }));
|
|
301
|
-
|
|
302
|
-
createFragment(fragmentDef, { setting: "value" }, [routeFactory], {});
|
|
303
|
-
|
|
304
|
-
expect(capturedConfig).toEqual({ setting: "value" });
|
|
305
|
-
expect(capturedDeps).toEqual({ tool: "hammer" });
|
|
306
|
-
expect(capturedServices).toEqual({ storage: "memory" });
|
|
307
|
-
});
|
|
308
|
-
});
|
|
309
|
-
|
|
310
|
-
describe("Type constraints", () => {
|
|
311
|
-
test("Services must extend Record<string, unknown>", () => {
|
|
312
|
-
const fragmentDef = defineFragment("test").withServices(() => ({
|
|
313
|
-
validService: { method: () => {} },
|
|
314
|
-
anotherService: "string value",
|
|
315
|
-
numberService: 123,
|
|
316
|
-
}));
|
|
317
|
-
|
|
318
|
-
const _fragment = createFragment(fragmentDef, {}, [], {});
|
|
319
|
-
|
|
320
|
-
expectTypeOf<typeof _fragment.services>().toEqualTypeOf<{
|
|
321
|
-
validService: { method: () => void };
|
|
322
|
-
anotherService: string;
|
|
323
|
-
numberService: number;
|
|
324
|
-
}>();
|
|
325
|
-
});
|
|
326
|
-
|
|
327
|
-
test("Route handler types are preserved", () => {
|
|
328
|
-
const OutputSchema = z.object({ data: z.string() });
|
|
329
|
-
|
|
330
|
-
const route = defineRoute({
|
|
331
|
-
method: "GET",
|
|
332
|
-
path: "/item/:id",
|
|
333
|
-
inputSchema: z.object({ id: z.number() }),
|
|
334
|
-
outputSchema: OutputSchema,
|
|
335
|
-
errorCodes: ["NOT_FOUND"],
|
|
336
|
-
queryParameters: ["page", "limit"],
|
|
337
|
-
handler: async ({ pathParams, input }, { json }) => {
|
|
338
|
-
expectTypeOf(pathParams).toEqualTypeOf<{ id: string }>();
|
|
339
|
-
const validated = await input.valid();
|
|
340
|
-
expectTypeOf(validated).toEqualTypeOf<{ id: number }>();
|
|
341
|
-
return json({ data: "test" });
|
|
342
|
-
},
|
|
343
|
-
});
|
|
344
|
-
expectTypeOf(route.method).toEqualTypeOf<"GET">();
|
|
345
|
-
expectTypeOf(route.path).toEqualTypeOf<"/item/:id">();
|
|
346
|
-
expectTypeOf<InferOr<typeof route.inputSchema, undefined>>().toEqualTypeOf<
|
|
347
|
-
{ id: number } | undefined
|
|
348
|
-
>();
|
|
349
|
-
expectTypeOf(route.outputSchema).toEqualTypeOf<typeof OutputSchema | undefined>();
|
|
350
|
-
expectTypeOf(route.errorCodes).toEqualTypeOf<readonly "NOT_FOUND"[] | undefined>();
|
|
351
|
-
expectTypeOf(route.queryParameters).toEqualTypeOf<
|
|
352
|
-
readonly ("page" | "limit")[] | undefined
|
|
353
|
-
>();
|
|
354
|
-
});
|
|
355
|
-
});
|
|
356
|
-
|
|
357
|
-
describe("resolveRouteFactories", () => {
|
|
358
|
-
test("resolveRouteFactories returns correct routes", () => {
|
|
359
|
-
const routeFactory = defineRoutes().create(() => {
|
|
360
|
-
const firstRoute = defineRoute({
|
|
361
|
-
method: "GET",
|
|
362
|
-
path: "/first",
|
|
363
|
-
inputSchema: z.object({ id: z.string() }),
|
|
364
|
-
outputSchema: z.object({ ok: z.boolean() }),
|
|
365
|
-
errorCodes: ["FIRST_NOT_FOUND"],
|
|
366
|
-
queryParameters: ["page", "limit"],
|
|
367
|
-
handler: async (_, { json }) => json({ ok: true }),
|
|
368
|
-
});
|
|
369
|
-
|
|
370
|
-
return [
|
|
371
|
-
firstRoute,
|
|
372
|
-
defineRoute({
|
|
373
|
-
method: "POST",
|
|
374
|
-
path: "/second",
|
|
375
|
-
inputSchema: z.object({ id: z.string() }),
|
|
376
|
-
outputSchema: z.object({ ok: z.boolean() }),
|
|
377
|
-
errorCodes: ["SECOND_NOT_FOUND"],
|
|
378
|
-
queryParameters: ["page", "limit"],
|
|
379
|
-
handler: async (_, { json }) => json({ ok: true }),
|
|
380
|
-
}),
|
|
381
|
-
];
|
|
382
|
-
});
|
|
383
|
-
|
|
384
|
-
type RouteFactoryRoutes =
|
|
385
|
-
typeof routeFactory extends RouteFactory<infer _T1, infer _T2, infer _T3, infer TRoutes>
|
|
386
|
-
? TRoutes
|
|
387
|
-
: never;
|
|
388
|
-
|
|
389
|
-
expectTypeOf<RouteFactoryRoutes[0]["path"]>().toEqualTypeOf<"/first">();
|
|
390
|
-
expectTypeOf<RouteFactoryRoutes[0]["method"]>().toEqualTypeOf<"GET">();
|
|
391
|
-
expectTypeOf<RouteFactoryRoutes[0]["errorCodes"]>().toEqualTypeOf<
|
|
392
|
-
readonly "FIRST_NOT_FOUND"[] | undefined
|
|
393
|
-
>();
|
|
394
|
-
expectTypeOf<RouteFactoryRoutes[0]["queryParameters"]>().toEqualTypeOf<
|
|
395
|
-
readonly ("page" | "limit")[] | undefined
|
|
396
|
-
>();
|
|
397
|
-
|
|
398
|
-
expectTypeOf<RouteFactoryRoutes[1]["path"]>().toEqualTypeOf<"/second">();
|
|
399
|
-
expectTypeOf<RouteFactoryRoutes[1]["method"]>().toEqualTypeOf<"POST">();
|
|
400
|
-
expectTypeOf<RouteFactoryRoutes[1]["errorCodes"]>().toEqualTypeOf<
|
|
401
|
-
readonly "SECOND_NOT_FOUND"[] | undefined
|
|
402
|
-
>();
|
|
403
|
-
expectTypeOf<RouteFactoryRoutes[1]["queryParameters"]>().toEqualTypeOf<
|
|
404
|
-
readonly ("page" | "limit")[] | undefined
|
|
405
|
-
>();
|
|
406
|
-
|
|
407
|
-
const routes = resolveRouteFactories(
|
|
408
|
-
{
|
|
409
|
-
config: {},
|
|
410
|
-
deps: {},
|
|
411
|
-
services: {},
|
|
412
|
-
},
|
|
413
|
-
[routeFactory],
|
|
414
|
-
);
|
|
415
|
-
|
|
416
|
-
const [r1, r2] = routes;
|
|
417
|
-
{
|
|
418
|
-
const { path, method, errorCodes, queryParameters } = r1;
|
|
419
|
-
expectTypeOf(path).toEqualTypeOf<"/first">();
|
|
420
|
-
expectTypeOf(method).toEqualTypeOf<"GET">();
|
|
421
|
-
expectTypeOf(errorCodes).toEqualTypeOf<readonly "FIRST_NOT_FOUND"[] | undefined>();
|
|
422
|
-
expectTypeOf(queryParameters).toEqualTypeOf<readonly ("page" | "limit")[] | undefined>();
|
|
423
|
-
|
|
424
|
-
expect(path).toBe("/first");
|
|
425
|
-
expect(method).toBe("GET");
|
|
426
|
-
expect(errorCodes).toEqual(["FIRST_NOT_FOUND"]);
|
|
427
|
-
expect(queryParameters).toEqual(["page", "limit"]);
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
{
|
|
431
|
-
const { path, method, errorCodes, queryParameters } = r2;
|
|
432
|
-
expectTypeOf(path).toEqualTypeOf<"/second">();
|
|
433
|
-
expectTypeOf(method).toEqualTypeOf<"POST">();
|
|
434
|
-
expectTypeOf(errorCodes).toEqualTypeOf<readonly "SECOND_NOT_FOUND"[] | undefined>();
|
|
435
|
-
expectTypeOf(queryParameters).toEqualTypeOf<readonly ("page" | "limit")[] | undefined>();
|
|
436
|
-
|
|
437
|
-
expect(path).toBe("/second");
|
|
438
|
-
expect(method).toBe("POST");
|
|
439
|
-
expect(errorCodes).toEqual(["SECOND_NOT_FOUND"]);
|
|
440
|
-
expect(queryParameters).toEqual(["page", "limit"]);
|
|
441
|
-
}
|
|
442
|
-
});
|
|
443
|
-
|
|
444
|
-
test("defineRoutes preserves route types with explicit context types", () => {
|
|
445
|
-
type Config = {
|
|
446
|
-
apiKey: string;
|
|
447
|
-
model: "gpt-3" | "gpt-4";
|
|
448
|
-
};
|
|
449
|
-
|
|
450
|
-
type Deps = {
|
|
451
|
-
openai: { complete: (prompt: string) => Promise<string> };
|
|
452
|
-
};
|
|
453
|
-
|
|
454
|
-
type Services = {
|
|
455
|
-
cache: Map<string, unknown>;
|
|
456
|
-
};
|
|
457
|
-
|
|
458
|
-
const routeFactory = defineRoutes<Config, Deps, Services>().create(
|
|
459
|
-
({ config, deps, services }) => {
|
|
460
|
-
expectTypeOf(config).toEqualTypeOf<Config>();
|
|
461
|
-
expectTypeOf(deps).toEqualTypeOf<Deps>();
|
|
462
|
-
expectTypeOf(services).toEqualTypeOf<Services>();
|
|
463
|
-
|
|
464
|
-
return [
|
|
465
|
-
defineRoute({
|
|
466
|
-
method: "POST",
|
|
467
|
-
path: "/complete",
|
|
468
|
-
inputSchema: z.object({ prompt: z.string() }),
|
|
469
|
-
outputSchema: z.object({ result: z.string() }),
|
|
470
|
-
errorCodes: ["RATE_LIMITED"],
|
|
471
|
-
handler: async ({ input }, { json }) => {
|
|
472
|
-
const { prompt } = await input.valid();
|
|
473
|
-
const result = await deps.openai.complete(prompt);
|
|
474
|
-
services.cache.set(prompt, result);
|
|
475
|
-
return json({ result });
|
|
476
|
-
},
|
|
477
|
-
}),
|
|
478
|
-
defineRoute({
|
|
479
|
-
method: "GET",
|
|
480
|
-
path: "/status",
|
|
481
|
-
outputSchema: z.object({ status: z.literal("ok") }),
|
|
482
|
-
handler: async (_, { json }) => json({ status: "ok" }),
|
|
483
|
-
}),
|
|
484
|
-
];
|
|
485
|
-
},
|
|
486
|
-
);
|
|
487
|
-
|
|
488
|
-
type RouteFactoryRoutes =
|
|
489
|
-
typeof routeFactory extends RouteFactory<infer _T1, infer _T2, infer _T3, infer TRoutes>
|
|
490
|
-
? TRoutes
|
|
491
|
-
: never;
|
|
492
|
-
|
|
493
|
-
expectTypeOf<RouteFactoryRoutes[0]["path"]>().toEqualTypeOf<"/complete">();
|
|
494
|
-
expectTypeOf<RouteFactoryRoutes[0]["method"]>().toEqualTypeOf<"POST">();
|
|
495
|
-
expectTypeOf<RouteFactoryRoutes[0]["errorCodes"]>().toEqualTypeOf<
|
|
496
|
-
readonly "RATE_LIMITED"[] | undefined
|
|
497
|
-
>();
|
|
498
|
-
|
|
499
|
-
expectTypeOf<RouteFactoryRoutes[1]["path"]>().toEqualTypeOf<"/status">();
|
|
500
|
-
expectTypeOf<RouteFactoryRoutes[1]["method"]>().toEqualTypeOf<"GET">();
|
|
501
|
-
|
|
502
|
-
const routes = resolveRouteFactories(
|
|
503
|
-
{
|
|
504
|
-
config: { apiKey: "test", model: "gpt-4" as const },
|
|
505
|
-
deps: { openai: { complete: async () => "result" } },
|
|
506
|
-
services: { cache: new Map() },
|
|
507
|
-
},
|
|
508
|
-
[routeFactory],
|
|
509
|
-
);
|
|
510
|
-
|
|
511
|
-
expectTypeOf(routes[0].path).toEqualTypeOf<"/complete">();
|
|
512
|
-
expectTypeOf(routes[1].path).toEqualTypeOf<"/status">();
|
|
513
|
-
});
|
|
514
|
-
});
|
|
515
|
-
|
|
516
|
-
describe("Database Integration", () => {
|
|
517
|
-
test("createFragment without database works without adapter", () => {
|
|
518
|
-
const fragmentDef = defineFragment("test").withDependencies(() => ({
|
|
519
|
-
service: { data: "test" },
|
|
520
|
-
}));
|
|
521
|
-
|
|
522
|
-
const fragment = createFragment(fragmentDef, {}, [], {});
|
|
523
|
-
|
|
524
|
-
expect(fragment.deps.service.data).toBe("test");
|
|
525
|
-
});
|
|
526
|
-
|
|
527
|
-
test("createFragment accepts options parameter", () => {
|
|
528
|
-
const fragmentDef = defineFragment("test").withDependencies(() => ({
|
|
529
|
-
service: { data: "test" },
|
|
530
|
-
}));
|
|
531
|
-
|
|
532
|
-
const fragment = createFragment(fragmentDef, {}, [], { mountRoute: "/custom" });
|
|
533
|
-
|
|
534
|
-
expect(fragment.mountRoute).toBe("/custom");
|
|
535
|
-
});
|
|
536
|
-
});
|
|
537
|
-
});
|