@confect/core 10.0.0-next.9 → 10.1.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 (103) hide show
  1. package/CHANGELOG.md +964 -47
  2. package/README.md +3 -1
  3. package/dist/FunctionProvenance.d.ts +166 -68
  4. package/dist/FunctionProvenance.d.ts.map +1 -1
  5. package/dist/FunctionProvenance.js +55 -13
  6. package/dist/FunctionProvenance.js.map +1 -1
  7. package/dist/FunctionSpec.d.ts +320 -138
  8. package/dist/FunctionSpec.d.ts.map +1 -1
  9. package/dist/FunctionSpec.js +56 -13
  10. package/dist/FunctionSpec.js.map +1 -1
  11. package/dist/GenericId.d.ts.map +1 -1
  12. package/dist/GenericId.js +1 -1
  13. package/dist/GroupPath.d.ts +6 -10
  14. package/dist/GroupPath.d.ts.map +1 -1
  15. package/dist/GroupPath.js.map +1 -1
  16. package/dist/GroupSpec.d.ts +25 -12
  17. package/dist/GroupSpec.d.ts.map +1 -1
  18. package/dist/GroupSpec.js +51 -14
  19. package/dist/GroupSpec.js.map +1 -1
  20. package/dist/Identifier.d.ts +6 -5
  21. package/dist/Identifier.d.ts.map +1 -1
  22. package/dist/Identifier.js +6 -5
  23. package/dist/Identifier.js.map +1 -1
  24. package/dist/Lazy.d.ts +11 -14
  25. package/dist/Lazy.d.ts.map +1 -1
  26. package/dist/Lazy.js +11 -14
  27. package/dist/Lazy.js.map +1 -1
  28. package/dist/MiddlewareAttachment.d.ts +171 -0
  29. package/dist/MiddlewareAttachment.d.ts.map +1 -0
  30. package/dist/MiddlewareAttachment.js +48 -0
  31. package/dist/MiddlewareAttachment.js.map +1 -0
  32. package/dist/MiddlewareSpec.d.ts +198 -0
  33. package/dist/MiddlewareSpec.d.ts.map +1 -0
  34. package/dist/MiddlewareSpec.js +60 -0
  35. package/dist/MiddlewareSpec.js.map +1 -0
  36. package/dist/PaginationError.d.ts +20 -0
  37. package/dist/PaginationError.d.ts.map +1 -0
  38. package/dist/PaginationError.js +31 -0
  39. package/dist/PaginationError.js.map +1 -0
  40. package/dist/PaginationOptions.d.ts +23 -0
  41. package/dist/PaginationOptions.d.ts.map +1 -0
  42. package/dist/PaginationOptions.js +29 -0
  43. package/dist/PaginationOptions.js.map +1 -0
  44. package/dist/PaginationResult.d.ts +1 -0
  45. package/dist/PaginationResult.d.ts.map +1 -1
  46. package/dist/PaginationResult.js.map +1 -1
  47. package/dist/Ref.d.ts +135 -47
  48. package/dist/Ref.d.ts.map +1 -1
  49. package/dist/Ref.js +104 -46
  50. package/dist/Ref.js.map +1 -1
  51. package/dist/Refs.d.ts +10 -5
  52. package/dist/Refs.d.ts.map +1 -1
  53. package/dist/Refs.js +5 -4
  54. package/dist/Refs.js.map +1 -1
  55. package/dist/SchemaToValidator.d.ts +99 -0
  56. package/dist/SchemaToValidator.d.ts.map +1 -0
  57. package/dist/SchemaToValidator.js +158 -0
  58. package/dist/SchemaToValidator.js.map +1 -0
  59. package/dist/Spec.d.ts +16 -12
  60. package/dist/Spec.d.ts.map +1 -1
  61. package/dist/Spec.js +14 -9
  62. package/dist/Spec.js.map +1 -1
  63. package/dist/SystemFields.d.ts +20 -19
  64. package/dist/SystemFields.d.ts.map +1 -1
  65. package/dist/SystemFields.js +22 -21
  66. package/dist/SystemFields.js.map +1 -1
  67. package/dist/Table.d.ts +75 -0
  68. package/dist/Table.d.ts.map +1 -0
  69. package/dist/Table.js +77 -0
  70. package/dist/Table.js.map +1 -0
  71. package/dist/Types.d.ts +8 -2
  72. package/dist/Types.d.ts.map +1 -1
  73. package/dist/Types.js.map +1 -1
  74. package/dist/UserIdentity.d.ts.map +1 -1
  75. package/dist/index.d.ts +6 -1
  76. package/dist/index.d.ts.map +1 -1
  77. package/dist/index.js +9 -4
  78. package/package.json +17 -15
  79. package/src/FunctionProvenance.ts +197 -34
  80. package/src/FunctionSpec.ts +332 -86
  81. package/src/GroupPath.ts +6 -12
  82. package/src/GroupSpec.ts +184 -25
  83. package/src/Identifier.ts +6 -5
  84. package/src/Lazy.ts +11 -14
  85. package/src/MiddlewareAttachment.ts +106 -0
  86. package/src/MiddlewareSpec.ts +392 -0
  87. package/src/PaginationError.ts +78 -0
  88. package/src/PaginationOptions.ts +23 -0
  89. package/src/PaginationResult.ts +4 -0
  90. package/src/Ref.ts +468 -236
  91. package/src/Refs.ts +43 -18
  92. package/src/SchemaToValidator.ts +612 -0
  93. package/src/Spec.ts +50 -39
  94. package/src/SystemFields.ts +40 -42
  95. package/src/Table.ts +427 -0
  96. package/src/Types.ts +9 -3
  97. package/src/index.ts +6 -1
  98. package/dist/Registry.d.ts +0 -24
  99. package/dist/Registry.d.ts.map +0 -1
  100. package/dist/Registry.js +0 -19
  101. package/dist/Registry.js.map +0 -1
  102. package/dist/tsconfig.src.tsbuildinfo +0 -1
  103. package/src/Registry.ts +0 -30
