@gaganref/confect-core 10.3.1-fork.8 → 10.3.1-fork.80

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 (89) hide show
  1. package/dist/ActionCall.d.ts +18 -0
  2. package/dist/ActionCall.d.ts.map +1 -0
  3. package/dist/ActionCall.js +21 -0
  4. package/dist/ActionCall.js.map +1 -0
  5. package/dist/App.d.ts +30 -93
  6. package/dist/App.d.ts.map +1 -1
  7. package/dist/App.js +24 -38
  8. package/dist/App.js.map +1 -1
  9. package/dist/Auth.d.ts +33 -9
  10. package/dist/Auth.d.ts.map +1 -1
  11. package/dist/Auth.js +17 -2
  12. package/dist/Auth.js.map +1 -1
  13. package/dist/Callback.d.ts +7 -2
  14. package/dist/Callback.d.ts.map +1 -1
  15. package/dist/Callback.js +11 -6
  16. package/dist/Callback.js.map +1 -1
  17. package/dist/Component.d.ts +98 -2
  18. package/dist/Component.d.ts.map +1 -1
  19. package/dist/Component.js +44 -18
  20. package/dist/Component.js.map +1 -1
  21. package/dist/FunctionSpec.d.ts +365 -2
  22. package/dist/FunctionSpec.d.ts.map +1 -1
  23. package/dist/FunctionSpec.js +66 -3
  24. package/dist/FunctionSpec.js.map +1 -1
  25. package/dist/GenericId.js +2 -2
  26. package/dist/GroupSpec.js +1 -1
  27. package/dist/Http.d.ts +4 -8
  28. package/dist/Http.d.ts.map +1 -1
  29. package/dist/Http.js +15 -22
  30. package/dist/Http.js.map +1 -1
  31. package/dist/ManifestSection.d.ts +70 -0
  32. package/dist/ManifestSection.d.ts.map +1 -0
  33. package/dist/ManifestSection.js +7 -0
  34. package/dist/ManifestSection.js.map +1 -0
  35. package/dist/Mount.d.ts +200 -0
  36. package/dist/Mount.d.ts.map +1 -0
  37. package/dist/Mount.js +116 -0
  38. package/dist/Mount.js.map +1 -0
  39. package/dist/PaginationError.js +1 -1
  40. package/dist/Profile.d.ts +95 -0
  41. package/dist/Profile.d.ts.map +1 -0
  42. package/dist/Profile.js +15 -0
  43. package/dist/Profile.js.map +1 -0
  44. package/dist/Ref.d.ts +4 -3
  45. package/dist/Ref.d.ts.map +1 -1
  46. package/dist/Ref.js +13 -19
  47. package/dist/Ref.js.map +1 -1
  48. package/dist/RefInternal.d.ts +3 -0
  49. package/dist/RefInternal.d.ts.map +1 -0
  50. package/dist/RefInternal.js +16 -0
  51. package/dist/RefInternal.js.map +1 -0
  52. package/dist/SchemaToValidator.d.ts +11 -2
  53. package/dist/SchemaToValidator.d.ts.map +1 -1
  54. package/dist/SchemaToValidator.js +43 -14
  55. package/dist/SchemaToValidator.js.map +1 -1
  56. package/dist/SystemFields.js +3 -3
  57. package/dist/Table.d.ts +48 -9
  58. package/dist/Table.d.ts.map +1 -1
  59. package/dist/Table.js +41 -13
  60. package/dist/Table.js.map +1 -1
  61. package/dist/index.d.ts +4 -0
  62. package/dist/index.d.ts.map +1 -1
  63. package/dist/index.js +10 -6
  64. package/dist/internal/annotations.d.ts +2 -0
  65. package/dist/internal/annotations.d.ts.map +1 -0
  66. package/dist/internal/annotations.js +6 -0
  67. package/dist/internal/annotations.js.map +1 -0
  68. package/dist/internal/scalarDocs.d.ts +5 -0
  69. package/dist/internal/scalarDocs.d.ts.map +1 -0
  70. package/dist/internal/scalarDocs.js +43 -0
  71. package/dist/internal/scalarDocs.js.map +1 -0
  72. package/package.json +3 -3
  73. package/src/ActionCall.ts +25 -0
  74. package/src/App.ts +36 -136
  75. package/src/Auth.ts +54 -11
  76. package/src/Callback.ts +17 -4
  77. package/src/Component.ts +196 -11
  78. package/src/FunctionSpec.ts +566 -5
  79. package/src/Http.ts +15 -37
  80. package/src/ManifestSection.ts +78 -0
  81. package/src/Mount.ts +401 -0
  82. package/src/Profile.ts +141 -0
  83. package/src/Ref.ts +19 -26
  84. package/src/RefInternal.ts +26 -0
  85. package/src/SchemaToValidator.ts +47 -5
  86. package/src/Table.ts +157 -23
  87. package/src/index.ts +4 -0
  88. package/src/internal/annotations.ts +1 -0
  89. package/src/internal/scalarDocs.ts +72 -0
