@confect/core 10.0.0-next.14 → 10.0.0-next.17
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 +241 -0
- package/dist/FunctionSpec.d.ts +49 -43
- package/dist/FunctionSpec.d.ts.map +1 -1
- package/dist/FunctionSpec.js +22 -4
- package/dist/FunctionSpec.js.map +1 -1
- package/dist/GroupSpec.d.ts +11 -7
- package/dist/GroupSpec.d.ts.map +1 -1
- package/dist/GroupSpec.js +33 -10
- package/dist/GroupSpec.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/Ref.d.ts +38 -32
- package/dist/Ref.d.ts.map +1 -1
- package/dist/Ref.js +53 -35
- package/dist/Ref.js.map +1 -1
- package/dist/Refs.d.ts +8 -3
- package/dist/Refs.d.ts.map +1 -1
- package/dist/Refs.js +3 -3
- package/dist/Refs.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/package.json +9 -8
- package/src/FunctionSpec.ts +88 -9
- package/src/GroupSpec.ts +119 -12
- package/src/MiddlewareSpec.ts +356 -0
- package/src/Ref.ts +271 -126
- package/src/Refs.ts +40 -16
- package/src/index.ts +1 -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
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
import type { FunctionType, FunctionVisibility } from "convex/server";
|
|
2
|
+
import type * as Effect from "effect/Effect";
|
|
3
|
+
import * as Predicate from "effect/Predicate";
|
|
4
|
+
import type * as Schema from "effect/Schema";
|
|
5
|
+
import type { unhandled } from "effect/Types";
|
|
6
|
+
import type * as FunctionProvenance from "./FunctionProvenance";
|
|
7
|
+
import type * as FunctionSpec from "./FunctionSpec";
|
|
8
|
+
import * as Lazy from "./Lazy";
|
|
9
|
+
import type * as RuntimeAndFunctionType from "./RuntimeAndFunctionType";
|
|
10
|
+
|
|
11
|
+
export const TypeId = "@confect/core/MiddlewareSpec";
|
|
12
|
+
export type TypeId = typeof TypeId;
|
|
13
|
+
|
|
14
|
+
export const isMiddlewareSpec = (u: unknown): u is AnyMiddlewareSpec =>
|
|
15
|
+
Predicate.hasProperty(u, TypeId);
|
|
16
|
+
|
|
17
|
+
export const allFunctionTypes: ReadonlyArray<FunctionType> = [
|
|
18
|
+
"query",
|
|
19
|
+
"mutation",
|
|
20
|
+
"action",
|
|
21
|
+
];
|
|
22
|
+
|
|
23
|
+
export interface SupportedFunctionTypes {
|
|
24
|
+
readonly query: boolean;
|
|
25
|
+
readonly mutation: boolean;
|
|
26
|
+
readonly action: boolean;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Marker success type used by middleware to represent the handler's outcome
|
|
31
|
+
* without exposing its concrete success value.
|
|
32
|
+
*
|
|
33
|
+
* Mirrors `RpcMiddleware.SuccessValue` in Effect.
|
|
34
|
+
*/
|
|
35
|
+
export interface SuccessValue {
|
|
36
|
+
readonly _: unique symbol;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The wrap-style middleware implementation shape: receives the downstream
|
|
41
|
+
* effect (the remaining middleware plus the function handler) and the
|
|
42
|
+
* covered invocation's metadata, and returns the effect that runs in its
|
|
43
|
+
* place.
|
|
44
|
+
*
|
|
45
|
+
* Mirrors `RpcMiddleware.RpcMiddleware` in Effect.
|
|
46
|
+
*/
|
|
47
|
+
export interface MiddlewareImpl<Provides_, E, R> {
|
|
48
|
+
(
|
|
49
|
+
effect: Effect.Effect<SuccessValue, E | unhandled, Provides_>,
|
|
50
|
+
options: MiddlewareOptions,
|
|
51
|
+
): Effect.Effect<SuccessValue, E | unhandled, R>;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface MiddlewareOptions {
|
|
55
|
+
readonly name: string;
|
|
56
|
+
readonly functionType: FunctionType;
|
|
57
|
+
readonly functionVisibility: FunctionVisibility;
|
|
58
|
+
readonly args: unknown;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface AnyMiddlewareImpl extends MiddlewareImpl<any, any, any> {}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* The class shape produced by {@link MiddlewareSpec}. Only the static side is ever
|
|
65
|
+
* used — the class is a value-level carrier for the middleware's key, its
|
|
66
|
+
* declared functionTypes, and its (lazily-evaluated) error schema, plus the
|
|
67
|
+
* type-level `Provides`/`Error` metadata. It is deliberately not a
|
|
68
|
+
* `Context.Tag`: implementations are registered through the group `Registry`
|
|
69
|
+
* (like `FunctionImpl`s), not resolved from a Layer context.
|
|
70
|
+
*/
|
|
71
|
+
export interface MiddlewareSpec<
|
|
72
|
+
in out Self,
|
|
73
|
+
Key_ extends string,
|
|
74
|
+
Provides_,
|
|
75
|
+
Requires_,
|
|
76
|
+
ErrorSchema_ extends Schema.Codec<any, any>,
|
|
77
|
+
FunctionTypes_ extends FunctionType,
|
|
78
|
+
> {
|
|
79
|
+
new (_: never): {
|
|
80
|
+
readonly [TypeId]: TypeId;
|
|
81
|
+
readonly key: Key_;
|
|
82
|
+
};
|
|
83
|
+
readonly [TypeId]: TypeId;
|
|
84
|
+
readonly key: Key_;
|
|
85
|
+
readonly functionTypes: SupportedFunctionTypes;
|
|
86
|
+
readonly error?: ErrorSchema_;
|
|
87
|
+
readonly "~Provides": Provides_;
|
|
88
|
+
readonly "~Requires": Requires_;
|
|
89
|
+
readonly "~Error": ErrorSchema_;
|
|
90
|
+
readonly "~FunctionTypes": FunctionTypes_;
|
|
91
|
+
readonly "~Self": Self;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export interface AnyMiddlewareSpec {
|
|
95
|
+
readonly [TypeId]: TypeId;
|
|
96
|
+
readonly key: string;
|
|
97
|
+
readonly functionTypes: SupportedFunctionTypes;
|
|
98
|
+
readonly error?: Schema.Codec<any, any>;
|
|
99
|
+
readonly "~Provides": any;
|
|
100
|
+
readonly "~Requires": any;
|
|
101
|
+
readonly "~Error": any;
|
|
102
|
+
readonly "~FunctionTypes": FunctionType;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export type Key<MiddlewareSpec_ extends AnyMiddlewareSpec> =
|
|
106
|
+
MiddlewareSpec_["key"];
|
|
107
|
+
|
|
108
|
+
export type Provides<MiddlewareSpec_ extends AnyMiddlewareSpec> =
|
|
109
|
+
MiddlewareSpec_ extends AnyMiddlewareSpec
|
|
110
|
+
? MiddlewareSpec_["~Provides"]
|
|
111
|
+
: never;
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* The services a middleware's implementation may consume from middleware
|
|
115
|
+
* that runs earlier in the same chain (type-level only, like `provides`).
|
|
116
|
+
*/
|
|
117
|
+
export type Requires<MiddlewareSpec_ extends AnyMiddlewareSpec> =
|
|
118
|
+
MiddlewareSpec_ extends AnyMiddlewareSpec
|
|
119
|
+
? MiddlewareSpec_["~Requires"]
|
|
120
|
+
: never;
|
|
121
|
+
|
|
122
|
+
export type ErrorSchema<MiddlewareSpec_ extends AnyMiddlewareSpec> =
|
|
123
|
+
MiddlewareSpec_ extends AnyMiddlewareSpec ? MiddlewareSpec_["~Error"] : never;
|
|
124
|
+
|
|
125
|
+
export type Error<MiddlewareSpec_ extends AnyMiddlewareSpec> =
|
|
126
|
+
MiddlewareSpec_ extends AnyMiddlewareSpec
|
|
127
|
+
? MiddlewareSpec_["~Error"]["Type"]
|
|
128
|
+
: never;
|
|
129
|
+
|
|
130
|
+
export type EncodedError<MiddlewareSpec_ extends AnyMiddlewareSpec> =
|
|
131
|
+
MiddlewareSpec_ extends AnyMiddlewareSpec
|
|
132
|
+
? MiddlewareSpec_["~Error"]["Encoded"]
|
|
133
|
+
: never;
|
|
134
|
+
|
|
135
|
+
export type FunctionTypes<MiddlewareSpec_ extends AnyMiddlewareSpec> =
|
|
136
|
+
MiddlewareSpec_ extends AnyMiddlewareSpec
|
|
137
|
+
? MiddlewareSpec_["~FunctionTypes"]
|
|
138
|
+
: never;
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Declare a middleware's client-safe interface: its identifying key, the
|
|
142
|
+
* service it provides to downstream handlers, the error schema its failures
|
|
143
|
+
* encode through, and the function types it may attach to.
|
|
144
|
+
*
|
|
145
|
+
* ```ts
|
|
146
|
+
* class RequireUser extends MiddlewareSpec.MiddlewareSpec<RequireUser, {
|
|
147
|
+
* provides: CurrentUser
|
|
148
|
+
* }>()("RequireUser", {
|
|
149
|
+
* error: () => NotSignedIn,
|
|
150
|
+
* functionTypes: { query: true, mutation: true, action: false },
|
|
151
|
+
* }) {}
|
|
152
|
+
* ```
|
|
153
|
+
*
|
|
154
|
+
* The implementation is server-only and supplied separately via
|
|
155
|
+
* `MiddlewareImpl.make` (or `makeByFunctionType`/`provides`) in `@confect/server`.
|
|
156
|
+
*/
|
|
157
|
+
export const MiddlewareSpec =
|
|
158
|
+
<Self, Config extends { provides?: any; requires?: any } = {}>() =>
|
|
159
|
+
<
|
|
160
|
+
const Key_ extends string,
|
|
161
|
+
const FunctionTypesConfig_ extends SupportedFunctionTypes,
|
|
162
|
+
ErrorSchema_ extends Schema.Codec<any, any> = never,
|
|
163
|
+
>(
|
|
164
|
+
key: Key_,
|
|
165
|
+
options: {
|
|
166
|
+
readonly error?: () => ErrorSchema_;
|
|
167
|
+
readonly functionTypes: FunctionTypesConfig_ &
|
|
168
|
+
ValidateFunctionTypesConfig<FunctionTypesConfig_>;
|
|
169
|
+
},
|
|
170
|
+
): MiddlewareSpec<
|
|
171
|
+
Self,
|
|
172
|
+
Key_,
|
|
173
|
+
"provides" extends keyof Config ? Config["provides"] : never,
|
|
174
|
+
"requires" extends keyof Config ? Config["requires"] : never,
|
|
175
|
+
ErrorSchema_,
|
|
176
|
+
FunctionTypesFromConfig<FunctionTypesConfig_>
|
|
177
|
+
> => {
|
|
178
|
+
const { query, mutation, action } = options.functionTypes;
|
|
179
|
+
if (!query && !mutation && !action) {
|
|
180
|
+
throw new Error(
|
|
181
|
+
`Middleware "${key}" must declare at least one function type`,
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function MiddlewareSpecClass() {}
|
|
186
|
+
const class_ = MiddlewareSpecClass as any;
|
|
187
|
+
class_[TypeId] = TypeId;
|
|
188
|
+
class_.key = key;
|
|
189
|
+
class_.functionTypes = {
|
|
190
|
+
query,
|
|
191
|
+
mutation,
|
|
192
|
+
action,
|
|
193
|
+
} satisfies SupportedFunctionTypes;
|
|
194
|
+
if (options.error !== undefined) {
|
|
195
|
+
Lazy.defineProperty(class_, "error", options.error);
|
|
196
|
+
}
|
|
197
|
+
return class_;
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
type FunctionTypesFromConfig<Config extends SupportedFunctionTypes> = {
|
|
201
|
+
[K in FunctionType]: Config[K] extends true ? K : never;
|
|
202
|
+
}[FunctionType];
|
|
203
|
+
|
|
204
|
+
type ValidateFunctionTypesConfig<Config extends SupportedFunctionTypes> =
|
|
205
|
+
ValidationResult<
|
|
206
|
+
| {
|
|
207
|
+
[K in keyof Config]: boolean extends Config[K]
|
|
208
|
+
? AttachmentError<`Function type flag "${K & string}" must be a literal \`true\` or \`false\`, not a computed \`boolean\``>
|
|
209
|
+
: never;
|
|
210
|
+
}[keyof Config]
|
|
211
|
+
| ([FunctionTypesFromConfig<Config>] extends [never]
|
|
212
|
+
? AttachmentError<`A middleware must declare at least one function type`>
|
|
213
|
+
: never)
|
|
214
|
+
>;
|
|
215
|
+
|
|
216
|
+
export const errorSchemas = (
|
|
217
|
+
middlewareSpecs: ReadonlyArray<AnyMiddlewareSpec>,
|
|
218
|
+
): ReadonlyArray<Schema.Codec<any, any>> =>
|
|
219
|
+
middlewareSpecs.flatMap((middlewareSpec) =>
|
|
220
|
+
"error" in middlewareSpec && middlewareSpec.error !== undefined
|
|
221
|
+
? [middlewareSpec.error]
|
|
222
|
+
: [],
|
|
223
|
+
);
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* A branded, unconstructible type carrying a spec-authoring error message.
|
|
227
|
+
* Surfaced as (part of) a parameter type so the diagnostic names the exact
|
|
228
|
+
* problem at the `.middleware()` / `.addFunction()` call site.
|
|
229
|
+
*/
|
|
230
|
+
export interface AttachmentError<Message extends string> {
|
|
231
|
+
readonly "~confect/MiddlewareSpec/AttachmentError": Message;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
type FunctionTypeOf<FunctionSpec_ extends FunctionSpec.AnyWithProps> =
|
|
235
|
+
FunctionSpec_["runtimeAndFunctionType"]["functionType"];
|
|
236
|
+
|
|
237
|
+
type ValidationResult<Errors> = [Errors] extends [never] ? unknown : Errors;
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* A matching-type Convex-provenance function is rejected rather than
|
|
241
|
+
* silently skipped: its raw handler passes through Confect untouched, so
|
|
242
|
+
* the middleware could not actually cover it, and skipping it would be a
|
|
243
|
+
* policy hole.
|
|
244
|
+
*/
|
|
245
|
+
export type ValidateFunction<
|
|
246
|
+
FunctionSpec_ extends FunctionSpec.AnyWithProps,
|
|
247
|
+
MiddlewareSpec_ extends AnyMiddlewareSpec,
|
|
248
|
+
> = FunctionSpec_ extends any
|
|
249
|
+
? MiddlewareSpec_ extends any
|
|
250
|
+
? FunctionSpec_ extends {
|
|
251
|
+
readonly functionProvenance: { readonly _tag: "Convex" };
|
|
252
|
+
}
|
|
253
|
+
? FunctionTypeOf<FunctionSpec_> extends FunctionTypes<MiddlewareSpec_>
|
|
254
|
+
? AttachmentError<`Convex-provenance function "${FunctionSpec_["name"]}" cannot be covered by middleware "${Key<MiddlewareSpec_>}" — attach middleware only to groups whose matching-type functions are all Confect-provenance`>
|
|
255
|
+
: never
|
|
256
|
+
: [
|
|
257
|
+
Extract<
|
|
258
|
+
FunctionSpec.MiddlewareSpecs<FunctionSpec_>,
|
|
259
|
+
{ readonly key: Key<MiddlewareSpec_> }
|
|
260
|
+
>,
|
|
261
|
+
] extends [never]
|
|
262
|
+
? FunctionTypeOf<FunctionSpec_> extends FunctionTypes<MiddlewareSpec_>
|
|
263
|
+
? never
|
|
264
|
+
: AttachmentError<`Middleware "${Key<MiddlewareSpec_>}" does not declare function type "${FunctionTypeOf<FunctionSpec_>}", the type of function "${FunctionSpec_["name"]}"`>
|
|
265
|
+
: AttachmentError<`Middleware "${Key<MiddlewareSpec_>}" is already attached to function "${FunctionSpec_["name"]}"`>
|
|
266
|
+
: never
|
|
267
|
+
: never;
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Attachment order is chain order, so requiring `requires` to be satisfied
|
|
271
|
+
* by already-attached middleware is exactly requiring the provider to run
|
|
272
|
+
* earlier.
|
|
273
|
+
*/
|
|
274
|
+
export type ValidateAttach<
|
|
275
|
+
MiddlewareSpec_ extends AnyMiddlewareSpec,
|
|
276
|
+
Functions_ extends FunctionSpec.AnyWithProps,
|
|
277
|
+
MiddlewareSpecs_ extends AnyMiddlewareSpec,
|
|
278
|
+
> = [
|
|
279
|
+
Extract<MiddlewareSpecs_, { readonly key: Key<MiddlewareSpec_> }>,
|
|
280
|
+
] extends [never]
|
|
281
|
+
? [Exclude<Requires<MiddlewareSpec_>, Provides<MiddlewareSpecs_>>] extends [
|
|
282
|
+
never,
|
|
283
|
+
]
|
|
284
|
+
? ValidationResult<ValidateFunction<Functions_, MiddlewareSpec_>>
|
|
285
|
+
: AttachmentError<`Middleware "${Key<MiddlewareSpec_>}" requires services that no middleware attached earlier to this group provides — attach the providing middleware first`>
|
|
286
|
+
: AttachmentError<`Middleware "${Key<MiddlewareSpec_>}" is already attached to this group`>;
|
|
287
|
+
|
|
288
|
+
export type ValidateAddedFunction<
|
|
289
|
+
FunctionSpec_ extends FunctionSpec.AnyWithProps,
|
|
290
|
+
MiddlewareSpecs_ extends AnyMiddlewareSpec,
|
|
291
|
+
> = [MiddlewareSpecs_] extends [never]
|
|
292
|
+
? unknown
|
|
293
|
+
: ValidationResult<
|
|
294
|
+
| ValidateFunction<FunctionSpec_, MiddlewareSpecs_>
|
|
295
|
+
| GroupOverlap<FunctionSpec_, MiddlewareSpecs_>
|
|
296
|
+
>;
|
|
297
|
+
|
|
298
|
+
type GroupOverlap<
|
|
299
|
+
FunctionSpec_ extends FunctionSpec.AnyWithProps,
|
|
300
|
+
MiddlewareSpecs_ extends AnyMiddlewareSpec,
|
|
301
|
+
> =
|
|
302
|
+
FunctionSpec.MiddlewareSpecs<FunctionSpec_> extends infer FunctionMiddlewareSpec
|
|
303
|
+
? FunctionMiddlewareSpec extends AnyMiddlewareSpec
|
|
304
|
+
? [
|
|
305
|
+
Extract<
|
|
306
|
+
MiddlewareSpecs_,
|
|
307
|
+
{ readonly key: Key<FunctionMiddlewareSpec> }
|
|
308
|
+
>,
|
|
309
|
+
] extends [never]
|
|
310
|
+
? never
|
|
311
|
+
: AttachmentError<`Middleware "${Key<FunctionMiddlewareSpec>}" is attached to both function "${FunctionSpec_["name"]}" and its group`>
|
|
312
|
+
: never
|
|
313
|
+
: never;
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* The parameter-type validation applied to the group spec at
|
|
317
|
+
* `GroupImpl.make`: every function's attached middleware must have its
|
|
318
|
+
* `requires` services provided by some middleware covering that function
|
|
319
|
+
* (the group's or the function's own). Checked here — with the group fully
|
|
320
|
+
* assembled — rather than at the spec builders, because a function-level
|
|
321
|
+
* middleware's `requires` may legitimately be satisfied by a group
|
|
322
|
+
* middleware the function spec never sees, and group attachment is
|
|
323
|
+
* declaratively order-independent with respect to `addFunction`.
|
|
324
|
+
*/
|
|
325
|
+
export type ValidateImplRequires<
|
|
326
|
+
Functions_ extends FunctionSpec.AnyWithProps,
|
|
327
|
+
GroupMiddlewareSpecs_ extends AnyMiddlewareSpec,
|
|
328
|
+
> = ValidationResult<
|
|
329
|
+
Functions_ extends any
|
|
330
|
+
? [
|
|
331
|
+
Exclude<
|
|
332
|
+
Requires<FunctionSpec.MiddlewareSpecs<Functions_>>,
|
|
333
|
+
Provides<
|
|
334
|
+
GroupMiddlewareSpecs_ | FunctionSpec.MiddlewareSpecs<Functions_>
|
|
335
|
+
>
|
|
336
|
+
>,
|
|
337
|
+
] extends [never]
|
|
338
|
+
? never
|
|
339
|
+
: AttachmentError<`Function "${Functions_["name"]}" has middleware requiring services that no middleware covering it provides`>
|
|
340
|
+
: never
|
|
341
|
+
>;
|
|
342
|
+
|
|
343
|
+
export type ValidateFunctionAttach<
|
|
344
|
+
MiddlewareSpec_ extends AnyMiddlewareSpec,
|
|
345
|
+
RuntimeAndFunctionType_ extends RuntimeAndFunctionType.RuntimeAndFunctionType,
|
|
346
|
+
FunctionProvenance_ extends FunctionProvenance.FunctionProvenance,
|
|
347
|
+
MiddlewareSpecs_ extends AnyMiddlewareSpec,
|
|
348
|
+
> = FunctionProvenance_ extends { readonly _tag: "Convex" }
|
|
349
|
+
? AttachmentError<`Plain Convex functions cannot have middleware — their raw handlers are passed through untouched`>
|
|
350
|
+
: [
|
|
351
|
+
Extract<MiddlewareSpecs_, { readonly key: Key<MiddlewareSpec_> }>,
|
|
352
|
+
] extends [never]
|
|
353
|
+
? RuntimeAndFunctionType_["functionType"] extends FunctionTypes<MiddlewareSpec_>
|
|
354
|
+
? unknown
|
|
355
|
+
: AttachmentError<`Middleware "${Key<MiddlewareSpec_>}" does not declare function type "${RuntimeAndFunctionType_["functionType"]}"`>
|
|
356
|
+
: AttachmentError<`Middleware "${Key<MiddlewareSpec_>}" is already attached to this function`>;
|