@confect/core 10.0.0-next.18 → 10.0.0-next.19

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,72 @@
1
+ import { ConvexError } from "convex/values";
2
+ import type { Value } from "convex/values";
3
+ import * as Match from "effect/Match";
4
+ import * as Option from "effect/Option";
5
+ import * as Schema from "effect/Schema";
6
+
7
+ /** A Convex pagination cursor that no longer describes the current query. */
8
+ export class InvalidCursor extends Schema.TaggedError<InvalidCursor>()(
9
+ "InvalidCursor",
10
+ {
11
+ cause: Schema.Defect(),
12
+ },
13
+ ) {}
14
+
15
+ interface InvalidCursorData {
16
+ readonly isConvexSystemError: true;
17
+ readonly paginationError: "InvalidCursor";
18
+ }
19
+
20
+ const ConvexErrorIdentifier = Symbol.for("ConvexError");
21
+
22
+ const isConvexError = (error: unknown): error is ConvexError<Value> =>
23
+ error instanceof ConvexError ||
24
+ (typeof error === "object" &&
25
+ error !== null &&
26
+ ConvexErrorIdentifier in error);
27
+
28
+ const isInvalidCursorData = (value: unknown): value is InvalidCursorData =>
29
+ typeof value === "object" &&
30
+ value !== null &&
31
+ "isConvexSystemError" in value &&
32
+ value.isConvexSystemError === true &&
33
+ "paginationError" in value &&
34
+ value.paginationError === "InvalidCursor";
35
+
36
+ const fromErrorMessage = (
37
+ error: globalThis.Error,
38
+ ): Option.Option<InvalidCursor> =>
39
+ error.message.includes("InvalidCursor")
40
+ ? Option.some(new InvalidCursor({ cause: error }))
41
+ : Option.none();
42
+
43
+ /** Recognizes an invalid-cursor error emitted by a Convex query. */
44
+ export const fromConvexQueryError = (
45
+ error: unknown,
46
+ ): Option.Option<InvalidCursor> =>
47
+ Match.value(error).pipe(
48
+ Match.when(isConvexError, (convexError) =>
49
+ Match.value(isInvalidCursorData(convexError.data)).pipe(
50
+ Match.when(true, () =>
51
+ Option.some(new InvalidCursor({ cause: error })),
52
+ ),
53
+ Match.when(false, () => fromErrorMessage(convexError)),
54
+ Match.exhaustive,
55
+ ),
56
+ ),
57
+ Match.when(Match.instanceOf(globalThis.Error), fromErrorMessage),
58
+ Match.when(Match.any, () => Option.none<InvalidCursor>()),
59
+ Match.exhaustive,
60
+ );
61
+
62
+ /** Recognizes the error data attached to Convex's invalid-cursor error. */
63
+ export const fromConvexErrorData = (
64
+ errorData: unknown,
65
+ ): Option.Option<InvalidCursor> =>
66
+ Match.value(errorData).pipe(
67
+ Match.when(isInvalidCursorData, (cause) =>
68
+ Option.some(new InvalidCursor({ cause })),
69
+ ),
70
+ Match.when(Match.any, () => Option.none<InvalidCursor>()),
71
+ Match.exhaustive,
72
+ );
package/src/Ref.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import type {
2
+ DefaultFunctionArgs,
2
3
  FunctionReference as ConvexFunctionReference,
3
4
  FunctionVisibility,
4
5
  PaginationOptions,
@@ -20,7 +21,7 @@ import type * as RuntimeAndFunctionType from "./RuntimeAndFunctionType";
20
21
  export interface Base<
21
22
  RuntimeAndFunctionType_ extends RuntimeAndFunctionType.RuntimeAndFunctionType,
22
23
  FunctionVisibility_ extends FunctionVisibility,
23
- Args_,
24
+ Args_ extends DefaultFunctionArgs,
24
25
  Returns_,
25
26
  Error_ = never,
26
27
  > {
@@ -29,7 +30,6 @@ export interface Base<
29
30
  readonly "~Args": Args_;
30
31
  readonly "~Returns": Returns_;
31
32
  readonly "~Error": Error_;
32
- /** @internal */
33
33
  readonly convexFunctionName: string;
34
34
  }
35
35
 
@@ -41,13 +41,14 @@ export interface Base<
41
41
  export type Ref<
42
42
  RuntimeAndFunctionType_ extends RuntimeAndFunctionType.RuntimeAndFunctionType,
43
43
  FunctionVisibility_ extends FunctionVisibility,
44
- Args_,
44
+ Args_ extends DefaultFunctionArgs,
45
45
  Returns_,
46
46
  Error_ = never,
47
47
  > =
48
- | ConfectRef<
48
+ | ConfectRefWithTypes<
49
49
  RuntimeAndFunctionType_,
50
50
  FunctionVisibility_,
51
+ FunctionProvenance.ArgsFields,
51
52
  Args_,
52
53
  Returns_,
53
54
  Error_
@@ -60,12 +61,35 @@ export type Ref<
60
61
  Error_
61
62
  >;
62
63
 
63
- export interface ConfectRef<
64
+ export type ConfectRef<
64
65
  RuntimeAndFunctionType_ extends RuntimeAndFunctionType.RuntimeAndFunctionType,
65
66
  FunctionVisibility_ extends FunctionVisibility,
66
- Args_,
67
+ ArgsFields_ extends FunctionProvenance.ArgsFields,
68
+ ReturnsSchema_ extends Schema.Codec<any, any>,
69
+ ErrorSchema_ extends Schema.Codec<any, any> = never,
70
+ MiddlewareError_ = never,
71
+ > = ConfectRefWithTypes<
72
+ RuntimeAndFunctionType_,
73
+ FunctionVisibility_,
74
+ ArgsFields_,
75
+ Schema.Struct.Type<ArgsFields_>,
76
+ ReturnsSchema_["Type"],
77
+ ErrorSchema_["Type"] | MiddlewareError_
78
+ >;
79
+
80
+ /**
81
+ * Internal form used while mapping a spec to a ref. Supplying the already
82
+ * derived value types avoids repeatedly expanding `Schema.Struct` for every
83
+ * leaf in a generated ref tree; the public `ConfectRef` alias still derives
84
+ * the same values from its fields and schemas.
85
+ */
86
+ interface ConfectRefWithTypes<
87
+ RuntimeAndFunctionType_ extends RuntimeAndFunctionType.RuntimeAndFunctionType,
88
+ FunctionVisibility_ extends FunctionVisibility,
89
+ ArgsFields_ extends FunctionProvenance.ArgsFields,
90
+ Args_ extends DefaultFunctionArgs,
67
91
  Returns_,
68
- Error_ = never,
92
+ Error_,
69
93
  > extends Base<
70
94
  RuntimeAndFunctionType_,
71
95
  FunctionVisibility_,
@@ -73,24 +97,18 @@ export interface ConfectRef<
73
97
  Returns_,
74
98
  Error_
75
99
  > {
76
- /** @internal */
77
100
  readonly _tag: "Confect";
78
- /** @internal */
79
- readonly args: Schema.Codec<any, any>;
80
- /** @internal */
101
+ readonly args: FunctionProvenance.ArgsSchema<ArgsFields_>;
81
102
  readonly returns: Schema.Codec<any, any>;
82
- /** @internal */
83
103
  readonly kind: FunctionProvenance.ConfectKind;
84
- /** @internal */
85
104
  readonly middlewareSpecs: ReadonlyArray<MiddlewareSpec.AnyMiddlewareSpec>;
86
- /** @internal */
87
105
  readonly error?: Schema.Codec<any, any>;
88
106
  }
89
107
 
90
108
  export interface ConvexRef<
91
109
  RuntimeAndFunctionType_ extends RuntimeAndFunctionType.RuntimeAndFunctionType,
92
110
  FunctionVisibility_ extends FunctionVisibility,
93
- Args_,
111
+ Args_ extends DefaultFunctionArgs,
94
112
  Returns_,
95
113
  Error_ = never,
96
114
  > extends Base<
@@ -100,12 +118,14 @@ export interface ConvexRef<
100
118
  Returns_,
101
119
  Error_
102
120
  > {
103
- /** @internal */
104
121
  readonly _tag: "Convex";
105
122
  }
106
123
 
107
124
  export type Any = Ref<any, any, any, any, any>;
108
125
 
126
+ /** Erased Confect-provenance ref used by provenance-specific APIs. */
127
+ export type AnyConfect = Extract<Any, { readonly _tag: "Confect" }>;
128
+
109
129
  export type AnyInternal = Ref<any, "internal", any, any, any>;
110
130
 
111
131
  export type AnyPublic = Ref<any, "public", any, any, any>;
@@ -118,16 +138,17 @@ export type AnyQuery = Ref<
118
138
  any
119
139
  >;
120
140
 
121
- export type AnyPublicPaginatedQuery = Ref<
122
- RuntimeAndFunctionType.AnyQuery,
123
- "public",
124
- {
125
- [key: string]: any;
126
- paginationOpts: PaginationOptions;
127
- },
128
- PaginationResult<any>,
129
- any
130
- >;
141
+ export type AnyPaginatedQuery = AnyQuery &
142
+ Base<
143
+ RuntimeAndFunctionType.AnyQuery,
144
+ FunctionVisibility,
145
+ {
146
+ [key: string]: any;
147
+ paginationOpts: PaginationOptions;
148
+ },
149
+ PaginationResult<any>,
150
+ any
151
+ >;
131
152
 
132
153
  export type AnyMutation = Ref<
133
154
  RuntimeAndFunctionType.AnyMutation,
@@ -153,6 +174,13 @@ export type AnyPublicQuery = Ref<
153
174
  any
154
175
  >;
155
176
 
177
+ export type AnyConfectPublicQuery = Extract<AnyPublicQuery, AnyConfect>;
178
+
179
+ export type AnyPublicPaginatedQuery = AnyPublicQuery & AnyPaginatedQuery;
180
+
181
+ export type AnyConfectPublicPaginatedQuery = AnyConfectPublicQuery &
182
+ AnyPublicPaginatedQuery;
183
+
156
184
  export type AnyPublicMutation = Ref<
157
185
  RuntimeAndFunctionType.AnyMutation,
158
186
  "public",
@@ -161,6 +189,8 @@ export type AnyPublicMutation = Ref<
161
189
  any
162
190
  >;
163
191
 
192
+ export type AnyConfectPublicMutation = Extract<AnyPublicMutation, AnyConfect>;
193
+
164
194
  export type AnyPublicAction = Ref<
165
195
  RuntimeAndFunctionType.AnyAction,
166
196
  "public",
@@ -169,6 +199,8 @@ export type AnyPublicAction = Ref<
169
199
  any
170
200
  >;
171
201
 
202
+ export type AnyConfectPublicAction = Extract<AnyPublicAction, AnyConfect>;
203
+
172
204
  export type GetRuntimeAndFunctionType<Ref_> = Ref_ extends {
173
205
  readonly "~RuntimeAndFunctionType": infer RuntimeAndFunctionType_ extends
174
206
  RuntimeAndFunctionType.RuntimeAndFunctionType;
@@ -194,6 +226,24 @@ export type Args<Ref_> = Ref_ extends { readonly "~Args": infer Args_ }
194
226
  ? Args_
195
227
  : never;
196
228
 
229
+ /** The field map captured by a Confect-provenance ref. */
230
+ export type ArgsFields<Ref_> = Ref_ extends {
231
+ readonly _tag: "Confect";
232
+ readonly args: {
233
+ readonly fields: infer ArgsFields_ extends FunctionProvenance.ArgsFields;
234
+ };
235
+ }
236
+ ? ArgsFields_
237
+ : never;
238
+
239
+ /** The assembled args schema carried by a Confect-provenance ref. */
240
+ export type ArgsSchema<Ref_> = Ref_ extends {
241
+ readonly _tag: "Confect";
242
+ readonly args: infer ArgsSchema_ extends FunctionProvenance.AnyArgsSchema;
243
+ }
244
+ ? ArgsSchema_
245
+ : never;
246
+
197
247
  export type OptionalArgs<Ref_ extends Any> = keyof Args<Ref_> extends never
198
248
  ? [args?: Args<Ref_>]
199
249
  : [args: Args<Ref_>];
@@ -218,6 +268,7 @@ export type FromFunctionSpec<
218
268
  FunctionSpec_,
219
269
  FunctionSpec.GetRuntimeAndFunctionType<FunctionSpec_>,
220
270
  FunctionSpec.GetFunctionVisibility<FunctionSpec_>,
271
+ FunctionSpec.ArgsFields<FunctionSpec_>,
221
272
  FunctionSpec.Args<FunctionSpec_>,
222
273
  FunctionSpec.Returns<FunctionSpec_>,
223
274
  FunctionSpec.Error<FunctionSpec_> | MiddlewareError
@@ -227,7 +278,8 @@ type FromFunctionSpecHelper<
227
278
  FunctionSpec_ extends FunctionSpec.AnyWithProps,
228
279
  RuntimeAndFunctionType_ extends RuntimeAndFunctionType.RuntimeAndFunctionType,
229
280
  FunctionVisibility_ extends FunctionVisibility,
230
- Args_,
281
+ ArgsFields_ extends FunctionProvenance.ArgsFields,
282
+ Args_ extends DefaultFunctionArgs,
231
283
  Returns_,
232
284
  Error_,
233
285
  > =
@@ -238,17 +290,18 @@ type FromFunctionSpecHelper<
238
290
  ? ConvexRef<
239
291
  RuntimeAndFunctionType_,
240
292
  FunctionVisibility_,
241
- Args_,
293
+ Args_ extends DefaultFunctionArgs ? Args_ : never,
242
294
  Returns_,
243
- Error_
295
+ never
244
296
  >
245
297
  : FunctionSpec_ extends FunctionSpec.WithFunctionProvenance<
246
298
  FunctionSpec_,
247
299
  FunctionProvenance.AnyConfect
248
300
  >
249
- ? ConfectRef<
301
+ ? ConfectRefWithTypes<
250
302
  RuntimeAndFunctionType_,
251
303
  FunctionVisibility_,
304
+ ArgsFields_,
252
305
  Args_,
253
306
  Returns_,
254
307
  Error_
@@ -517,14 +570,12 @@ const missingPaginatedProvenanceError = (ref: Any) =>
517
570
  "those constructors store. Define the function as, e.g.:\n\n" +
518
571
  " FunctionSpec.publicPaginatedQuery({\n" +
519
572
  ' name: "...",\n' +
520
- " args: () => Schema.Struct({ ... }), // optional; without paginationOpts\n" +
573
+ " args: () => ({ ... }), // optional; without paginationOpts\n" +
521
574
  " item: () => ItemSchema,\n" +
522
575
  " })",
523
576
  );
524
577
 
525
- const paginatedKind = (
526
- ref: ConfectRef<any, any, any, any, any>,
527
- ): FunctionProvenance.Paginated =>
578
+ const paginatedKind = (ref: AnyConfect): FunctionProvenance.Paginated =>
528
579
  Match.value(ref.kind).pipe(
529
580
  Match.tag("Paginated", (kind) => kind),
530
581
  Match.tag("Standard", () => {
package/src/Refs.ts CHANGED
@@ -42,6 +42,7 @@ type FunctionSpecMatchesPredicate<
42
42
  FunctionSpec.GetRuntimeAndFunctionType<FunctionSpec_>,
43
43
  FunctionSpec.GetFunctionVisibility<FunctionSpec_>,
44
44
  any,
45
+ any,
45
46
  any
46
47
  > extends Predicate
47
48
  ? true
package/src/index.ts CHANGED
@@ -6,6 +6,7 @@ export * as GroupSpec from "./GroupSpec";
6
6
  export * as Identifier from "./Identifier";
7
7
  export * as Lazy from "./Lazy";
8
8
  export * as MiddlewareSpec from "./MiddlewareSpec";
9
+ export * as PaginationError from "./PaginationError";
9
10
  export * as PaginationOptions from "./PaginationOptions";
10
11
  export * as PaginationResult from "./PaginationResult";
11
12
  export * as Ref from "./Ref";