package/src/Profile.ts ADDED
@@ -0,0 +1,141 @@
1
+ import type * as Schema from "effect/Schema";
2
+ import type * as FunctionProvenance from "./FunctionProvenance.js";
3
+ import type * as MiddlewareSpec from "./MiddlewareSpec.js";
4
+
5
+ /**
6
+ * A type-level function from a function's name and args fields to the fields a
7
+ * profile's builders require. Read `this["Name"]` and `this["Args"]` directly
8
+ * in `Type`, passing them to a type alias; a nested object type cannot name
9
+ * `this`.
10
+ */
11
+ export interface Fields {
12
+ readonly Name: string;
13
+ // `unknown`, not `ArgsFields`: an index signature here would leak into
14
+ // `keyof this["Args"]`.
15
+ readonly Args: unknown;
16
+ readonly Type: object;
17
+ }
18
+
19
+ export interface NoFields extends Fields {
20
+ readonly Type: {};
21
+ }
22
+
23
+ export type Apply<
24
+ Fields_ extends Fields,
25
+ Name_ extends string,
26
+ Args_,
27
+ > = (Fields_ & { readonly Name: Name_; readonly Args: Args_ })["Type"];
28
+
29
+ /**
30
+ * A base profile's fields and its extension's, for one function.
31
+ */
32
+ export interface Both<
33
+ Base extends Fields,
34
+ Extension extends Fields,
35
+ > extends Fields {
36
+ readonly Type: Apply<Base, this["Name"], this["Args"]> &
37
+ Apply<Extension, this["Name"], this["Args"]>;
38
+ }
39
+
40
+ /**
41
+ * What a profile callback sees. `args` stays a thunk.
42
+ */
43
+ export interface Info<FieldsType> {
44
+ readonly name: string;
45
+ readonly description?: string;
46
+ readonly args: () => FunctionProvenance.ArgsFields;
47
+ readonly fields: "description" extends keyof FieldsType
48
+ ? Omit<FieldsType, "description">
49
+ : FieldsType;
50
+ }
51
+
52
+ export interface Attachment<
53
+ MiddlewareSpec_ extends MiddlewareSpec.AnyMiddlewareSpec,
54
+ FieldsType,
55
+ > {
56
+ readonly spec: MiddlewareSpec_;
57
+ readonly options: (
58
+ info: Info<FieldsType>,
59
+ ) => MiddlewareSpec.Options<MiddlewareSpec_>;
60
+ }
61
+
62
+ /**
63
+ * A middleware without options has none to compute and is attached at most
64
+ * once, so it joins a profile's list directly.
65
+ */
66
+ type TakesOptions<MiddlewareSpec_ extends MiddlewareSpec.AnyMiddlewareSpec> = [
67
+ MiddlewareSpec.WithoutOptions<MiddlewareSpec_>,
68
+ ] extends [never]
69
+ ? unknown
70
+ : MiddlewareSpec.AttachmentError<`Middleware "${MiddlewareSpec.Key<MiddlewareSpec_>}" takes no options, so list it without Profile.attach`>;
71
+
72
+ /**
73
+ * Attach a middleware that takes options to every function a profile builds,
74
+ * with options computed from each function's fields.
75
+ */
76
+ export const attach = <
77
+ MiddlewareSpec_ extends MiddlewareSpec.AnyMiddlewareSpec,
78
+ FieldsType,
79
+ >(
80
+ spec: MiddlewareSpec_ & TakesOptions<MiddlewareSpec_>,
81
+ options: (
82
+ info: Info<FieldsType>,
83
+ ) => MiddlewareSpec.Options<NoInfer<MiddlewareSpec_>>,
84
+ ): Attachment<MiddlewareSpec_, FieldsType> => ({ spec, options });
85
+
86
+ /**
87
+ * A type function for a schema wrapper, applied to each declared codec.
88
+ */
89
+ export interface SchemaTransform {
90
+ readonly Input: Schema.Codec<unknown, unknown>;
91
+ readonly Type: Schema.Codec<any, any>;
92
+ }
93
+
94
+ /**
95
+ * Args wrappers transform the declared field map, before Struct composition.
96
+ */
97
+ export interface ArgsTransform {
98
+ readonly Input: unknown;
99
+ readonly Type: FunctionProvenance.ArgsFields;
100
+ }
101
+
102
+ export interface Transforms {
103
+ readonly args?: ArgsTransform;
104
+ readonly returns?: SchemaTransform;
105
+ readonly item?: SchemaTransform;
106
+ readonly error?: SchemaTransform;
107
+ }
108
+
109
+ export type Transform<T, Input> = (T & { readonly Input: Input })["Type" &
110
+ keyof T];
111
+
112
+ /**
113
+ * Error wrappers opt into Schema.Never when no error thunk was declared.
114
+ */
115
+ export type Wrappers<T extends Transforms, FieldsType> = {
116
+ readonly [K in keyof T]-?: K extends keyof Transforms
117
+ ? <
118
+ Input extends (K extends "args"
119
+ ? FunctionProvenance.ArgsFields
120
+ : Schema.Codec<any, any>),
121
+ >(
122
+ thunk: () => Input,
123
+ info: Info<FieldsType>,
124
+ ) => () => Transform<T[K], Input>
125
+ : never;
126
+ } & { readonly [K in Exclude<keyof Transforms, keyof T>]?: never };
127
+
128
+ type NonemptyError<T extends Transforms> = T extends {
129
+ readonly error: infer E extends SchemaTransform;
130
+ }
131
+ ? [Transform<E, typeof Schema.Never>["Type"]] extends [never]
132
+ ? MiddlewareSpec.AttachmentError<"Profile error wrapper must add an error to Schema.Never">
133
+ : unknown
134
+ : unknown;
135
+
136
+ export type SchemaConfig<T extends Transforms, FieldsType> = ([
137
+ keyof T,
138
+ ] extends [never]
139
+ ? { readonly schemas?: never }
140
+ : { readonly schemas: Wrappers<T, FieldsType> }) &
141
+ NonemptyError<T>;
package/src/Ref.ts CHANGED
@@ -6,7 +6,7 @@ import type {
6
6
  PaginationOptions,
7
7
  PaginationResult,
8
8
  } from "convex/server";
