@fragno-dev/core 0.1.11 → 0.2.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.
Files changed (69) hide show
  1. package/.turbo/turbo-build.log +50 -42
  2. package/CHANGELOG.md +51 -0
  3. package/dist/api/api.d.ts +19 -1
  4. package/dist/api/api.d.ts.map +1 -1
  5. package/dist/api/api.js.map +1 -1
  6. package/dist/api/fragment-definition-builder.d.ts +17 -7
  7. package/dist/api/fragment-definition-builder.d.ts.map +1 -1
  8. package/dist/api/fragment-definition-builder.js +3 -2
  9. package/dist/api/fragment-definition-builder.js.map +1 -1
  10. package/dist/api/fragment-instantiator.d.ts +23 -16
  11. package/dist/api/fragment-instantiator.d.ts.map +1 -1
  12. package/dist/api/fragment-instantiator.js +163 -19
  13. package/dist/api/fragment-instantiator.js.map +1 -1
  14. package/dist/api/request-input-context.d.ts +57 -1
  15. package/dist/api/request-input-context.d.ts.map +1 -1
  16. package/dist/api/request-input-context.js +67 -0
  17. package/dist/api/request-input-context.js.map +1 -1
  18. package/dist/api/request-middleware.d.ts +1 -1
  19. package/dist/api/request-middleware.d.ts.map +1 -1
  20. package/dist/api/request-middleware.js.map +1 -1
  21. package/dist/api/route.d.ts +7 -7
  22. package/dist/api/route.d.ts.map +1 -1
  23. package/dist/api/route.js.map +1 -1
  24. package/dist/client/client.d.ts +4 -3
  25. package/dist/client/client.d.ts.map +1 -1
  26. package/dist/client/client.js +103 -7
  27. package/dist/client/client.js.map +1 -1
  28. package/dist/client/vue.d.ts +7 -3
  29. package/dist/client/vue.d.ts.map +1 -1
  30. package/dist/client/vue.js +16 -1
  31. package/dist/client/vue.js.map +1 -1
  32. package/dist/internal/trace-context.d.ts +23 -0
  33. package/dist/internal/trace-context.d.ts.map +1 -0
  34. package/dist/internal/trace-context.js +14 -0
  35. package/dist/internal/trace-context.js.map +1 -0
  36. package/dist/mod-client.d.ts +3 -17
  37. package/dist/mod-client.d.ts.map +1 -1
  38. package/dist/mod-client.js +20 -10
  39. package/dist/mod-client.js.map +1 -1
  40. package/dist/mod.d.ts +3 -2
  41. package/dist/mod.js +2 -1
  42. package/dist/runtime.d.ts +15 -0
  43. package/dist/runtime.d.ts.map +1 -0
  44. package/dist/runtime.js +33 -0
  45. package/dist/runtime.js.map +1 -0
  46. package/dist/test/test.d.ts +2 -2
  47. package/dist/test/test.d.ts.map +1 -1
  48. package/dist/test/test.js.map +1 -1
  49. package/package.json +23 -17
  50. package/src/api/api.ts +22 -0
  51. package/src/api/fragment-definition-builder.ts +36 -17
  52. package/src/api/fragment-instantiator.test.ts +286 -0
  53. package/src/api/fragment-instantiator.ts +338 -31
  54. package/src/api/internal/path-runtime.test.ts +7 -0
  55. package/src/api/request-input-context.test.ts +152 -0
  56. package/src/api/request-input-context.ts +85 -0
  57. package/src/api/request-middleware.test.ts +47 -1
  58. package/src/api/request-middleware.ts +1 -1
  59. package/src/api/route.ts +7 -2
  60. package/src/client/client.test.ts +195 -0
  61. package/src/client/client.ts +185 -10
  62. package/src/client/vue.test.ts +253 -3
  63. package/src/client/vue.ts +44 -1
  64. package/src/internal/trace-context.ts +35 -0
  65. package/src/mod-client.ts +51 -7
  66. package/src/mod.ts +6 -1
  67. package/src/runtime.ts +48 -0
  68. package/src/test/test.ts +13 -4
  69. package/tsdown.config.ts +1 -0
package/src/mod-client.ts CHANGED
@@ -35,7 +35,7 @@ export function defineFragment(_name: string) {
35
35
  name: _name,
36
36
  };
37
37
 