@@ -1,89 +1,252 @@
1
1
  import type { DefaultFunctionArgs } from "convex/server";
2
- import type { Schema } from "effect";
3
2
  import * as Data from "effect/Data";
3
+ import * as Schema from "effect/Schema";
4
4
  import * as Lazy from "./Lazy";
5
+ import * as PaginationOptions from "./PaginationOptions";
6
+ import * as PaginationResult from "./PaginationResult";
7
+
8
+ /**
9
+ * A field map accepted as Confect function args. Restricting each field to a
10
+ * context-free codec means the struct assembled from the fields can always be
11
+ * encoded and decoded synchronously at a Convex boundary.
12
+ */
13
+ export type ArgsFields = {
14
+ readonly [key: PropertyKey]: Schema.ConstraintCodec<any, any>;
15
+ };
16
+
17
+ /**
18
+ * A struct-shaped Confect args schema that keeps its exact field map while
19
+ * erasing the rest of `Schema.Struct`'s large implementation type. Decoded and
20
+ * encoded args are derived directly from `ArgsFields_`; keeping the runtime
21
+ * codec structural prevents every ref lookup from expanding the full Effect
22
+ * schema type.
23
+ */
24
+ export interface ArgsSchema<
25
+ ArgsFields_ extends ArgsFields,
26
+ > extends Schema.Codec<any, any> {
27
+ readonly fields: ArgsFields_;
28
+ }
29
+
30
+ /**
31
+ * Erased structural view used after the exact field map is no longer needed.
32
+ */
33
+ export type AnyArgsSchema = ArgsSchema<ArgsFields>;
5
34
 
