@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
package/dist/FunctionSpec.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FunctionSpec.js","names":["FunctionProvenance.Confect","FunctionProvenance.ConfectPaginated","RuntimeAndFunctionType.ConvexQuery","RuntimeAndFunctionType.ConvexMutation","RuntimeAndFunctionType.ConvexAction","RuntimeAndFunctionType.NodeAction","FunctionProvenance.Convex"],"sources":["../src/FunctionSpec.ts"],"sourcesContent":["import type {\n FunctionType,\n FunctionVisibility,\n RegisteredAction,\n RegisteredMutation,\n RegisteredQuery,\n} from \"convex/server\";\nimport * as Schema from \"effect/Schema\";\nimport * as Predicate from \"effect/Predicate\";\nimport * as FunctionProvenance from \"./FunctionProvenance\";\nimport { validateConfectFunctionIdentifier } from \"./Identifier\";\nimport * as RuntimeAndFunctionType from \"./RuntimeAndFunctionType\";\n\nexport const TypeId = \"@confect/core/FunctionSpec\";\nexport type TypeId = typeof TypeId;\n\nexport const isFunctionSpec = (u: unknown): u is AnyWithProps =>\n Predicate.hasProperty(u, TypeId);\n\nexport interface FunctionSpec<\n RuntimeAndFunctionType_ extends RuntimeAndFunctionType.RuntimeAndFunctionType,\n FunctionVisibility_ extends FunctionVisibility,\n Name_ extends string,\n FunctionProvenance_ extends FunctionProvenance.FunctionProvenance,\n> {\n readonly [TypeId]: TypeId;\n readonly runtimeAndFunctionType: RuntimeAndFunctionType_;\n readonly functionVisibility: FunctionVisibility_;\n readonly name: Name_;\n readonly functionProvenance: FunctionProvenance_;\n}\n\nexport interface Any {\n readonly [TypeId]: TypeId;\n}\n\nexport interface AnyWithProps extends FunctionSpec<\n RuntimeAndFunctionType.RuntimeAndFunctionType,\n FunctionVisibility,\n string,\n FunctionProvenance.FunctionProvenance\n> {}\n\nexport interface AnyConfect extends FunctionSpec<\n RuntimeAndFunctionType.RuntimeAndFunctionType,\n FunctionVisibility,\n string,\n FunctionProvenance.AnyConfect\n> {}\n\nexport interface AnyConvex extends FunctionSpec<\n RuntimeAndFunctionType.RuntimeAndFunctionType,\n FunctionVisibility,\n string,\n FunctionProvenance.AnyConvex\n> {}\n\nexport interface AnyWithPropsWithRuntime<\n Runtime extends RuntimeAndFunctionType.Runtime,\n> extends FunctionSpec<\n RuntimeAndFunctionType.WithRuntime<Runtime>,\n FunctionVisibility,\n string,\n FunctionProvenance.FunctionProvenance\n> {}\n\nexport interface AnyWithPropsWithFunctionType<\n RuntimeAndFunctionType_ extends RuntimeAndFunctionType.RuntimeAndFunctionType,\n> extends FunctionSpec<\n RuntimeAndFunctionType_,\n FunctionVisibility,\n string,\n FunctionProvenance.FunctionProvenance\n> {}\n\nexport interface AnyWithPropsWithFunctionProvenance<\n FunctionProvenance_ extends FunctionProvenance.FunctionProvenance,\n> extends FunctionSpec<\n RuntimeAndFunctionType.RuntimeAndFunctionType,\n FunctionVisibility,\n string,\n FunctionProvenance_\n> {}\n\nexport type GetRuntimeAndFunctionType<FunctionSpec_ extends AnyWithProps> =\n FunctionSpec_[\"runtimeAndFunctionType\"];\n\nexport type GetFunctionVisibility<FunctionSpec_ extends AnyWithProps> =\n FunctionSpec_[\"functionVisibility\"];\n\nexport type Name<FunctionSpec_ extends AnyWithProps> = FunctionSpec_[\"name\"];\n\nexport type Args<FunctionSpec_ extends AnyWithProps> = FunctionSpec_ extends {\n functionProvenance: {\n _tag: \"Confect\";\n args: infer ArgsSchema_ extends Schema.Codec<any, any>;\n };\n}\n ? ArgsSchema_[\"Type\"]\n : FunctionSpec_ extends {\n functionProvenance: { _tag: \"Convex\"; _args: infer Args_ };\n }\n ? Args_\n : never;\n\nexport type Returns<FunctionSpec_ extends AnyWithProps> =\n FunctionSpec_ extends {\n functionProvenance: {\n _tag: \"Confect\";\n returns: infer ReturnsSchema_ extends Schema.Codec<any, any>;\n };\n }\n ? ReturnsSchema_[\"Type\"]\n : FunctionSpec_ extends {\n functionProvenance: { _tag: \"Convex\"; _returns: infer Returns_ };\n }\n ? Awaited<Returns_>\n : never;\n\nexport type EncodedArgs<FunctionSpec_ extends AnyWithProps> =\n FunctionSpec_ extends {\n functionProvenance: {\n _tag: \"Confect\";\n args: infer ArgsSchema_ extends Schema.Codec<any, any>;\n };\n }\n ? ArgsSchema_[\"Encoded\"]\n : FunctionSpec_ extends {\n functionProvenance: { _tag: \"Convex\"; _args: infer Args_ };\n }\n ? Args_\n : never;\n\nexport type EncodedReturns<FunctionSpec_ extends AnyWithProps> =\n FunctionSpec_ extends {\n functionProvenance: {\n _tag: \"Confect\";\n returns: infer ReturnsSchema_ extends Schema.Codec<any, any>;\n };\n }\n ? ReturnsSchema_[\"Encoded\"]\n : FunctionSpec_ extends {\n functionProvenance: { _tag: \"Convex\"; _returns: infer Returns_ };\n }\n ? Returns_\n : never;\n\nexport type Error<FunctionSpec_ extends AnyWithProps> = FunctionSpec_ extends {\n functionProvenance: FunctionProvenance.Confect<\n any,\n any,\n infer ErrorSchema_ extends Schema.Codec<any, any>\n >;\n}\n ? ErrorSchema_[\"Type\"]\n : never;\n\nexport type EncodedError<FunctionSpec_ extends AnyWithProps> =\n FunctionSpec_ extends {\n functionProvenance: FunctionProvenance.Confect<\n any,\n any,\n infer ErrorSchema_ extends Schema.Codec<any, any>\n >;\n }\n ? ErrorSchema_[\"Encoded\"]\n : never;\n\nexport type WithName<\n FunctionSpec_ extends AnyWithProps,\n Name_ extends string,\n> = Extract<FunctionSpec_, { readonly name: Name_ }>;\n\nexport type WithRuntimeAndFunctionType<\n FunctionSpec_ extends AnyWithProps,\n RuntimeAndFunctionType_ extends RuntimeAndFunctionType.RuntimeAndFunctionType,\n> = Extract<\n FunctionSpec_,\n { readonly runtimeAndFunctionType: RuntimeAndFunctionType_ }\n>;\n\nexport type WithFunctionType<\n FunctionSpec_ extends AnyWithProps,\n FunctionType_ extends FunctionType,\n> = Extract<\n FunctionSpec_,\n { readonly runtimeAndFunctionType: { readonly functionType: FunctionType_ } }\n>;\n\nexport type WithFunctionProvenance<\n FunctionSpec_ extends AnyWithProps,\n FunctionProvenance_ extends FunctionProvenance.FunctionProvenance,\n> = Extract<\n FunctionSpec_,\n { readonly functionProvenance: FunctionProvenance_ }\n>;\n\nexport type WithoutName<\n FunctionSpec_ extends AnyWithProps,\n Name_ extends Name<FunctionSpec_>,\n> = Exclude<FunctionSpec_, { readonly name: Name_ }>;\n\nconst Proto = {\n [TypeId]: TypeId,\n};\n\nconst make =\n <\n RuntimeAndFunctionType_ extends\n RuntimeAndFunctionType.RuntimeAndFunctionType,\n FunctionVisibility_ extends FunctionVisibility,\n >(\n runtimeAndFunctionType: RuntimeAndFunctionType_,\n functionVisibility: FunctionVisibility_,\n ) =>\n <\n const Name_ extends string,\n Args_ extends Schema.Codec<any, any>,\n Returns_ extends Schema.Codec<any, any>,\n Error_ extends Schema.Codec<any, any> = never,\n >({\n name,\n args,\n returns,\n error,\n }: {\n name: Name_;\n args: () => Args_;\n returns: () => Returns_;\n error?: () => Error_;\n }): FunctionSpec<\n RuntimeAndFunctionType_,\n FunctionVisibility_,\n Name_,\n FunctionProvenance.Confect<Args_, Returns_, Error_>\n > => {\n validateConfectFunctionIdentifier(name);\n\n return Object.assign(Object.create(Proto), {\n runtimeAndFunctionType,\n functionVisibility,\n name,\n functionProvenance: FunctionProvenance.Confect(args, returns, error),\n });\n };\n\n/**\n * `Schema.Struct` fields that must not declare `paginationOpts`. Used to\n * reject a user args schema that redeclares the field the paginated\n * constructors add themselves; the check reports at the `args` thunk with an\n * unsatisfiable type when violated.\n */\ntype ForbidPaginationOpts<UserArgs extends FunctionProvenance.AnyUserArgs> =\n \"paginationOpts\" extends keyof UserArgs[\"fields\"]\n ? { readonly fields: { readonly paginationOpts: never } }\n : unknown;\n\nconst makePaginated =\n <\n RuntimeAndFunctionType_ extends\n RuntimeAndFunctionType.RuntimeAndFunctionType,\n FunctionVisibility_ extends FunctionVisibility,\n >(\n runtimeAndFunctionType: RuntimeAndFunctionType_,\n functionVisibility: FunctionVisibility_,\n ) =>\n <\n const Name_ extends string,\n Item_ extends Schema.Codec<any, any>,\n UserArgs_ extends FunctionProvenance.AnyUserArgs = Schema.Struct<{}>,\n Error_ extends Schema.Codec<any, any> = never,\n >({\n name,\n args,\n item,\n error,\n }: {\n name: Name_;\n /** User-declared args, without `paginationOpts` — it is added automatically. */\n args?: (() => UserArgs_) & ForbidPaginationOpts<UserArgs_>;\n /** The page element schema. */\n item: () => Item_;\n error?: () => Error_;\n }): FunctionSpec<\n RuntimeAndFunctionType_,\n FunctionVisibility_,\n Name_,\n FunctionProvenance.ConfectPaginated<UserArgs_, Item_, Error_>\n > => {\n validateConfectFunctionIdentifier(name);\n\n return Object.assign(Object.create(Proto), {\n runtimeAndFunctionType,\n functionVisibility,\n name,\n functionProvenance: FunctionProvenance.ConfectPaginated(\n // When `args` is omitted, `UserArgs_` is its `Schema.Struct<{}>` default.\n args ?? ((() => Schema.Struct({})) as unknown as () => UserArgs_),\n item,\n error,\n ),\n });\n };\n\nexport const publicQuery = make(RuntimeAndFunctionType.ConvexQuery, \"public\");\nexport const internalQuery = make(\n RuntimeAndFunctionType.ConvexQuery,\n \"internal\",\n);\nexport const publicPaginatedQuery = makePaginated(\n RuntimeAndFunctionType.ConvexQuery,\n \"public\",\n);\nexport const internalPaginatedQuery = makePaginated(\n RuntimeAndFunctionType.ConvexQuery,\n \"internal\",\n);\nexport const publicMutation = make(\n RuntimeAndFunctionType.ConvexMutation,\n \"public\",\n);\nexport const internalMutation = make(\n RuntimeAndFunctionType.ConvexMutation,\n \"internal\",\n);\nexport const publicAction = make(RuntimeAndFunctionType.ConvexAction, \"public\");\nexport const internalAction = make(\n RuntimeAndFunctionType.ConvexAction,\n \"internal\",\n);\n\nexport const publicNodeAction = make(\n RuntimeAndFunctionType.NodeAction,\n \"public\",\n);\nexport const internalNodeAction = make(\n RuntimeAndFunctionType.NodeAction,\n \"internal\",\n);\n\ntype MatchingRegisteredFunction<\n RuntimeAndFunctionType_ extends RuntimeAndFunctionType.RuntimeAndFunctionType,\n FunctionVisibility_ extends FunctionVisibility,\n> =\n RuntimeAndFunctionType.GetFunctionType<RuntimeAndFunctionType_> extends \"query\"\n ? RegisteredQuery<FunctionVisibility_, any, any>\n : RuntimeAndFunctionType.GetFunctionType<RuntimeAndFunctionType_> extends \"mutation\"\n ? RegisteredMutation<FunctionVisibility_, any, any>\n : RuntimeAndFunctionType.GetFunctionType<RuntimeAndFunctionType_> extends \"action\"\n ? RegisteredAction<FunctionVisibility_, any, any>\n : never;\n\ntype ExtractArgs<F> =\n F extends RegisteredQuery<any, infer A, any>\n ? A\n : F extends RegisteredMutation<any, infer A, any>\n ? A\n : F extends RegisteredAction<any, infer A, any>\n ? A\n : never;\n\ntype ExtractReturns<F> =\n F extends RegisteredQuery<any, any, infer R>\n ? R\n : F extends RegisteredMutation<any, any, infer R>\n ? R\n : F extends RegisteredAction<any, any, infer R>\n ? R\n : never;\n\nconst makeConvex =\n <\n RuntimeAndFunctionType_ extends\n RuntimeAndFunctionType.RuntimeAndFunctionType,\n FunctionVisibility_ extends FunctionVisibility,\n >(\n runtimeAndFunctionType: RuntimeAndFunctionType_,\n functionVisibility: FunctionVisibility_,\n ) =>\n <\n F extends MatchingRegisteredFunction<\n RuntimeAndFunctionType_,\n FunctionVisibility_\n >,\n >() =>\n <const Name_ extends string>(\n name: Name_,\n ): FunctionSpec<\n RuntimeAndFunctionType_,\n FunctionVisibility_,\n Name_,\n FunctionProvenance.Convex<ExtractArgs<F>, ExtractReturns<F>>\n > => {\n validateConfectFunctionIdentifier(name);\n\n return Object.assign(Object.create(Proto), {\n runtimeAndFunctionType,\n functionVisibility,\n name,\n functionProvenance: FunctionProvenance.Convex<\n ExtractArgs<F>,\n ExtractReturns<F>\n >(),\n }) as any;\n };\n\nexport const convexPublicQuery = makeConvex(\n RuntimeAndFunctionType.ConvexQuery,\n \"public\",\n);\nexport const convexInternalQuery = makeConvex(\n RuntimeAndFunctionType.ConvexQuery,\n \"internal\",\n);\nexport const convexPublicMutation = makeConvex(\n RuntimeAndFunctionType.ConvexMutation,\n \"public\",\n);\nexport const convexInternalMutation = makeConvex(\n RuntimeAndFunctionType.ConvexMutation,\n \"internal\",\n);\nexport const convexPublicAction = makeConvex(\n RuntimeAndFunctionType.ConvexAction,\n \"public\",\n);\nexport const convexInternalAction = makeConvex(\n RuntimeAndFunctionType.ConvexAction,\n \"internal\",\n);\nexport const convexPublicNodeAction = makeConvex(\n RuntimeAndFunctionType.NodeAction,\n \"public\",\n);\nexport const convexInternalNodeAction = makeConvex(\n RuntimeAndFunctionType.NodeAction,\n \"internal\",\n);\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAaA,MAAa,SAAS;AAGtB,MAAa,kBAAkB,MAC7B,UAAU,YAAY,GAAG,MAAM;AAyLjC,MAAM,QAAQ,GACX,SAAS,OACZ;AAEA,MAAM,QAMF,wBACA,wBAOA,EACA,MACA,MACA,SACA,YAWG;CACH,kCAAkC,IAAI;CAEtC,OAAO,OAAO,OAAO,OAAO,OAAO,KAAK,GAAG;EACzC;EACA;EACA;EACA,oBAAoBA,QAA2B,MAAM,SAAS,KAAK;CACrE,CAAC;AACH;AAaF,MAAM,iBAMF,wBACA,wBAOA,EACA,MACA,MACA,MACA,YAaG;CACH,kCAAkC,IAAI;CAEtC,OAAO,OAAO,OAAO,OAAO,OAAO,KAAK,GAAG;EACzC;EACA;EACA;EACA,oBAAoBC,iBAElB,eAAgB,OAAO,OAAO,CAAC,CAAC,IAChC,MACA,KACF;CACF,CAAC;AACH;AAEF,MAAa,cAAc,KAAKC,aAAoC,QAAQ;AAC5E,MAAa,gBAAgB,KAC3BA,aACA,UACF;AACA,MAAa,uBAAuB,cAClCA,aACA,QACF;AACA,MAAa,yBAAyB,cACpCA,aACA,UACF;AACA,MAAa,iBAAiB,KAC5BC,gBACA,QACF;AACA,MAAa,mBAAmB,KAC9BA,gBACA,UACF;AACA,MAAa,eAAe,KAAKC,cAAqC,QAAQ;AAC9E,MAAa,iBAAiB,KAC5BA,cACA,UACF;AAEA,MAAa,mBAAmB,KAC9BC,YACA,QACF;AACA,MAAa,qBAAqB,KAChCA,YACA,UACF;AAgCA,MAAM,cAMF,wBACA,8BASA,SAMG;CACH,kCAAkC,IAAI;CAEtC,OAAO,OAAO,OAAO,OAAO,OAAO,KAAK,GAAG;EACzC;EACA;EACA;EACA,oBAAoBC,OAGlB;CACJ,CAAC;AACH;AAEF,MAAa,oBAAoB,WAC/BJ,aACA,QACF;AACA,MAAa,sBAAsB,WACjCA,aACA,UACF;AACA,MAAa,uBAAuB,WAClCC,gBACA,QACF;AACA,MAAa,yBAAyB,WACpCA,gBACA,UACF;AACA,MAAa,qBAAqB,WAChCC,cACA,QACF;AACA,MAAa,uBAAuB,WAClCA,cACA,UACF;AACA,MAAa,yBAAyB,WACpCC,YACA,QACF;AACA,MAAa,2BAA2B,WACtCA,YACA,UACF"}
|
|
1
|
+
{"version":3,"file":"FunctionSpec.js","names":["FunctionProvenance.Confect","FunctionProvenance.ConfectPaginated","RuntimeAndFunctionType.ConvexQuery","RuntimeAndFunctionType.ConvexMutation","RuntimeAndFunctionType.ConvexAction","RuntimeAndFunctionType.NodeAction","FunctionProvenance.Convex"],"sources":["../src/FunctionSpec.ts"],"sourcesContent":["import type {\n FunctionType,\n FunctionVisibility,\n RegisteredAction,\n RegisteredMutation,\n RegisteredQuery,\n} from \"convex/server\";\nimport * as Schema from \"effect/Schema\";\nimport * as Predicate from \"effect/Predicate\";\nimport * as FunctionProvenance from \"./FunctionProvenance\";\nimport { validateConfectFunctionIdentifier } from \"./Identifier\";\nimport type * as MiddlewareSpec from \"./MiddlewareSpec\";\nimport * as RuntimeAndFunctionType from \"./RuntimeAndFunctionType\";\n\nexport const TypeId = \"@confect/core/FunctionSpec\";\nexport type TypeId = typeof TypeId;\n\nexport const isFunctionSpec = (u: unknown): u is AnyWithProps =>\n Predicate.hasProperty(u, TypeId);\n\nexport interface FunctionSpec<\n RuntimeAndFunctionType_ extends RuntimeAndFunctionType.RuntimeAndFunctionType,\n FunctionVisibility_ extends FunctionVisibility,\n Name_ extends string,\n FunctionProvenance_ extends FunctionProvenance.FunctionProvenance,\n MiddlewareSpecs_ extends MiddlewareSpec.AnyMiddlewareSpec = never,\n> {\n readonly [TypeId]: TypeId;\n readonly runtimeAndFunctionType: RuntimeAndFunctionType_;\n readonly functionVisibility: FunctionVisibility_;\n readonly name: Name_;\n readonly functionProvenance: FunctionProvenance_;\n readonly middlewareSpecs: ReadonlyArray<MiddlewareSpecs_>;\n}\n\nexport interface Builder<\n RuntimeAndFunctionType_ extends RuntimeAndFunctionType.RuntimeAndFunctionType,\n FunctionVisibility_ extends FunctionVisibility,\n Name_ extends string,\n FunctionProvenance_ extends FunctionProvenance.FunctionProvenance,\n MiddlewareSpecs_ extends MiddlewareSpec.AnyMiddlewareSpec = never,\n> extends FunctionSpec<\n RuntimeAndFunctionType_,\n FunctionVisibility_,\n Name_,\n FunctionProvenance_,\n MiddlewareSpecs_\n> {\n middleware<MiddlewareSpec_ extends MiddlewareSpec.AnyMiddlewareSpec>(\n middlewareSpec: MiddlewareSpec_ &\n MiddlewareSpec.ValidateFunctionAttach<\n MiddlewareSpec_,\n RuntimeAndFunctionType_,\n FunctionProvenance_,\n MiddlewareSpecs_\n >,\n ): Builder<\n RuntimeAndFunctionType_,\n FunctionVisibility_,\n Name_,\n FunctionProvenance_,\n MiddlewareSpecs_ | MiddlewareSpec_\n >;\n}\n\nexport interface Any {\n readonly [TypeId]: TypeId;\n}\n\nexport interface AnyWithProps extends FunctionSpec<\n RuntimeAndFunctionType.RuntimeAndFunctionType,\n FunctionVisibility,\n string,\n FunctionProvenance.FunctionProvenance,\n MiddlewareSpec.AnyMiddlewareSpec\n> {}\n\nexport interface AnyConfect extends FunctionSpec<\n RuntimeAndFunctionType.RuntimeAndFunctionType,\n FunctionVisibility,\n string,\n FunctionProvenance.AnyConfect,\n MiddlewareSpec.AnyMiddlewareSpec\n> {}\n\nexport interface AnyConvex extends FunctionSpec<\n RuntimeAndFunctionType.RuntimeAndFunctionType,\n FunctionVisibility,\n string,\n FunctionProvenance.AnyConvex,\n MiddlewareSpec.AnyMiddlewareSpec\n> {}\n\nexport interface AnyWithPropsWithRuntime<\n Runtime extends RuntimeAndFunctionType.Runtime,\n> extends FunctionSpec<\n RuntimeAndFunctionType.WithRuntime<Runtime>,\n FunctionVisibility,\n string,\n FunctionProvenance.FunctionProvenance,\n MiddlewareSpec.AnyMiddlewareSpec\n> {}\n\nexport interface AnyWithPropsWithFunctionType<\n RuntimeAndFunctionType_ extends RuntimeAndFunctionType.RuntimeAndFunctionType,\n> extends FunctionSpec<\n RuntimeAndFunctionType_,\n FunctionVisibility,\n string,\n FunctionProvenance.FunctionProvenance,\n MiddlewareSpec.AnyMiddlewareSpec\n> {}\n\nexport interface AnyWithPropsWithFunctionProvenance<\n FunctionProvenance_ extends FunctionProvenance.FunctionProvenance,\n> extends FunctionSpec<\n RuntimeAndFunctionType.RuntimeAndFunctionType,\n FunctionVisibility,\n string,\n FunctionProvenance_,\n MiddlewareSpec.AnyMiddlewareSpec\n> {}\n\nexport type GetRuntimeAndFunctionType<FunctionSpec_ extends AnyWithProps> =\n FunctionSpec_[\"runtimeAndFunctionType\"];\n\nexport type GetFunctionVisibility<FunctionSpec_ extends AnyWithProps> =\n FunctionSpec_[\"functionVisibility\"];\n\nexport type Name<FunctionSpec_ extends AnyWithProps> = FunctionSpec_[\"name\"];\n\nexport type MiddlewareSpecs<FunctionSpec_ extends AnyWithProps> =\n FunctionSpec_[\"middlewareSpecs\"][number];\n\nexport type Args<FunctionSpec_ extends AnyWithProps> = FunctionSpec_ extends {\n functionProvenance: {\n _tag: \"Confect\";\n args: infer ArgsSchema_ extends Schema.Codec<any, any>;\n };\n}\n ? ArgsSchema_[\"Type\"]\n : FunctionSpec_ extends {\n functionProvenance: { _tag: \"Convex\"; _args: infer Args_ };\n }\n ? Args_\n : never;\n\nexport type Returns<FunctionSpec_ extends AnyWithProps> =\n FunctionSpec_ extends {\n functionProvenance: {\n _tag: \"Confect\";\n returns: infer ReturnsSchema_ extends Schema.Codec<any, any>;\n };\n }\n ? ReturnsSchema_[\"Type\"]\n : FunctionSpec_ extends {\n functionProvenance: { _tag: \"Convex\"; _returns: infer Returns_ };\n }\n ? Awaited<Returns_>\n : never;\n\nexport type EncodedArgs<FunctionSpec_ extends AnyWithProps> =\n FunctionSpec_ extends {\n functionProvenance: {\n _tag: \"Confect\";\n args: infer ArgsSchema_ extends Schema.Codec<any, any>;\n };\n }\n ? ArgsSchema_[\"Encoded\"]\n : FunctionSpec_ extends {\n functionProvenance: { _tag: \"Convex\"; _args: infer Args_ };\n }\n ? Args_\n : never;\n\nexport type EncodedReturns<FunctionSpec_ extends AnyWithProps> =\n FunctionSpec_ extends {\n functionProvenance: {\n _tag: \"Confect\";\n returns: infer ReturnsSchema_ extends Schema.Codec<any, any>;\n };\n }\n ? ReturnsSchema_[\"Encoded\"]\n : FunctionSpec_ extends {\n functionProvenance: { _tag: \"Convex\"; _returns: infer Returns_ };\n }\n ? Returns_\n : never;\n\nexport type Error<FunctionSpec_ extends AnyWithProps> = FunctionSpec_ extends {\n functionProvenance: FunctionProvenance.Confect<\n any,\n any,\n infer ErrorSchema_ extends Schema.Codec<any, any>\n >;\n}\n ? ErrorSchema_[\"Type\"]\n : never;\n\nexport type EncodedError<FunctionSpec_ extends AnyWithProps> =\n FunctionSpec_ extends {\n functionProvenance: FunctionProvenance.Confect<\n any,\n any,\n infer ErrorSchema_ extends Schema.Codec<any, any>\n >;\n }\n ? ErrorSchema_[\"Encoded\"]\n : never;\n\nexport type WithName<\n FunctionSpec_ extends AnyWithProps,\n Name_ extends string,\n> = Extract<FunctionSpec_, { readonly name: Name_ }>;\n\nexport type WithRuntimeAndFunctionType<\n FunctionSpec_ extends AnyWithProps,\n RuntimeAndFunctionType_ extends RuntimeAndFunctionType.RuntimeAndFunctionType,\n> = Extract<\n FunctionSpec_,\n { readonly runtimeAndFunctionType: RuntimeAndFunctionType_ }\n>;\n\nexport type WithFunctionType<\n FunctionSpec_ extends AnyWithProps,\n FunctionType_ extends FunctionType,\n> = Extract<\n FunctionSpec_,\n { readonly runtimeAndFunctionType: { readonly functionType: FunctionType_ } }\n>;\n\nexport type WithFunctionProvenance<\n FunctionSpec_ extends AnyWithProps,\n FunctionProvenance_ extends FunctionProvenance.FunctionProvenance,\n> = Extract<\n FunctionSpec_,\n { readonly functionProvenance: FunctionProvenance_ }\n>;\n\nexport type WithoutName<\n FunctionSpec_ extends AnyWithProps,\n Name_ extends Name<FunctionSpec_>,\n> = Exclude<FunctionSpec_, { readonly name: Name_ }>;\n\nconst Proto = {\n [TypeId]: TypeId,\n\n middleware(\n this: AnyWithProps,\n middlewareSpec: MiddlewareSpec.AnyMiddlewareSpec,\n ) {\n if (this.functionProvenance._tag === \"Convex\") {\n throw new Error(\n `Plain Convex function \"${this.name}\" cannot have middleware`,\n );\n }\n const functionType = this.runtimeAndFunctionType.functionType;\n if (!middlewareSpec.functionTypes[functionType]) {\n throw new Error(\n `Middleware \"${middlewareSpec.key}\" does not declare function type \"${functionType}\" of function \"${this.name}\"`,\n );\n }\n if (\n this.middlewareSpecs.some(\n (existing) => existing.key === middlewareSpec.key,\n )\n ) {\n throw new Error(\n `Middleware \"${middlewareSpec.key}\" is already attached to function \"${this.name}\"`,\n );\n }\n\n return Object.assign(Object.create(Proto), {\n runtimeAndFunctionType: this.runtimeAndFunctionType,\n functionVisibility: this.functionVisibility,\n name: this.name,\n functionProvenance: this.functionProvenance,\n middlewareSpecs: [...this.middlewareSpecs, middlewareSpec],\n });\n },\n};\n\nconst make =\n <\n RuntimeAndFunctionType_ extends\n RuntimeAndFunctionType.RuntimeAndFunctionType,\n FunctionVisibility_ extends FunctionVisibility,\n >(\n runtimeAndFunctionType: RuntimeAndFunctionType_,\n functionVisibility: FunctionVisibility_,\n ) =>\n <\n const Name_ extends string,\n Args_ extends Schema.Codec<any, any>,\n Returns_ extends Schema.Codec<any, any>,\n Error_ extends Schema.Codec<any, any> = never,\n >({\n name,\n args,\n returns,\n error,\n }: {\n name: Name_;\n args: () => Args_;\n returns: () => Returns_;\n error?: () => Error_;\n }): Builder<\n RuntimeAndFunctionType_,\n FunctionVisibility_,\n Name_,\n FunctionProvenance.Confect<Args_, Returns_, Error_>\n > => {\n validateConfectFunctionIdentifier(name);\n\n return Object.assign(Object.create(Proto), {\n runtimeAndFunctionType,\n functionVisibility,\n name,\n functionProvenance: FunctionProvenance.Confect(args, returns, error),\n middlewareSpecs: [],\n });\n };\n\n/**\n * `Schema.Struct` fields that must not declare `paginationOpts`. Used to\n * reject a user args schema that redeclares the field the paginated\n * constructors add themselves; the check reports at the `args` thunk with an\n * unsatisfiable type when violated.\n */\ntype ForbidPaginationOpts<UserArgs extends FunctionProvenance.AnyUserArgs> =\n \"paginationOpts\" extends keyof UserArgs[\"fields\"]\n ? { readonly fields: { readonly paginationOpts: never } }\n : unknown;\n\nconst makePaginated =\n <\n RuntimeAndFunctionType_ extends\n RuntimeAndFunctionType.RuntimeAndFunctionType,\n FunctionVisibility_ extends FunctionVisibility,\n >(\n runtimeAndFunctionType: RuntimeAndFunctionType_,\n functionVisibility: FunctionVisibility_,\n ) =>\n <\n const Name_ extends string,\n Item_ extends Schema.Codec<any, any>,\n UserArgs_ extends FunctionProvenance.AnyUserArgs = Schema.Struct<{}>,\n Error_ extends Schema.Codec<any, any> = never,\n >({\n name,\n args,\n item,\n error,\n }: {\n name: Name_;\n /** User-declared args, without `paginationOpts` — it is added automatically. */\n args?: (() => UserArgs_) & ForbidPaginationOpts<UserArgs_>;\n /** The page element schema. */\n item: () => Item_;\n error?: () => Error_;\n }): Builder<\n RuntimeAndFunctionType_,\n FunctionVisibility_,\n Name_,\n FunctionProvenance.ConfectPaginated<UserArgs_, Item_, Error_>\n > => {\n validateConfectFunctionIdentifier(name);\n\n return Object.assign(Object.create(Proto), {\n runtimeAndFunctionType,\n functionVisibility,\n name,\n functionProvenance: FunctionProvenance.ConfectPaginated(\n // When `args` is omitted, `UserArgs_` is its `Schema.Struct<{}>` default.\n args ?? ((() => Schema.Struct({})) as unknown as () => UserArgs_),\n item,\n error,\n ),\n middlewareSpecs: [],\n });\n };\n\nexport const publicQuery = make(RuntimeAndFunctionType.ConvexQuery, \"public\");\nexport const internalQuery = make(\n RuntimeAndFunctionType.ConvexQuery,\n \"internal\",\n);\nexport const publicPaginatedQuery = makePaginated(\n RuntimeAndFunctionType.ConvexQuery,\n \"public\",\n);\nexport const internalPaginatedQuery = makePaginated(\n RuntimeAndFunctionType.ConvexQuery,\n \"internal\",\n);\nexport const publicMutation = make(\n RuntimeAndFunctionType.ConvexMutation,\n \"public\",\n);\nexport const internalMutation = make(\n RuntimeAndFunctionType.ConvexMutation,\n \"internal\",\n);\nexport const publicAction = make(RuntimeAndFunctionType.ConvexAction, \"public\");\nexport const internalAction = make(\n RuntimeAndFunctionType.ConvexAction,\n \"internal\",\n);\n\nexport const publicNodeAction = make(\n RuntimeAndFunctionType.NodeAction,\n \"public\",\n);\nexport const internalNodeAction = make(\n RuntimeAndFunctionType.NodeAction,\n \"internal\",\n);\n\ntype MatchingRegisteredFunction<\n RuntimeAndFunctionType_ extends RuntimeAndFunctionType.RuntimeAndFunctionType,\n FunctionVisibility_ extends FunctionVisibility,\n> =\n RuntimeAndFunctionType.GetFunctionType<RuntimeAndFunctionType_> extends \"query\"\n ? RegisteredQuery<FunctionVisibility_, any, any>\n : RuntimeAndFunctionType.GetFunctionType<RuntimeAndFunctionType_> extends \"mutation\"\n ? RegisteredMutation<FunctionVisibility_, any, any>\n : RuntimeAndFunctionType.GetFunctionType<RuntimeAndFunctionType_> extends \"action\"\n ? RegisteredAction<FunctionVisibility_, any, any>\n : never;\n\ntype ExtractArgs<F> =\n F extends RegisteredQuery<any, infer A, any>\n ? A\n : F extends RegisteredMutation<any, infer A, any>\n ? A\n : F extends RegisteredAction<any, infer A, any>\n ? A\n : never;\n\ntype ExtractReturns<F> =\n F extends RegisteredQuery<any, any, infer R>\n ? R\n : F extends RegisteredMutation<any, any, infer R>\n ? R\n : F extends RegisteredAction<any, any, infer R>\n ? R\n : never;\n\nconst makeConvex =\n <\n RuntimeAndFunctionType_ extends\n RuntimeAndFunctionType.RuntimeAndFunctionType,\n FunctionVisibility_ extends FunctionVisibility,\n >(\n runtimeAndFunctionType: RuntimeAndFunctionType_,\n functionVisibility: FunctionVisibility_,\n ) =>\n <\n F extends MatchingRegisteredFunction<\n RuntimeAndFunctionType_,\n FunctionVisibility_\n >,\n >() =>\n <const Name_ extends string>(\n name: Name_,\n ): Builder<\n RuntimeAndFunctionType_,\n FunctionVisibility_,\n Name_,\n FunctionProvenance.Convex<ExtractArgs<F>, ExtractReturns<F>>\n > => {\n validateConfectFunctionIdentifier(name);\n\n return Object.assign(Object.create(Proto), {\n runtimeAndFunctionType,\n functionVisibility,\n name,\n functionProvenance: FunctionProvenance.Convex<\n ExtractArgs<F>,\n ExtractReturns<F>\n >(),\n middlewareSpecs: [],\n }) as any;\n };\n\nexport const convexPublicQuery = makeConvex(\n RuntimeAndFunctionType.ConvexQuery,\n \"public\",\n);\nexport const convexInternalQuery = makeConvex(\n RuntimeAndFunctionType.ConvexQuery,\n \"internal\",\n);\nexport const convexPublicMutation = makeConvex(\n RuntimeAndFunctionType.ConvexMutation,\n \"public\",\n);\nexport const convexInternalMutation = makeConvex(\n RuntimeAndFunctionType.ConvexMutation,\n \"internal\",\n);\nexport const convexPublicAction = makeConvex(\n RuntimeAndFunctionType.ConvexAction,\n \"public\",\n);\nexport const convexInternalAction = makeConvex(\n RuntimeAndFunctionType.ConvexAction,\n \"internal\",\n);\nexport const convexPublicNodeAction = makeConvex(\n RuntimeAndFunctionType.NodeAction,\n \"public\",\n);\nexport const convexInternalNodeAction = makeConvex(\n RuntimeAndFunctionType.NodeAction,\n \"internal\",\n);\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAcA,MAAa,SAAS;AAGtB,MAAa,kBAAkB,MAC7B,UAAU,YAAY,GAAG,MAAM;AAkOjC,MAAM,QAAQ;EACX,SAAS;CAEV,WAEE,gBACA;EACA,IAAI,KAAK,mBAAmB,SAAS,UACnC,MAAM,IAAI,MACR,0BAA0B,KAAK,KAAK,yBACtC;EAEF,MAAM,eAAe,KAAK,uBAAuB;EACjD,IAAI,CAAC,eAAe,cAAc,eAChC,MAAM,IAAI,MACR,eAAe,eAAe,IAAI,oCAAoC,aAAa,iBAAi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ACH,kCAAkC,IAAI;CAEtC,OAAO,OAAO,OAAO,OAAO,OAAO,KAAK,GAAG;EACzC;EACA;EACA;EACA,oBAAoBC,OAGlB;EACF,iBAAiB,CAAC;CACpB,CAAC;AACH;AAEF,MAAa,oBAAoB,WAC/BJ,aACA,QACF;AACA,MAAa,sBAAsB,WACjCA,aACA,UACF;AACA,MAAa,uBAAuB,WAClCC,gBACA,QACF;AACA,MAAa,yBAAyB,WACpCA,gBACA,UACF;AACA,MAAa,qBAAqB,WAChCC,cACA,QACF;AACA,MAAa,uBAAuB,WAClCA,cACA,UACF;AACA,MAAa,yBAAyB,WACpCC,YACA,QACF;AACA,MAAa,2BAA2B,WACtCA,YACA,UACF"}
|
package/dist/GroupSpec.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import type * as FunctionSpec from "./FunctionSpec";
|
|
2
|
+
import type * as MiddlewareSpec from "./MiddlewareSpec";
|
|
2
3
|
import type * as RuntimeAndFunctionType from "./RuntimeAndFunctionType";
|
|
3
4
|
export declare const TypeId = "@confect/core/GroupSpec";
|
|
4
5
|
export type TypeId = typeof TypeId;
|
|
5
6
|
export declare const isGroupSpec: (u: unknown) => u is AnyWithProps;
|
|
6
|
-
export interface GroupSpec<Runtime extends RuntimeAndFunctionType.Runtime, Name_ extends string, Functions_ extends FunctionSpec.AnyWithPropsWithRuntime<Runtime> = never, Groups_ extends AnyWithProps = never> {
|
|
7
|
+
export interface GroupSpec<Runtime extends RuntimeAndFunctionType.Runtime, Name_ extends string, Functions_ extends FunctionSpec.AnyWithPropsWithRuntime<Runtime> = never, Groups_ extends AnyWithProps = never, MiddlewareSpecs_ extends MiddlewareSpec.AnyMiddlewareSpec = never> {
|
|
7
8
|
readonly [TypeId]: TypeId;
|
|
8
9
|
readonly runtime: Runtime;
|
|
9
10
|
readonly name: Name_;
|
|
@@ -13,20 +14,23 @@ export interface GroupSpec<Runtime extends RuntimeAndFunctionType.Runtime, Name_
|
|
|
13
14
|
readonly groups: {
|
|
14
15
|
[GroupName in Name<Groups_>]: WithName<Groups_, GroupName>;
|
|
15
16
|
};
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
readonly middlewareSpecs: ReadonlyArray<MiddlewareSpecs_>;
|
|
18
|
+
addFunction<Function extends FunctionSpec.AnyWithPropsWithRuntime<Runtime>>(function_: Function & MiddlewareSpec.ValidateAddedFunction<Function, MiddlewareSpecs_>): GroupSpec<Runtime, Name_, Functions_ | Function, Groups_, MiddlewareSpecs_>;
|
|
19
|
+
addGroup<Group extends AnyWithProps>(group: Group): GroupSpec<Runtime, Name_, Functions_, Groups_ | Group, MiddlewareSpecs_>;
|
|
20
|
+
addGroupAt<const AtName extends string, Group extends AnyWithProps>(name: AtName, group: Group): GroupSpec<Runtime, Name_, Functions_, Groups_ | NamedAt<Group, AtName>, MiddlewareSpecs_>;
|
|
21
|
+
middleware<MiddlewareSpec_ extends MiddlewareSpec.AnyMiddlewareSpec>(middlewareSpec: MiddlewareSpec_ & MiddlewareSpec.ValidateAttach<MiddlewareSpec_, Functions_, MiddlewareSpecs_>): GroupSpec<Runtime, Name_, Functions_, Groups_, MiddlewareSpecs_ | MiddlewareSpec_>;
|
|
19
22
|
}
|
|
20
23
|
export interface Any {
|
|
21
24
|
readonly [TypeId]: TypeId;
|
|
22
25
|
}
|
|
23
|
-
export interface AnyWithProps extends GroupSpec<RuntimeAndFunctionType.Runtime, string, FunctionSpec.AnyWithProps, AnyWithProps> {
|
|
26
|
+
export interface AnyWithProps extends GroupSpec<RuntimeAndFunctionType.Runtime, string, FunctionSpec.AnyWithProps, AnyWithProps, MiddlewareSpec.AnyMiddlewareSpec> {
|
|
24
27
|
}
|
|
25
|
-
export interface AnyWithPropsWithRuntime<Runtime extends RuntimeAndFunctionType.Runtime> extends GroupSpec<Runtime, string, FunctionSpec.AnyWithPropsWithRuntime<Runtime>, AnyWithPropsWithRuntime<Runtime
|
|
28
|
+
export interface AnyWithPropsWithRuntime<Runtime extends RuntimeAndFunctionType.Runtime> extends GroupSpec<Runtime, string, FunctionSpec.AnyWithPropsWithRuntime<Runtime>, AnyWithPropsWithRuntime<Runtime>, MiddlewareSpec.AnyMiddlewareSpec> {
|
|
26
29
|
}
|
|
27
30
|
export type Name<Group extends AnyWithProps> = Group["name"];
|
|
28
31
|
export type Functions<Group extends AnyWithProps> = Group["functions"][keyof Group["functions"]];
|
|
29
32
|
export type Groups<Group extends AnyWithProps> = Group["groups"][keyof Group["groups"]];
|
|
33
|
+
export type MiddlewareSpecs<Group extends AnyWithProps> = Group["middlewareSpecs"][number];
|
|
30
34
|
export type GroupNames<Group extends AnyWithProps> = [Groups<Group>] extends [
|
|
31
35
|
never
|
|
32
36
|
] ? never : Name<Groups<Group>>;
|
|
@@ -37,7 +41,7 @@ export type WithName<Group extends AnyWithProps, Name_ extends Name<Group>> = Ex
|
|
|
37
41
|
export type NamedAt<Group extends Any, Name_ extends string> = Omit<Group, "name"> & {
|
|
38
42
|
readonly name: Name_;
|
|
39
43
|
};
|
|
40
|
-
export type AddGroups<Group extends AnyWithProps, ExtraGroups extends AnyWithProps> = Group extends GroupSpec<infer Runtime, infer Name_, infer Functions_, infer Groups_> ? GroupSpec<Runtime, Name_, Functions_, Groups_ | ExtraGroups> : never;
|
|
44
|
+
export type AddGroups<Group extends AnyWithProps, ExtraGroups extends AnyWithProps> = Group extends GroupSpec<infer Runtime, infer Name_, infer Functions_, infer Groups_, infer MiddlewareSpecs_> ? GroupSpec<Runtime, Name_, Functions_, Groups_ | ExtraGroups, MiddlewareSpecs_> : never;
|
|
41
45
|
export declare const make: () => GroupSpec<"Convex", "">;
|
|
42
46
|
export declare const makeAt: <const Name_ extends string>(name: Name_) => GroupSpec<"Convex", Name_>;
|
|
43
47
|
export declare const makeNode: () => GroupSpec<"Node", "">;
|
package/dist/GroupSpec.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GroupSpec.d.ts","sourceRoot":"","sources":["../src/GroupS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
|
|
21
|
+
const overlapping = function_.middlewareSpecs.find((functionMiddlewareSpec) => this_.middlewareSpecs.some((groupMiddlewareSpec) => groupMiddlewareSpec.key === functionMiddlewareSpec.key));
|
|
22
|
+
if (overlapping !== void 0) throw new Error(`Middleware "${overlapping.key}" is attached to both function "${function_.name}" and its group`);
|
|
21
23
|
return makeProto({
|
|
22
24
|
runtime: this_.runtime,
|
|
23
25
|
name: this_.name,
|
|
24
26
|
functions: Record.set(this_.functions, function_.name, function_),
|
|
25
|
-
groups: this_.groups
|
|
27
|
+
groups: this_.groups,
|
|
28
|
+
middlewareSpecs: this_.middlewareSpecs
|
|
26
29
|
});
|
|
27
30
|
},
|
|
28
31
|
addGroup(group) {
|
|
@@ -32,7 +35,8 @@ const Proto = {
|
|
|
32
35
|
runtime: this_.runtime,
|
|
33
36
|
name: this_.name,
|
|
34
37
|
functions: this_.functions,
|
|
35
|
-
groups: Record.set(this_.groups, group_.name, group_)
|
|
38
|
+
groups: Record.set(this_.groups, group_.name, group_),
|
|
39
|
+
middlewareSpecs: this_.middlewareSpecs
|
|
36
40
|
});
|
|
37
41
|
},
|
|
38
42
|
addGroupAt(name, group) {
|
|
@@ -42,21 +46,36 @@ const Proto = {
|
|
|
42
46
|
runtime: this_.runtime,
|
|
43
47
|
name: this_.name,
|
|
44
48
|
functions: this_.functions,
|
|
45
|
-
groups: Record.set(this_.groups, name, withName(name, group_))
|
|
49
|
+
groups: Record.set(this_.groups, name, withName(name, group_)),
|
|
50
|
+
middlewareSpecs: this_.middlewareSpecs
|
|
51
|
+
});
|
|
52
|
+
},
|
|
53
|
+
middleware(middlewareSpec) {
|
|
54
|
+
const this_ = this;
|
|
55
|
+
if (this_.middlewareSpecs.some((existing) => existing.key === middlewareSpec.key)) throw new Error(`Middleware "${middlewareSpec.key}" is already attached to this group`);
|
|
56
|
+
for (const function_ of Object.values(this_.functions)) if (function_.middlewareSpecs.some((existing) => existing.key === middlewareSpec.key)) throw new Error(`Middleware "${middlewareSpec.key}" is attached to both function "${function_.name}" and its group`);
|
|
57
|
+
return makeProto({
|
|
58
|
+
runtime: this_.runtime,
|
|
59
|
+
name: this_.name,
|
|
60
|
+
functions: this_.functions,
|
|
61
|
+
groups: this_.groups,
|
|
62
|
+
middlewareSpecs: [...this_.middlewareSpecs, middlewareSpec]
|
|
46
63
|
});
|
|
47
64
|
}
|
|
48
65
|
};
|
|
49
|
-
const makeProto = ({ runtime, name, functions, groups }) => Object.assign(Object.create(Proto), {
|
|
66
|
+
const makeProto = ({ runtime, name, functions, groups, middlewareSpecs }) => Object.assign(Object.create(Proto), {
|
|
50
67
|
runtime,
|
|
51
68
|
name,
|
|
52
69
|
functions,
|
|
53
|
-
groups
|
|
70
|
+
groups,
|
|
71
|
+
middlewareSpecs
|
|
54
72
|
});
|
|
55
73
|
const make = () => makeProto({
|
|
56
74
|
runtime: "Convex",
|
|
57
75
|
name: "",
|
|
58
76
|
functions: Record.empty(),
|
|
59
|
-
groups: Record.empty()
|
|
77
|
+
groups: Record.empty(),
|
|
78
|
+
middlewareSpecs: []
|
|
60
79
|
});
|
|
61
80
|
const makeAt = (name) => {
|
|
62
81
|
validateConfectFunctionIdentifier(name);
|
|
@@ -64,14 +83,16 @@ const makeAt = (name) => {
|
|
|
64
83
|
runtime: "Convex",
|
|
65
84
|
name,
|
|
66
85
|
functions: Record.empty(),
|
|
67
|
-
groups: Record.empty()
|
|
86
|
+
groups: Record.empty(),
|
|
87
|
+
middlewareSpecs: []
|
|
68
88
|
});
|
|
69
89
|
};
|
|
70
90
|
const makeNode = () => makeProto({
|
|
71
91
|
runtime: "Node",
|
|
72
92
|
name: "",
|
|
73
93
|
functions: Record.empty(),
|
|
74
|
-
groups: Record.empty()
|
|
94
|
+
groups: Record.empty(),
|
|
95
|
+
middlewareSpecs: []
|
|
75
96
|
});
|
|
76
97
|
const makeNodeAt = (name) => {
|
|
77
98
|
validateConfectFunctionIdentifier(name);
|
|
@@ -79,7 +100,8 @@ const makeNodeAt = (name) => {
|
|
|
79
100
|
runtime: "Node",
|
|
80
101
|
name,
|
|
81
102
|
functions: Record.empty(),
|
|
82
|
-
groups: Record.empty()
|
|
103
|
+
groups: Record.empty(),
|
|
104
|
+
middlewareSpecs: []
|
|
83
105
|
});
|
|
84
106
|
};
|
|
85
107
|
const withName = (name, group) => {
|
|
@@ -90,7 +112,8 @@ const withName = (name, group) => {
|
|
|
90
112
|
runtime: group_.runtime,
|
|
91
113
|
name,
|
|
92
114
|
functions: group_.functions,
|
|
93
|
-
groups: group_.groups
|
|
115
|
+
groups: group_.groups,
|
|
116
|
+
middlewareSpecs: group_.middlewareSpecs
|
|
94
117
|
});
|
|
95
118
|
};
|
|
96
119
|
//#endregion
|
package/dist/GroupSpec.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GroupSpec.js","names":[],"sources":["../src/GroupSpec.ts"],"sourcesContent":["import * as Predicate from \"effect/Predicate\";\nimport * as Record from \"effect/Record\";\nimport type * as FunctionSpec from \"./FunctionSpec\";\nimport type * as RuntimeAndFunctionType from \"./RuntimeAndFunctionType\";\nimport { validateConfectFunctionIdentifier } from \"./Identifier\";\n\nexport const TypeId = \"@confect/core/GroupSpec\";\nexport type TypeId = typeof TypeId;\n\nexport const isGroupSpec = (u: unknown): u is AnyWithProps =>\n Predicate.hasProperty(u, TypeId);\n\nexport interface GroupSpec<\n Runtime extends RuntimeAndFunctionType.Runtime,\n Name_ extends string,\n Functions_ extends FunctionSpec.AnyWithPropsWithRuntime<Runtime> = never,\n // Subgroups may be of any runtime, independent of this group's own runtime: a\n // group is only a namespace for its children, which are otherwise-independent\n // modules. Functions, by contrast, stay homogeneous (a Node group only accepts\n // Node actions) — `addFunction` keeps the `<Runtime>` bound below.\n Groups_ extends AnyWithProps = never,\n> {\n readonly [TypeId]: TypeId;\n readonly runtime: Runtime;\n readonly name: Name_;\n readonly functions: {\n [FunctionName in FunctionSpec.Name<\n FunctionSpec.AnyWithPropsWithRuntime<Runtime>\n >]: FunctionSpec.WithName<Functions_, FunctionName>;\n };\n readonly groups: {\n [GroupName in Name<Groups_>]: WithName<Groups_, GroupName>;\n };\n\n addFunction<Function extends FunctionSpec.AnyWithPropsWithRuntime<Runtime>>(\n function_: Function,\n ): GroupSpec<Runtime, Name_, Functions_ | Function, Groups_>;\n\n addGroup<Group extends AnyWithProps>(\n group: Group,\n ): GroupSpec<Runtime, Name_, Functions_, Groups_ | Group>;\n\n addGroupAt<const AtName extends string, Group extends AnyWithProps>(\n name: AtName,\n group: Group,\n ): GroupSpec<Runtime, Name_, Functions_, Groups_ | NamedAt<Group, AtName>>;\n}\n\nexport interface Any {\n readonly [TypeId]: TypeId;\n}\n\nexport interface AnyWithProps extends GroupSpec<\n RuntimeAndFunctionType.Runtime,\n string,\n FunctionSpec.AnyWithProps,\n AnyWithProps\n> {}\n\nexport interface AnyWithPropsWithRuntime<\n Runtime extends RuntimeAndFunctionType.Runtime,\n> extends GroupSpec<\n Runtime,\n string,\n FunctionSpec.AnyWithPropsWithRuntime<Runtime>,\n AnyWithPropsWithRuntime<Runtime>\n> {}\n\nexport type Name<Group extends AnyWithProps> = Group[\"name\"];\n\nexport type Functions<Group extends AnyWithProps> =\n Group[\"functions\"][keyof Group[\"functions\"]];\n\nexport type Groups<Group extends AnyWithProps> =\n Group[\"groups\"][keyof Group[\"groups\"]];\n\nexport type GroupNames<Group extends AnyWithProps> = [Groups<Group>] extends [\n never,\n]\n ? never\n : Name<Groups<Group>>;\n\nexport type WithName<\n Group extends AnyWithProps,\n Name_ extends Name<Group>,\n> = Extract<Group, { readonly name: Name_ }>;\n\n/** Assigns a segment name to a leaf group created with {@link make} for typing and refs. */\nexport type NamedAt<Group extends Any, Name_ extends string> = Omit<\n Group,\n \"name\"\n> & {\n readonly name: Name_;\n};\n\nexport type AddGroups<\n Group extends AnyWithProps,\n ExtraGroups extends AnyWithProps,\n> =\n Group extends GroupSpec<\n infer Runtime,\n infer Name_,\n infer Functions_,\n infer Groups_\n >\n ? GroupSpec<Runtime, Name_, Functions_, Groups_ | ExtraGroups>\n : never;\n\nconst Proto = {\n [TypeId]: TypeId,\n\n addFunction<Function extends FunctionSpec.AnyWithProps>(\n this: Any,\n function_: Function,\n ) {\n const this_ = this as AnyWithProps;\n\n return makeProto({\n runtime: this_.runtime,\n name: this_.name,\n functions: Record.set(this_.functions, function_.name, function_),\n groups: this_.groups,\n });\n },\n\n addGroup<Group extends Any>(this: Any, group: Group) {\n const this_ = this as AnyWithProps;\n const group_ = group as unknown as AnyWithProps;\n\n return makeProto({\n runtime: this_.runtime,\n name: this_.name,\n functions: this_.functions,\n groups: Record.set(this_.groups, group_.name, group_),\n });\n },\n\n addGroupAt<Group extends Any>(this: Any, name: string, group: Group) {\n const this_ = this as AnyWithProps;\n const group_ = group as unknown as AnyWithProps;\n\n return makeProto({\n runtime: this_.runtime,\n name: this_.name,\n functions: this_.functions,\n groups: Record.set(this_.groups, name, withName(name, group_)),\n });\n },\n};\n\nconst makeProto = <\n Runtime extends RuntimeAndFunctionType.Runtime,\n Name_ extends string,\n Functions_ extends FunctionSpec.AnyWithPropsWithRuntime<Runtime>,\n Groups_ extends AnyWithPropsWithRuntime<Runtime>,\n>({\n runtime,\n name,\n functions,\n groups,\n}: {\n runtime: Runtime;\n name: Name_;\n functions: Record.ReadonlyRecord<string, Functions_>;\n groups: Record.ReadonlyRecord<string, Groups_>;\n}): GroupSpec<Runtime, Name_, Functions_, Groups_> =>\n Object.assign(Object.create(Proto), {\n runtime,\n name,\n functions,\n groups,\n });\n\nexport const make = (): GroupSpec<\"Convex\", \"\"> =>\n makeProto({\n runtime: \"Convex\",\n name: \"\",\n functions: Record.empty(),\n groups: Record.empty(),\n });\n\nexport const makeAt = <const Name_ extends string>(\n name: Name_,\n): GroupSpec<\"Convex\", Name_> => {\n validateConfectFunctionIdentifier(name);\n\n return makeProto({\n runtime: \"Convex\",\n name,\n functions: Record.empty(),\n groups: Record.empty(),\n });\n};\n\nexport const makeNode = (): GroupSpec<\"Node\", \"\"> =>\n makeProto({\n runtime: \"Node\",\n name: \"\",\n functions: Record.empty(),\n groups: Record.empty(),\n });\n\nexport const makeNodeAt = <const Name_ extends string>(\n name: Name_,\n): GroupSpec<\"Node\", Name_> => {\n validateConfectFunctionIdentifier(name);\n\n return makeProto({\n runtime: \"Node\",\n name,\n functions: Record.empty(),\n groups: Record.empty(),\n });\n};\n\nexport const withName = <const Name_ extends string>(\n name: Name_,\n group: Any,\n): AnyWithProps => {\n validateConfectFunctionIdentifier(name);\n const group_ = group as AnyWithProps;\n\n if (group_.name === name) {\n return group_;\n }\n\n return makeProto({\n runtime: group_.runtime,\n name,\n functions: group_.functions,\n groups: group_.groups,\n });\n};\n"],"mappings":";;;;;;;;;;;;;;AAMA,MAAa,SAAS;AAGtB,MAAa,eAAe,MAC1B,UAAU,YAAY,GAAG,MAAM;AAkGjC,MAAM,QAAQ;EACX,SAAS;CAEV,YAEE,WACA;EACA,MAAM,QAAQ;EAEd,OAAO,UAAU;GACf,SAAS,MAAM;GACf,MAAM,MAAM;GACZ,WAAW,OAAO,IAAI,MAAM,WAAW,UAAU,MAAM,SAAS;GAChE,QAAQ,MAAM;EAChB,CAAC;CACH;CAEA,SAAuC,OAAc;EACnD,MAAM,QAAQ;EACd,MAAM,SAAS;EAEf,OAAO,UAAU;GACf,SAAS,MAAM;GACf,MAAM,MAAM;GACZ,WAAW,MAAM;GACjB,QAAQ,OAAO,IAAI,MAAM,QAAQ,OAAO,MAAM,MAAM;EACtD,CAAC;CACH;CAEA,WAAyC,MAAc,OAAc;EACnE,MAAM,QAAQ;EACd,MAAM,SAAS;EAEf,OAAO,UAAU;GACf,SAAS,MAAM;GACf,MAAM,MAAM;GACZ,WAAW,MAAM;GACjB,QAAQ,OAAO,IAAI,MAAM,QAAQ,MAAM,SAAS,MAAM,MAAM,CAAC;EAC/D,CAAC;CACH;AACF;AAEA,MAAM,aAKJ,EACA,SACA,MACA,WACA,aAOA,OAAO,OAAO,OAAO,OAAO,KAAK,GAAG;CAClC;CACA;CACA;CACA;AACF,CAAC;AAEH,MAAa,aACX,UAAU;CACR,SAAS;CACT,MAAM;CACN,WAAW,OAAO,MAAM;CACxB,QAAQ,OAAO,MAAM;AACvB,CAAC;AAEH,MAAa,UACX,SAC+B;CAC/B,kCAAkC,IAAI;CAEtC,OAAO,UAAU;EACf,SAAS;EACT;EACA,WAAW,OAAO,MAAM;EACxB,QAAQ,OAAO,MAAM;CACvB,CAAC;AACH;AAEA,MAAa,iBACX,UAAU;CACR,SAAS;CACT,MAAM;CACN,WAAW,OAAO,MAAM;CACxB,QAAQ,OAAO,MAAM;AACvB,CAAC;AAEH,MAAa,cACX,SAC6B;CAC7B,kCAAkC,IAAI;CAEtC,OAAO,UAAU;EACf,SAAS;EACT;EACA,WAAW,OAAO,MAAM;EACxB,QAAQ,OAAO,MAAM;CACvB,CAAC;AACH;AAEA,MAAa,YACX,MACA,UACiB;CACjB,kCAAkC,IAAI;CACtC,MAAM,SAAS;CAEf,IAAI,OAAO,SAAS,MAClB,OAAO;CAGT,OAAO,UAAU;EACf,SAAS,OAAO;EAChB;EACA,WAAW,OAAO;EAClB,QAAQ,OAAO;CACjB,CAAC;AACH"}
|
|
1
|
+
{"version":3,"file":"GroupSpec.js","names":[],"sources":["../src/GroupSpec.ts"],"sourcesContent":["import * as Predicate from \"effect/Predicate\";\nimport * as Record from \"effect/Record\";\nimport type * as FunctionSpec from \"./FunctionSpec\";\nimport type * as MiddlewareSpec from \"./MiddlewareSpec\";\nimport type * as RuntimeAndFunctionType from \"./RuntimeAndFunctionType\";\nimport { validateConfectFunctionIdentifier } from \"./Identifier\";\n\nexport const TypeId = \"@confect/core/GroupSpec\";\nexport type TypeId = typeof TypeId;\n\nexport const isGroupSpec = (u: unknown): u is AnyWithProps =>\n Predicate.hasProperty(u, TypeId);\n\nexport interface GroupSpec<\n Runtime extends RuntimeAndFunctionType.Runtime,\n Name_ extends string,\n Functions_ extends FunctionSpec.AnyWithPropsWithRuntime<Runtime> = never,\n // Subgroups may be of any runtime, independent of this group's own runtime: a\n // group is only a namespace for its children, which are otherwise-independent\n // modules. Functions, by contrast, stay homogeneous (a Node group only accepts\n // Node actions) — `addFunction` keeps the `<Runtime>` bound below.\n Groups_ extends AnyWithProps = never,\n MiddlewareSpecs_ extends MiddlewareSpec.AnyMiddlewareSpec = never,\n> {\n readonly [TypeId]: TypeId;\n readonly runtime: Runtime;\n readonly name: Name_;\n readonly functions: {\n [\n FunctionName in FunctionSpec.Name<\n FunctionSpec.AnyWithPropsWithRuntime<Runtime>\n >\n ]: FunctionSpec.WithName<Functions_, FunctionName>;\n };\n readonly groups: {\n [GroupName in Name<Groups_>]: WithName<Groups_, GroupName>;\n };\n readonly middlewareSpecs: ReadonlyArray<MiddlewareSpecs_>;\n\n addFunction<Function extends FunctionSpec.AnyWithPropsWithRuntime<Runtime>>(\n function_: Function &\n MiddlewareSpec.ValidateAddedFunction<Function, MiddlewareSpecs_>,\n ): GroupSpec<\n Runtime,\n Name_,\n Functions_ | Function,\n Groups_,\n MiddlewareSpecs_\n >;\n\n addGroup<Group extends AnyWithProps>(\n group: Group,\n ): GroupSpec<Runtime, Name_, Functions_, Groups_ | Group, MiddlewareSpecs_>;\n\n addGroupAt<const AtName extends string, Group extends AnyWithProps>(\n name: AtName,\n group: Group,\n ): GroupSpec<\n Runtime,\n Name_,\n Functions_,\n Groups_ | NamedAt<Group, AtName>,\n MiddlewareSpecs_\n >;\n\n middleware<MiddlewareSpec_ extends MiddlewareSpec.AnyMiddlewareSpec>(\n middlewareSpec: MiddlewareSpec_ &\n MiddlewareSpec.ValidateAttach<\n MiddlewareSpec_,\n Functions_,\n MiddlewareSpecs_\n >,\n ): GroupSpec<\n Runtime,\n Name_,\n Functions_,\n Groups_,\n MiddlewareSpecs_ | MiddlewareSpec_\n >;\n}\n\nexport interface Any {\n readonly [TypeId]: TypeId;\n}\n\nexport interface AnyWithProps extends GroupSpec<\n RuntimeAndFunctionType.Runtime,\n string,\n FunctionSpec.AnyWithProps,\n AnyWithProps,\n MiddlewareSpec.AnyMiddlewareSpec\n> {}\n\nexport interface AnyWithPropsWithRuntime<\n Runtime extends RuntimeAndFunctionType.Runtime,\n> extends GroupSpec<\n Runtime,\n string,\n FunctionSpec.AnyWithPropsWithRuntime<Runtime>,\n AnyWithPropsWithRuntime<Runtime>,\n MiddlewareSpec.AnyMiddlewareSpec\n> {}\n\nexport type Name<Group extends AnyWithProps> = Group[\"name\"];\n\nexport type Functions<Group extends AnyWithProps> =\n Group[\"functions\"][keyof Group[\"functions\"]];\n\nexport type Groups<Group extends AnyWithProps> =\n Group[\"groups\"][keyof Group[\"groups\"]];\n\nexport type MiddlewareSpecs<Group extends AnyWithProps> =\n Group[\"middlewareSpecs\"][number];\n\nexport type GroupNames<Group extends AnyWithProps> = [Groups<Group>] extends [\n never,\n]\n ? never\n : Name<Groups<Group>>;\n\nexport type WithName<\n Group extends AnyWithProps,\n Name_ extends Name<Group>,\n> = Extract<Group, { readonly name: Name_ }>;\n\n/** Assigns a segment name to a leaf group created with {@link make} for typing and refs. */\nexport type NamedAt<Group extends Any, Name_ extends string> = Omit<\n Group,\n \"name\"\n> & {\n readonly name: Name_;\n};\n\nexport type AddGroups<\n Group extends AnyWithProps,\n ExtraGroups extends AnyWithProps,\n> =\n Group extends GroupSpec<\n infer Runtime,\n infer Name_,\n infer Functions_,\n infer Groups_,\n infer MiddlewareSpecs_\n >\n ? GroupSpec<\n Runtime,\n Name_,\n Functions_,\n Groups_ | ExtraGroups,\n MiddlewareSpecs_\n >\n : never;\n\nconst Proto = {\n [TypeId]: TypeId,\n\n addFunction<Function extends FunctionSpec.AnyWithProps>(\n this: Any,\n function_: Function,\n ) {\n const this_ = this as AnyWithProps;\n\n const overlapping = function_.middlewareSpecs.find(\n (functionMiddlewareSpec) =>\n this_.middlewareSpecs.some(\n (groupMiddlewareSpec) =>\n groupMiddlewareSpec.key === functionMiddlewareSpec.key,\n ),\n );\n if (overlapping !== undefined) {\n throw new Error(\n `Middleware \"${overlapping.key}\" is attached to both function \"${function_.name}\" and its group`,\n );\n }\n\n return makeProto({\n runtime: this_.runtime,\n name: this_.name,\n functions: Record.set(this_.functions, function_.name, function_),\n groups: this_.groups,\n middlewareSpecs: this_.middlewareSpecs,\n });\n },\n\n addGroup<Group extends Any>(this: Any, group: Group) {\n const this_ = this as AnyWithProps;\n const group_ = group as unknown as AnyWithProps;\n\n return makeProto({\n runtime: this_.runtime,\n name: this_.name,\n functions: this_.functions,\n groups: Record.set(this_.groups, group_.name, group_),\n middlewareSpecs: this_.middlewareSpecs,\n });\n },\n\n addGroupAt<Group extends Any>(this: Any, name: string, group: Group) {\n const this_ = this as AnyWithProps;\n const group_ = group as unknown as AnyWithProps;\n\n return makeProto({\n runtime: this_.runtime,\n name: this_.name,\n functions: this_.functions,\n groups: Record.set(this_.groups, name, withName(name, group_)),\n middlewareSpecs: this_.middlewareSpecs,\n });\n },\n\n middleware<MiddlewareSpec_ extends MiddlewareSpec.AnyMiddlewareSpec>(\n this: Any,\n middlewareSpec: MiddlewareSpec_,\n ) {\n const this_ = this as AnyWithProps;\n\n if (\n this_.middlewareSpecs.some(\n (existing) => existing.key === middlewareSpec.key,\n )\n ) {\n throw new Error(\n `Middleware \"${middlewareSpec.key}\" is already attached to this group`,\n );\n }\n\n for (const function_ of Object.values(this_.functions)) {\n if (\n function_.middlewareSpecs.some(\n (existing) => existing.key === middlewareSpec.key,\n )\n ) {\n throw new Error(\n `Middleware \"${middlewareSpec.key}\" is attached to both function \"${function_.name}\" and its group`,\n );\n }\n }\n\n return makeProto({\n runtime: this_.runtime,\n name: this_.name,\n functions: this_.functions,\n groups: this_.groups,\n middlewareSpecs: [...this_.middlewareSpecs, middlewareSpec],\n });\n },\n};\n\nconst makeProto = <\n Runtime extends RuntimeAndFunctionType.Runtime,\n Name_ extends string,\n Functions_ extends FunctionSpec.AnyWithPropsWithRuntime<Runtime>,\n Groups_ extends AnyWithPropsWithRuntime<Runtime>,\n MiddlewareSpecs_ extends MiddlewareSpec.AnyMiddlewareSpec,\n>({\n runtime,\n name,\n functions,\n groups,\n middlewareSpecs,\n}: {\n runtime: Runtime;\n name: Name_;\n functions: Record.ReadonlyRecord<string, Functions_>;\n groups: Record.ReadonlyRecord<string, Groups_>;\n middlewareSpecs: ReadonlyArray<MiddlewareSpecs_>;\n}): GroupSpec<Runtime, Name_, Functions_, Groups_, MiddlewareSpecs_> =>\n Object.assign(Object.create(Proto), {\n runtime,\n name,\n functions,\n groups,\n middlewareSpecs,\n });\n\nexport const make = (): GroupSpec<\"Convex\", \"\"> =>\n makeProto({\n runtime: \"Convex\",\n name: \"\",\n functions: Record.empty(),\n groups: Record.empty(),\n middlewareSpecs: [],\n });\n\nexport const makeAt = <const Name_ extends string>(\n name: Name_,\n): GroupSpec<\"Convex\", Name_> => {\n validateConfectFunctionIdentifier(name);\n\n return makeProto({\n runtime: \"Convex\",\n name,\n functions: Record.empty(),\n groups: Record.empty(),\n middlewareSpecs: [],\n });\n};\n\nexport const makeNode = (): GroupSpec<\"Node\", \"\"> =>\n makeProto({\n runtime: \"Node\",\n name: \"\",\n functions: Record.empty(),\n groups: Record.empty(),\n middlewareSpecs: [],\n });\n\nexport const makeNodeAt = <const Name_ extends string>(\n name: Name_,\n): GroupSpec<\"Node\", Name_> => {\n validateConfectFunctionIdentifier(name);\n\n return makeProto({\n runtime: \"Node\",\n name,\n functions: Record.empty(),\n groups: Record.empty(),\n middlewareSpecs: [],\n });\n};\n\nexport const withName = <const Name_ extends string>(\n name: Name_,\n group: Any,\n): AnyWithProps => {\n validateConfectFunctionIdentifier(name);\n const group_ = group as AnyWithProps;\n\n if (group_.name === name) {\n return group_;\n }\n\n return makeProto({\n runtime: group_.runtime,\n name,\n functions: group_.functions,\n groups: group_.groups,\n middlewareSpecs: group_.middlewareSpecs,\n });\n};\n"],"mappings":";;;;;;;;;;;;;;AAOA,MAAa,SAAS;AAGtB,MAAa,eAAe,MAC1B,UAAU,YAAY,GAAG,MAAM;AA8IjC,MAAM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AAM;GACf,MAAM,MAAM;GACZ,WAAW,MAAM;GACjB,QAAQ,MAAM;GACd,iBAAiB,CAAC,GAAG,MAAM,iBAAiB,cAAc;EAC5D,CAAC;CACH;AACF;AAEA,MAAM,aAMJ,EACA,SACA,MACA,WACA,QACA,sBAQA,OAAO,OAAO,OAAO,OAAO,KAAK,GAAG;CAClC;CACA;CACA;CACA;CACA;AACF,CAAC;AAEH,MAAa,aACX,UAAU;CACR,SAAS;CACT,MAAM;CACN,WAAW,OAAO,MAAM;CACxB,QAAQ,OAAO,MAAM;CACrB,iBAAiB,CAAC;AACpB,CAAC;AAEH,MAAa,UACX,SAC+B;CAC/B,kCAAkC,IAAI;CAEtC,OAAO,UAAU;EACf,SAAS;EACT;EACA,WAAW,OAAO,MAAM;EACxB,QAAQ,OAAO,MAAM;EACrB,iBAAiB,CAAC;CACpB,CAAC;AACH;AAEA,MAAa,iBACX,UAAU;CACR,SAAS;CACT,MAAM;CACN,WAAW,OAAO,MAAM;CACxB,QAAQ,OAAO,MAAM;CACrB,iBAAiB,CAAC;AACpB,CAAC;AAEH,MAAa,cACX,SAC6B;CAC7B,kCAAkC,IAAI;CAEtC,OAAO,UAAU;EACf,SAAS;EACT;EACA,WAAW,OAAO,MAAM;EACxB,QAAQ,OAAO,MAAM;EACrB,iBAAiB,CAAC;CACpB,CAAC;AACH;AAEA,MAAa,YACX,MACA,UACiB;CACjB,kCAAkC,IAAI;CACtC,MAAM,SAAS;CAEf,IAAI,OAAO,SAAS,MAClB,OAAO;CAGT,OAAO,UAAU;EACf,SAAS,OAAO;EAChB;EACA,WAAW,OAAO;EAClB,QAAQ,OAAO;EACf,iBAAiB,OAAO;CAC1B,CAAC;AACH"}
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import type { FunctionType, FunctionVisibility } from "convex/server";
|
|
2
|
+
import type * as Effect from "effect/Effect";
|
|
3
|
+
import type * as Schema from "effect/Schema";
|
|
4
|
+
import type { unhandled } from "effect/Types";
|
|
5
|
+
import type * as FunctionProvenance from "./FunctionProvenance";
|
|
6
|
+
import type * as FunctionSpec from "./FunctionSpec";
|
|
7
|
+
import type * as RuntimeAndFunctionType from "./RuntimeAndFunctionType";
|
|
8
|
+
export declare const TypeId = "@confect/core/MiddlewareSpec";
|
|
9
|
+
export type TypeId = typeof TypeId;
|
|
10
|
+
export declare const isMiddlewareSpec: (u: unknown) => u is AnyMiddlewareSpec;
|
|
11
|
+
export declare const allFunctionTypes: ReadonlyArray<FunctionType>;
|
|
12
|
+
export interface SupportedFunctionTypes {
|
|
13
|
+
readonly query: boolean;
|
|
14
|
+
readonly mutation: boolean;
|
|
15
|
+
readonly action: boolean;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Marker success type used by middleware to represent the handler's outcome
|
|
19
|
+
* without exposing its concrete success value.
|
|
20
|
+
*
|
|
21
|
+
* Mirrors `RpcMiddleware.SuccessValue` in Effect.
|
|
22
|
+
*/
|
|
23
|
+
export interface SuccessValue {
|
|
24
|
+
readonly _: unique symbol;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The wrap-style middleware implementation shape: receives the downstream
|
|
28
|
+
* effect (the remaining middleware plus the function handler) and the
|
|
29
|
+
* covered invocation's metadata, and returns the effect that runs in its
|
|
30
|
+
* place.
|
|
31
|
+
*
|
|
32
|
+
* Mirrors `RpcMiddleware.RpcMiddleware` in Effect.
|
|
33
|
+
*/
|
|
34
|
+
export interface MiddlewareImpl<Provides_, E, R> {
|
|
35
|
+
(effect: Effect.Effect<SuccessValue, E | unhandled, Provides_>, options: MiddlewareOptions): Effect.Effect<SuccessValue, E | unhandled, R>;
|
|
36
|
+
}
|
|
37
|
+
export interface MiddlewareOptions {
|
|
38
|
+
readonly name: string;
|
|
39
|
+
readonly functionType: FunctionType;
|
|
40
|
+
readonly functionVisibility: FunctionVisibility;
|
|
41
|
+
readonly args: unknown;
|
|
42
|
+
}
|
|
43
|
+
export interface AnyMiddlewareImpl extends MiddlewareImpl<any, any, any> {
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The class shape produced by {@link MiddlewareSpec}. Only the static side is ever
|
|
47
|
+
* used — the class is a value-level carrier for the middleware's key, its
|
|
48
|
+
* declared functionTypes, and its (lazily-evaluated) error schema, plus the
|
|
49
|
+
* type-level `Provides`/`Error` metadata. It is deliberately not a
|
|
50
|
+
* `Context.Tag`: implementations are registered through the group `Registry`
|
|
51
|
+
* (like `FunctionImpl`s), not resolved from a Layer context.
|
|
52
|
+
*/
|
|
53
|
+
export interface MiddlewareSpec<in out Self, Key_ extends string, Provides_, Requires_, ErrorSchema_ extends Schema.Codec<any, any>, FunctionTypes_ extends FunctionType> {
|
|
54
|
+
new (_: never): {
|
|
55
|
+
readonly [TypeId]: TypeId;
|
|
56
|
+
readonly key: Key_;
|
|
57
|
+
};
|
|
58
|
+
readonly [TypeId]: TypeId;
|
|
59
|
+
readonly key: Key_;
|
|
60
|
+
readonly functionTypes: SupportedFunctionTypes;
|
|
61
|
+
readonly error?: ErrorSchema_;
|
|
62
|
+
readonly "~Provides": Provides_;
|
|
63
|
+
readonly "~Requires": Requires_;
|
|
64
|
+
readonly "~Error": ErrorSchema_;
|
|
65
|
+
readonly "~FunctionTypes": FunctionTypes_;
|
|
66
|
+
readonly "~Self": Self;
|
|
67
|
+
}
|
|
68
|
+
export interface AnyMiddlewareSpec {
|
|
69
|
+
readonly [TypeId]: TypeId;
|
|
70
|
+
readonly key: string;
|
|
71
|
+
readonly functionTypes: SupportedFunctionTypes;
|
|
72
|
+
readonly error?: Schema.Codec<any, any>;
|
|
73
|
+
readonly "~Provides": any;
|
|
74
|
+
readonly "~Requires": any;
|
|
75
|
+
readonly "~Error": any;
|
|
76
|
+
readonly "~FunctionTypes": FunctionType;
|
|
77
|
+
}
|
|
78
|
+
export type Key<MiddlewareSpec_ extends AnyMiddlewareSpec> = MiddlewareSpec_["key"];
|
|
79
|
+
export type Provides<MiddlewareSpec_ extends AnyMiddlewareSpec> = MiddlewareSpec_ extends AnyMiddlewareSpec ? MiddlewareSpec_["~Provides"] : never;
|
|
80
|
+
/**
|
|
81
|
+
* The services a middleware's implementation may consume from middleware
|
|
82
|
+
* that runs earlier in the same chain (type-level only, like `provides`).
|
|
83
|
+
*/
|
|
84
|
+
export type Requires<MiddlewareSpec_ extends AnyMiddlewareSpec> = MiddlewareSpec_ extends AnyMiddlewareSpec ? MiddlewareSpec_["~Requires"] : never;
|
|
85
|
+
export type ErrorSchema<MiddlewareSpec_ extends AnyMiddlewareSpec> = MiddlewareSpec_ extends AnyMiddlewareSpec ? MiddlewareSpec_["~Error"] : never;
|
|
86
|
+
export type Error<MiddlewareSpec_ extends AnyMiddlewareSpec> = MiddlewareSpec_ extends AnyMiddlewareSpec ? MiddlewareSpec_["~Error"]["Type"] : never;
|
|
87
|
+
export type EncodedError<MiddlewareSpec_ extends AnyMiddlewareSpec> = MiddlewareSpec_ extends AnyMiddlewareSpec ? MiddlewareSpec_["~Error"]["Encoded"] : never;
|
|
88
|
+
export type FunctionTypes<MiddlewareSpec_ extends AnyMiddlewareSpec> = MiddlewareSpec_ extends AnyMiddlewareSpec ? MiddlewareSpec_["~FunctionTypes"] : never;
|
|
89
|
+
/**
|
|
90
|
+
* Declare a middleware's client-safe interface: its identifying key, the
|
|
91
|
+
* service it provides to downstream handlers, the error schema its failures
|
|
92
|
+
* encode through, and the function types it may attach to.
|
|
93
|
+
*
|
|
94
|
+
* ```ts
|
|
95
|
+
* class RequireUser extends MiddlewareSpec.MiddlewareSpec<RequireUser, {
|
|
96
|
+
* provides: CurrentUser
|
|
97
|
+
* }>()("RequireUser", {
|
|
98
|
+
* error: () => NotSignedIn,
|
|
99
|
+
* functionTypes: { query: true, mutation: true, action: false },
|
|
100
|
+
* }) {}
|
|
101
|
+
* ```
|
|
102
|
+
*
|
|
103
|
+
* The implementation is server-only and supplied separately via
|
|
104
|
+
* `MiddlewareImpl.make` (or `makeByFunctionType`/`provides`) in `@confect/server`.
|
|
105
|
+
*/
|
|
106
|
+
export declare const MiddlewareSpec: <Self, Config extends {
|
|
107
|
+
provides?: any;
|
|
108
|
+
requires?: any;
|
|
109
|
+
} = {}>() => <const Key_ extends string, const FunctionTypesConfig_ extends SupportedFunctionTypes, ErrorSchema_ extends Schema.Codec<any, any> = never>(key: Key_, options: {
|
|
110
|
+
readonly error?: () => ErrorSchema_;
|
|
111
|
+
readonly functionTypes: FunctionTypesConfig_ & ValidateFunctionTypesConfig<FunctionTypesConfig_>;
|
|
112
|
+
}) => MiddlewareSpec<Self, Key_, "provides" extends keyof Config ? Config["provides"] : never, "requires" extends keyof Config ? Config["requires"] : never, ErrorSchema_, FunctionTypesFromConfig<FunctionTypesConfig_>>;
|
|
113
|
+
type FunctionTypesFromConfig<Config extends SupportedFunctionTypes> = {
|
|
114
|
+
[K in FunctionType]: Config[K] extends true ? K : never;
|
|
115
|
+
}[FunctionType];
|
|
116
|
+
type ValidateFunctionTypesConfig<Config extends SupportedFunctionTypes> = ValidationResult<{
|
|
117
|
+
[K in keyof Config]: boolean extends Config[K] ? AttachmentError<`Function type flag "${K & string}" must be a literal \`true\` or \`false\`, not a computed \`boolean\``> : never;
|
|
118
|
+
}[keyof Config] | ([FunctionTypesFromConfig<Config>] extends [never] ? AttachmentError<`A middleware must declare at least one function type`> : never)>;
|
|
119
|
+
export declare const errorSchemas: (middlewareSpecs: ReadonlyArray<AnyMiddlewareSpec>) => ReadonlyArray<Schema.Codec<any, any>>;
|
|
120
|
+
/**
|
|
121
|
+
* A branded, unconstructible type carrying a spec-authoring error message.
|
|
122
|
+
* Surfaced as (part of) a parameter type so the diagnostic names the exact
|
|
123
|
+
* problem at the `.middleware()` / `.addFunction()` call site.
|
|
124
|
+
*/
|
|
125
|
+
export interface AttachmentError<Message extends string> {
|
|
126
|
+
readonly "~confect/MiddlewareSpec/AttachmentError": Message;
|
|
127
|
+
}
|
|
128
|
+
type FunctionTypeOf<FunctionSpec_ extends FunctionSpec.AnyWithProps> = FunctionSpec_["runtimeAndFunctionType"]["functionType"];
|
|
129
|
+
type ValidationResult<Errors> = [Errors] extends [never] ? unknown : Errors;
|
|
130
|
+
/**
|
|
131
|
+
* A matching-type Convex-provenance function is rejected rather than
|
|
132
|
+
* silently skipped: its raw handler passes through Confect untouched, so
|
|
133
|
+
* the middleware could not actually cover it, and skipping it would be a
|
|
134
|
+
* policy hole.
|
|
135
|
+
*/
|
|
136
|
+
export type ValidateFunction<FunctionSpec_ extends FunctionSpec.AnyWithProps, MiddlewareSpec_ extends AnyMiddlewareSpec> = FunctionSpec_ extends any ? MiddlewareSpec_ extends any ? FunctionSpec_ extends {
|
|
137
|
+
readonly functionProvenance: {
|
|
138
|
+
readonly _tag: "Convex";
|
|
139
|
+
};
|
|
140
|
+
} ? FunctionTypeOf<FunctionSpec_> extends FunctionTypes<MiddlewareSpec_> ? 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`> : never : [
|
|
141
|
+
Extract<FunctionSpec.MiddlewareSpecs<FunctionSpec_>, {
|
|
142
|
+
readonly key: Key<MiddlewareSpec_>;
|
|
143
|
+
}>
|
|
144
|
+
] extends [never] ? FunctionTypeOf<FunctionSpec_> extends FunctionTypes<MiddlewareSpec_> ? never : AttachmentError<`Middleware "${Key<MiddlewareSpec_>}" does not declare function type "${FunctionTypeOf<FunctionSpec_>}", the type of function "${FunctionSpec_["name"]}"`> : AttachmentError<`Middleware "${Key<MiddlewareSpec_>}" is already attached to function "${FunctionSpec_["name"]}"`> : never : never;
|
|
145
|
+
/**
|
|
146
|
+
* Attachment order is chain order, so requiring `requires` to be satisfied
|
|
147
|
+
* by already-attached middleware is exactly requiring the provider to run
|
|
148
|
+
* earlier.
|
|
149
|
+
*/
|
|
150
|
+
export type ValidateAttach<MiddlewareSpec_ extends AnyMiddlewareSpec, Functions_ extends FunctionSpec.AnyWithProps, MiddlewareSpecs_ extends AnyMiddlewareSpec> = [
|
|
151
|
+
Extract<MiddlewareSpecs_, {
|
|
152
|
+
readonly key: Key<MiddlewareSpec_>;
|
|
153
|
+
}>
|
|
154
|
+
] extends [never] ? [Exclude<Requires<MiddlewareSpec_>, Provides<MiddlewareSpecs_>>] extends [
|
|
155
|
+
never
|
|
156
|
+
] ? ValidationResult<ValidateFunction<Functions_, MiddlewareSpec_>> : AttachmentError<`Middleware "${Key<MiddlewareSpec_>}" requires services that no middleware attached earlier to this group provides — attach the providing middleware first`> : AttachmentError<`Middleware "${Key<MiddlewareSpec_>}" is already attached to this group`>;
|
|
157
|
+
export type ValidateAddedFunction<FunctionSpec_ extends FunctionSpec.AnyWithProps, MiddlewareSpecs_ extends AnyMiddlewareSpec> = [MiddlewareSpecs_] extends [never] ? unknown : ValidationResult<ValidateFunction<FunctionSpec_, MiddlewareSpecs_> | GroupOverlap<FunctionSpec_, MiddlewareSpecs_>>;
|
|
158
|
+
type GroupOverlap<FunctionSpec_ extends FunctionSpec.AnyWithProps, MiddlewareSpecs_ extends AnyMiddlewareSpec> = FunctionSpec.MiddlewareSpecs<FunctionSpec_> extends infer FunctionMiddlewareSpec ? FunctionMiddlewareSpec extends AnyMiddlewareSpec ? [
|
|
159
|
+
Extract<MiddlewareSpecs_, {
|
|
160
|
+
readonly key: Key<FunctionMiddlewareSpec>;
|
|
161
|
+
}>
|
|
162
|
+
] extends [never] ? never : AttachmentError<`Middleware "${Key<FunctionMiddlewareSpec>}" is attached to both function "${FunctionSpec_["name"]}" and its group`> : never : never;
|
|
163
|
+
/**
|
|
164
|
+
* The parameter-type validation applied to the group spec at
|
|
165
|
+
* `GroupImpl.make`: every function's attached middleware must have its
|
|
166
|
+
* `requires` services provided by some middleware covering that function
|
|
167
|
+
* (the group's or the function's own). Checked here — with the group fully
|
|
168
|
+
* assembled — rather than at the spec builders, because a function-level
|
|
169
|
+
* middleware's `requires` may legitimately be satisfied by a group
|
|
170
|
+
* middleware the function spec never sees, and group attachment is
|
|
171
|
+
* declaratively order-independent with respect to `addFunction`.
|
|
172
|
+
*/
|
|
173
|
+
export type ValidateImplRequires<Functions_ extends FunctionSpec.AnyWithProps, GroupMiddlewareSpecs_ extends AnyMiddlewareSpec> = ValidationResult<Functions_ extends any ? [
|
|
174
|
+
Exclude<Requires<FunctionSpec.MiddlewareSpecs<Functions_>>, Provides<GroupMiddlewareSpecs_ | FunctionSpec.MiddlewareSpecs<Functions_>>>
|
|
175
|
+
] extends [never] ? never : AttachmentError<`Function "${Functions_["name"]}" has middleware requiring services that no middleware covering it provides`> : never>;
|
|
176
|
+
export type ValidateFunctionAttach<MiddlewareSpec_ extends AnyMiddlewareSpec, RuntimeAndFunctionType_ extends RuntimeAndFunctionType.RuntimeAndFunctionType, FunctionProvenance_ extends FunctionProvenance.FunctionProvenance, MiddlewareSpecs_ extends AnyMiddlewareSpec> = FunctionProvenance_ extends {
|
|
177
|
+
readonly _tag: "Convex";
|
|
178
|
+
} ? AttachmentError<`Plain Convex functions cannot have middleware — their raw handlers are passed through untouched`> : [
|
|
179
|
+
Extract<MiddlewareSpecs_, {
|
|
180
|
+
readonly key: Key<MiddlewareSpec_>;
|
|
181
|
+
}>
|
|
182
|
+
] extends [never] ? RuntimeAndFunctionType_["functionType"] extends FunctionTypes<MiddlewareSpec_> ? unknown : AttachmentError<`Middleware "${Key<MiddlewareSpec_>}" does not declare function type "${RuntimeAndFunctionType_["functionType"]}"`> : AttachmentError<`Middleware "${Key<MiddlewareSpec_>}" is already attached to this function`>;
|
|
183
|
+
export {};
|
|
184
|
+
//# sourceMappingURL=MiddlewareSpec.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MiddlewareSpec.d.ts","sourceRoot":"","sources":["../src/MiddlewareSpec.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AACtE,OAAO,KAAK,KAAK,MAAM,MAAM,eAAe,CAAC;AAE7C,OAAO,KAAK,KAA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
|
|
2
|
+
import { defineProperty } from "./Lazy.js";
|
|
3
|
+
import * as Predicate from "effect/Predicate";
|
|
4
|
+
//#region src/MiddlewareSpec.ts
|
|
5
|
+
var MiddlewareSpec_exports = /* @__PURE__ */ __exportAll({
|
|
6
|
+
MiddlewareSpec: () => MiddlewareSpec,
|
|
7
|
+
TypeId: () => TypeId,
|
|
8
|
+
allFunctionTypes: () => allFunctionTypes,
|
|
9
|
+
errorSchemas: () => errorSchemas,
|
|
10
|
+
isMiddlewareSpec: () => isMiddlewareSpec
|
|
11
|
+
});
|
|
12
|
+
const TypeId = "@confect/core/MiddlewareSpec";
|
|
13
|
+
const isMiddlewareSpec = (u) => Predicate.hasProperty(u, TypeId);
|
|
14
|
+
const allFunctionTypes = [
|
|
15
|
+
"query",
|
|
16
|
+
"mutation",
|
|
17
|
+
"action"
|
|
18
|
+
];
|
|
19
|
+
/**
|
|
20
|
+
* Declare a middleware's client-safe interface: its identifying key, the
|
|
21
|
+
* service it provides to downstream handlers, the error schema its failures
|
|
22
|
+
* encode through, and the function types it may attach to.
|
|
23
|
+
*
|
|
24
|
+
* ```ts
|
|
25
|
+
* class RequireUser extends MiddlewareSpec.MiddlewareSpec<RequireUser, {
|
|
26
|
+
* provides: CurrentUser
|
|
27
|
+
* }>()("RequireUser", {
|
|
28
|
+
* error: () => NotSignedIn,
|
|
29
|
+
* functionTypes: { query: true, mutation: true, action: false },
|
|
30
|
+
* }) {}
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* The implementation is server-only and supplied separately via
|
|
34
|
+
* `MiddlewareImpl.make` (or `makeByFunctionType`/`provides`) in `@confect/server`.
|
|
35
|
+
*/
|
|
36
|
+
const MiddlewareSpec = () => (key, options) => {
|
|
37
|
+
const { query, mutation, action } = options.functionTypes;
|
|
38
|
+
if (!query && !mutation && !action) throw new Error(`Middleware "${key}" must declare at least one function type`);
|
|
39
|
+
function MiddlewareSpecClass() {}
|
|
40
|
+
const class_ = MiddlewareSpecClass;
|
|
41
|
+
class_[TypeId] = TypeId;
|
|
42
|
+
class_.key = key;
|
|
43
|
+
class_.functionTypes = {
|
|
44
|
+
query,
|
|
45
|
+
mutation,
|
|
46
|
+
action
|
|
47
|
+
};
|
|
48
|
+
if (options.error !== void 0) defineProperty(class_, "error", options.error);
|
|
49
|
+
return class_;
|
|
50
|
+
};
|
|
51
|
+
const errorSchemas = (middlewareSpecs) => middlewareSpecs.flatMap((middlewareSpec) => "error" in middlewareSpec && middlewareSpec.error !== void 0 ? [middlewareSpec.error] : []);
|
|
52
|
+
//#endregion
|
|
53
|
+
export { MiddlewareSpec, MiddlewareSpec_exports, TypeId, allFunctionTypes, errorSchemas, isMiddlewareSpec };
|
|
54
|
+
|
|
55
|
+
//# sourceMappingURL=MiddlewareSpec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MiddlewareSpec.js","names":[],"sources":["../src/MiddlewareSpec.ts"],"sourcesContent":["import type { FunctionType, FunctionVisibility } from \"convex/server\";\nimport type * as Effect from \"effect/Effect\";\nimport * as Predicate from \"effect/Predicate\";\nimport type * as Schema from \"effect/Schema\";\nimport type { unhandled } from \"effect/Types\";\nimport type * as FunctionProvenance from \"./FunctionProvenance\";\nimport type * as FunctionSpec from \"./FunctionSpec\";\nimport * as Lazy from \"./Lazy\";\nimport type * as RuntimeAndFunctionType from \"./RuntimeAndFunctionType\";\n\nexport const TypeId = \"@confect/core/MiddlewareSpec\";\nexport type TypeId = typeof TypeId;\n\nexport const isMiddlewareSpec = (u: unknown): u is AnyMiddlewareSpec =>\n Predicate.hasProperty(u, TypeId);\n\nexport const allFunctionTypes: ReadonlyArray<FunctionType> = [\n \"query\",\n \"mutation\",\n \"action\",\n];\n\nexport interface SupportedFunctionTypes {\n readonly query: boolean;\n readonly mutation: boolean;\n readonly action: boolean;\n}\n\n/**\n * Marker success type used by middleware to represent the handler's outcome\n * without exposing its concrete success value.\n *\n * Mirrors `RpcMiddleware.SuccessValue` in Effect.\n */\nexport interface SuccessValue {\n readonly _: unique symbol;\n}\n\n/**\n * The wrap-style middleware implementation shape: receives the downstream\n * effect (the remaining middleware plus the function handler) and the\n * covered invocation's metadata, and returns the effect that runs in its\n * place.\n *\n * Mirrors `RpcMiddleware.RpcMiddleware` in Effect.\n */\nexport interface MiddlewareImpl<Provides_, E, R> {\n (\n effect: Effect.Effect<SuccessValue, E | unhandled, Provides_>,\n options: MiddlewareOptions,\n ): Effect.Effect<SuccessValue, E | unhandled, R>;\n}\n\nexport interface MiddlewareOptions {\n readonly name: string;\n readonly functionType: FunctionType;\n readonly functionVisibility: FunctionVisibility;\n readonly args: unknown;\n}\n\nexport interface AnyMiddlewareImpl extends MiddlewareImpl<any, any, any> {}\n\n/**\n * The class shape produced by {@link MiddlewareSpec}. Only the static side is ever\n * used — the class is a value-level carrier for the middleware's key, its\n * declared functionTypes, and its (lazily-evaluated) error schema, plus the\n * type-level `Provides`/`Error` metadata. It is deliberately not a\n * `Context.Tag`: implementations are registered through the group `Registry`\n * (like `FunctionImpl`s), not resolved from a Layer context.\n */\nexport interface MiddlewareSpec<\n in out Self,\n Key_ extends string,\n Provides_,\n Requires_,\n ErrorSchema_ extends Schema.Codec<any, any>,\n FunctionTypes_ extends FunctionType,\n> {\n new (_: never): {\n readonly [TypeId]: TypeId;\n readonly key: Key_;\n };\n readonly [TypeId]: TypeId;\n readonly key: Key_;\n readonly functionTypes: SupportedFunctionTypes;\n readonly error?: ErrorSchema_;\n readonly \"~Provides\": Provides_;\n readonly \"~Requires\": Requires_;\n readonly \"~Error\": ErrorSchema_;\n readonly \"~FunctionTypes\": FunctionTypes_;\n readonly \"~Self\": Self;\n}\n\nexport interface AnyMiddlewareSpec {\n readonly [TypeId]: TypeId;\n readonly key: string;\n readonly functionTypes: SupportedFunctionTypes;\n readonly error?: Schema.Codec<any, any>;\n readonly \"~Provides\": any;\n readonly \"~Requires\": any;\n readonly \"~Error\": any;\n readonly \"~FunctionTypes\": FunctionType;\n}\n\nexport type Key<MiddlewareSpec_ extends AnyMiddlewareSpec> =\n MiddlewareSpec_[\"key\"];\n\nexport type Provides<MiddlewareSpec_ extends AnyMiddlewareSpec> =\n MiddlewareSpec_ extends AnyMiddlewareSpec\n ? MiddlewareSpec_[\"~Provides\"]\n : never;\n\n/**\n * The services a middleware's implementation may consume from middleware\n * that runs earlier in the same chain (type-level only, like `provides`).\n */\nexport type Requires<MiddlewareSpec_ extends AnyMiddlewareSpec> =\n MiddlewareSpec_ extends AnyMiddlewareSpec\n ? MiddlewareSpec_[\"~Requires\"]\n : never;\n\nexport type ErrorSchema<MiddlewareSpec_ extends AnyMiddlewareSpec> =\n MiddlewareSpec_ extends AnyMiddlewareSpec ? MiddlewareSpec_[\"~Error\"] : never;\n\nexport type Error<MiddlewareSpec_ extends AnyMiddlewareSpec> =\n MiddlewareSpec_ extends AnyMiddlewareSpec\n ? MiddlewareSpec_[\"~Error\"][\"Type\"]\n : never;\n\nexport type EncodedError<MiddlewareSpec_ extends AnyMiddlewareSpec> =\n MiddlewareSpec_ extends AnyMiddlewareSpec\n ? MiddlewareSpec_[\"~Error\"][\"Encoded\"]\n : never;\n\nexport type FunctionTypes<MiddlewareSpec_ extends AnyMiddlewareSpec> =\n MiddlewareSpec_ extends AnyMiddlewareSpec\n ? MiddlewareSpec_[\"~FunctionTypes\"]\n : never;\n\n/**\n * Declare a middleware's client-safe interface: its identifying key, the\n * service it provides to downstream handlers, the error schema its failures\n * encode through, and the function types it may attach to.\n *\n * ```ts\n * class RequireUser extends MiddlewareSpec.MiddlewareSpec<RequireUser, {\n * provides: CurrentUser\n * }>()(\"RequireUser\", {\n * error: () => NotSignedIn,\n * functionTypes: { query: true, mutation: true, action: false },\n * }) {}\n * ```\n *\n * The implementation is server-only and supplied separately via\n * `MiddlewareImpl.make` (or `makeByFunctionType`/`provides`) in `@confect/server`.\n */\nexport const MiddlewareSpec =\n <Self, Config extends { provides?: any; requires?: any } = {}>() =>\n <\n const Key_ extends string,\n const FunctionTypesConfig_ extends SupportedFunctionTypes,\n ErrorSchema_ extends Schema.Codec<any, any> = never,\n >(\n key: Key_,\n options: {\n readonly error?: () => ErrorSchema_;\n readonly functionTypes: FunctionTypesConfig_ &\n ValidateFunctionTypesConfig<FunctionTypesConfig_>;\n },\n ): MiddlewareSpec<\n Self,\n Key_,\n \"provides\" extends keyof Config ? Config[\"provides\"] : never,\n \"requires\" extends keyof Config ? Config[\"requires\"] : never,\n ErrorSchema_,\n FunctionTypesFromConfig<FunctionTypesConfig_>\n > => {\n const { query, mutation, action } = options.functionTypes;\n if (!query && !mutation && !action) {\n throw new Error(\n `Middleware \"${key}\" must declare at least one function type`,\n );\n }\n\n function MiddlewareSpecClass() {}\n const class_ = MiddlewareSpecClass as any;\n class_[TypeId] = TypeId;\n class_.key = key;\n class_.functionTypes = {\n query,\n mutation,\n action,\n } satisfies SupportedFunctionTypes;\n if (options.error !== undefined) {\n Lazy.defineProperty(class_, \"error\", options.error);\n }\n return class_;\n };\n\ntype FunctionTypesFromConfig<Config extends SupportedFunctionTypes> = {\n [K in FunctionType]: Config[K] extends true ? K : never;\n}[FunctionType];\n\ntype ValidateFunctionTypesConfig<Config extends SupportedFunctionTypes> =\n ValidationResult<\n | {\n [K in keyof Config]: boolean extends Config[K]\n ? AttachmentError<`Function type flag \"${K & string}\" must be a literal \\`true\\` or \\`false\\`, not a computed \\`boolean\\``>\n : never;\n }[keyof Config]\n | ([FunctionTypesFromConfig<Config>] extends [never]\n ? AttachmentError<`A middleware must declare at least one function type`>\n : never)\n >;\n\nexport const errorSchemas = (\n middlewareSpecs: ReadonlyArray<AnyMiddlewareSpec>,\n): ReadonlyArray<Schema.Codec<any, any>> =>\n middlewareSpecs.flatMap((middlewareSpec) =>\n \"error\" in middlewareSpec && middlewareSpec.error !== undefined\n ? [middlewareSpec.error]\n : [],\n );\n\n/**\n * A branded, unconstructible type carrying a spec-authoring error message.\n * Surfaced as (part of) a parameter type so the diagnostic names the exact\n * problem at the `.middleware()` / `.addFunction()` call site.\n */\nexport interface AttachmentError<Message extends string> {\n readonly \"~confect/MiddlewareSpec/AttachmentError\": Message;\n}\n\ntype FunctionTypeOf<FunctionSpec_ extends FunctionSpec.AnyWithProps> =\n FunctionSpec_[\"runtimeAndFunctionType\"][\"functionType\"];\n\ntype ValidationResult<Errors> = [Errors] extends [never] ? unknown : Errors;\n\n/**\n * A matching-type Convex-provenance function is rejected rather than\n * silently skipped: its raw handler passes through Confect untouched, so\n * the middleware could not actually cover it, and skipping it would be a\n * policy hole.\n */\nexport type ValidateFunction<\n FunctionSpec_ extends FunctionSpec.AnyWithProps,\n MiddlewareSpec_ extends AnyMiddlewareSpec,\n> = FunctionSpec_ extends any\n ? MiddlewareSpec_ extends any\n ? FunctionSpec_ extends {\n readonly functionProvenance: { readonly _tag: \"Convex\" };\n }\n ? FunctionTypeOf<FunctionSpec_> extends FunctionTypes<MiddlewareSpec_>\n ? 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`>\n : never\n : [\n Extract<\n FunctionSpec.MiddlewareSpecs<FunctionSpec_>,\n { readonly key: Key<MiddlewareSpec_> }\n >,\n ] extends [never]\n ? FunctionTypeOf<FunctionSpec_> extends FunctionTypes<MiddlewareSpec_>\n ? never\n : AttachmentError<`Middleware \"${Key<MiddlewareSpec_>}\" does not declare function type \"${FunctionTypeOf<FunctionSpec_>}\", the type of function \"${FunctionSpec_[\"name\"]}\"`>\n : AttachmentError<`Middleware \"${Key<MiddlewareSpec_>}\" is already attached to function \"${FunctionSpec_[\"name\"]}\"`>\n : never\n : never;\n\n/**\n * Attachment order is chain order, so requiring `requires` to be satisfied\n * by already-attached middleware is exactly requiring the provider to run\n * earlier.\n */\nexport type ValidateAttach<\n MiddlewareSpec_ extends AnyMiddlewareSpec,\n Functions_ extends FunctionSpec.AnyWithProps,\n MiddlewareSpecs_ extends AnyMiddlewareSpec,\n> = [\n Extract<MiddlewareSpecs_, { readonly key: Key<MiddlewareSpec_> }>,\n] extends [never]\n ? [Exclude<Requires<MiddlewareSpec_>, Provides<MiddlewareSpecs_>>] extends [\n never,\n ]\n ? ValidationResult<ValidateFunction<Functions_, MiddlewareSpec_>>\n : AttachmentError<`Middleware \"${Key<MiddlewareSpec_>}\" requires services that no middleware attached earlier to this group provides — attach the providing middleware first`>\n : AttachmentError<`Middleware \"${Key<MiddlewareSpec_>}\" is already attached to this group`>;\n\nexport type ValidateAddedFunction<\n FunctionSpec_ extends FunctionSpec.AnyWithProps,\n MiddlewareSpecs_ extends AnyMiddlewareSpec,\n> = [MiddlewareSpecs_] extends [never]\n ? unknown\n : ValidationResult<\n | ValidateFunction<FunctionSpec_, MiddlewareSpecs_>\n | GroupOverlap<FunctionSpec_, MiddlewareSpecs_>\n >;\n\ntype GroupOverlap<\n FunctionSpec_ extends FunctionSpec.AnyWithProps,\n MiddlewareSpecs_ extends AnyMiddlewareSpec,\n> =\n FunctionSpec.MiddlewareSpecs<FunctionSpec_> extends infer FunctionMiddlewareSpec\n ? FunctionMiddlewareSpec extends AnyMiddlewareSpec\n ? [\n Extract<\n MiddlewareSpecs_,\n { readonly key: Key<FunctionMiddlewareSpec> }\n >,\n ] extends [never]\n ? never\n : AttachmentError<`Middleware \"${Key<FunctionMiddlewareSpec>}\" is attached to both function \"${FunctionSpec_[\"name\"]}\" and its group`>\n : never\n : never;\n\n/**\n * The parameter-type validation applied to the group spec at\n * `GroupImpl.make`: every function's attached middleware must have its\n * `requires` services provided by some middleware covering that function\n * (the group's or the function's own). Checked here — with the group fully\n * assembled — rather than at the spec builders, because a function-level\n * middleware's `requires` may legitimately be satisfied by a group\n * middleware the function spec never sees, and group attachment is\n * declaratively order-independent with respect to `addFunction`.\n */\nexport type ValidateImplRequires<\n Functions_ extends FunctionSpec.AnyWithProps,\n GroupMiddlewareSpecs_ extends AnyMiddlewareSpec,\n> = ValidationResult<\n Functions_ extends any\n ? [\n Exclude<\n Requires<FunctionSpec.MiddlewareSpecs<Functions_>>,\n Provides<\n GroupMiddlewareSpecs_ | FunctionSpec.MiddlewareSpecs<Functions_>\n >\n >,\n ] extends [never]\n ? never\n : AttachmentError<`Function \"${Functions_[\"name\"]}\" has middleware requiring services that no middleware covering it provides`>\n : never\n>;\n\nexport type ValidateFunctionAttach<\n MiddlewareSpec_ extends AnyMiddlewareSpec,\n RuntimeAndFunctionType_ extends RuntimeAndFunctionType.RuntimeAndFunctionType,\n FunctionProvenance_ extends FunctionProvenance.FunctionProvenance,\n MiddlewareSpecs_ extends AnyMiddlewareSpec,\n> = FunctionProvenance_ extends { readonly _tag: \"Convex\" }\n ? AttachmentError<`Plain Convex functions cannot have middleware — their raw handlers are passed through untouched`>\n : [\n Extract<MiddlewareSpecs_, { readonly key: Key<MiddlewareSpec_> }>,\n ] extends [never]\n ? RuntimeAndFunctionType_[\"functionType\"] extends FunctionTypes<MiddlewareSpec_>\n ? unknown\n : AttachmentError<`Middleware \"${Key<MiddlewareSpec_>}\" does not declare function type \"${RuntimeAndFunctionType_[\"functionType\"]}\"`>\n : AttachmentError<`Middleware \"${Key<MiddlewareSpec_>}\" is already attached to this function`>;\n"],"mappings":";;;;;;;;;;;AAUA,MAAa,SAAS;AAGtB,MAAa,oBAAoB,MAC/B,UAAU,YAAY,GAAG,MAAM;AAEjC,MAAa,mBAAgD;CAC3D;CACA;CACA;AACF;;;;;;;;;;;;;;;;;;AAwIA,MAAa,wBAOT,KACA,YAYG;CACH,MAAM,EAAE,OAAO,UAAU,WAAW,QAAQ;CAC5C,IAAI,CAAC,SAAS,CAAC,YAAY,CAAC,QAC1B,MAAM,IAAI,MACR,eAAe,IAAI,0CACrB;CAGF,SAAS,sBAAsB,CAAC;CAChC,MAAM,SAAS;CACf,OAAO,UAAU;CACjB,OAAO,MAAM;CACb,OAAO,gBAAgB;EACrB;EACA;EACA;CACF;CACA,IAAI,QAAQ,UAAU,KAAA,GACpB,eAAoB,QAAQ,SAAS,QAAQ,KAAK;CAEpD,OAAO;AACT;AAkBF,MAAa,gBACX,oBAEA,gBAAgB,SAAS,mBACvB,WAAW,kBAAkB,eAAe,UAAU,KAAA,IAClD,CAAC,eAAe,KAAK,IACrB,CAAC,CACP"}
|