38
- const stub = {
38
+ const stubMethods = {
39
39
  withDependencies: () => stub,
40
40
  providesBaseService: () => stub,
41
41
  providesService: () => stub,
@@ -48,7 +48,21 @@ export function defineFragment(_name: string) {
48
48
  withLinkedFragment: () => stub,
49
49
  extend: () => stub,
50
50
  build: () => definitionStub,
51
+ // From fragno-db
52
+ provideHooks: () => stub,
51
53
  };
54
+
55
+ // Wrap with Proxy to handle any additional methods (e.g. from extend())
56
+ const stub: object = new Proxy(stubMethods, {
57
+ get(target, prop) {
58
+ if (prop in target) {
59
+ return target[prop as keyof typeof target];
60
+ }
61
+ // Return a function that returns the stub for method chaining
62
+ return () => stub;
63
+ },
64
+ });
65
+
52
66
  return stub;
53
67
  }
54
68
 
@@ -58,7 +72,7 @@ export { FragmentDefinitionBuilder } from "./api/fragment-definition-builder";
58
72
  // Stub implementation for instantiate
59
73
  // This is stripped by unplugin-fragno in browser builds
60
74
  export function instantiate(_definition: unknown) {
61
- const fragmentStub: IFragnoInstantiatedFragment = {
75
+ const fragmentStubMethods = {
62
76
  [instantiatedFragmentFakeSymbol]: instantiatedFragmentFakeSymbol,
63
77
  name: "",
64
78
  routes: [],
@@ -71,10 +85,7 @@ export function instantiate(_definition: unknown) {
71
85
  linkedFragments: {},
72
86
  };
73
87
  },
74
- withMiddleware: () => {
75
- // throw new Error("withMiddleware is not supported in browser builds");
76
- return fragmentStub;
77
- },
88
+ withMiddleware: () => fragmentStub,
78
89
  inContext: <T>(callback: () => T) => callback(),
79
90
  handlersFor: () => ({}),
80
91
  handler: async () => new Response(),
@@ -82,7 +93,21 @@ export function instantiate(_definition: unknown) {
82
93
  callRouteRaw: async () => new Response(),
83
94
  };
84
95
 
85
- const builderStub: IFragmentInstantiationBuilder = {
96
+ // Wrap with Proxy to handle any additional methods
97
+ const fragmentStub: IFragnoInstantiatedFragment = new Proxy(
98
+ fragmentStubMethods as IFragnoInstantiatedFragment,
99
+ {
100
+ get(target, prop) {
101
+ if (prop in target) {
102
+ return target[prop as keyof typeof target];
103
+ }
104
+ // Return a function that returns the stub for method chaining
105
+ return () => fragmentStub;
106
+ },
107
+ },
108
+ );
109
+
110
+ const builderStubMethods = {
86
111
  withConfig: () => builderStub,
87
112
  withRoutes: () => builderStub,
88
113
  withOptions: () => builderStub,
@@ -94,6 +119,20 @@ export function instantiate(_definition: unknown) {
94
119
  options: undefined,
95
120
  };
96
121
 
122
+ // Wrap with Proxy to handle any additional methods
123
+ const builderStub: IFragmentInstantiationBuilder = new Proxy(
124
+ builderStubMethods as IFragmentInstantiationBuilder,
125
+ {
126
+ get(target, prop) {
127
+ if (prop in target) {
128
+ return target[prop as keyof typeof target];
129
+ }
130
+ // Return a function that returns the stub for method chaining
131
+ return () => builderStub;
132
+ },
133
+ },
134
+ );
135
+
97
136
  return builderStub;
98
137
  }
99
138
 
@@ -102,6 +141,11 @@ export function instantiate(_definition: unknown) {
102
141
  // ============================================================================
103
142
  export type { FragnoPublicConfig } from "./api/shared-types";
104
143
 
144
+ // ============================================================================
145
+ // Runtime
146
+ // ============================================================================
147
+ export { defaultFragnoRuntime, type FragnoRuntime } from "./runtime";
148
+
105
149
  // ============================================================================
106
150
  // Route Definition
107
151
  // ============================================================================
package/src/mod.ts CHANGED
@@ -25,6 +25,11 @@ export {
25
25
  // ============================================================================
26
26
  export type { FragnoPublicConfig } from "./api/shared-types";
27
27
 
28
+ // ============================================================================
29
+ // Runtime
30
+ // ============================================================================
31
+ export { defaultFragnoRuntime, type FragnoRuntime } from "./runtime";
32
+
28
33
  // ============================================================================
29
34
  // Route Definition
30
35
  // ============================================================================
@@ -35,4 +40,4 @@ export {
35
40
  type RouteFactoryContext,
36
41
  } from "./api/route";
37
42
 
38
- export { type FragnoRouteConfig, type RequestThisContext } from "./api/api";
43
+ export { type FragnoRouteConfig, type RequestThisContext, type RouteContentType } from "./api/api";
package/src/runtime.ts ADDED
@@ -0,0 +1,48 @@
1
+ import { createId } from "@paralleldrive/cuid2";
2
+
3
+ export type FragnoRuntime = {
4
+ time: {
5
+ now: () => Date;
6
+ };
7
+ random: {
8
+ float: () => number;
9
+ uuid: () => string;
10
+ cuid: () => string;
11
+ };
12
+ };
13
+
14
+ const fallbackUuid = () => {
15
+ const bytes = new Uint8Array(16);
16
+ for (let i = 0; i < bytes.length; i += 1) {
17
+ bytes[i] = Math.floor(Math.random() * 256);
18
+ }
19
+
20
+ // RFC 4122 variant + version 4
21
+ bytes[6] = (bytes[6] & 0x0f) | 0x40;
22
+ bytes[8] = (bytes[8] & 0x3f) | 0x80;
23
+
24
+ const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0"));
25
+ return [
26
+ hex.slice(0, 4).join(""),
27
+ hex.slice(4, 6).join(""),
28
+ hex.slice(6, 8).join(""),
29
+ hex.slice(8, 10).join(""),
30
+ hex.slice(10, 16).join(""),
31
+ ].join("-");
32
+ };
33
+
34
+ const defaultUuid = () => {
35
+ const cryptoApi = globalThis.crypto;
36
+ return cryptoApi?.randomUUID ? cryptoApi.randomUUID() : fallbackUuid();
37
+ };
38
+
39
+ export const defaultFragnoRuntime: FragnoRuntime = {
40
+ time: {
41
+ now: () => new Date(),
42
+ },
43
+ random: {
44
+ float: () => Math.random(),
45
+ uuid: () => defaultUuid(),
46
+ cuid: () => createId(),
47
+ },
48
+ };
package/src/test/test.ts CHANGED
@@ -6,7 +6,12 @@ import {
6
6
  type FragmentDefinition,
7
7
  type ServiceConstructorFn,
8
8
  } from "../api/fragment-definition-builder";
9
- import { instantiateFragment, type FragnoInstantiatedFragment } from "../api/fragment-instantiator";
9
+ import {
10
+ instantiateFragment,
11
+ type AnyFragnoInstantiatedFragment,
12
+ type FragnoInstantiatedFragment,
13
+ type RoutesWithInternal,
14
+ } from "../api/fragment-instantiator";
10
15
  import type { BoundServices } from "../api/bind-services";
11
16
 
12
17
  // Re-export for convenience
@@ -200,6 +205,7 @@ export function createFragmentForTest<
200
205
  THandlerThisContext extends RequestThisContext,
201
206
  TRequestStorage,
202
207
  const TRoutesOrFactories extends readonly AnyRouteOrFactory[],
208
+ TLinkedFragments extends Record<string, AnyFragnoInstantiatedFragment> = {},
203
209
  >(
204
210
  definition: FragmentDefinition<
205
211
  TConfig,
@@ -211,17 +217,20 @@ export function createFragmentForTest<
211
217
  TPrivateServices,
212
218
  TServiceThisContext,
213
219
  THandlerThisContext,
214
- TRequestStorage
220
+ TRequestStorage,
221
+ TLinkedFragments
215
222
  >,
216
223
  routesOrFactories: TRoutesOrFactories,
217
224
  options: CreateFragmentForTestOptions<TConfig, TOptions, TServiceDependencies>,
218
225
  ): FragnoInstantiatedFragment<
219
- FlattenRouteFactories<TRoutesOrFactories>,
226
+ RoutesWithInternal<FlattenRouteFactories<TRoutesOrFactories>, TLinkedFragments>,
220
227
  TDeps,
221
228
  BoundServices<TBaseServices & TServices>,
222
229
  TServiceThisContext,
223
230
  THandlerThisContext,
224
- TRequestStorage
231
+ TRequestStorage,
232
+ TOptions,
233
+ TLinkedFragments
225
234
  > {
226
235
  const { config, options: fragmentOptions = {} as TOptions, serviceImplementations } = options;
227
236
 
package/tsdown.config.ts CHANGED
@@ -22,6 +22,7 @@ export default defineConfig({
22
22
  "./src/integrations/react-ssr.ts",
23
23
  "./src/integrations/svelte-kit.ts",
24
24
  "./src/test/test.ts",
25
+ "./src/internal/trace-context.ts",
25
26
  "./src/internal/symbols.ts",
26
27
  ],
27
28
  dts: true,