6
35
  export type FunctionProvenance = Data.TaggedEnum<{
7
36
  Confect: {
8
- args: Schema.Codec<any, any>;
37
+ args: AnyArgsSchema;
9
38
  returns: Schema.Codec<any, any>;
10
39
  error?: Schema.Codec<any, any>;
40
+ kind: ConfectKind;
11
41
  };
12
42
  Convex: {
13
- /** @internal */
14
- _args: DefaultFunctionArgs;
15
- /** @internal */
16
- _returns: any;
43
+ "~args": DefaultFunctionArgs;
44
+ "~returns": any;
17
45
  };
18
46
  }>;
19
47
 
48
+ /**
49
+ * The declaration shape of a Confect function—orthogonal to both the provenance
50
+ * origin (Confect vs Convex) and the function type (query/mutation/action). A
51
+ * `Standard` function declares its args fields and returns schema directly; a
52
+ * `Paginated` function declares user-args fields and an item schema from which
53
+ * the Convex-facing `args`/`returns` are composed.
54
+ */
55
+ export type ConfectKind = Standard | Paginated;
56
+
57
+ export interface Standard {
58
+ readonly _tag: "Standard";
59
+ }
60
+
61
+ export interface Paginated<
62
+ UserArgsFields_ extends ArgsFields = ArgsFields,
63
+ Item extends Schema.Codec<any, any> = Schema.Codec<any, any>,
64
+ > {
65
+ readonly _tag: "Paginated";
66
+ /**
67
+ * User-declared args—no `paginationOpts`.
68
+ */
69
+ readonly userArgs: ArgsSchema<UserArgsFields_>;
70
+ /**
71
+ * Page element schema.
72
+ */
73
+ readonly item: Item;
74
+ /**
75
+ * Mutable array of items—the page decode target.
76
+ */
77
+ readonly page: Schema.Codec<any, any>;
78
+ }
79
+
20
80
  export interface Confect<
21
- Args extends Schema.Codec<any, any>,
81
+ ArgsFields_ extends ArgsFields,
22
82
  Returns extends Schema.Codec<any, any>,
23
83
  Error extends Schema.Codec<any, any> = never,
84
+ Kind extends ConfectKind = ConfectKind,
24
85
  > {
25
86
  readonly _tag: "Confect";
26
- readonly args: Args;
87
+ readonly "~ArgsFields": ArgsFields_;
88
+ readonly args: ArgsSchema<ArgsFields_>;
27
89
  readonly returns: Returns;
28
90
  readonly error?: Error;
91
+ readonly kind: Kind;
29
92
  }
30
93
 
31
- export interface AnyConfect extends Confect<
32
- Schema.Codec<any, any>,
33
- Schema.Codec<any, any>,
34
- Schema.Codec<any, any>
35
- > {}
94
+ export interface AnyConfect {
95
+ readonly _tag: "Confect";
96
+ readonly args: AnyArgsSchema;
97
+ readonly returns: Schema.Codec<any, any>;
98
+ readonly error?: Schema.Codec<any, any>;
99
+ readonly kind: ConfectKind;
100
+ }
36
101
 
37
102
  export interface Convex<Args extends DefaultFunctionArgs, Returns> {
38
103
  readonly _tag: "Convex";
39
- readonly _args: Args;
40
- readonly _returns: Returns;
104
+ readonly "~args": Args;
105
+ readonly "~returns": Returns;
41
106
  }
42
107
 
43
108
  export interface AnyConvex extends Convex<DefaultFunctionArgs, any> {}
44
109
 
45
110
  export const FunctionProvenance = Data.taggedEnum<FunctionProvenance>();
46
111
 
