@confect/core 10.0.0-next.2 → 10.0.0-next.20

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