9
- import { getFunctionAddress, makeFunctionReference } from "convex/server";
9
+ import { getFunctionAddress } from "convex/server";
10
10
  import type { Value } from "convex/values";
11
11
  import { ConvexError } from "convex/values";
12
12
  import * as Effect from "effect/Effect";
@@ -18,6 +18,7 @@ import type * as FunctionProvenance from "./FunctionProvenance.js";
18
18
  import type * as FunctionSpec from "./FunctionSpec.js";
19
19
  import * as Lazy from "./Lazy.js";
20
20
  import * as MiddlewareSpec from "./MiddlewareSpec.js";
21
+ import { getFunctionReference } from "./RefInternal.js";
21
22
  import type * as RuntimeAndFunctionType from "./RuntimeAndFunctionType.js";
22
23
 
23
24
  export const TypeId = "~@confect/core/Ref";
@@ -41,7 +42,9 @@ export interface Base<
41
42
  : (args: Args_) => void;
42
43
  readonly "~Returns": Returns_;
43
44
  readonly "~Error": Error_;
45
+ readonly functionType?: RuntimeAndFunctionType_["functionType"];
44
46
  readonly convexFunctionName: string;
47
+ readonly reservedArgument?: string;
45
48
  /**
46
49
  * Preserved for component references and function handles, which have no
47
50
  * local name.
@@ -447,14 +450,19 @@ export const make = <
447
450
  ({
448
451
  [TypeId]: TypeId,
449
452
  _tag: "Convex",
453
+ functionType: functionSpec.runtimeAndFunctionType.functionType,
450
454
  convexFunctionName,
451
455
  }) as Any,
452
456
  ),
453
457
  Match.tag("Confect", (provenance): Any =>
454
- fromProvenance(convexFunctionName, provenance, [
455
- ...groupMiddlewareAttachments,
456
- ...functionSpec.middlewareAttachments,
457
- ]),
458
+ fromProvenance(
459
+ convexFunctionName,
460
+ provenance,
461
+ [...groupMiddlewareAttachments, ...functionSpec.middlewareAttachments],
462
+ undefined,
463
+ functionSpec.runtimeAndFunctionType.functionType,
464
+ functionSpec.reservedArgument,
465
+ ),
458
466
  ),
459
467
  Match.exhaustive,
460
468
  ) as FromFunctionSpec<
@@ -476,11 +484,15 @@ export const fromProvenance = (
476
484
  provenance: FunctionProvenance.AnyConfect,
477
485
  middlewareAttachments: ReadonlyArray<MiddlewareAttachment.MiddlewareAttachment>,
478
486
  functionReference?: ConvexFunctionReference<any, any>,
487
+ functionType?: "query" | "mutation" | "action",
488
+ reservedArgument?: string,
479
489
  ): AnyConfect => {
480
490
  const ref = {
481
491
  [TypeId]: TypeId as TypeId,
482
492
  _tag: "Confect" as const,
493
+ ...(functionType === undefined ? {} : { functionType }),
483
494
  convexFunctionName,
495
+ ...(reservedArgument === undefined ? {} : { reservedArgument }),
484
496
  ...(functionReference === undefined ? {} : { functionReference }),
485
497
  kind: provenance.kind,
486
498
  middlewareAttachments,
@@ -501,27 +513,6 @@ export const fromProvenance = (
501
513
  export const getConvexFunctionName = (ref: Any): string =>
502
514
  ref.convexFunctionName;
503
515
 
504
- const functionReferenceCache = new Map<string, FunctionReference<Any>>();
505
-
506
- export const getFunctionReference = <Ref_ extends Any>(
507
- ref: Ref_,
508
- ): FunctionReference<Ref_> => {
509
- if (ref.functionReference !== undefined) {
510
- return ref.functionReference as FunctionReference<Ref_>;
511
- }
512
- const convexFunctionName = getConvexFunctionName(ref);
513
-
514
- const cached = functionReferenceCache.get(convexFunctionName);
515
- if (cached !== undefined) {
516
- return cached as FunctionReference<Ref_>;
517
- }
518
-
519
- const functionReference = makeFunctionReference(convexFunctionName);
520
- functionReferenceCache.set(convexFunctionName, functionReference);
521
-
522
- return functionReference as FunctionReference<Ref_>;
523
- };
524
-
525
516
  /**
526
517
  * Adapt a native reference without assigning Confect codecs or typed errors.
527
518
  */