112
+ const Standard: Standard = { _tag: "Standard" };
113
+
47
114
  /**
48
- * Build a `Confect` provenance from lazy schema thunks. `args`, `returns`,
49
- * and `error` are exposed as sync lazy memoised getters (via {@link Lazy.defineProperty})
50
- * that only evaluate their thunk on first access, mirroring how `Table`
51
- * defers `Fields`/`Doc`/`tableDefinition`. This keeps importing the assembled
52
- * `_generated/spec.ts` cheap — no `Schema.Struct(...)` / `Schema.Array(...)`
53
- * work runs at module load; it is deferred to the first invocation that
54
- * actually compiles validators or runs a codec.
115
+ * Build a `Confect` provenance from lazy args-fields and schema thunks. `args`,
116
+ * `returns`, and `error` are exposed as sync lazy memoised getters (via
117
+ * {@link Lazy.defineProperty}) that only evaluate their thunk on first access,
118
+ * mirroring how `Table` defers `Fields`/`Doc`. This keeps importing the
119
+ * assembled `_generated/spec.ts` cheap—no
120
+ * `Schema.Struct(...)`/`Schema.Array(...)` work runs at module load; it is
121
+ * deferred to the first invocation that actually compiles validators or runs a
122
+ * codec.
55
123
  *
56
124
  * The object is built by hand rather than through `FunctionProvenance.Confect`
57
125
  * because the `Data` constructor copies its input with `Object.assign`, which
58
- * would force the getters at construction time and defeat the laziness.
59
- * `error` is only installed when an `errorThunk` is provided, so its absence
60
- * is observable via `"error" in provenance` without forcing anything; nothing
126
+ * would force the getters at construction time and defeat the laziness. `error`
127
+ * is only installed when an `errorThunk` is provided, so its absence is
128
+ * observable via `"error" in provenance` without forcing anything; nothing
61
129
  * relies on `Data`'s structural `Equal`/`Hash` for provenance values.
62
130
  */
63
131
  export const Confect = <
64
- Args extends Schema.Codec<any, any>,
132
+ const ArgsFields_ extends ArgsFields,
65
133
  Returns extends Schema.Codec<any, any>,
66
134
  Error extends Schema.Codec<any, any> = never,
