@confect/core 10.0.0-next.2 → 10.0.0-next.21
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.
- package/CHANGELOG.md +531 -0
- package/README.md +3 -1
- package/dist/FunctionProvenance.d.ts +155 -65
- package/dist/FunctionProvenance.d.ts.map +1 -1
- package/dist/FunctionProvenance.js +51 -10
- package/dist/FunctionProvenance.js.map +1 -1
- package/dist/FunctionSpec.d.ts +314 -138
- package/dist/FunctionSpec.d.ts.map +1 -1
- package/dist/FunctionSpec.js +50 -13
- package/dist/FunctionSpec.js.map +1 -1
- package/dist/GenericId.d.ts.map +1 -1
- package/dist/GenericId.js +1 -1
- package/dist/GroupPath.d.ts +1 -4
- package/dist/GroupPath.d.ts.map +1 -1
- package/dist/GroupPath.js.map +1 -1
- package/dist/GroupSpec.d.ts +16 -10
- package/dist/GroupSpec.d.ts.map +1 -1
- package/dist/GroupSpec.js +34 -11
- package/dist/GroupSpec.js.map +1 -1
- package/dist/Identifier.d.ts.map +1 -1
- package/dist/Lazy.d.ts +2 -3
- package/dist/Lazy.d.ts.map +1 -1
- package/dist/Lazy.js +2 -3
- package/dist/Lazy.js.map +1 -1
- package/dist/MiddlewareSpec.d.ts +184 -0
- package/dist/MiddlewareSpec.d.ts.map +1 -0
- package/dist/MiddlewareSpec.js +55 -0
- package/dist/MiddlewareSpec.js.map +1 -0
- package/dist/PaginationError.d.ts +14 -0
- package/dist/PaginationError.d.ts.map +1 -0
- package/dist/PaginationError.js +25 -0
- package/dist/PaginationError.js.map +1 -0
- package/dist/PaginationOptions.d.ts +23 -0
- package/dist/PaginationOptions.d.ts.map +1 -0
- package/dist/PaginationOptions.js +29 -0
- package/dist/PaginationOptions.js.map +1 -0
- package/dist/PaginationResult.d.ts +1 -0
- package/dist/PaginationResult.d.ts.map +1 -1
- package/dist/PaginationResult.js.map +1 -1
- package/dist/Ref.d.ts +120 -39
- package/dist/Ref.d.ts.map +1 -1
- package/dist/Ref.js +94 -38
- package/dist/Ref.js.map +1 -1
- package/dist/Refs.d.ts +9 -4
- package/dist/Refs.d.ts.map +1 -1
- package/dist/Refs.js +3 -3
- package/dist/Refs.js.map +1 -1
- package/dist/SchemaToValidator.d.ts +99 -0
- package/dist/SchemaToValidator.d.ts.map +1 -0
- package/dist/SchemaToValidator.js +156 -0
- package/dist/SchemaToValidator.js.map +1 -0
- package/dist/Spec.d.ts +3 -2
- package/dist/Spec.d.ts.map +1 -1
- package/dist/Spec.js +1 -1
- package/dist/Spec.js.map +1 -1
- package/dist/SystemFields.d.ts +1 -1
- package/dist/SystemFields.d.ts.map +1 -1
- package/dist/SystemFields.js +2 -2
- package/dist/SystemFields.js.map +1 -1
- package/dist/Table.d.ts +74 -0
- package/dist/Table.d.ts.map +1 -0
- package/dist/Table.js +77 -0
- package/dist/Table.js.map +1 -0
- package/dist/Types.d.ts +8 -2
- package/dist/Types.d.ts.map +1 -1
- package/dist/Types.js.map +1 -1
- package/dist/UserIdentity.d.ts.map +1 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -3
- package/package.json +15 -14
- package/src/FunctionProvenance.ts +186 -31
- package/src/FunctionSpec.ts +313 -85
- package/src/GroupPath.ts +1 -6
- package/src/GroupSpec.ts +125 -18
- package/src/Lazy.ts +2 -3
- package/src/MiddlewareSpec.ts +356 -0
- package/src/PaginationError.ts +72 -0
- package/src/PaginationOptions.ts +23 -0
- package/src/PaginationResult.ts +4 -0
- package/src/Ref.ts +429 -208
- package/src/Refs.ts +41 -16
- package/src/SchemaToValidator.ts +603 -0
- package/src/Spec.ts +4 -4
- package/src/SystemFields.ts +6 -9
- package/src/Table.ts +426 -0
- package/src/Types.ts +9 -3
- package/src/index.ts +5 -1
- package/dist/Registry.d.ts +0 -24
- package/dist/Registry.d.ts.map +0 -1
- package/dist/Registry.js +0 -19
- package/dist/Registry.js.map +0 -1
- package/dist/tsconfig.src.tsbuildinfo +0 -1
- package/src/Registry.ts +0 -30
package/dist/Ref.d.ts
CHANGED
|
@@ -1,53 +1,107 @@
|
|
|
1
|
-
import type { FunctionReference as ConvexFunctionReference, FunctionVisibility } from "convex/server";
|
|
1
|
+
import type { DefaultFunctionArgs, FunctionReference as ConvexFunctionReference, FunctionVisibility, PaginationOptions, PaginationResult } from "convex/server";
|
|
2
2
|
import type { Value } from "convex/values";
|
|
3
3
|
import { ConvexError } from "convex/values";
|
|
4
4
|
import * as Effect from "effect/Effect";
|
|
5
5
|
import * as Option from "effect/Option";
|
|
6
6
|
import * as Schema from "effect/Schema";
|
|
7
|
+
import type * as FunctionProvenance from "./FunctionProvenance";
|
|
7
8
|
import type * as FunctionSpec from "./FunctionSpec";
|
|
9
|
+
import * as MiddlewareSpec from "./MiddlewareSpec";
|
|
8
10
|
import type * as RuntimeAndFunctionType from "./RuntimeAndFunctionType";
|
|
9
|
-
export interface
|
|
10
|
-
readonly
|
|
11
|
-
readonly
|
|
12
|
-
readonly
|
|
13
|
-
readonly
|
|
14
|
-
readonly
|
|
11
|
+
export interface Base<RuntimeAndFunctionType_ extends RuntimeAndFunctionType.RuntimeAndFunctionType, FunctionVisibility_ extends FunctionVisibility, Args_ extends DefaultFunctionArgs, Returns_, Error_ = never> {
|
|
12
|
+
readonly "~RuntimeAndFunctionType": RuntimeAndFunctionType_;
|
|
13
|
+
readonly "~FunctionVisibility": FunctionVisibility_;
|
|
14
|
+
readonly "~Args": Args_;
|
|
15
|
+
readonly "~Returns": Returns_;
|
|
16
|
+
readonly "~Error": Error_;
|
|
17
|
+
readonly convexFunctionName: string;
|
|
15
18
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
export
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
19
|
+
/**
|
|
20
|
+
* A reference to a single Convex function, as callers see it: the wire name
|
|
21
|
+
* plus the data needed to encode args, decode returns, and decode typed
|
|
22
|
+
* errors. A ref is one of two shapes, keyed by the spec's provenance.
|
|
23
|
+
*/
|
|
24
|
+
export type Ref<RuntimeAndFunctionType_ extends RuntimeAndFunctionType.RuntimeAndFunctionType, FunctionVisibility_ extends FunctionVisibility, Args_ extends DefaultFunctionArgs, Returns_, Error_ = never> = ConfectRefWithTypes<RuntimeAndFunctionType_, FunctionVisibility_, FunctionProvenance.ArgsFields, Args_, Returns_, Error_> | ConvexRef<RuntimeAndFunctionType_, FunctionVisibility_, Args_, Returns_, Error_>;
|
|
25
|
+
export type ConfectRef<RuntimeAndFunctionType_ extends RuntimeAndFunctionType.RuntimeAndFunctionType, FunctionVisibility_ extends FunctionVisibility, ArgsFields_ extends FunctionProvenance.ArgsFields, ReturnsSchema_ extends Schema.Codec<any, any>, ErrorSchema_ extends Schema.Codec<any, any> = never, MiddlewareError_ = never> = ConfectRefWithTypes<RuntimeAndFunctionType_, FunctionVisibility_, ArgsFields_, Schema.Struct.Type<ArgsFields_>, ReturnsSchema_["Type"], ErrorSchema_["Type"] | MiddlewareError_>;
|
|
26
|
+
/**
|
|
27
|
+
* Internal form used while mapping a spec to a ref. Supplying the already
|
|
28
|
+
* derived value types avoids repeatedly expanding `Schema.Struct` for every
|
|
29
|
+
* leaf in a generated ref tree; the public `ConfectRef` alias still derives
|
|
30
|
+
* the same values from its fields and schemas.
|
|
31
|
+
*/
|
|
32
|
+
interface ConfectRefWithTypes<RuntimeAndFunctionType_ extends RuntimeAndFunctionType.RuntimeAndFunctionType, FunctionVisibility_ extends FunctionVisibility, ArgsFields_ extends FunctionProvenance.ArgsFields, Args_ extends DefaultFunctionArgs, Returns_, Error_> extends Base<RuntimeAndFunctionType_, FunctionVisibility_, Args_, Returns_, Error_> {
|
|
33
|
+
readonly _tag: "Confect";
|
|
34
|
+
readonly args: FunctionProvenance.ArgsSchema<ArgsFields_>;
|
|
35
|
+
readonly returns: Schema.Codec<any, any>;
|
|
36
|
+
readonly kind: FunctionProvenance.ConfectKind;
|
|
37
|
+
readonly middlewareSpecs: ReadonlyArray<MiddlewareSpec.AnyMiddlewareSpec>;
|
|
38
|
+
readonly error?: Schema.Codec<any, any>;
|
|
31
39
|
}
|
|
32
|
-
export interface
|
|
40
|
+
export interface ConvexRef<RuntimeAndFunctionType_ extends RuntimeAndFunctionType.RuntimeAndFunctionType, FunctionVisibility_ extends FunctionVisibility, Args_ extends DefaultFunctionArgs, Returns_, Error_ = never> extends Base<RuntimeAndFunctionType_, FunctionVisibility_, Args_, Returns_, Error_> {
|
|
41
|
+
readonly _tag: "Convex";
|
|
33
42
|
}
|
|
34
|
-
export type
|
|
35
|
-
|
|
36
|
-
export type
|
|
37
|
-
|
|
38
|
-
|
|
43
|
+
export type Any = Ref<any, any, any, any, any>;
|
|
44
|
+
/** Erased Confect-provenance ref used by provenance-specific APIs. */
|
|
45
|
+
export type AnyConfect = Extract<Any, {
|
|
46
|
+
readonly _tag: "Confect";
|
|
47
|
+
}>;
|
|
48
|
+
export type AnyInternal = Ref<any, "internal", any, any, any>;
|
|
49
|
+
export type AnyPublic = Ref<any, "public", any, any, any>;
|
|
50
|
+
export type AnyQuery = Ref<RuntimeAndFunctionType.AnyQuery, FunctionVisibility, any, any, any>;
|
|
51
|
+
export type AnyPaginatedQuery = AnyQuery & Base<RuntimeAndFunctionType.AnyQuery, FunctionVisibility, {
|
|
52
|
+
[key: string]: any;
|
|
53
|
+
paginationOpts: PaginationOptions;
|
|
54
|
+
}, PaginationResult<any>, any>;
|
|
55
|
+
export type AnyMutation = Ref<RuntimeAndFunctionType.AnyMutation, FunctionVisibility, any, any, any>;
|
|
56
|
+
export type AnyAction = Ref<RuntimeAndFunctionType.AnyAction, FunctionVisibility, any, any, any>;
|
|
57
|
+
export type AnyPublicQuery = Ref<RuntimeAndFunctionType.AnyQuery, "public", any, any, any>;
|
|
58
|
+
export type AnyConfectPublicQuery = Extract<AnyPublicQuery, AnyConfect>;
|
|
59
|
+
export type AnyPublicPaginatedQuery = AnyPublicQuery & AnyPaginatedQuery;
|
|
60
|
+
export type AnyConfectPublicPaginatedQuery = AnyConfectPublicQuery & AnyPublicPaginatedQuery;
|
|
61
|
+
export type AnyPublicMutation = Ref<RuntimeAndFunctionType.AnyMutation, "public", any, any, any>;
|
|
62
|
+
export type AnyConfectPublicMutation = Extract<AnyPublicMutation, AnyConfect>;
|
|
63
|
+
export type AnyPublicAction = Ref<RuntimeAndFunctionType.AnyAction, "public", any, any, any>;
|
|
64
|
+
export type AnyConfectPublicAction = Extract<AnyPublicAction, AnyConfect>;
|
|
65
|
+
export type GetRuntimeAndFunctionType<Ref_> = Ref_ extends {
|
|
66
|
+
readonly "~RuntimeAndFunctionType": infer RuntimeAndFunctionType_ extends RuntimeAndFunctionType.RuntimeAndFunctionType;
|
|
67
|
+
} ? RuntimeAndFunctionType_ : never;
|
|
68
|
+
export type GetRuntime<Ref_> = RuntimeAndFunctionType.GetRuntime<GetRuntimeAndFunctionType<Ref_>>;
|
|
69
|
+
export type GetFunctionType<Ref_> = RuntimeAndFunctionType.GetFunctionType<GetRuntimeAndFunctionType<Ref_>>;
|
|
70
|
+
export type GetFunctionVisibility<Ref_> = Ref_ extends {
|
|
71
|
+
readonly "~FunctionVisibility": infer FunctionVisibility_;
|
|
72
|
+
} ? FunctionVisibility_ : never;
|
|
73
|
+
export type Args<Ref_> = Ref_ extends {
|
|
74
|
+
readonly "~Args": infer Args_;
|
|
75
|
+
} ? Args_ : never;
|
|
76
|
+
/** The field map captured by a Confect-provenance ref. */
|
|
77
|
+
export type ArgsFields<Ref_> = Ref_ extends {
|
|
78
|
+
readonly _tag: "Confect";
|
|
79
|
+
readonly args: {
|
|
80
|
+
readonly fields: infer ArgsFields_ extends FunctionProvenance.ArgsFields;
|
|
81
|
+
};
|
|
82
|
+
} ? ArgsFields_ : never;
|
|
83
|
+
/** The assembled args schema carried by a Confect-provenance ref. */
|
|
84
|
+
export type ArgsSchema<Ref_> = Ref_ extends {
|
|
85
|
+
readonly _tag: "Confect";
|
|
86
|
+
readonly args: infer ArgsSchema_ extends FunctionProvenance.AnyArgsSchema;
|
|
87
|
+
} ? ArgsSchema_ : never;
|
|
39
88
|
export type OptionalArgs<Ref_ extends Any> = keyof Args<Ref_> extends never ? [args?: Args<Ref_>] : [args: Args<Ref_>];
|
|
40
|
-
export type Returns<Ref_> = Ref_ extends
|
|
41
|
-
|
|
89
|
+
export type Returns<Ref_> = Ref_ extends {
|
|
90
|
+
readonly "~Returns": infer Returns_;
|
|
91
|
+
} ? Returns_ : never;
|
|
92
|
+
export type Error<Ref_> = Ref_ extends {
|
|
93
|
+
readonly "~Error": infer Error_;
|
|
94
|
+
} ? Error_ : never;
|
|
42
95
|
export type FunctionReference<Ref_ extends Any> = ConvexFunctionReference<GetFunctionType<Ref_>, GetFunctionVisibility<Ref_>>;
|
|
43
|
-
export type FromFunctionSpec<FunctionSpec_ extends FunctionSpec.AnyWithProps> =
|
|
96
|
+
export type FromFunctionSpec<FunctionSpec_ extends FunctionSpec.AnyWithProps, MiddlewareError = never> = FromFunctionSpecHelper<FunctionSpec_, FunctionSpec.GetRuntimeAndFunctionType<FunctionSpec_>, FunctionSpec.GetFunctionVisibility<FunctionSpec_>, FunctionSpec.ArgsFields<FunctionSpec_>, FunctionSpec.Args<FunctionSpec_>, FunctionSpec.Returns<FunctionSpec_>, FunctionSpec.Error<FunctionSpec_> | MiddlewareError>;
|
|
97
|
+
type FromFunctionSpecHelper<FunctionSpec_ extends FunctionSpec.AnyWithProps, RuntimeAndFunctionType_ extends RuntimeAndFunctionType.RuntimeAndFunctionType, FunctionVisibility_ extends FunctionVisibility, ArgsFields_ extends FunctionProvenance.ArgsFields, Args_ extends DefaultFunctionArgs, Returns_, Error_> = FunctionSpec_ extends FunctionSpec.WithFunctionProvenance<FunctionSpec_, FunctionProvenance.AnyConvex> ? ConvexRef<RuntimeAndFunctionType_, FunctionVisibility_, Args_ extends DefaultFunctionArgs ? Args_ : never, Returns_, never> : FunctionSpec_ extends FunctionSpec.WithFunctionProvenance<FunctionSpec_, FunctionProvenance.AnyConfect> ? ConfectRefWithTypes<RuntimeAndFunctionType_, FunctionVisibility_, ArgsFields_, Args_, Returns_, Error_> : Ref<RuntimeAndFunctionType_, FunctionVisibility_, Args_, Returns_, Error_>;
|
|
44
98
|
export declare const make: <FunctionSpec_ extends FunctionSpec.AnyWithProps>(
|
|
45
99
|
/**
|
|
46
100
|
* The namespace portion of a Convex function name, i.e. the part before the
|
|
47
101
|
* colon. For example, for `myGroupDir/myGroupMod:myFunc` this would be
|
|
48
102
|
* `myGroupDir/myGroupMod`.
|
|
49
103
|
*/
|
|
50
|
-
|
|
104
|
+
convexFunctionNamespace: string, functionSpec: FunctionSpec_, groupMiddlewareSpecs?: ReadonlyArray<MiddlewareSpec.AnyMiddlewareSpec>) => FromFunctionSpec<FunctionSpec_>;
|
|
51
105
|
export declare const getConvexFunctionName: (ref: Any) => string;
|
|
52
106
|
export declare const getFunctionReference: <Ref_ extends Any>(ref: Ref_) => FunctionReference<Ref_>;
|
|
53
107
|
export declare const hasErrorSchema: (ref: Any) => boolean;
|
|
@@ -69,19 +123,45 @@ export declare const isConvexError: (error: unknown) => error is ConvexError<Val
|
|
|
69
123
|
*/
|
|
70
124
|
export declare const decodeErrorOrElse: <Ref_ extends Any, E>(ref: Ref_, mapUnknownError: (error: unknown) => E) => (error: unknown) => Error<Ref_> | E;
|
|
71
125
|
/**
|
|
72
|
-
* Decode `encodedError` against the ref's error schema
|
|
73
|
-
*
|
|
74
|
-
*
|
|
126
|
+
* Decode `encodedError` against the ref's error schema — the function's
|
|
127
|
+
* declared `error` schema unioned with its covering middlewares' error
|
|
128
|
+
* schemas. Returns `None` if the ref declares no typed error at all (Confect
|
|
129
|
+
* ref without an `error` schema and without failing middleware, or a
|
|
130
|
+
* Convex-provenance ref)—by definition there's nothing to decode the value
|
|
75
131
|
* into, and the caller is responsible for deciding what to do (typically:
|
|
76
132
|
* surface the original value as a defect).
|
|
77
133
|
*/
|
|
78
134
|
export declare const decodeError: <Ref_ extends Any>(ref: Ref_, encodedError: unknown) => Effect.Effect<Option.Option<Error<Ref_>>, Schema.SchemaError>;
|
|
79
135
|
/**
|
|
80
|
-
* Synchronous counterpart to `decodeError`.
|
|
81
|
-
*
|
|
136
|
+
* Synchronous counterpart to `decodeError`. Returns `None` when the value is
|
|
137
|
+
* not this ref's typed error — either because the ref declares no `error`
|
|
138
|
+
* schema, or because `encodedError` doesn't match the one it declares.
|
|
139
|
+
*
|
|
140
|
+
* The second case is reachable in normal operation: Convex raises its own
|
|
141
|
+
* `ConvexError`s (an `InvalidCursor` pagination error, for instance), and
|
|
142
|
+
* those never match a user-declared error schema. Callers pair this with a
|
|
143
|
+
* fallback that surfaces the original error, so failing to decode must not
|
|
144
|
+
* throw — a `ParseError` here would replace the real error with an opaque one
|
|
145
|
+
* and lose the only useful diagnostic. Hence the `Option` suffix rather than
|
|
146
|
+
* `Sync`, matching `Schema.decodeUnknownOption`: the sibling `*Sync` helpers
|
|
147
|
+
* in this module all throw on a parse failure, and this one deliberately
|
|
148
|
+
* doesn't.
|
|
149
|
+
*/
|
|
150
|
+
export declare const decodeErrorOption: <Ref_ extends Any>(ref: Ref_, encodedError: unknown) => Option.Option<Error<Ref_>>;
|
|
151
|
+
/**
|
|
152
|
+
* Encode the args of a paginated query ref via its user-args schema —
|
|
153
|
+
* `paginationOpts` is excluded, since the pagination protocol fields are
|
|
154
|
+
* managed by the client (e.g. `usePaginatedQuery` from `convex/react`), not by
|
|
155
|
+
* the caller. Requires a ref built with `FunctionSpec.publicPaginatedQuery`
|
|
156
|
+
* (or `internalPaginatedQuery`).
|
|
157
|
+
*/
|
|
158
|
+
export declare const encodePaginatedQueryArgsSync: <Ref_ extends AnyPublicPaginatedQuery>(ref: Ref_, args: Omit<Args<Ref_>, "paginationOpts">) => unknown;
|
|
159
|
+
/**
|
|
160
|
+
* Decode a page of a paginated query's results via the ref's item schema.
|
|
161
|
+
* Requires a ref built with `FunctionSpec.publicPaginatedQuery` (or
|
|
162
|
+
* `internalPaginatedQuery`).
|
|
82
163
|
*/
|
|
83
|
-
export declare const
|
|
84
|
-
export declare const maybeDecodeErrorSync: <Ref_ extends Any>(ref: Ref_, error: unknown) => unknown;
|
|
164
|
+
export declare const decodePaginationPageSync: <Ref_ extends AnyPublicPaginatedQuery>(ref: Ref_, encodedPage: unknown) => Returns<Ref_>["page"];
|
|
85
165
|
/**
|
|
86
166
|
* Encode args via the ref's args schema, invoke `call`, decode returns via the
|
|
87
167
|
* ref's returns schema, and translate any thrown `ConvexError` into the ref's
|
|
@@ -91,4 +171,5 @@ export declare const maybeDecodeErrorSync: <Ref_ extends Any>(ref: Ref_, error:
|
|
|
91
171
|
* no handler is provided.
|
|
92
172
|
*/
|
|
93
173
|
export declare const runWithCodec: <Ref_ extends Any, E = never>(ref: Ref_, args: Args<Ref_>, call: (functionReference: FunctionReference<Ref_>, encodedArgs: unknown) => PromiseLike<unknown>, mapUnknownError?: (error: unknown) => E) => Effect.Effect<Returns<Ref_>, E | Error<Ref_> | Schema.SchemaError>;
|
|
174
|
+
export {};
|
|
94
175
|
//# sourceMappingURL=Ref.d.ts.map
|
package/dist/Ref.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Ref.d.ts","sourceRoot":"","sources":["../src/Ref.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,iBAAiB,IAAI,uBAAuB,EAC5C,kBAAkB,
|
|
1
|
+
{"version":3,"file":"Ref.d.ts","sourceRoot":"","sources":["../src/Ref.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,mBAAmB,EACnB,iBAAiB,IAAI,uBAAuB,EAC5C,kBAAkB,EAClB,iBAAiB,EACjB,gBAAgB,EACjB,MAAM,eAAe,CAAC;AAEvB,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,C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,GAAG,KAAK,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,WAAW,CAyBhE,CAAC"}
|
package/dist/Ref.js
CHANGED
|
@@ -1,50 +1,67 @@
|
|
|
1
1
|
import { __exportAll } from "./_virtual/_rolldown/runtime.js";
|
|
2
|
-
import
|
|
2
|
+
import { defineProperty } from "./Lazy.js";
|
|
3
|
+
import { errorSchemas } from "./MiddlewareSpec.js";
|
|
3
4
|
import * as Schema from "effect/Schema";
|
|
4
|
-
import
|
|
5
|
+
import * as Option from "effect/Option";
|
|
5
6
|
import { ConvexError } from "convex/values";
|
|
6
|
-
import * as Effect from "effect/Effect";
|
|
7
7
|
import * as Match from "effect/Match";
|
|
8
|
+
import { makeFunctionReference } from "convex/server";
|
|
9
|
+
import * as Effect from "effect/Effect";
|
|
8
10
|
//#region src/Ref.ts
|
|
9
11
|
var Ref_exports = /* @__PURE__ */ __exportAll({
|
|
10
12
|
decodeArgsSync: () => decodeArgsSync,
|
|
11
13
|
decodeError: () => decodeError,
|
|
14
|
+
decodeErrorOption: () => decodeErrorOption,
|
|
12
15
|
decodeErrorOrElse: () => decodeErrorOrElse,
|
|
13
|
-
|
|
16
|
+
decodePaginationPageSync: () => decodePaginationPageSync,
|
|
14
17
|
decodeReturns: () => decodeReturns,
|
|
15
18
|
decodeReturnsSync: () => decodeReturnsSync,
|
|
16
19
|
encodeArgs: () => encodeArgs,
|
|
17
20
|
encodeArgsSync: () => encodeArgsSync,
|
|
21
|
+
encodePaginatedQueryArgsSync: () => encodePaginatedQueryArgsSync,
|
|
18
22
|
encodeReturnsSync: () => encodeReturnsSync,
|
|
19
23
|
getConvexFunctionName: () => getConvexFunctionName,
|
|
20
24
|
getFunctionReference: () => getFunctionReference,
|
|
21
25
|
hasErrorSchema: () => hasErrorSchema,
|
|
22
26
|
isConvexError: () => isConvexError,
|
|
23
27
|
make: () => make,
|
|
24
|
-
maybeDecodeErrorSync: () => maybeDecodeErrorSync,
|
|
25
28
|
runWithCodec: () => runWithCodec
|
|
26
29
|
});
|
|
27
|
-
const make = (
|
|
28
|
-
functionSpec
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
const make = (convexFunctionNamespace, functionSpec, groupMiddlewareSpecs = []) => {
|
|
31
|
+
const convexFunctionName = `${convexFunctionNamespace}:${functionSpec.name}`;
|
|
32
|
+
return Match.value(functionSpec.functionProvenance).pipe(Match.tag("Convex", () => ({
|
|
33
|
+
_tag: "Convex",
|
|
34
|
+
convexFunctionName
|
|
35
|
+
})), Match.tag("Confect", (provenance) => {
|
|
36
|
+
const ref = {
|
|
37
|
+
_tag: "Confect",
|
|
38
|
+
convexFunctionName,
|
|
39
|
+
kind: provenance.kind,
|
|
40
|
+
middlewareSpecs: [...groupMiddlewareSpecs, ...functionSpec.middlewareSpecs]
|
|
41
|
+
};
|
|
42
|
+
defineProperty(ref, "args", () => provenance.args);
|
|
43
|
+
defineProperty(ref, "returns", () => provenance.returns);
|
|
44
|
+
if ("error" in provenance) defineProperty(ref, "error", () => provenance.error);
|
|
45
|
+
return ref;
|
|
46
|
+
}), Match.exhaustive);
|
|
47
|
+
};
|
|
48
|
+
const getConvexFunctionName = (ref) => ref.convexFunctionName;
|
|
32
49
|
const functionReferenceCache = /* @__PURE__ */ new Map();
|
|
33
50
|
const getFunctionReference = (ref) => {
|
|
34
|
-
const
|
|
35
|
-
const cached = functionReferenceCache.get(
|
|
51
|
+
const convexFunctionName = getConvexFunctionName(ref);
|
|
52
|
+
const cached = functionReferenceCache.get(convexFunctionName);
|
|
36
53
|
if (cached !== void 0) return cached;
|
|
37
|
-
const functionReference = makeFunctionReference(
|
|
38
|
-
functionReferenceCache.set(
|
|
54
|
+
const functionReference = makeFunctionReference(convexFunctionName);
|
|
55
|
+
functionReferenceCache.set(convexFunctionName, functionReference);
|
|
39
56
|
return functionReference;
|
|
40
57
|
};
|
|
41
|
-
const hasErrorSchema = (ref) => Match.value(ref
|
|
42
|
-
const encodeArgs = (ref, args) => Match.value(ref
|
|
43
|
-
const decodeReturns = (ref, returns) => Match.value(ref
|
|
44
|
-
const encodeArgsSync = (ref, args) => Match.value(ref
|
|
45
|
-
const decodeArgsSync = (ref, encodedArgs) => Match.value(ref
|
|
46
|
-
const encodeReturnsSync = (ref, returns) => Match.value(ref
|
|
47
|
-
const decodeReturnsSync = (ref, encodedReturns) => Match.value(ref
|
|
58
|
+
const hasErrorSchema = (ref) => Match.value(ref).pipe(Match.tag("Confect", (confectRef) => "error" in confectRef || confectRef.middlewareSpecs.some((middlewareSpec) => "error" in middlewareSpec)), Match.tag("Convex", () => false), Match.exhaustive);
|
|
59
|
+
const encodeArgs = (ref, args) => Match.value(ref).pipe(Match.tag("Confect", (confectRef) => Schema.encodeEffect(confectRef.args)(args)), Match.tag("Convex", () => Effect.succeed(args)), Match.exhaustive);
|
|
60
|
+
const decodeReturns = (ref, returns) => Match.value(ref).pipe(Match.tag("Confect", (confectRef) => Schema.decodeUnknownEffect(confectRef.returns)(returns)), Match.tag("Convex", () => Effect.succeed(returns)), Match.exhaustive);
|
|
61
|
+
const encodeArgsSync = (ref, args) => Match.value(ref).pipe(Match.tag("Confect", (confectRef) => Schema.encodeSync(confectRef.args)(args)), Match.tag("Convex", () => args), Match.exhaustive);
|
|
62
|
+
const decodeArgsSync = (ref, encodedArgs) => Match.value(ref).pipe(Match.tag("Confect", (confectRef) => Schema.decodeUnknownSync(confectRef.args)(encodedArgs)), Match.tag("Convex", () => encodedArgs), Match.exhaustive);
|
|
63
|
+
const encodeReturnsSync = (ref, returns) => Match.value(ref).pipe(Match.tag("Confect", (confectRef) => Schema.encodeSync(confectRef.returns)(returns)), Match.tag("Convex", () => returns), Match.exhaustive);
|
|
64
|
+
const decodeReturnsSync = (ref, encodedReturns) => Match.value(ref).pipe(Match.tag("Confect", (confectRef) => Schema.decodeUnknownSync(confectRef.returns)(encodedReturns)), Match.tag("Convex", () => encodedReturns), Match.exhaustive);
|
|
48
65
|
const ConvexErrorIdentifier = Symbol.for("ConvexError");
|
|
49
66
|
const isConvexError = (error) => error instanceof ConvexError || typeof error === "object" && error !== null && ConvexErrorIdentifier in error;
|
|
50
67
|
/**
|
|
@@ -58,25 +75,68 @@ const isConvexError = (error) => error instanceof ConvexError || typeof error ==
|
|
|
58
75
|
*/
|
|
59
76
|
const decodeErrorOrElse = (ref, mapUnknownError) => (error) => {
|
|
60
77
|
if (isConvexError(error)) {
|
|
61
|
-
const decoded =
|
|
78
|
+
const decoded = decodeErrorOption(ref, error.data);
|
|
62
79
|
if (Option.isSome(decoded)) return decoded.value;
|
|
63
80
|
}
|
|
64
81
|
return mapUnknownError(error);
|
|
65
82
|
};
|
|
83
|
+
const errorSchemaOf = (ref) => Match.value(ref).pipe(Match.tag("Confect", (confectRef) => {
|
|
84
|
+
const schemas = [..."error" in confectRef && confectRef.error !== void 0 ? [confectRef.error] : [], ...errorSchemas(confectRef.middlewareSpecs)];
|
|
85
|
+
return schemas.length === 0 ? Option.none() : Option.some(schemas.length === 1 ? schemas[0] : Schema.Union(schemas));
|
|
86
|
+
}), Match.tag("Convex", () => Option.none()), Match.exhaustive);
|
|
66
87
|
/**
|
|
67
|
-
* Decode `encodedError` against the ref's error schema
|
|
68
|
-
*
|
|
69
|
-
*
|
|
88
|
+
* Decode `encodedError` against the ref's error schema — the function's
|
|
89
|
+
* declared `error` schema unioned with its covering middlewares' error
|
|
90
|
+
* schemas. Returns `None` if the ref declares no typed error at all (Confect
|
|
91
|
+
* ref without an `error` schema and without failing middleware, or a
|
|
92
|
+
* Convex-provenance ref)—by definition there's nothing to decode the value
|
|
70
93
|
* into, and the caller is responsible for deciding what to do (typically:
|
|
71
94
|
* surface the original value as a defect).
|
|
72
95
|
*/
|
|
73
|
-
const decodeError = (ref, encodedError) =>
|
|
96
|
+
const decodeError = (ref, encodedError) => Option.match(errorSchemaOf(ref), {
|
|
97
|
+
onNone: () => Effect.succeed(Option.none()),
|
|
98
|
+
onSome: (schema) => Effect.map(Schema.decodeUnknownEffect(schema)(encodedError), Option.some)
|
|
99
|
+
});
|
|
100
|
+
/**
|
|
101
|
+
* Synchronous counterpart to `decodeError`. Returns `None` when the value is
|
|
102
|
+
* not this ref's typed error — either because the ref declares no `error`
|
|
103
|
+
* schema, or because `encodedError` doesn't match the one it declares.
|
|
104
|
+
*
|
|
105
|
+
* The second case is reachable in normal operation: Convex raises its own
|
|
106
|
+
* `ConvexError`s (an `InvalidCursor` pagination error, for instance), and
|
|
107
|
+
* those never match a user-declared error schema. Callers pair this with a
|
|
108
|
+
* fallback that surfaces the original error, so failing to decode must not
|
|
109
|
+
* throw — a `ParseError` here would replace the real error with an opaque one
|
|
110
|
+
* and lose the only useful diagnostic. Hence the `Option` suffix rather than
|
|
111
|
+
* `Sync`, matching `Schema.decodeUnknownOption`: the sibling `*Sync` helpers
|
|
112
|
+
* in this module all throw on a parse failure, and this one deliberately
|
|
113
|
+
* doesn't.
|
|
114
|
+
*/
|
|
115
|
+
const decodeErrorOption = (ref, encodedError) => Option.flatMap(errorSchemaOf(ref), (schema) => Schema.decodeUnknownOption(schema)(encodedError));
|
|
116
|
+
const missingPaginatedProvenanceError = (ref) => new globalThis.Error(`Paginated query ref "${getConvexFunctionName(ref)}" was not built with \`FunctionSpec.publicPaginatedQuery\` (or \`FunctionSpec.internalPaginatedQuery\`). Paginated encoding and decoding require the user-args and item schemas those constructors store. Define the function as, e.g.:
|
|
117
|
+
|
|
118
|
+
FunctionSpec.publicPaginatedQuery({
|
|
119
|
+
name: "...",
|
|
120
|
+
args: () => ({ ... }), // optional; without paginationOpts
|
|
121
|
+
item: () => ItemSchema,
|
|
122
|
+
})`);
|
|
123
|
+
const paginatedKind = (ref) => Match.value(ref.kind).pipe(Match.tag("Paginated", (kind) => kind), Match.tag("Standard", () => {
|
|
124
|
+
throw missingPaginatedProvenanceError(ref);
|
|
125
|
+
}), Match.exhaustive);
|
|
126
|
+
/**
|
|
127
|
+
* Encode the args of a paginated query ref via its user-args schema —
|
|
128
|
+
* `paginationOpts` is excluded, since the pagination protocol fields are
|
|
129
|
+
* managed by the client (e.g. `usePaginatedQuery` from `convex/react`), not by
|
|
130
|
+
* the caller. Requires a ref built with `FunctionSpec.publicPaginatedQuery`
|
|
131
|
+
* (or `internalPaginatedQuery`).
|
|
132
|
+
*/
|
|
133
|
+
const encodePaginatedQueryArgsSync = (ref, args) => Match.value(ref).pipe(Match.tag("Confect", (confectRef) => Schema.encodeUnknownSync(paginatedKind(confectRef).userArgs)(args)), Match.tag("Convex", () => args), Match.exhaustive);
|
|
74
134
|
/**
|
|
75
|
-
*
|
|
76
|
-
*
|
|
135
|
+
* Decode a page of a paginated query's results via the ref's item schema.
|
|
136
|
+
* Requires a ref built with `FunctionSpec.publicPaginatedQuery` (or
|
|
137
|
+
* `internalPaginatedQuery`).
|
|
77
138
|
*/
|
|
78
|
-
const
|
|
79
|
-
const maybeDecodeErrorSync = (ref, error) => isConvexError(error) ? Match.value(ref.functionSpec.functionProvenance).pipe(Match.tag("Confect", (confectFunctionProvenance) => "error" in confectFunctionProvenance ? Schema.decodeUnknownSync(confectFunctionProvenance.error)(error.data) : error), Match.tag("Convex", () => error), Match.exhaustive) : error;
|
|
139
|
+
const decodePaginationPageSync = (ref, encodedPage) => Match.value(ref).pipe(Match.tag("Confect", (confectRef) => Schema.decodeUnknownSync(paginatedKind(confectRef).page)(encodedPage)), Match.tag("Convex", () => encodedPage), Match.exhaustive);
|
|
80
140
|
/**
|
|
81
141
|
* Encode args via the ref's args schema, invoke `call`, decode returns via the
|
|
82
142
|
* ref's returns schema, and translate any thrown `ConvexError` into the ref's
|
|
@@ -87,25 +147,21 @@ const maybeDecodeErrorSync = (ref, error) => isConvexError(error) ? Match.value(
|
|
|
87
147
|
*/
|
|
88
148
|
const runWithCodec = (ref, args, call, mapUnknownError) => Effect.gen(function* () {
|
|
89
149
|
const functionReference = getFunctionReference(ref);
|
|
90
|
-
const functionProvenance = ref.functionSpec.functionProvenance;
|
|
91
150
|
const invoke = (encodedArgs) => Effect.tryPromise({
|
|
92
151
|
try: () => Promise.resolve(call(functionReference, encodedArgs)),
|
|
93
152
|
catch: (error) => {
|
|
94
153
|
if (isConvexError(error)) {
|
|
95
|
-
const decoded =
|
|
154
|
+
const decoded = decodeErrorOption(ref, error.data);
|
|
96
155
|
if (Option.isSome(decoded)) return decoded.value;
|
|
97
156
|
}
|
|
98
157
|
if (mapUnknownError !== void 0) return mapUnknownError(error);
|
|
99
158
|
throw error;
|
|
100
159
|
}
|
|
101
160
|
});
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
const encodedReturns = yield* invoke(encodedArgs);
|
|
105
|
-
return yield* Schema.decodeUnknownEffect(confectFunctionProvenance.returns)(encodedReturns);
|
|
106
|
-
})), Match.tag("Convex", () => invoke(args)), Match.exhaustive);
|
|
161
|
+
const encodedReturns = yield* invoke(yield* encodeArgs(ref, args));
|
|
162
|
+
return yield* decodeReturns(ref, encodedReturns);
|
|
107
163
|
});
|
|
108
164
|
//#endregion
|
|
109
|
-
export { Ref_exports, decodeArgsSync, decodeError, decodeErrorOrElse,
|
|
165
|
+
export { Ref_exports, decodeArgsSync, decodeError, decodeErrorOption, decodeErrorOrElse, decodePaginationPageSync, decodeReturns, decodeReturnsSync, encodeArgs, encodeArgsSync, encodePaginatedQueryArgsSync, encodeReturnsSync, getConvexFunctionName, getFunctionReference, hasErrorSchema, isConvexError, make, runWithCodec };
|
|
110
166
|
|
|
111
167
|
//# sourceMappingURL=Ref.js.map
|
package/dist/Ref.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Ref.js","names":[],"sources":["../src/Ref.ts"],"sourcesContent":["import type {\n FunctionReference as ConvexFunctionReference,\n FunctionVisibility,\n} from \"convex/server\";\nimport { makeFunctionReference } from \"convex/server\";\nimport type { Value } from \"convex/values\";\nimport { ConvexError } from \"convex/values\";\nimport * as Effect from \"effect/Effect\";\nimport * as Match from \"effect/Match\";\nimport * as Option from \"effect/Option\";\nimport * as Schema from \"effect/Schema\";\nimport type * as FunctionSpec from \"./FunctionSpec\";\nimport type * as RuntimeAndFunctionType from \"./RuntimeAndFunctionType\";\n\nexport interface Ref<\n _RuntimeAndFunctionType extends RuntimeAndFunctionType.RuntimeAndFunctionType,\n _FunctionVisibility extends FunctionVisibility,\n _Args,\n _Returns,\n _Error = never,\n> {\n readonly _RuntimeAndFunctionType?: _RuntimeAndFunctionType;\n readonly _FunctionVisibility?: _FunctionVisibility;\n readonly _Args?: _Args;\n readonly _Returns?: _Returns;\n readonly _Error?: _Error;\n /** @internal */\n readonly functionSpec: FunctionSpec.AnyWithProps;\n /** @internal */\n readonly functionNamespace: string;\n}\n\nexport interface Any extends Ref<any, any, any, any, any> {}\n\nexport interface AnyInternal extends Ref<any, \"internal\", any, any, any> {}\n\nexport interface AnyPublic extends Ref<any, \"public\", any, any, any> {}\n\nexport interface AnyQuery extends Ref<\n RuntimeAndFunctionType.AnyQuery,\n FunctionVisibility,\n any,\n any,\n any\n> {}\n\nexport interface AnyMutation extends Ref<\n RuntimeAndFunctionType.AnyMutation,\n FunctionVisibility,\n any,\n any,\n any\n> {}\n\nexport interface AnyAction extends Ref<\n RuntimeAndFunctionType.AnyAction,\n FunctionVisibility,\n any,\n any,\n any\n> {}\n\nexport interface AnyPublicQuery extends Ref<\n RuntimeAndFunctionType.AnyQuery,\n \"public\",\n any,\n any,\n any\n> {}\n\nexport interface AnyPublicMutation extends Ref<\n RuntimeAndFunctionType.AnyMutation,\n \"public\",\n any,\n any,\n any\n> {}\n\nexport interface AnyPublicAction extends Ref<\n RuntimeAndFunctionType.AnyAction,\n \"public\",\n any,\n any,\n any\n> {}\n\nexport type GetRuntimeAndFunctionType<Ref_> =\n Ref_ extends Ref<\n infer RuntimeAndFunctionType_,\n infer _FunctionVisibility,\n infer _Args,\n infer _Returns,\n infer _Error\n >\n ? RuntimeAndFunctionType_\n : never;\n\nexport type GetRuntime<Ref_> =\n Ref_ extends Ref<\n infer RuntimeAndFunctionType_,\n infer _FunctionVisibility,\n infer _Args,\n infer _Returns,\n infer _Error\n >\n ? RuntimeAndFunctionType.GetRuntime<RuntimeAndFunctionType_>\n : never;\n\nexport type GetFunctionType<Ref_> =\n Ref_ extends Ref<\n infer RuntimeAndFunctionType_,\n infer _FunctionVisibility,\n infer _Args,\n infer _Returns,\n infer _Error\n >\n ? RuntimeAndFunctionType.GetFunctionType<RuntimeAndFunctionType_>\n : never;\n\nexport type GetFunctionVisibility<Ref_> =\n Ref_ extends Ref<\n infer _RuntimeAndFunctionType,\n infer FunctionVisibility_,\n infer _Args,\n infer _Returns,\n infer _Error\n >\n ? FunctionVisibility_\n : never;\n\nexport type Args<Ref_> =\n Ref_ extends Ref<\n infer _RuntimeAndFunctionType,\n infer _FunctionVisibility,\n infer Args_,\n infer _Returns,\n infer _Error\n >\n ? Args_\n : never;\n\nexport type OptionalArgs<Ref_ extends Any> = keyof Args<Ref_> extends never\n ? [args?: Args<Ref_>]\n : [args: Args<Ref_>];\n\nexport type Returns<Ref_> =\n Ref_ extends Ref<\n infer _RuntimeAndFunctionType,\n infer _FunctionVisibility,\n infer _Args,\n infer Returns_,\n infer _Error\n >\n ? Returns_\n : never;\n\nexport type Error<Ref_> =\n Ref_ extends Ref<\n infer _RuntimeAndFunctionType,\n infer _FunctionVisibility,\n infer _Args,\n infer _Returns,\n infer Error_\n >\n ? Error_\n : never;\n\nexport type FunctionReference<Ref_ extends Any> = ConvexFunctionReference<\n GetFunctionType<Ref_>,\n GetFunctionVisibility<Ref_>\n>;\n\nexport type FromFunctionSpec<FunctionSpec_ extends FunctionSpec.AnyWithProps> =\n Ref<\n FunctionSpec.GetRuntimeAndFunctionType<FunctionSpec_>,\n FunctionSpec.GetFunctionVisibility<FunctionSpec_>,\n FunctionSpec.Args<FunctionSpec_>,\n FunctionSpec.Returns<FunctionSpec_>,\n FunctionSpec.Error<FunctionSpec_>\n >;\n\nexport const make = <FunctionSpec_ extends FunctionSpec.AnyWithProps>(\n /**\n * The namespace portion of a Convex function name, i.e. the part before the\n * colon. For example, for `myGroupDir/myGroupMod:myFunc` this would be\n * `myGroupDir/myGroupMod`.\n */\n functionNamespace: string,\n functionSpec: FunctionSpec_,\n): FromFunctionSpec<FunctionSpec_> => ({ functionSpec, functionNamespace });\n\nexport const getConvexFunctionName = (ref: Any): string =>\n `${ref.functionNamespace}:${ref.functionSpec.name}`;\n\nconst functionReferenceCache = new Map<string, FunctionReference<Any>>();\n\nexport const getFunctionReference = <Ref_ extends Any>(\n ref: Ref_,\n): FunctionReference<Ref_> => {\n const functionName = getConvexFunctionName(ref);\n\n const cached = functionReferenceCache.get(functionName);\n if (cached !== undefined) {\n return cached as FunctionReference<Ref_>;\n }\n\n const functionReference = makeFunctionReference(functionName);\n functionReferenceCache.set(functionName, functionReference);\n\n return functionReference as FunctionReference<Ref_>;\n};\n\nexport const hasErrorSchema = (ref: Any): boolean =>\n Match.value(ref.functionSpec.functionProvenance).pipe(\n Match.tag(\n \"Confect\",\n (confectFunctionProvenance) => \"error\" in confectFunctionProvenance,\n ),\n Match.tag(\"Convex\", () => false),\n Match.exhaustive,\n );\n\nexport const encodeArgs = <Ref_ extends Any>(\n ref: Ref_,\n args: Args<Ref_>,\n): Effect.Effect<unknown, Schema.SchemaError> =>\n Match.value(ref.functionSpec.functionProvenance).pipe(\n Match.tag(\"Confect\", (confectFunctionProvenance) =>\n Schema.encodeEffect(confectFunctionProvenance.args)(args),\n ),\n Match.tag(\"Convex\", () => Effect.succeed(args)),\n Match.exhaustive,\n );\n\nexport const decodeReturns = <Ref_ extends Any>(\n ref: Ref_,\n returns: unknown,\n): Effect.Effect<Returns<Ref_>, Schema.SchemaError> =>\n Match.value(ref.functionSpec.functionProvenance).pipe(\n Match.tag(\"Confect\", (confectFunctionProvenance) =>\n Schema.decodeUnknownEffect(confectFunctionProvenance.returns)(returns),\n ),\n Match.tag(\"Convex\", () => Effect.succeed(returns)),\n Match.exhaustive,\n );\n\nexport const encodeArgsSync = <Ref_ extends Any>(\n ref: Ref_,\n args: Args<Ref_>,\n): unknown =>\n Match.value(ref.functionSpec.functionProvenance).pipe(\n Match.tag(\"Confect\", (confectFunctionProvenance) =>\n Schema.encodeSync(confectFunctionProvenance.args)(args),\n ),\n Match.tag(\"Convex\", () => args),\n Match.exhaustive,\n );\n\nexport const decodeArgsSync = <Ref_ extends Any>(\n ref: Ref_,\n encodedArgs: unknown,\n): Args<Ref_> =>\n Match.value(ref.functionSpec.functionProvenance).pipe(\n Match.tag(\"Confect\", (confectFunctionProvenance) =>\n Schema.decodeUnknownSync(confectFunctionProvenance.args)(encodedArgs),\n ),\n Match.tag(\"Convex\", () => encodedArgs),\n Match.exhaustive,\n ) as Args<Ref_>;\n\nexport const encodeReturnsSync = <Ref_ extends Any>(\n ref: Ref_,\n returns: Returns<Ref_>,\n): unknown =>\n Match.value(ref.functionSpec.functionProvenance).pipe(\n Match.tag(\"Confect\", (confectFunctionProvenance) =>\n Schema.encodeSync(confectFunctionProvenance.returns)(returns),\n ),\n Match.tag(\"Convex\", () => returns),\n Match.exhaustive,\n );\n\nexport const decodeReturnsSync = <Ref_ extends Any>(\n ref: Ref_,\n encodedReturns: unknown,\n): Returns<Ref_> =>\n Match.value(ref.functionSpec.functionProvenance).pipe(\n Match.tag(\"Confect\", (confectFunctionProvenance) =>\n Schema.decodeUnknownSync(confectFunctionProvenance.returns)(\n encodedReturns,\n ),\n ),\n Match.tag(\"Convex\", () => encodedReturns),\n Match.exhaustive,\n ) as Returns<Ref_>;\n\nconst ConvexErrorIdentifier = Symbol.for(\"ConvexError\");\n\nexport const isConvexError = (error: unknown): error is ConvexError<Value> =>\n error instanceof ConvexError ||\n (typeof error === \"object\" &&\n error !== null &&\n ConvexErrorIdentifier in error);\n\n/**\n * Build a callback-style handler that decodes the ref's typed error from a\n * caught `ConvexError`, or else forwards the value to `mapUnknownError`. The\n * fallback is also invoked when the input *is* a `ConvexError` but the ref\n * doesn't declare a typed-error schema—by definition such a value falls\n * outside the ref's error contract. Useful when adapting non-Effect APIs (e.g.\n * emitter callbacks for streamed subscriptions) to the same error semantics\n * that `runWithCodec` provides.\n */\nexport const decodeErrorOrElse =\n <Ref_ extends Any, E>(ref: Ref_, mapUnknownError: (error: unknown) => E) =>\n (error: unknown): Error<Ref_> | E => {\n if (isConvexError(error)) {\n const decoded = decodeErrorSync(ref, error.data);\n if (Option.isSome(decoded)) {\n return decoded.value;\n }\n }\n return mapUnknownError(error);\n };\n\n/**\n * Decode `encodedError` against the ref's error schema. Returns `None` if the\n * ref doesn't declare a typed error (Confect ref without an `error` schema, or\n * a Convex-provenance ref)—by definition there's nothing to decode the value\n * into, and the caller is responsible for deciding what to do (typically:\n * surface the original value as a defect).\n */\nexport const decodeError = <Ref_ extends Any>(\n ref: Ref_,\n encodedError: unknown,\n): Effect.Effect<Option.Option<Error<Ref_>>, Schema.SchemaError> =>\n Match.value(ref.functionSpec.functionProvenance).pipe(\n Match.tag(\"Confect\", (confectFunctionProvenance) =>\n \"error\" in confectFunctionProvenance\n ? Effect.map(\n Schema.decodeUnknownEffect(confectFunctionProvenance.error)(\n encodedError,\n ),\n Option.some,\n )\n : Effect.succeed(Option.none<Error<Ref_>>()),\n ),\n Match.tag(\"Convex\", () => Effect.succeed(Option.none<Error<Ref_>>())),\n Match.exhaustive,\n );\n\n/**\n * Synchronous counterpart to `decodeError`. Throws on schema decode failure;\n * returns `None` when the ref doesn't declare a typed error.\n */\nexport const decodeErrorSync = <Ref_ extends Any>(\n ref: Ref_,\n encodedError: unknown,\n): Option.Option<Error<Ref_>> =>\n Match.value(ref.functionSpec.functionProvenance).pipe(\n Match.tag(\"Confect\", (confectFunctionProvenance) =>\n \"error\" in confectFunctionProvenance\n ? Option.some(\n Schema.decodeUnknownSync(confectFunctionProvenance.error)(\n encodedError,\n ) as Error<Ref_>,\n )\n : Option.none<Error<Ref_>>(),\n ),\n Match.tag(\"Convex\", () => Option.none<Error<Ref_>>()),\n Match.exhaustive,\n );\n\nexport const maybeDecodeErrorSync = <Ref_ extends Any>(\n ref: Ref_,\n error: unknown,\n): unknown =>\n isConvexError(error)\n ? Match.value(ref.functionSpec.functionProvenance).pipe(\n Match.tag(\"Confect\", (confectFunctionProvenance) =>\n \"error\" in confectFunctionProvenance\n ? Schema.decodeUnknownSync(confectFunctionProvenance.error)(\n error.data,\n )\n : error,\n ),\n Match.tag(\"Convex\", () => error),\n Match.exhaustive,\n )\n : error;\n\n/**\n * Encode args via the ref's args schema, invoke `call`, decode returns via the\n * ref's returns schema, and translate any thrown `ConvexError` into the ref's\n * typed error. Anything else the Promise rejects with—network failures,\n * server-side runtime errors, validation failures, etc.—is passed to\n * `mapUnknownError` to be turned into a typed `E`, or surfaced as a defect when\n * no handler is provided.\n */\nexport const runWithCodec = <Ref_ extends Any, E = never>(\n ref: Ref_,\n args: Args<Ref_>,\n call: (\n functionReference: FunctionReference<Ref_>,\n encodedArgs: unknown,\n ) => PromiseLike<unknown>,\n mapUnknownError?: (error: unknown) => E,\n): Effect.Effect<Returns<Ref_>, E | Error<Ref_> | Schema.SchemaError> =>\n Effect.gen(function* () {\n const functionReference = getFunctionReference(ref);\n const functionProvenance = ref.functionSpec.functionProvenance;\n const invoke = (\n encodedArgs: unknown,\n ): Effect.Effect<unknown, Error<Ref_> | E> =>\n Effect.tryPromise({\n try: () => Promise.resolve(call(functionReference, encodedArgs)),\n catch: (error): Error<Ref_> | E => {\n if (isConvexError(error)) {\n const decoded = decodeErrorSync(ref, error.data);\n if (Option.isSome(decoded)) {\n return decoded.value;\n }\n }\n if (mapUnknownError !== undefined) {\n return mapUnknownError(error);\n }\n throw error;\n },\n });\n return yield* Match.value(functionProvenance).pipe(\n Match.tag(\"Confect\", (confectFunctionProvenance) =>\n Effect.gen(function* () {\n const encodedArgs = yield* Schema.encodeEffect(\n confectFunctionProvenance.args,\n )(args);\n const encodedReturns = yield* invoke(encodedArgs);\n return yield* Schema.decodeUnknownEffect(\n confectFunctionProvenance.returns,\n )(encodedReturns);\n }),\n ),\n Match.tag(\"Convex\", () => invoke(args)),\n Match.exhaustive,\n );\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OAAO,OAAO,WAAW;EAChD,OAAO,OAAO,OAAO,oBACnB,0BAA0B,OAC5B,CAAC,CAAC,cAAc;CAClB,CAAC,CACH,GACA,MAAM,IAAI,gBAAgB,OAAO,IAAI,CAAC,GACtC,MAAM,UACR;AACF,CAAC"}
|
|
1
|
+
{"version":3,"file":"Ref.js","names":["MiddlewareSpec.errorSchemas"],"sources":["../src/Ref.ts"],"sourcesContent":["import type {\n DefaultFunctionArgs,\n FunctionReference as ConvexFunctionReference,\n FunctionVisibility,\n PaginationOptions,\n PaginationResult,\n} from \"convex/server\";\nimport { makeFunctionReference } from \"convex/server\";\nimport type { Value } from \"convex/values\";\nimport { ConvexError } from \"convex/values\";\nimport * as Effect from \"effect/Effect\";\nimport * as Match from \"effect/Match\";\nimport * as Option from \"effect/Option\";\nimport * as Schema from \"effect/Schema\";\nimport type * as FunctionProvenance from \"./FunctionProvenance\";\nimport type * as FunctionSpec from \"./FunctionSpec\";\nimport * as Lazy from \"./Lazy\";\nimport * as MiddlewareSpec from \"./MiddlewareSpec\";\nimport type * as RuntimeAndFunctionType from \"./RuntimeAndFunctionType\";\n\nexport interface Base<\n RuntimeAndFunctionType_ extends RuntimeAndFunctionType.RuntimeAndFunctionType,\n FunctionVisibility_ extends FunctionVisibility,\n Args_ extends DefaultFunctionArgs,\n Returns_,\n Error_ = never,\n> {\n readonly \"~RuntimeAndFunctionType\": RuntimeAndFunctionType_;\n readonly \"~FunctionVisibility\": FunctionVisibility_;\n readonly \"~Args\": Args_;\n readonly \"~Returns\": Returns_;\n readonly \"~Error\": Error_;\n readonly convexFunctionName: string;\n}\n\n/**\n * A reference to a single Convex function, as callers see it: the wire name\n * plus the data needed to encode args, decode returns, and decode typed\n * errors. A ref is one of two shapes, keyed by the spec's provenance.\n */\nexport type Ref<\n RuntimeAndFunctionType_ extends RuntimeAndFunctionType.RuntimeAndFunctionType,\n FunctionVisibility_ extends FunctionVisibility,\n Args_ extends DefaultFunctionArgs,\n Returns_,\n Error_ = never,\n> =\n | ConfectRefWithTypes<\n RuntimeAndFunctionType_,\n FunctionVisibility_,\n FunctionProvenance.ArgsFields,\n Args_,\n Returns_,\n Error_\n >\n | ConvexRef<\n RuntimeAndFunctionType_,\n FunctionVisibility_,\n Args_,\n Returns_,\n Error_\n >;\n\nexport type ConfectRef<\n RuntimeAndFunctionType_ extends RuntimeAndFunctionType.RuntimeAndFunctionType,\n FunctionVisibility_ extends FunctionVisibility,\n ArgsFields_ extends FunctionProvenance.ArgsFields,\n ReturnsSchema_ extends Schema.Codec<any, any>,\n ErrorSchema_ extends Schema.Codec<any, any> = never,\n MiddlewareError_ = never,\n> = ConfectRefWithTypes<\n RuntimeAndFunctionType_,\n FunctionVisibility_,\n ArgsFields_,\n Schema.Struct.Type<ArgsFields_>,\n ReturnsSchema_[\"Type\"],\n ErrorSchema_[\"Type\"] | MiddlewareError_\n>;\n\n/**\n * Internal form used while mapping a spec to a ref. Supplying the already\n * derived value types avoids repeatedly expanding `Schema.Struct` for every\n * leaf in a generated ref tree; the public `ConfectRef` alias still derives\n * the same values from its fields and schemas.\n */\ninterface ConfectRefWithTypes<\n RuntimeAndFunctionType_ extends RuntimeAndFunctionType.RuntimeAndFunctionType,\n FunctionVisibility_ extends FunctionVisibility,\n ArgsFields_ extends FunctionProvenance.ArgsFields,\n Args_ extends DefaultFunctionArgs,\n Returns_,\n Error_,\n> extends Base<\n RuntimeAndFunctionType_,\n FunctionVisibility_,\n Args_,\n Returns_,\n Error_\n> {\n readonly _tag: \"Confect\";\n readonly args: FunctionProvenance.ArgsSchema<ArgsFields_>;\n readonly returns: Schema.Codec<any, any>;\n readonly kind: FunctionProvenance.ConfectKind;\n readonly middlewareSpecs: ReadonlyArray<MiddlewareSpec.AnyMiddlewareSpec>;\n readonly error?: Schema.Codec<any, any>;\n}\n\nexport interface ConvexRef<\n RuntimeAndFunctionType_ extends RuntimeAndFunctionType.RuntimeAndFunctionType,\n FunctionVisibility_ extends FunctionVisibility,\n Args_ extends DefaultFunctionArgs,\n Returns_,\n Error_ = never,\n> extends Base<\n RuntimeAndFunctionType_,\n FunctionVisibility_,\n Args_,\n Returns_,\n Error_\n> {\n readonly _tag: \"Convex\";\n}\n\nexport type Any = Ref<any, any, any, any, any>;\n\n/** Erased Confect-provenance ref used by provenance-specific APIs. */\nexport type AnyConfect = Extract<Any, { readonly _tag: \"Confect\" }>;\n\nexport type AnyInternal = Ref<any, \"internal\", any, any, any>;\n\nexport type AnyPublic = Ref<any, \"public\", any, any, any>;\n\nexport type AnyQuery = Ref<\n RuntimeAndFunctionType.AnyQuery,\n FunctionVisibility,\n any,\n any,\n any\n>;\n\nexport type AnyPaginatedQuery = AnyQuery &\n Base<\n RuntimeAndFunctionType.AnyQuery,\n FunctionVisibility,\n {\n [key: string]: any;\n paginationOpts: PaginationOptions;\n },\n PaginationResult<any>,\n any\n >;\n\nexport type AnyMutation = Ref<\n RuntimeAndFunctionType.AnyMutation,\n FunctionVisibility,\n any,\n any,\n any\n>;\n\nexport type AnyAction = Ref<\n RuntimeAndFunctionType.AnyAction,\n FunctionVisibility,\n any,\n any,\n any\n>;\n\nexport type AnyPublicQuery = Ref<\n RuntimeAndFunctionType.AnyQuery,\n \"public\",\n any,\n any,\n any\n>;\n\nexport type AnyConfectPublicQuery = Extract<AnyPublicQuery, AnyConfect>;\n\nexport type AnyPublicPaginatedQuery = AnyPublicQuery & AnyPaginatedQuery;\n\nexport type AnyConfectPublicPaginatedQuery = AnyConfectPublicQuery &\n AnyPublicPaginatedQuery;\n\nexport type AnyPublicMutation = Ref<\n RuntimeAndFunctionType.AnyMutation,\n \"public\",\n any,\n any,\n any\n>;\n\nexport type AnyConfectPublicMutation = Extract<AnyPublicMutation, AnyConfect>;\n\nexport type AnyPublicAction = Ref<\n RuntimeAndFunctionType.AnyAction,\n \"public\",\n any,\n any,\n any\n>;\n\nexport type AnyConfectPublicAction = Extract<AnyPublicAction, AnyConfect>;\n\nexport type GetRuntimeAndFunctionType<Ref_> = Ref_ extends {\n readonly \"~RuntimeAndFunctionType\": infer RuntimeAndFunctionType_ extends\n RuntimeAndFunctionType.RuntimeAndFunctionType;\n}\n ? RuntimeAndFunctionType_\n : never;\n\nexport type GetRuntime<Ref_> = RuntimeAndFunctionType.GetRuntime<\n GetRuntimeAndFunctionType<Ref_>\n>;\n\nexport type GetFunctionType<Ref_> = RuntimeAndFunctionType.GetFunctionType<\n GetRuntimeAndFunctionType<Ref_>\n>;\n\nexport type GetFunctionVisibility<Ref_> = Ref_ extends {\n readonly \"~FunctionVisibility\": infer FunctionVisibility_;\n}\n ? FunctionVisibility_\n : never;\n\nexport type Args<Ref_> = Ref_ extends { readonly \"~Args\": infer Args_ }\n ? Args_\n : never;\n\n/** The field map captured by a Confect-provenance ref. */\nexport type ArgsFields<Ref_> = Ref_ extends {\n readonly _tag: \"Confect\";\n readonly args: {\n readonly fields: infer ArgsFields_ extends FunctionProvenance.ArgsFields;\n };\n}\n ? ArgsFields_\n : never;\n\n/** The assembled args schema carried by a Confect-provenance ref. */\nexport type ArgsSchema<Ref_> = Ref_ extends {\n readonly _tag: \"Confect\";\n readonly args: infer ArgsSchema_ extends FunctionProvenance.AnyArgsSchema;\n}\n ? ArgsSchema_\n : never;\n\nexport type OptionalArgs<Ref_ extends Any> = keyof Args<Ref_> extends never\n ? [args?: Args<Ref_>]\n : [args: Args<Ref_>];\n\nexport type Returns<Ref_> = Ref_ extends { readonly \"~Returns\": infer Returns_ }\n ? Returns_\n : never;\n\nexport type Error<Ref_> = Ref_ extends { readonly \"~Error\": infer Error_ }\n ? Error_\n : never;\n\nexport type FunctionReference<Ref_ extends Any> = ConvexFunctionReference<\n GetFunctionType<Ref_>,\n GetFunctionVisibility<Ref_>\n>;\n\nexport type FromFunctionSpec<\n FunctionSpec_ extends FunctionSpec.AnyWithProps,\n MiddlewareError = never,\n> = FromFunctionSpecHelper<\n FunctionSpec_,\n FunctionSpec.GetRuntimeAndFunctionType<FunctionSpec_>,\n FunctionSpec.GetFunctionVisibility<FunctionSpec_>,\n FunctionSpec.ArgsFields<FunctionSpec_>,\n FunctionSpec.Args<FunctionSpec_>,\n FunctionSpec.Returns<FunctionSpec_>,\n FunctionSpec.Error<FunctionSpec_> | MiddlewareError\n>;\n\ntype FromFunctionSpecHelper<\n FunctionSpec_ extends FunctionSpec.AnyWithProps,\n RuntimeAndFunctionType_ extends RuntimeAndFunctionType.RuntimeAndFunctionType,\n FunctionVisibility_ extends FunctionVisibility,\n ArgsFields_ extends FunctionProvenance.ArgsFields,\n Args_ extends DefaultFunctionArgs,\n Returns_,\n Error_,\n> =\n FunctionSpec_ extends FunctionSpec.WithFunctionProvenance<\n FunctionSpec_,\n FunctionProvenance.AnyConvex\n >\n ? ConvexRef<\n RuntimeAndFunctionType_,\n FunctionVisibility_,\n Args_ extends DefaultFunctionArgs ? Args_ : never,\n Returns_,\n never\n >\n : FunctionSpec_ extends FunctionSpec.WithFunctionProvenance<\n FunctionSpec_,\n FunctionProvenance.AnyConfect\n >\n ? ConfectRefWithTypes<\n RuntimeAndFunctionType_,\n FunctionVisibility_,\n ArgsFields_,\n Args_,\n Returns_,\n Error_\n >\n : Ref<\n RuntimeAndFunctionType_,\n FunctionVisibility_,\n Args_,\n Returns_,\n Error_\n >;\n\nexport const make = <FunctionSpec_ extends FunctionSpec.AnyWithProps>(\n /**\n * The namespace portion of a Convex function name, i.e. the part before the\n * colon. For example, for `myGroupDir/myGroupMod:myFunc` this would be\n * `myGroupDir/myGroupMod`.\n */\n convexFunctionNamespace: string,\n functionSpec: FunctionSpec_,\n groupMiddlewareSpecs: ReadonlyArray<MiddlewareSpec.AnyMiddlewareSpec> = [],\n): FromFunctionSpec<FunctionSpec_> => {\n const convexFunctionName = `${convexFunctionNamespace}:${functionSpec.name}`;\n\n return Match.value(functionSpec.functionProvenance).pipe(\n Match.tag(\n \"Convex\",\n (): Any =>\n ({\n _tag: \"Convex\",\n convexFunctionName,\n }) as Any,\n ),\n Match.tag(\"Confect\", (provenance): Any => {\n const ref = {\n _tag: \"Confect\" as const,\n convexFunctionName,\n kind: provenance.kind,\n middlewareSpecs: [\n ...groupMiddlewareSpecs,\n ...functionSpec.middlewareSpecs,\n ],\n };\n\n Lazy.defineProperty(ref, \"args\", () => provenance.args);\n Lazy.defineProperty(ref, \"returns\", () => provenance.returns);\n if (\"error\" in provenance) {\n Lazy.defineProperty(ref, \"error\", () => provenance.error);\n }\n\n return ref as unknown as Any;\n }),\n Match.exhaustive,\n ) as FromFunctionSpec<FunctionSpec_>;\n};\n\nexport const getConvexFunctionName = (ref: Any): string =>\n ref.convexFunctionName;\n\nconst functionReferenceCache = new Map<string, FunctionReference<Any>>();\n\nexport const getFunctionReference = <Ref_ extends Any>(\n ref: Ref_,\n): FunctionReference<Ref_> => {\n const convexFunctionName = getConvexFunctionName(ref);\n\n const cached = functionReferenceCache.get(convexFunctionName);\n if (cached !== undefined) {\n return cached as FunctionReference<Ref_>;\n }\n\n const functionReference = makeFunctionReference(convexFunctionName);\n functionReferenceCache.set(convexFunctionName, functionReference);\n\n return functionReference as FunctionReference<Ref_>;\n};\n\nexport const hasErrorSchema = (ref: Any): boolean =>\n Match.value(ref).pipe(\n Match.tag(\n \"Confect\",\n (confectRef) =>\n \"error\" in confectRef ||\n confectRef.middlewareSpecs.some(\n (middlewareSpec) => \"error\" in middlewareSpec,\n ),\n ),\n Match.tag(\"Convex\", () => false),\n Match.exhaustive,\n );\n\nexport const encodeArgs = <Ref_ extends Any>(\n ref: Ref_,\n args: Args<Ref_>,\n): Effect.Effect<unknown, Schema.SchemaError> =>\n Match.value<Any>(ref).pipe(\n Match.tag(\"Confect\", (confectRef) =>\n Schema.encodeEffect(confectRef.args)(args),\n ),\n Match.tag(\"Convex\", () => Effect.succeed(args)),\n Match.exhaustive,\n );\n\nexport const decodeReturns = <Ref_ extends Any>(\n ref: Ref_,\n returns: unknown,\n): Effect.Effect<Returns<Ref_>, Schema.SchemaError> =>\n Match.value<Any>(ref).pipe(\n Match.tag(\"Confect\", (confectRef) =>\n Schema.decodeUnknownEffect(confectRef.returns)(returns),\n ),\n Match.tag(\"Convex\", () => Effect.succeed(returns as Returns<Ref_>)),\n Match.exhaustive,\n );\n\nexport const encodeArgsSync = <Ref_ extends Any>(\n ref: Ref_,\n args: Args<Ref_>,\n): unknown =>\n Match.value<Any>(ref).pipe(\n Match.tag(\"Confect\", (confectRef) =>\n Schema.encodeSync(confectRef.args)(args),\n ),\n Match.tag(\"Convex\", () => args),\n Match.exhaustive,\n );\n\nexport const decodeArgsSync = <Ref_ extends Any>(\n ref: Ref_,\n encodedArgs: unknown,\n): Args<Ref_> =>\n Match.value<Any>(ref).pipe(\n Match.tag(\"Confect\", (confectRef) =>\n Schema.decodeUnknownSync(confectRef.args)(encodedArgs),\n ),\n Match.tag(\"Convex\", () => encodedArgs),\n Match.exhaustive,\n ) as Args<Ref_>;\n\nexport const encodeReturnsSync = <Ref_ extends Any>(\n ref: Ref_,\n returns: Returns<Ref_>,\n): unknown =>\n Match.value<Any>(ref).pipe(\n Match.tag(\"Confect\", (confectRef) =>\n Schema.encodeSync(confectRef.returns)(returns),\n ),\n Match.tag(\"Convex\", () => returns),\n Match.exhaustive,\n );\n\nexport const decodeReturnsSync = <Ref_ extends Any>(\n ref: Ref_,\n encodedReturns: unknown,\n): Returns<Ref_> =>\n Match.value<Any>(ref).pipe(\n Match.tag(\"Confect\", (confectRef) =>\n Schema.decodeUnknownSync(confectRef.returns)(encodedReturns),\n ),\n Match.tag(\"Convex\", () => encodedReturns),\n Match.exhaustive,\n ) as Returns<Ref_>;\n\nconst ConvexErrorIdentifier = Symbol.for(\"ConvexError\");\n\nexport const isConvexError = (error: unknown): error is ConvexError<Value> =>\n error instanceof ConvexError ||\n (typeof error === \"object\" &&\n error !== null &&\n ConvexErrorIdentifier in error);\n\n/**\n * Build a callback-style handler that decodes the ref's typed error from a\n * caught `ConvexError`, or else forwards the value to `mapUnknownError`. The\n * fallback is also invoked when the input *is* a `ConvexError` but the ref\n * doesn't declare a typed-error schema—by definition such a value falls\n * outside the ref's error contract. Useful when adapting non-Effect APIs (e.g.\n * emitter callbacks for streamed subscriptions) to the same error semantics\n * that `runWithCodec` provides.\n */\nexport const decodeErrorOrElse =\n <Ref_ extends Any, E>(ref: Ref_, mapUnknownError: (error: unknown) => E) =>\n (error: unknown): Error<Ref_> | E => {\n if (isConvexError(error)) {\n const decoded = decodeErrorOption(ref, error.data);\n if (Option.isSome(decoded)) {\n return decoded.value;\n }\n }\n return mapUnknownError(error);\n };\n\nconst errorSchemaOf = (ref: Any): Option.Option<Schema.Codec<any, any>> =>\n Match.value(ref).pipe(\n Match.tag(\"Confect\", (confectRef) => {\n const schemas = [\n ...(\"error\" in confectRef && confectRef.error !== undefined\n ? [confectRef.error]\n : []),\n ...MiddlewareSpec.errorSchemas(confectRef.middlewareSpecs),\n ];\n return schemas.length === 0\n ? Option.none<Schema.Codec<any, any>>()\n : Option.some(\n schemas.length === 1 ? schemas[0]! : Schema.Union(schemas),\n );\n }),\n Match.tag(\"Convex\", () => Option.none<Schema.Codec<any, any>>()),\n Match.exhaustive,\n );\n\n/**\n * Decode `encodedError` against the ref's error schema — the function's\n * declared `error` schema unioned with its covering middlewares' error\n * schemas. Returns `None` if the ref declares no typed error at all (Confect\n * ref without an `error` schema and without failing middleware, or a\n * Convex-provenance ref)—by definition there's nothing to decode the value\n * into, and the caller is responsible for deciding what to do (typically:\n * surface the original value as a defect).\n */\nexport const decodeError = <Ref_ extends Any>(\n ref: Ref_,\n encodedError: unknown,\n): Effect.Effect<Option.Option<Error<Ref_>>, Schema.SchemaError> =>\n Option.match(errorSchemaOf(ref), {\n onNone: () => Effect.succeed(Option.none<Error<Ref_>>()),\n onSome: (schema) =>\n Effect.map(\n Schema.decodeUnknownEffect(schema)(encodedError),\n Option.some,\n ) as Effect.Effect<Option.Option<Error<Ref_>>, Schema.SchemaError>,\n });\n\n/**\n * Synchronous counterpart to `decodeError`. Returns `None` when the value is\n * not this ref's typed error — either because the ref declares no `error`\n * schema, or because `encodedError` doesn't match the one it declares.\n *\n * The second case is reachable in normal operation: Convex raises its own\n * `ConvexError`s (an `InvalidCursor` pagination error, for instance), and\n * those never match a user-declared error schema. Callers pair this with a\n * fallback that surfaces the original error, so failing to decode must not\n * throw — a `ParseError` here would replace the real error with an opaque one\n * and lose the only useful diagnostic. Hence the `Option` suffix rather than\n * `Sync`, matching `Schema.decodeUnknownOption`: the sibling `*Sync` helpers\n * in this module all throw on a parse failure, and this one deliberately\n * doesn't.\n */\nexport const decodeErrorOption = <Ref_ extends Any>(\n ref: Ref_,\n encodedError: unknown,\n): Option.Option<Error<Ref_>> =>\n Option.flatMap(\n errorSchemaOf(ref),\n (schema) =>\n Schema.decodeUnknownOption(schema)(encodedError) as Option.Option<\n Error<Ref_>\n >,\n );\n\nconst missingPaginatedProvenanceError = (ref: Any) =>\n new globalThis.Error(\n `Paginated query ref \"${getConvexFunctionName(ref)}\" was not built with ` +\n \"`FunctionSpec.publicPaginatedQuery` (or `FunctionSpec.internalPaginatedQuery`). \" +\n \"Paginated encoding and decoding require the user-args and item schemas \" +\n \"those constructors store. Define the function as, e.g.:\\n\\n\" +\n \" FunctionSpec.publicPaginatedQuery({\\n\" +\n ' name: \"...\",\\n' +\n \" args: () => ({ ... }), // optional; without paginationOpts\\n\" +\n \" item: () => ItemSchema,\\n\" +\n \" })\",\n );\n\nconst paginatedKind = (ref: AnyConfect): FunctionProvenance.Paginated =>\n Match.value(ref.kind).pipe(\n Match.tag(\"Paginated\", (kind) => kind),\n Match.tag(\"Standard\", () => {\n throw missingPaginatedProvenanceError(ref);\n }),\n Match.exhaustive,\n );\n\n/**\n * Encode the args of a paginated query ref via its user-args schema —\n * `paginationOpts` is excluded, since the pagination protocol fields are\n * managed by the client (e.g. `usePaginatedQuery` from `convex/react`), not by\n * the caller. Requires a ref built with `FunctionSpec.publicPaginatedQuery`\n * (or `internalPaginatedQuery`).\n */\nexport const encodePaginatedQueryArgsSync = <\n Ref_ extends AnyPublicPaginatedQuery,\n>(\n ref: Ref_,\n args: Omit<Args<Ref_>, \"paginationOpts\">,\n): unknown =>\n Match.value<Any>(ref).pipe(\n Match.tag(\"Confect\", (confectRef) =>\n Schema.encodeUnknownSync(paginatedKind(confectRef).userArgs)(args),\n ),\n Match.tag(\"Convex\", () => args),\n Match.exhaustive,\n );\n\n/**\n * Decode a page of a paginated query's results via the ref's item schema.\n * Requires a ref built with `FunctionSpec.publicPaginatedQuery` (or\n * `internalPaginatedQuery`).\n */\nexport const decodePaginationPageSync = <Ref_ extends AnyPublicPaginatedQuery>(\n ref: Ref_,\n encodedPage: unknown,\n): Returns<Ref_>[\"page\"] =>\n Match.value<Any>(ref).pipe(\n Match.tag(\"Confect\", (confectRef) =>\n Schema.decodeUnknownSync(paginatedKind(confectRef).page)(encodedPage),\n ),\n Match.tag(\"Convex\", () => encodedPage),\n Match.exhaustive,\n ) as Returns<Ref_>[\"page\"];\n\n/**\n * Encode args via the ref's args schema, invoke `call`, decode returns via the\n * ref's returns schema, and translate any thrown `ConvexError` into the ref's\n * typed error. Anything else the Promise rejects with—network failures,\n * server-side runtime errors, validation failures, etc.—is passed to\n * `mapUnknownError` to be turned into a typed `E`, or surfaced as a defect when\n * no handler is provided.\n */\nexport const runWithCodec = <Ref_ extends Any, E = never>(\n ref: Ref_,\n args: Args<Ref_>,\n call: (\n functionReference: FunctionReference<Ref_>,\n encodedArgs: unknown,\n ) => PromiseLike<unknown>,\n mapUnknownError?: (error: unknown) => E,\n): Effect.Effect<Returns<Ref_>, E | Error<Ref_> | Schema.SchemaError> =>\n Effect.gen(function* () {\n const functionReference = getFunctionReference(ref);\n const invoke = (\n encodedArgs: unknown,\n ): Effect.Effect<unknown, Error<Ref_> | E> =>\n Effect.tryPromise({\n try: () => Promise.resolve(call(functionReference, encodedArgs)),\n catch: (error): Error<Ref_> | E => {\n if (isConvexError(error)) {\n const decoded = decodeErrorOption(ref, error.data);\n if (Option.isSome(decoded)) {\n return decoded.value;\n }\n }\n if (mapUnknownError !== undefined) {\n return mapUnknownError(error);\n }\n throw error;\n },\n });\n\n const encodedArgs = yield* encodeArgs(ref, args);\n const encodedReturns = yield* invoke(encodedArgs);\n return yield* decodeReturns(ref, encodedReturns);\n });\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4TA,MAAa,QAMX,yBACA,cACA,uBAAwE,CAAC,MACrC;CACpC,MAAM,qBAAqB,GAAG,wBAAwB,GAAG,aAAa;CAEtE,OAAO,MAAM,MAAM,aAAa,kBAAkB,CAAC,CAAC,KAClD,MAAM,IACJ,iBAEG;EACC,MAAM;EACN;CACF,EACJ,GACA,MAAM,IAAI,YAAY,eAAoB;EACxC,MAAM,MAAM;GACV,MAAM;GACN;GACA,MAAM,WAAW;GACjB,iBAAiB,CACf,GAAG,sBACH,GAAG,aAAa,eAClB;EACF;EAEA,eAAoB,KAAK,cAAc,WAAW,IAAI;EACtD,eAAoB,KAAK,iBAAiB,WAAW,OAAO;EAC5D,IAAI,WAAW,YACb,eAAoB,KAAK,eAAe,WAAW,KAAK;EAG1D,OAAO;CACT,CAAC,GACD,MAAM,UACR;AACF;AAEA,MAAa,yBAAyB,QACpC,IAAI;AAEN,MAAM,yCAAyB,IAAI,IAAoC;AAEvE,MAAa,wBACX,QAC4B;CAC5B,MAAM,qBAAqB,sBAAsB,GAAG;CAEpD,MAAM,SAAS,uBAAuB,IAAI,kBAAkB;CAC5D,IAAI,WAAW,KAAA,GACb,OAAO;CAGT,MAAM,oBAAoB,sBAAsB,kBAAkB;CAClE,uBAAuB,IAAI,oBAAoB,iBAAiB;CAEhE,OAAO;AACT;AAEA,MAAa,kBAAkB,QAC7B,MAAM,MAAM,GAAG,CAAC,CAAC,KACf,MAAM,IACJ,YACC,eACC,WAAW,cACX,WAAW,gBAAgB,MACxB,mBAAmB,WAAW,cACjC,CACJ,GACA,MAAM,IAAI,gBAAgB,KAAK,GAC/B,MAAM,UACR;AAEF,MAAa,cACX,KACA,SAEA,MAAM,MAAW,GAAG,CAAC,CAAC,KACpB,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F,MAAa,gCAGX,KACA,SAEA,MAAM,MAAW,GAAG,CAAC,CAAC,KACpB,MAAM,IAAI,YAAY,eACpB,OAAO,kBAAkB,cAAc,UAAU,CAAC,CAAC,QAAQ,CAAC,CAAC,IAAI,CACnE,GACA,MAAM,IAAI,gBAAgB,IAAI,GAC9B,MAAM,UACR;;;;;;AAOF,MAAa,4BACX,KACA,gBAEA,MAAM,MAAW,GAAG,CAAC,CAAC,KACpB,MAAM,IAAI,YAAY,eACpB,OAAO,kBAAkB,cAAc,UAAU,CAAC,CAAC,IAAI,CAAC,CAAC,WAAW,CACtE,GACA,MAAM,IAAI,gBAAgB,WAAW,GACrC,MAAM,UACR;;;;;;;;;AAUF,MAAa,gBACX,KACA,MACA,MAIA,oBAEA,OAAO,IAAI,aAAa;CACtB,MAAM,oBAAoB,qBAAqB,GAAG;CAClD,MAAM,UACJ,gBAEA,OAAO,WAAW;EAChB,WAAW,QAAQ,QAAQ,KAAK,mBAAmB,WAAW,CAAC;EAC/D,QAAQ,UAA2B;GACjC,IAAI,cAAc,KAAK,GAAG;IACxB,MAAM,UAAU,kBAAkB,KAAK,MAAM,IAAI;IACjD,IAAI,OAAO,OAAO,OAAO,GACvB,OAAO,QAAQ;GAEnB;GACA,IAAI,oBAAoB,KAAA,GACtB,OAAO,gBAAgB,KAAK;GAE9B,MAAM;EACR;CACF,CAAC;CAGH,MAAM,iBAAiB,OAAO,OAAO,OADV,WAAW,KAAK,IAAI,CACC;CAChD,OAAO,OAAO,cAAc,KAAK,cAAc;AACjD,CAAC"}
|
package/dist/Refs.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Types } from "effect";
|
|
2
2
|
import type * as FunctionSpec from "./FunctionSpec";
|
|
3
3
|
import type * as GroupSpec from "./GroupSpec";
|
|
4
|
+
import type * as MiddlewareSpec from "./MiddlewareSpec";
|
|
4
5
|
import * as Ref from "./Ref";
|
|
5
6
|
import type * as Spec from "./Spec";
|
|
6
7
|
export type Refs<Spec_ extends Spec.AnyWithProps, Predicate extends Ref.Any = Ref.Any> = Types.Simplify<OmitEmpty<Helper<Spec.Groups<Spec_>, Predicate>>>;
|
|
@@ -8,13 +9,17 @@ export interface FromSpec<Spec_ extends Spec.AnyWithProps> {
|
|
|
8
9
|
public: Refs<Spec_, Ref.AnyPublic>;
|
|
9
10
|
internal: Refs<Spec_, Ref.AnyInternal>;
|
|
10
11
|
}
|
|
11
|
-
type GroupRefs<Group extends GroupSpec.AnyWithProps, Predicate extends Ref.Any> = Types.Simplify<OmitEmpty<Helper<GroupSpec.Groups<Group>, Predicate>> & FilteredFunctions<GroupSpec.Functions<Group>, Predicate
|
|
12
|
+
type GroupRefs<Group extends GroupSpec.AnyWithProps, Predicate extends Ref.Any> = Types.Simplify<OmitEmpty<Helper<GroupSpec.Groups<Group>, Predicate>> & FilteredFunctions<GroupSpec.Functions<Group>, Predicate, MiddlewareSpec.Error<GroupSpec.MiddlewareSpecs<Group>>>>;
|
|
12
13
|
type OmitEmpty<T> = {
|
|
13
14
|
[K in keyof T as keyof T[K] extends never ? never : K]: T[K];
|
|
14
15
|
};
|
|
15
|
-
type FunctionSpecMatchesPredicate<FunctionSpec_ extends FunctionSpec.AnyWithProps, Predicate extends Ref.Any> = Ref.Ref<FunctionSpec.GetRuntimeAndFunctionType<FunctionSpec_>, FunctionSpec.GetFunctionVisibility<FunctionSpec_>, any, any> extends Predicate ? true : false;
|
|
16
|
-
type FilteredFunctions<FunctionSpecs extends FunctionSpec.AnyWithProps, Predicate extends Ref.Any> = {
|
|
17
|
-
[Name in FunctionSpec.Name<FunctionSpecs> as FunctionSpec.WithName<FunctionSpecs, Name> extends infer FunctionSpec_ extends FunctionSpec.AnyWithProps ? FunctionSpecMatchesPredicate<FunctionSpec_, Predicate> extends true ? Name : never : never]: FunctionSpec.WithName<FunctionSpecs, Name> extends infer F extends FunctionSpec.AnyWithProps ? Ref.FromFunctionSpec<F
|
|
16
|
+
type FunctionSpecMatchesPredicate<FunctionSpec_ extends FunctionSpec.AnyWithProps, Predicate extends Ref.Any> = Ref.Ref<FunctionSpec.GetRuntimeAndFunctionType<FunctionSpec_>, FunctionSpec.GetFunctionVisibility<FunctionSpec_>, any, any, any> extends Predicate ? true : false;
|
|
17
|
+
type FilteredFunctions<FunctionSpecs extends FunctionSpec.AnyWithProps, Predicate extends Ref.Any, MiddlewareError> = {
|
|
18
|
+
[Name in FunctionSpec.Name<FunctionSpecs> as FunctionSpec.WithName<FunctionSpecs, Name> extends infer FunctionSpec_ extends FunctionSpec.AnyWithProps ? FunctionSpecMatchesPredicate<FunctionSpec_, Predicate> extends true ? Name : never : never]: FunctionSpec.WithName<FunctionSpecs, Name> extends infer F extends FunctionSpec.AnyWithProps ? Ref.FromFunctionSpec<F, F extends {
|
|
19
|
+
readonly functionProvenance: {
|
|
20
|
+
readonly _tag: "Confect";
|
|
21
|
+
};
|
|
22
|
+
} ? MiddlewareError | MiddlewareSpec.Error<FunctionSpec.MiddlewareSpecs<F>> : never> : never;
|
|
18
23
|
};
|
|
19
24
|
type Helper<Groups extends GroupSpec.AnyWithProps, Predicate extends Ref.Any> = {
|
|
20
25
|
[GroupName in GroupSpec.Name<Groups>]: GroupSpec.WithName<Groups, GroupName> extends infer Group extends GroupSpec.AnyWithProps ? GroupRefs<Group, Predicate> : never;
|