cvx-kit 0.0.1
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/LICENSE +21 -0
- package/README.md +200 -0
- package/dist/_virtual/_rolldown/runtime.mjs +1 -0
- package/dist/auth.d.mts +353 -0
- package/dist/auth.mjs +2 -0
- package/dist/auth.mjs.map +1 -0
- package/dist/components/approvals/_generated/api.d.mts +47 -0
- package/dist/components/approvals/_generated/api.mjs +2 -0
- package/dist/components/approvals/_generated/api.mjs.map +1 -0
- package/dist/components/approvals/_generated/component.d.mts +297 -0
- package/dist/components/approvals/_generated/component.mjs +1 -0
- package/dist/components/approvals/_generated/dataModel.d.mts +41 -0
- package/dist/components/approvals/_generated/dataModel.mjs +1 -0
- package/dist/components/approvals/_generated/server.d.mts +116 -0
- package/dist/components/approvals/_generated/server.mjs +2 -0
- package/dist/components/approvals/_generated/server.mjs.map +1 -0
- package/dist/components/approvals/audit.d.mts +40 -0
- package/dist/components/approvals/audit.mjs +2 -0
- package/dist/components/approvals/audit.mjs.map +1 -0
- package/dist/components/approvals/client.d.mts +98 -0
- package/dist/components/approvals/client.mjs +2 -0
- package/dist/components/approvals/client.mjs.map +1 -0
- package/dist/components/approvals/constants.d.mts +31 -0
- package/dist/components/approvals/constants.mjs +2 -0
- package/dist/components/approvals/constants.mjs.map +1 -0
- package/dist/components/approvals/convex.config.d.mts +5 -0
- package/dist/components/approvals/convex.config.mjs +2 -0
- package/dist/components/approvals/convex.config.mjs.map +1 -0
- package/dist/components/approvals/decisions.d.mts +61 -0
- package/dist/components/approvals/decisions.mjs +2 -0
- package/dist/components/approvals/decisions.mjs.map +1 -0
- package/dist/components/approvals/functions.d.mts +9 -0
- package/dist/components/approvals/functions.mjs +2 -0
- package/dist/components/approvals/functions.mjs.map +1 -0
- package/dist/components/approvals/requests.d.mts +323 -0
- package/dist/components/approvals/requests.mjs +2 -0
- package/dist/components/approvals/requests.mjs.map +1 -0
- package/dist/components/approvals/schema.d.mts +376 -0
- package/dist/components/approvals/schema.mjs +2 -0
- package/dist/components/approvals/schema.mjs.map +1 -0
- package/dist/components/approvals/validators.d.mts +302 -0
- package/dist/components/approvals/validators.mjs +2 -0
- package/dist/components/approvals/validators.mjs.map +1 -0
- package/dist/components/approvals/workflow.d.mts +11 -0
- package/dist/components/approvals/workflow.mjs +2 -0
- package/dist/components/approvals/workflow.mjs.map +1 -0
- package/dist/components/approvals/workflow_steps.d.mts +83 -0
- package/dist/components/approvals/workflow_steps.mjs +2 -0
- package/dist/components/approvals/workflow_steps.mjs.map +1 -0
- package/dist/components/foundation/_generated/api.d.mts +40 -0
- package/dist/components/foundation/_generated/api.mjs +2 -0
- package/dist/components/foundation/_generated/api.mjs.map +1 -0
- package/dist/components/foundation/_generated/component.d.mts +21 -0
- package/dist/components/foundation/_generated/component.mjs +1 -0
- package/dist/components/foundation/_generated/dataModel.d.mts +41 -0
- package/dist/components/foundation/_generated/dataModel.mjs +1 -0
- package/dist/components/foundation/_generated/server.d.mts +116 -0
- package/dist/components/foundation/_generated/server.mjs +2 -0
- package/dist/components/foundation/_generated/server.mjs.map +1 -0
- package/dist/components/foundation/client.d.mts +93 -0
- package/dist/components/foundation/client.mjs +2 -0
- package/dist/components/foundation/client.mjs.map +1 -0
- package/dist/components/foundation/convex.config.d.mts +5 -0
- package/dist/components/foundation/convex.config.mjs +2 -0
- package/dist/components/foundation/convex.config.mjs.map +1 -0
- package/dist/components/foundation/functions.d.mts +8 -0
- package/dist/components/foundation/functions.mjs +2 -0
- package/dist/components/foundation/functions.mjs.map +1 -0
- package/dist/components/foundation/modules/command/command.d.mts +47 -0
- package/dist/components/foundation/modules/command/command.mjs +2 -0
- package/dist/components/foundation/modules/command/command.mjs.map +1 -0
- package/dist/components/foundation/modules/observability/observability.d.mts +31 -0
- package/dist/components/foundation/modules/observability/observability.mjs +2 -0
- package/dist/components/foundation/modules/observability/observability.mjs.map +1 -0
- package/dist/components/foundation/modules/query/query.d.mts +24 -0
- package/dist/components/foundation/modules/query/query.mjs +2 -0
- package/dist/components/foundation/modules/query/query.mjs.map +1 -0
- package/dist/components/foundation/result.d.mts +27 -0
- package/dist/components/foundation/result.mjs +2 -0
- package/dist/components/foundation/result.mjs.map +1 -0
- package/dist/components/foundation/schema.d.mts +6 -0
- package/dist/components/foundation/schema.mjs +2 -0
- package/dist/components/foundation/schema.mjs.map +1 -0
- package/dist/components/foundation/telemetry.d.mts +9 -0
- package/dist/components/foundation/telemetry.mjs +2 -0
- package/dist/components/foundation/telemetry.mjs.map +1 -0
- package/dist/errors.d.mts +24 -0
- package/dist/errors.mjs +2 -0
- package/dist/errors.mjs.map +1 -0
- package/dist/index.d.mts +6 -0
- package/dist/index.mjs +1 -0
- package/dist/triggers.d.mts +27 -0
- package/dist/triggers.mjs +2 -0
- package/dist/triggers.mjs.map +1 -0
- package/dist/zod-table.d.mts +124 -0
- package/dist/zod-table.mjs +2 -0
- package/dist/zod-table.mjs.map +1 -0
- package/package.json +108 -0
- package/src/auth.ts +360 -0
- package/src/components/approvals/_generated/api.ts +69 -0
- package/src/components/approvals/_generated/component.ts +335 -0
- package/src/components/approvals/_generated/dataModel.ts +60 -0
- package/src/components/approvals/_generated/server.ts +156 -0
- package/src/components/approvals/audit.ts +148 -0
- package/src/components/approvals/client.ts +321 -0
- package/src/components/approvals/constants.ts +51 -0
- package/src/components/approvals/convex.config.ts +10 -0
- package/src/components/approvals/decisions.ts +376 -0
- package/src/components/approvals/functions.ts +27 -0
- package/src/components/approvals/requests.ts +240 -0
- package/src/components/approvals/schema.ts +23 -0
- package/src/components/approvals/validators.ts +272 -0
- package/src/components/approvals/workflow.ts +204 -0
- package/src/components/approvals/workflow_steps.ts +189 -0
- package/src/components/foundation/_generated/api.ts +62 -0
- package/src/components/foundation/_generated/component.ts +29 -0
- package/src/components/foundation/_generated/dataModel.ts +60 -0
- package/src/components/foundation/_generated/server.ts +156 -0
- package/src/components/foundation/client.ts +190 -0
- package/src/components/foundation/convex.config.ts +3 -0
- package/src/components/foundation/functions.ts +10 -0
- package/src/components/foundation/modules/command/__tests__/command.test.ts +106 -0
- package/src/components/foundation/modules/command/command.ts +154 -0
- package/src/components/foundation/modules/observability/__tests__/observability.test.ts +79 -0
- package/src/components/foundation/modules/observability/observability.ts +95 -0
- package/src/components/foundation/modules/query/__tests__/query.test.ts +25 -0
- package/src/components/foundation/modules/query/query.ts +47 -0
- package/src/components/foundation/result.ts +77 -0
- package/src/components/foundation/schema.ts +4 -0
- package/src/components/foundation/telemetry.ts +23 -0
- package/src/errors.ts +25 -0
- package/src/index.ts +41 -0
- package/src/test.ts +36 -0
- package/src/triggers.ts +90 -0
- package/src/zod-table.ts +197 -0
package/src/auth.ts
ADDED
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
import {
|
|
2
|
+
zCustomAction,
|
|
3
|
+
zCustomMutation,
|
|
4
|
+
zCustomQuery,
|
|
5
|
+
} from 'convex-helpers/server/zod4'
|
|
6
|
+
import type {
|
|
7
|
+
ActionBuilder,
|
|
8
|
+
DocumentByInfo,
|
|
9
|
+
GenericActionCtx,
|
|
10
|
+
GenericDataModel,
|
|
11
|
+
GenericMutationCtx,
|
|
12
|
+
GenericQueryCtx,
|
|
13
|
+
GenericTableInfo,
|
|
14
|
+
IndexNames,
|
|
15
|
+
IndexRange,
|
|
16
|
+
IndexRangeBuilder,
|
|
17
|
+
MutationBuilder,
|
|
18
|
+
NamedIndex,
|
|
19
|
+
Query,
|
|
20
|
+
QueryBuilder,
|
|
21
|
+
QueryInitializer,
|
|
22
|
+
UserIdentity,
|
|
23
|
+
} from 'convex/server'
|
|
24
|
+
import type { Triggers } from 'convex-helpers/server/triggers'
|
|
25
|
+
import { defaultErrors, type ErrorFactory } from './errors'
|
|
26
|
+
|
|
27
|
+
/** The default vocabulary; every app can supply its own via the Role generic. */
|
|
28
|
+
export type DefaultRole = 'reader' | 'writer' | 'admin'
|
|
29
|
+
|
|
30
|
+
export type Actor<Role extends string> = Readonly<{
|
|
31
|
+
userId: string
|
|
32
|
+
organizationId: string
|
|
33
|
+
role: Role
|
|
34
|
+
}>
|
|
35
|
+
|
|
36
|
+
export type AuthBundle<Role extends string> = Readonly<{
|
|
37
|
+
identity: UserIdentity
|
|
38
|
+
user: Readonly<{ id: string }>
|
|
39
|
+
org: Readonly<{ organizationId: string; role: Role }>
|
|
40
|
+
role: Role
|
|
41
|
+
actor: Actor<Role>
|
|
42
|
+
}>
|
|
43
|
+
|
|
44
|
+
type AnyAuthContext<DataModel extends GenericDataModel> =
|
|
45
|
+
| GenericQueryCtx<DataModel>
|
|
46
|
+
| GenericMutationCtx<DataModel>
|
|
47
|
+
| GenericActionCtx<DataModel>
|
|
48
|
+
|
|
49
|
+
export type AuthFunctionsConfig<
|
|
50
|
+
DataModel extends GenericDataModel,
|
|
51
|
+
Role extends string,
|
|
52
|
+
> = {
|
|
53
|
+
/** The consumer's generated function builders (from ./_generated/server). */
|
|
54
|
+
query: QueryBuilder<DataModel, 'public'>
|
|
55
|
+
mutation: MutationBuilder<DataModel, 'public'>
|
|
56
|
+
action: ActionBuilder<DataModel, 'public'>
|
|
57
|
+
internalMutation: MutationBuilder<DataModel, 'internal'>
|
|
58
|
+
internalAction: ActionBuilder<DataModel, 'internal'>
|
|
59
|
+
/** Resolves the synchronized principal, e.g. (ctx) => authKit.getAuthUser(ctx). */
|
|
60
|
+
getAuthUser: (
|
|
61
|
+
ctx: AnyAuthContext<DataModel>,
|
|
62
|
+
) => Promise<{ id: string } | null>
|
|
63
|
+
/**
|
|
64
|
+
* Maps an identity role slug onto the app's role vocabulary. Returning
|
|
65
|
+
* null rejects the caller with FORBIDDEN — unknown roles never pass.
|
|
66
|
+
*/
|
|
67
|
+
mapRole: (roleSlug?: string) => Role | null
|
|
68
|
+
/** Roles allowed through the admin* constructors. */
|
|
69
|
+
adminRoles: readonly NoInfer<Role>[]
|
|
70
|
+
/**
|
|
71
|
+
* Live membership check for actions (queries/mutations trust the JWT).
|
|
72
|
+
* Authentication fails closed with FORBIDDEN when verification errors.
|
|
73
|
+
* Omit to let actions trust the JWT like queries do.
|
|
74
|
+
*/
|
|
75
|
+
verifyMembership?: (input: {
|
|
76
|
+
userId: string
|
|
77
|
+
organizationId: string
|
|
78
|
+
}) => Promise<{ organizationId: string; roleSlug: string } | null>
|
|
79
|
+
/** Host error policy; defaults to throwing KitError. */
|
|
80
|
+
errors?: ErrorFactory
|
|
81
|
+
/**
|
|
82
|
+
* Application trigger registry. When set, every authMutation,
|
|
83
|
+
* roleMutation, and systemMutation write runs through triggers.wrapDB —
|
|
84
|
+
* evaluations registered on tables fire structurally.
|
|
85
|
+
*/
|
|
86
|
+
triggers?: Triggers<DataModel>
|
|
87
|
+
/** Lower-level alternative to `triggers`: a custom ctx wrapper. */
|
|
88
|
+
wrapDB?: (
|
|
89
|
+
ctx: GenericMutationCtx<DataModel>,
|
|
90
|
+
) => GenericMutationCtx<DataModel>
|
|
91
|
+
/** Upper bound for include() reads. */
|
|
92
|
+
maxIncludedQueryRows?: number
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** WorkOS-style default: `member` writes; reader/writer/admin pass through. */
|
|
96
|
+
export function defaultRoleMap(roleSlug?: string): DefaultRole | null {
|
|
97
|
+
const normalized = roleSlug?.trim().toLowerCase()
|
|
98
|
+
if (normalized === 'member') return 'writer'
|
|
99
|
+
return normalized === 'reader' ||
|
|
100
|
+
normalized === 'writer' ||
|
|
101
|
+
normalized === 'admin'
|
|
102
|
+
? normalized
|
|
103
|
+
: null
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export type IncludedQuery<Table extends GenericTableInfo> = {
|
|
107
|
+
when<Value>(
|
|
108
|
+
value: Value | null | undefined,
|
|
109
|
+
select: (query: QueryInitializer<Table>, value: Value) => Query<Table>,
|
|
110
|
+
): IncludedQuery<Table>
|
|
111
|
+
matching<IndexName extends IndexNames<Table>>(
|
|
112
|
+
indexName: IndexName,
|
|
113
|
+
indexRange?: (
|
|
114
|
+
query: IndexRangeBuilder<
|
|
115
|
+
DocumentByInfo<Table>,
|
|
116
|
+
NamedIndex<Table, IndexName>
|
|
117
|
+
>,
|
|
118
|
+
) => IndexRange,
|
|
119
|
+
shouldMatch?: boolean,
|
|
120
|
+
): IncludedQuery<Table>
|
|
121
|
+
otherwise(
|
|
122
|
+
select: (query: QueryInitializer<Table>) => Query<Table>,
|
|
123
|
+
): Query<Table>
|
|
124
|
+
resolve(): Query<Table>
|
|
125
|
+
execute<Result = Awaited<ReturnType<Query<Table>['take']>>>(
|
|
126
|
+
limit: number,
|
|
127
|
+
transform?: (
|
|
128
|
+
data: Awaited<ReturnType<Query<Table>['take']>>,
|
|
129
|
+
) => Result | Promise<Result>,
|
|
130
|
+
): Promise<Result>
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export type Include = <Table extends GenericTableInfo>(
|
|
134
|
+
query: QueryInitializer<Table>,
|
|
135
|
+
) => IncludedQuery<Table>
|
|
136
|
+
|
|
137
|
+
/** Builds an include() that selects the first matching indexed query and bounds the read. */
|
|
138
|
+
export function createInclude(options?: {
|
|
139
|
+
errors?: ErrorFactory
|
|
140
|
+
maxRows?: number
|
|
141
|
+
}): Include {
|
|
142
|
+
const errors = options?.errors ?? defaultErrors
|
|
143
|
+
const maxRows = options?.maxRows ?? 100
|
|
144
|
+
|
|
145
|
+
function attach<Table extends GenericTableInfo>(
|
|
146
|
+
query: QueryInitializer<Table>,
|
|
147
|
+
selected?: Query<Table>,
|
|
148
|
+
): IncludedQuery<Table> {
|
|
149
|
+
return {
|
|
150
|
+
when: (value, select) =>
|
|
151
|
+
selected || value === undefined || value === null
|
|
152
|
+
? attach(query, selected)
|
|
153
|
+
: attach(query, select(query, value)),
|
|
154
|
+
matching: (indexName, indexRange, shouldMatch = true) =>
|
|
155
|
+
selected || !shouldMatch
|
|
156
|
+
? attach(query, selected)
|
|
157
|
+
: attach(query, query.withIndex(indexName, indexRange)),
|
|
158
|
+
otherwise: (select) => selected ?? select(query),
|
|
159
|
+
resolve: () => selected ?? query.fullTableScan(),
|
|
160
|
+
execute: async (limit, transform) => {
|
|
161
|
+
if (!Number.isSafeInteger(limit) || limit < 1 || limit > maxRows) {
|
|
162
|
+
return errors.throw({
|
|
163
|
+
code: 'INVALID_REQUEST_BOUNDARY',
|
|
164
|
+
message: `A bounded query limit must be from 1 to ${maxRows}`,
|
|
165
|
+
})
|
|
166
|
+
}
|
|
167
|
+
const data = await (selected ?? query.fullTableScan()).take(limit)
|
|
168
|
+
return transform ? transform(data) : (data as never)
|
|
169
|
+
},
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return (query) => attach(query)
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Builds the auth-aware function constructors from the host's injected
|
|
178
|
+
* policy: identity resolution, the app's own role vocabulary, live
|
|
179
|
+
* membership verification for actions, trigger wrapping for mutations,
|
|
180
|
+
* and errors. Role-based access is first-class — every constructor family
|
|
181
|
+
* has a role-gated variant (roleQuery/roleMutation/roleAction), and the
|
|
182
|
+
* admin* constructors are just that gate applied to config.adminRoles.
|
|
183
|
+
*/
|
|
184
|
+
export function createAuthFunctions<
|
|
185
|
+
DataModel extends GenericDataModel,
|
|
186
|
+
const Role extends string = DefaultRole,
|
|
187
|
+
>(config: AuthFunctionsConfig<DataModel, Role>) {
|
|
188
|
+
const errors = config.errors ?? defaultErrors
|
|
189
|
+
const wrapDB = config.triggers
|
|
190
|
+
? (ctx: GenericMutationCtx<DataModel>) =>
|
|
191
|
+
(config.triggers as Triggers<DataModel>).wrapDB(ctx)
|
|
192
|
+
: (config.wrapDB ?? ((ctx) => ctx))
|
|
193
|
+
const include = createInclude({
|
|
194
|
+
errors,
|
|
195
|
+
maxRows: config.maxIncludedQueryRows,
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
async function authenticatedUser(
|
|
199
|
+
ctx: AnyAuthContext<DataModel>,
|
|
200
|
+
): Promise<AuthBundle<Role>> {
|
|
201
|
+
const identity = await ctx.auth.getUserIdentity()
|
|
202
|
+
const user = identity ? await config.getAuthUser(ctx) : null
|
|
203
|
+
if (!identity || !user) return errors.throw({ code: 'UNAUTHENTICATED' })
|
|
204
|
+
const organization = identity.organization
|
|
205
|
+
const organizationId =
|
|
206
|
+
organization &&
|
|
207
|
+
typeof organization === 'object' &&
|
|
208
|
+
'organizationId' in organization &&
|
|
209
|
+
typeof organization.organizationId === 'string'
|
|
210
|
+
? organization.organizationId
|
|
211
|
+
: typeof identity.org_id === 'string'
|
|
212
|
+
? identity.org_id
|
|
213
|
+
: null
|
|
214
|
+
if (!organizationId) return errors.throw({ code: 'UNAUTHENTICATED' })
|
|
215
|
+
const organizationRole =
|
|
216
|
+
organization &&
|
|
217
|
+
typeof organization === 'object' &&
|
|
218
|
+
'role' in organization &&
|
|
219
|
+
typeof organization.role === 'string'
|
|
220
|
+
? organization.role
|
|
221
|
+
: typeof identity.role === 'string'
|
|
222
|
+
? identity.role
|
|
223
|
+
: undefined
|
|
224
|
+
const role = config.mapRole(organizationRole)
|
|
225
|
+
if (!role) return errors.throw({ code: 'FORBIDDEN' })
|
|
226
|
+
return Object.freeze({
|
|
227
|
+
identity,
|
|
228
|
+
user: Object.freeze({ id: user.id }),
|
|
229
|
+
org: Object.freeze({ organizationId, role }),
|
|
230
|
+
role,
|
|
231
|
+
actor: Object.freeze({ userId: user.id, organizationId, role }),
|
|
232
|
+
})
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
async function verifiedMembership(userId: string, organizationId: string) {
|
|
236
|
+
if (!config.verifyMembership) return null
|
|
237
|
+
try {
|
|
238
|
+
const membership = await config.verifyMembership({
|
|
239
|
+
userId,
|
|
240
|
+
organizationId,
|
|
241
|
+
})
|
|
242
|
+
if (membership && membership.organizationId === organizationId) {
|
|
243
|
+
return membership
|
|
244
|
+
}
|
|
245
|
+
} catch {
|
|
246
|
+
// Authentication fails closed when membership cannot be verified.
|
|
247
|
+
}
|
|
248
|
+
return errors.throw({ code: 'FORBIDDEN' })
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function requireRole(role: Role, allowed: readonly Role[]) {
|
|
252
|
+
if (!allowed.includes(role)) return errors.throw({ code: 'FORBIDDEN' })
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
async function queryInput(ctx: GenericQueryCtx<DataModel>) {
|
|
256
|
+
return {
|
|
257
|
+
ctx: { ...ctx, include, ...(await authenticatedUser(ctx)) },
|
|
258
|
+
args: {},
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
async function mutationInput(ctx: GenericMutationCtx<DataModel>) {
|
|
263
|
+
return {
|
|
264
|
+
ctx: { ...wrapDB(ctx), include, ...(await authenticatedUser(ctx)) },
|
|
265
|
+
args: {},
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
async function actionInput(ctx: GenericActionCtx<DataModel>) {
|
|
270
|
+
const authenticated = await authenticatedUser(ctx)
|
|
271
|
+
const membership = await verifiedMembership(
|
|
272
|
+
authenticated.user.id,
|
|
273
|
+
authenticated.org.organizationId,
|
|
274
|
+
)
|
|
275
|
+
if (!membership) return { ctx: { ...ctx, ...authenticated }, args: {} }
|
|
276
|
+
const role = config.mapRole(membership.roleSlug)
|
|
277
|
+
if (!role) return errors.throw({ code: 'FORBIDDEN' })
|
|
278
|
+
return {
|
|
279
|
+
ctx: {
|
|
280
|
+
...ctx,
|
|
281
|
+
...authenticated,
|
|
282
|
+
org: Object.freeze({
|
|
283
|
+
organizationId: membership.organizationId,
|
|
284
|
+
role,
|
|
285
|
+
}),
|
|
286
|
+
role,
|
|
287
|
+
actor: Object.freeze({
|
|
288
|
+
userId: authenticated.user.id,
|
|
289
|
+
organizationId: membership.organizationId,
|
|
290
|
+
role,
|
|
291
|
+
}),
|
|
292
|
+
},
|
|
293
|
+
args: {},
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/** Query constructor allowing only the given roles. */
|
|
298
|
+
function roleQuery(...allowed: readonly Role[]) {
|
|
299
|
+
return zCustomQuery(config.query, {
|
|
300
|
+
args: {},
|
|
301
|
+
input: async (ctx) => {
|
|
302
|
+
const authenticated = await queryInput(ctx)
|
|
303
|
+
requireRole(authenticated.ctx.actor.role, allowed)
|
|
304
|
+
return authenticated
|
|
305
|
+
},
|
|
306
|
+
})
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/** Mutation constructor allowing only the given roles. */
|
|
310
|
+
function roleMutation(...allowed: readonly Role[]) {
|
|
311
|
+
return zCustomMutation(config.mutation, {
|
|
312
|
+
args: {},
|
|
313
|
+
input: async (ctx) => {
|
|
314
|
+
const authenticated = await mutationInput(ctx)
|
|
315
|
+
requireRole(authenticated.ctx.actor.role, allowed)
|
|
316
|
+
return authenticated
|
|
317
|
+
},
|
|
318
|
+
})
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/** Action constructor allowing only the given roles (post live verification). */
|
|
322
|
+
function roleAction(...allowed: readonly Role[]) {
|
|
323
|
+
return zCustomAction(config.action, {
|
|
324
|
+
args: {},
|
|
325
|
+
input: async (ctx) => {
|
|
326
|
+
const authenticated = await actionInput(ctx)
|
|
327
|
+
requireRole(authenticated.ctx.actor.role, allowed)
|
|
328
|
+
return authenticated
|
|
329
|
+
},
|
|
330
|
+
})
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
return {
|
|
334
|
+
include,
|
|
335
|
+
authenticatedUser,
|
|
336
|
+
roleQuery,
|
|
337
|
+
roleMutation,
|
|
338
|
+
roleAction,
|
|
339
|
+
authQuery: zCustomQuery(config.query, { args: {}, input: queryInput }),
|
|
340
|
+
authMutation: zCustomMutation(config.mutation, {
|
|
341
|
+
args: {},
|
|
342
|
+
input: mutationInput,
|
|
343
|
+
}),
|
|
344
|
+
authAction: zCustomAction(config.action, {
|
|
345
|
+
args: {},
|
|
346
|
+
input: actionInput,
|
|
347
|
+
}),
|
|
348
|
+
adminQuery: roleQuery(...config.adminRoles),
|
|
349
|
+
adminMutation: roleMutation(...config.adminRoles),
|
|
350
|
+
adminAction: roleAction(...config.adminRoles),
|
|
351
|
+
systemMutation: zCustomMutation(config.internalMutation, {
|
|
352
|
+
args: {},
|
|
353
|
+
input: async (ctx) => ({ ctx: { ...wrapDB(ctx), include }, args: {} }),
|
|
354
|
+
}),
|
|
355
|
+
systemAction: zCustomAction(config.internalAction, {
|
|
356
|
+
args: {},
|
|
357
|
+
input: async (ctx) => ({ ctx, args: {} }),
|
|
358
|
+
}),
|
|
359
|
+
}
|
|
360
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
/**
|
|
3
|
+
* Generated `api` utility.
|
|
4
|
+
*
|
|
5
|
+
* THIS CODE IS AUTOMATICALLY GENERATED.
|
|
6
|
+
*
|
|
7
|
+
* To regenerate, run `npx convex dev`.
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type * as audit from "../audit.js";
|
|
12
|
+
import type * as client from "../client.js";
|
|
13
|
+
import type * as constants from "../constants.js";
|
|
14
|
+
import type * as decisions from "../decisions.js";
|
|
15
|
+
import type * as functions from "../functions.js";
|
|
16
|
+
import type * as requests from "../requests.js";
|
|
17
|
+
import type * as validators from "../validators.js";
|
|
18
|
+
import type * as workflow from "../workflow.js";
|
|
19
|
+
import type * as workflow_steps from "../workflow_steps.js";
|
|
20
|
+
|
|
21
|
+
import type {
|
|
22
|
+
ApiFromModules,
|
|
23
|
+
FilterApi,
|
|
24
|
+
FunctionReference,
|
|
25
|
+
} from "convex/server";
|
|
26
|
+
import { anyApi, componentsGeneric } from "convex/server";
|
|
27
|
+
|
|
28
|
+
const fullApi: ApiFromModules<{
|
|
29
|
+
audit: typeof audit;
|
|
30
|
+
client: typeof client;
|
|
31
|
+
constants: typeof constants;
|
|
32
|
+
decisions: typeof decisions;
|
|
33
|
+
functions: typeof functions;
|
|
34
|
+
requests: typeof requests;
|
|
35
|
+
validators: typeof validators;
|
|
36
|
+
workflow: typeof workflow;
|
|
37
|
+
workflow_steps: typeof workflow_steps;
|
|
38
|
+
}> = anyApi as any;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* A utility for referencing Convex functions in your app's public API.
|
|
42
|
+
*
|
|
43
|
+
* Usage:
|
|
44
|
+
* ```js
|
|
45
|
+
* const myFunctionReference = api.myModule.myFunction;
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export const api: FilterApi<
|
|
49
|
+
typeof fullApi,
|
|
50
|
+
FunctionReference<any, "public">
|
|
51
|
+
> = anyApi as any;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* A utility for referencing Convex functions in your app's internal API.
|
|
55
|
+
*
|
|
56
|
+
* Usage:
|
|
57
|
+
* ```js
|
|
58
|
+
* const myFunctionReference = internal.myModule.myFunction;
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
export const internal: FilterApi<
|
|
62
|
+
typeof fullApi,
|
|
63
|
+
FunctionReference<any, "internal">
|
|
64
|
+
> = anyApi as any;
|
|
65
|
+
|
|
66
|
+
export const components = componentsGeneric() as unknown as {
|
|
67
|
+
workflow: import("@convex-dev/workflow/_generated/component.js").ComponentApi<"workflow">;
|
|
68
|
+
auditLog: import("convex-audit-log/_generated/component.js").ComponentApi<"auditLog">;
|
|
69
|
+
};
|