67
135
  >(
68
- args: () => Args,
136
+ args: () => ArgsFields_,
69
137
  returns: () => Returns,
70
138
  error?: () => Error,
71
- ): Confect<Args, Returns, Error> => {
72
- const provenance = { _tag: "Confect" as const };
139
+ ): Confect<ArgsFields_, Returns, Error, Standard> => {
140
+ const provenance = { _tag: "Confect" as const, kind: Standard };
73
141
 
74
- Lazy.defineProperty(provenance, "args", args);
142
+ Lazy.defineProperty(provenance, "args", () => Schema.Struct(args()));
75
143
  Lazy.defineProperty(provenance, "returns", returns);
76
144
  if (error !== undefined) {
77
145
  Lazy.defineProperty(provenance, "error", error);
78
146
  }
79
147
 
80
- return provenance as Confect<Args, Returns, Error>;
148
+ return provenance as Confect<ArgsFields_, Returns, Error, Standard>;
149
+ };
150
+
151
+ /**
152
+ * The composed args schema of a paginated query: the user-declared fields plus
153
+ * the `paginationOpts` field managed by Convex's pagination protocol.
154
+ *
155
+ * The fields-first representation keeps the user-declared map available for
156
+ * type-level composition without reconstructing it from an erased schema.
157
+ */
158
+ export type PaginatedArgsFields<UserArgsFields extends ArgsFields> =
159
+ UserArgsFields & {
160
+ readonly paginationOpts: typeof PaginationOptions.PaginationOptions;
161
+ };
162
+
163
+ export type PaginatedArgs<UserArgsFields extends ArgsFields> = Schema.Struct<
164
+ PaginatedArgsFields<UserArgsFields>
165
+ >;
166
+
167
+ /**
168
+ * The composed returns schema of a paginated query: a `PaginationResult` of the
169
+ * item schema. Same deferred-conditional device as {@link PaginatedArgs}.
170
+ */
171
+ export type PaginatedReturns<Item extends Schema.Codec<any, any>> =
172
+ PaginationResult.PaginationResult<Item> extends infer ComposedReturns extends
173
+ Schema.Codec<any, any>
174
+ ? ComposedReturns
175
+ : never;
176
+
177
+ export interface ConfectPaginated<
178
+ UserArgsFields extends ArgsFields,
179
+ Item extends Schema.Codec<any, any>,
180
+ Error extends Schema.Codec<any, any> = never,
181
+ > extends Confect<
182
+ PaginatedArgsFields<UserArgsFields>,
183
+ PaginatedReturns<Item>,
184
+ Error,
185
+ Paginated<UserArgsFields, Item>
186
+ > {}
187
+
188
+ export interface AnyConfectPaginated extends AnyConfect {
189
+ readonly kind: Paginated;
190
+ }
191
+
192
+ /**
193
+ * Build the provenance of a paginated query from lazy schema thunks, with the
194
+ * same laziness contract as {@link Confect}. The user-facing schemas live on the
195
+ * `Paginated` kind (`kind.userArgs`, `kind.item`, `kind.page`)—the kind
196
+ * container is built eagerly (it is cheap) while the schemas inside it stay
197
+ * lazy. The composed Convex-facing `args`/`returns` are derived lazily from
198
+ * them: `args` spreads the user fields plus `paginationOpts`, and `returns`
199
+ * wraps the item in `PaginationResult`. Composition living here means the
200
+ * composed schemas can never drift from the stored user schemas.
201
+ */
202
+ export const ConfectPaginated = <
203
+ const UserArgsFields_ extends ArgsFields,
204
+ Item extends Schema.Codec<any, any>,
205
+ Error extends Schema.Codec<any, any> = never,
206
+ >(
207
+ userArgs: () => UserArgsFields_,
208
+ item: () => Item,
209
+ error?: () => Error,
210
+ ): ConfectPaginated<UserArgsFields_, Item, Error> => {
211
+ const kind = { _tag: "Paginated" as const };
212
+ const paginatedKind = kind as Paginated<UserArgsFields_, Item>;
213
+
214
+ Lazy.defineProperty(kind, "userArgs", () => Schema.Struct(userArgs()));
215
+ Lazy.defineProperty(kind, "item", item);
216
+ Lazy.defineProperty(kind, "page", () =>
217
+ Schema.mutable(Schema.Array(paginatedKind.item)),
218
+ );
219
+
220
+ const provenance = { _tag: "Confect" as const, kind: paginatedKind };
221
+ const self = provenance as ConfectPaginated<UserArgsFields_, Item, Error>;
222
+
223
+ Lazy.defineProperty(provenance, "args", () => {
224
+ const fields = paginatedKind.userArgs.fields;
225
+ if ("paginationOpts" in fields) {
226
+ throw new globalThis.Error(
227
+ "A paginated query's args schema must not declare `paginationOpts`—" +
228
+ "it is added automatically from the `PaginationOptions` schema",
229
+ );
230
+ }
231
+ return Schema.Struct({
232
+ ...fields,
233
+ paginationOpts: PaginationOptions.PaginationOptions,
234
+ });
235
+ });
236
+ Lazy.defineProperty(provenance, "returns", () =>
237
+ PaginationResult.PaginationResult(paginatedKind.item),
238
+ );
239
+ if (error !== undefined) {
240
+ Lazy.defineProperty(provenance, "error", error);
241
+ }
242
+
243
+ return self;
81
244
  };
82
245
 
83
- export const Convex = <_Args extends DefaultFunctionArgs, _Returns>() =>
246
+ export const Convex = <Args extends DefaultFunctionArgs, Returns>() =>
84
247
  FunctionProvenance.Convex(
85
248
  {} as {
86
- _args: _Args;
87
- _returns: _Returns;
249
+ "~args": Args;
250
+ "~returns": Returns;
88
251
  },
89
252
  );