@@ -532,6 +523,7 @@ export const fromFunctionReference = <
532
523
  Returns_,
533
524
  >(
534
525
  functionReference: ConvexFunctionReference<Type, Visibility, Args_, Returns_>,
526
+ functionType?: NoInfer<Type>,
535
527
  ): ConvexRef<
536
528
  Extract<
537
529
  RuntimeAndFunctionType.RuntimeAndFunctionType,
@@ -545,6 +537,7 @@ export const fromFunctionReference = <
545
537
  return {
546
538
  [TypeId]: TypeId,
547
539
  _tag: "Convex",
540
+ ...(functionType === undefined ? {} : { functionType }),
548
541
  convexFunctionName:
549
542
  address.name ??
550
543
  address.reference ??
@@ -0,0 +1,26 @@
1
+ import { makeFunctionReference } from "convex/server";
2
+ import type * as Ref from "./Ref.js";
3
+
4
+ const functionReferenceCache = new Map<
5
+ string,
6
+ Ref.FunctionReference<Ref.Any>
7
+ >();
8
+
9
+ export const getFunctionReference = <Ref_ extends Ref.Any>(
10
+ ref: Ref_,
11
+ ): Ref.FunctionReference<Ref_> => {
12
+ if (ref.functionReference !== undefined) {
13
+ return ref.functionReference as Ref.FunctionReference<Ref_>;
14
+ }
15
+ const convexFunctionName = ref.convexFunctionName;
16
+
17
+ const cached = functionReferenceCache.get(convexFunctionName);
18
+ if (cached !== undefined) {
19
+ return cached as Ref.FunctionReference<Ref_>;
20
+ }
21
+
22
+ const functionReference = makeFunctionReference(convexFunctionName);
23
+ functionReferenceCache.set(convexFunctionName, functionReference);
24
+
25
+ return functionReference as Ref.FunctionReference<Ref_>;
26
+ };
@@ -1,4 +1,6 @@
1
1
  import * as IdScope from "./IdScope.js";
2
+ import type * as Callback from "./Callback.js";
3
+ import { CallbackContract } from "./internal/annotations.js";
2
4
  import type {
3
5
  GenericId as ConvexGenericId,
4
6
  PropertyValidators,
@@ -46,10 +48,24 @@ import type {
46
48
  export const compileArgsSchema = <ConfectValue, ConvexValue>(
47
49
  argsSchema: Schema.Codec<ConfectValue, ConvexValue>,
48
50
  scope: IdScope.IdScope = IdScope.app,
51
+ reservedArgument?: string,
52
+ functionName?: string,
49
53
  ): PropertyValidators => {
50
54
  const ast = Schema.toEncoded(argsSchema).ast;
51
55
 
52
- return pipe(
56
+ const validators = Effect.runSync(compileArgsAst(ast, scope));
57
+ if (reservedArgument === undefined) return validators;
58
+ if (Object.hasOwn(validators, reservedArgument)) {
59
+ throw new ReservedArgumentCollision({
60
+ functionName,
61
+ argument: reservedArgument,
62
+ });
63
+ }
64
+ return { ...validators, [reservedArgument]: v.optional(v.any()) };
65
+ };
66
+
67
+ const compileArgsAst = (ast: SchemaAST.AST, scope: IdScope.IdScope) =>
68
+ pipe(
53
69
  ast,
54
70
  Match.value,
55
71
  Match.tag("Objects", (objectsAst) =>
@@ -58,9 +74,18 @@ export const compileArgsSchema = <ConfectValue, ConvexValue>(
58
74
  : Effect.fail(new IndexSignaturesAreNotSupportedError()),
59
75
  ),
60
76
  Match.orElse(() => Effect.fail(new TopLevelMustBeObjectError())),
61
- Effect.runSync,
62
77
  );
63
- };
78
+
79
+ export class ReservedArgumentCollision extends Data.TaggedError(
80
+ "ReservedArgumentCollision",
81
+ )<{
82
+ readonly functionName: string | undefined;
83
+ readonly argument: string;
84
+ }> {
85
+ override get message() {
86
+ return `${this.functionName} declares the argument "${this.argument}", which its profile reserves. Rename the argument.`;
87
+ }
88
+ }
64
89
 
65
90
  // Returns
66
91
 
@@ -313,6 +338,7 @@ export const compileAst = (
313
338
  scope: IdScope.IdScope = IdScope.app,
314
339
  ): Effect.Effect<
315
340
  Validator<any, any, any>,
341
+ | TopLevelMustBeObjectError
316
342
  | UnsupportedSchemaTypeError
317
343
  | UnsupportedPropertySignatureKeyTypeError
318
344
  | IndexSignaturesAreNotSupportedError
@@ -320,8 +346,23 @@ export const compileAst = (
320
346
  | OptionalTupleElementsAreNotSupportedError
321
347
  | EmptyTupleIsNotSupportedError
322
348
  | EmptyUnionIsNotSupportedError
323
- > =>
324
- isRecursive(ast)
349
+ > => {
350
+ const contract = SchemaAST.resolveAt<Callback.Any>(CallbackContract)(ast);
351
+ if (contract !== undefined) {
352
+ return Effect.gen(function* () {
353
+ const args = yield* compileArgsAst(
354
+ Schema.toEncoded(contract.functionProvenance.args).ast,
355
+ scope,
356
+ );
357
+ const returns = yield* compileAst(
358
+ Schema.toEncoded(contract.functionProvenance.returns).ast,
359
+ false,
360
+ scope,
361
+ );
362
+ return v.function({ fnType: contract.functionType, args, returns });
363
+ });
364
+ }
365
+ return isRecursive(ast)
325
366
  ? Effect.succeed(v.any())
326
367
  : pipe(
327
368
  ast,
@@ -400,6 +441,7 @@ export const compileAst = (
400
441
  ),
401
442
  Match.exhaustive,
402
443
  );
444
+ };
403
445
 
404
446
  const handleUnion = (
405
447
  { types }: SchemaAST.Union,