{"version":3,"file":"auth.mjs","names":["createEffectAuthFunctions","auth","createAuthFunctions","config","effectZodApiBuilder","options","allowed","adaptService","principal","wrapEffectBuilder","builder"],"sources":["../../../src/modules/effect/auth.ts"],"sourcesContent":["import type { Context } from 'effect'\nimport type {\n\tGenericDataModel,\n\tGenericQueryCtx,\n\tGenericMutationCtx,\n\tGenericActionCtx,\n} from 'convex/server'\nimport type { CustomBuilder } from 'convex-helpers/server/zod4'\nimport {\n\tcreateAuthFunctions,\n\ttype AuthFunctions,\n\ttype AuthFunctionsConfig,\n\ttype AuthBundle,\n\ttype DefaultRole,\n\ttype Include,\n\ttype ServicePrincipal,\n\ttype ServiceQuery,\n\ttype ServiceMutation,\n} from '../../auth'\nimport { effectZodApiBuilder, type EffectZodApiBuilder } from './api-zod'\nimport {\n\twrapEffectBuilder,\n\ttype EffectApiOptions,\n\ttype EffectApiServices,\n\ttype EffectApiContext,\n\ttype ProvidedServices,\n} from './api-runtime'\nimport type { EffectValue } from './operation'\n\n// Derive the adapter from the symbolic auth contract, never from inferred native spreads.\ntype Adapted<Builder, Services, Added> =\n\tBuilder extends CustomBuilder<\n\t\tinfer Kind,\n\t\tinfer Args,\n\t\tinfer Ctx,\n\t\tinfer Made,\n\t\tinfer Input,\n\t\tinfer Visibility,\n\t\tinfer Extra\n\t>\n\t\t? EffectZodApiBuilder<\n\t\t\t\tKind,\n\t\t\t\tArgs,\n\t\t\t\tCtx,\n\t\t\t\tMade,\n\t\t\t\tInput,\n\t\t\t\tVisibility,\n\t\t\t\tExtra,\n\t\t\t\tProvidedServices<Services>,\n\t\t\t\tEffectValue<Added>,\n\t\t\t\tAdded\n\t\t\t>\n\t\t: never\n\n/** Shared post-auth composition; internal/system policies are configured separately. */\nexport type EffectAuthFunctions<\n\tDataModel extends GenericDataModel,\n\tRole extends string = DefaultRole,\n\tQueryServices extends EffectApiServices = Context.Context<never>,\n\tQueryAdded extends EffectApiContext = {},\n\tMutationServices extends EffectApiServices = Context.Context<never>,\n\tMutationAdded extends EffectApiContext = {},\n\tActionServices extends EffectApiServices = Context.Context<never>,\n\tActionAdded extends EffectApiContext = {},\n\tSystemQueryServices extends EffectApiServices = Context.Context<never>,\n\tSystemQueryAdded extends EffectApiContext = {},\n\tSystemMutationServices extends EffectApiServices = Context.Context<never>,\n\tSystemMutationAdded extends EffectApiContext = {},\n\tSystemActionServices extends EffectApiServices = Context.Context<never>,\n\tSystemActionAdded extends EffectApiContext = {},\n> = Pick<AuthFunctions<DataModel, Role>, 'include' | 'authenticatedUser'> & {\n\tauthQuery: Adapted<AuthFunctions<DataModel, Role>['authQuery'], QueryServices, QueryAdded>\n\troleQuery: (\n\t\t...allowed: readonly Role[]\n\t) => Adapted<AuthFunctions<DataModel, Role>['authQuery'], QueryServices, QueryAdded>\n\tadminQuery: Adapted<AuthFunctions<DataModel, Role>['authQuery'], QueryServices, QueryAdded>\n\tauthMutation: Adapted<\n\t\tAuthFunctions<DataModel, Role>['authMutation'],\n\t\tMutationServices,\n\t\tMutationAdded\n\t>\n\troleMutation: (\n\t\t...allowed: readonly Role[]\n\t) => Adapted<AuthFunctions<DataModel, Role>['authMutation'], MutationServices, MutationAdded>\n\tadminMutation: Adapted<\n\t\tAuthFunctions<DataModel, Role>['authMutation'],\n\t\tMutationServices,\n\t\tMutationAdded\n\t>\n\tauthAction: Adapted<AuthFunctions<DataModel, Role>['authAction'], ActionServices, ActionAdded>\n\troleAction: (\n\t\t...allowed: readonly Role[]\n\t) => Adapted<AuthFunctions<DataModel, Role>['authAction'], ActionServices, ActionAdded>\n\tadminAction: Adapted<AuthFunctions<DataModel, Role>['authAction'], ActionServices, ActionAdded>\n\t/** Service principals share the `query` policy; the host factory is the only principal source. */\n\tserviceQuery: <Extra extends Record<string, unknown> = {}>(\n\t\tprincipal: ServicePrincipal<GenericQueryCtx<DataModel>, Role, Extra>,\n\t) => Adapted<ServiceQuery<DataModel, Role, Extra>, QueryServices, QueryAdded>\n\t/** Service principals share the `mutation` policy. */\n\tserviceMutation: <Extra extends Record<string, unknown> = {}>(\n\t\tprincipal: ServicePrincipal<GenericMutationCtx<DataModel>, Role, Extra>,\n\t) => Adapted<ServiceMutation<DataModel, Role, Extra>, MutationServices, MutationAdded>\n\tsystemQuery: Adapted<\n\t\tAuthFunctions<DataModel, Role>['systemQuery'],\n\t\tSystemQueryServices,\n\t\tSystemQueryAdded\n\t>\n\tsystemMutation: Adapted<\n\t\tAuthFunctions<DataModel, Role>['systemMutation'],\n\t\tSystemMutationServices,\n\t\tSystemMutationAdded\n\t>\n\tsystemAction: Adapted<\n\t\tAuthFunctions<DataModel, Role>['systemAction'],\n\t\tSystemActionServices,\n\t\tSystemActionAdded\n\t>\n}\n\n/** Providers and handles run once per invocation after security and live role checks. */\nexport function createEffectAuthFunctions<\n\tDataModel extends GenericDataModel,\n\tconst Role extends string = DefaultRole,\n\tQueryServices extends EffectApiServices = Context.Context<never>,\n\tQueryAdded extends EffectApiContext = {},\n\tMutationServices extends EffectApiServices = Context.Context<never>,\n\tMutationAdded extends EffectApiContext = {},\n\tActionServices extends EffectApiServices = Context.Context<never>,\n\tActionAdded extends EffectApiContext = {},\n\tSystemQueryServices extends EffectApiServices = Context.Context<never>,\n\tSystemQueryAdded extends EffectApiContext = {},\n\tSystemMutationServices extends EffectApiServices = Context.Context<never>,\n\tSystemMutationAdded extends EffectApiContext = {},\n\tSystemActionServices extends EffectApiServices = Context.Context<never>,\n\tSystemActionAdded extends EffectApiContext = {},\n>(\n\tconfig: AuthFunctionsConfig<DataModel, Role>,\n\toptions: {\n\t\tquery: EffectApiOptions<\n\t\t\tGenericQueryCtx<DataModel> & AuthBundle<Role> & { include: Include },\n\t\t\tQueryServices,\n\t\t\tQueryAdded\n\t\t>\n\t\tmutation: EffectApiOptions<\n\t\t\tGenericMutationCtx<DataModel> & AuthBundle<Role> & { include: Include },\n\t\t\tMutationServices,\n\t\t\tMutationAdded\n\t\t>\n\t\taction: EffectApiOptions<\n\t\t\tGenericActionCtx<DataModel> & AuthBundle<Role>,\n\t\t\tActionServices,\n\t\t\tActionAdded\n\t\t>\n\t\tsystemQuery: EffectApiOptions<\n\t\t\tGenericQueryCtx<DataModel> & { include: Include },\n\t\t\tSystemQueryServices,\n\t\t\tSystemQueryAdded\n\t\t>\n\t\tsystemMutation: EffectApiOptions<\n\t\t\tGenericMutationCtx<DataModel> & { include: Include },\n\t\t\tSystemMutationServices,\n\t\t\tSystemMutationAdded\n\t\t>\n\t\tsystemAction: EffectApiOptions<\n\t\t\tGenericActionCtx<DataModel>,\n\t\t\tSystemActionServices,\n\t\t\tSystemActionAdded\n\t\t>\n\t},\n): EffectAuthFunctions<\n\tDataModel,\n\tRole,\n\tQueryServices,\n\tQueryAdded,\n\tMutationServices,\n\tMutationAdded,\n\tActionServices,\n\tActionAdded,\n\tSystemQueryServices,\n\tSystemQueryAdded,\n\tSystemMutationServices,\n\tSystemMutationAdded,\n\tSystemActionServices,\n\tSystemActionAdded\n> {\n\tconst auth = createAuthFunctions(config)\n\treturn {\n\t\tinclude: auth.include,\n\t\tauthenticatedUser: auth.authenticatedUser,\n\t\tauthQuery: effectZodApiBuilder(auth.authQuery, options.query),\n\t\troleQuery: (...allowed) => effectZodApiBuilder(auth.roleQuery(...allowed), options.query),\n\t\tadminQuery: effectZodApiBuilder(auth.adminQuery, options.query),\n\t\tauthMutation: effectZodApiBuilder(auth.authMutation, options.mutation),\n\t\troleMutation: (...allowed) =>\n\t\t\teffectZodApiBuilder(auth.roleMutation(...allowed), options.mutation),\n\t\tadminMutation: effectZodApiBuilder(auth.adminMutation, options.mutation),\n\t\tauthAction: effectZodApiBuilder(auth.authAction, options.action),\n\t\troleAction: (...allowed) => effectZodApiBuilder(auth.roleAction(...allowed), options.action),\n\t\tadminAction: effectZodApiBuilder(auth.adminAction, options.action),\n\t\tserviceQuery: <Extra extends Record<string, unknown> = {}>(\n\t\t\tprincipal: ServicePrincipal<GenericQueryCtx<DataModel>, Role, Extra>,\n\t\t) =>\n\t\t\tadaptService<ServiceQuery<DataModel, Role, Extra>, QueryServices, QueryAdded>(\n\t\t\t\tauth.serviceQuery(principal),\n\t\t\t\toptions.query,\n\t\t\t),\n\t\tserviceMutation: <Extra extends Record<string, unknown> = {}>(\n\t\t\tprincipal: ServicePrincipal<GenericMutationCtx<DataModel>, Role, Extra>,\n\t\t) =>\n\t\t\tadaptService<ServiceMutation<DataModel, Role, Extra>, MutationServices, MutationAdded>(\n\t\t\t\tauth.serviceMutation(principal),\n\t\t\t\toptions.mutation,\n\t\t\t),\n\t\tsystemQuery: effectZodApiBuilder(auth.systemQuery, options.systemQuery),\n\t\tsystemMutation: effectZodApiBuilder(auth.systemMutation, options.systemMutation),\n\t\tsystemAction: effectZodApiBuilder(auth.systemAction, options.systemAction),\n\t}\n}\n\n/** Service builders reuse the query/mutation policy; the ctx adds only host extras. */\nfunction adaptService<Builder extends Function, Services extends EffectApiServices, Added>(\n\tbuilder: Builder,\n\toptions: Parameters<typeof wrapEffectBuilder>[1],\n): Adapted<Builder, Services, Added> {\n\t// SAFETY: the shared policy callbacks read only the standard auth bundle, which every\n\t// service ctx carries; wrapEffectBuilder swaps the handler and keeps the builder's\n\t// registration kind, validation and visibility, as effectZodApiBuilder does.\n\treturn wrapEffectBuilder(builder, options) as Adapted<Builder, Services, Added>\n}\n"],"mappings":"+JAwHA,SAAgBA,EAgBf,EACA,EA+CC,CACD,IAAMC,EAAOC,EAAoBC,CAAM,EACvC,MAAO,CACN,QAASF,EAAK,QACd,kBAAmBA,EAAK,kBACxB,UAAWG,EAAoBH,EAAK,UAAWI,EAAQ,KAAK,EAC5D,WAAY,GAAGC,IAAYF,EAAoBH,EAAK,UAAU,GAAGK,CAAO,EAAGD,EAAQ,KAAK,EACxF,WAAYD,EAAoBH,EAAK,WAAYI,EAAQ,KAAK,EAC9D,aAAcD,EAAoBH,EAAK,aAAcI,EAAQ,QAAQ,EACrE,cAAe,GAAGC,IACjBF,EAAoBH,EAAK,aAAa,GAAGK,CAAO,EAAGD,EAAQ,QAAQ,EACpE,cAAeD,EAAoBH,EAAK,cAAeI,EAAQ,QAAQ,EACvE,WAAYD,EAAoBH,EAAK,WAAYI,EAAQ,MAAM,EAC/D,YAAa,GAAGC,IAAYF,EAAoBH,EAAK,WAAW,GAAGK,CAAO,EAAGD,EAAQ,MAAM,EAC3F,YAAaD,EAAoBH,EAAK,YAAaI,EAAQ,MAAM,EACjE,aACC,GAEAE,EACCN,EAAK,aAAaO,CAAS,EAC3BH,EAAQ,KACT,EACD,gBACC,GAEAE,EACCN,EAAK,gBAAgBO,CAAS,EAC9BH,EAAQ,QACT,EACD,YAAaD,EAAoBH,EAAK,YAAaI,EAAQ,WAAW,EACtE,eAAgBD,EAAoBH,EAAK,eAAgBI,EAAQ,cAAc,EAC/E,aAAcD,EAAoBH,EAAK,aAAcI,EAAQ,YAAY,CAC1E,CACD,CAGA,SAASE,EACR,EACA,EACoC,CAIpC,OAAOE,EAAkBC,EAASL,CAAO,CAC1C"}