cvx-kit 0.0.3 → 0.0.5
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/dist/components/foundation/client.d.mts +6 -2
- package/dist/components/foundation/client.mjs +1 -1
- package/dist/components/foundation/client.mjs.map +1 -1
- package/docs/commands.md +2 -1
- package/docs/llms.md +1 -1
- package/package.json +2 -8
- package/src/test.ts +7 -5
- package/src/auth.ts +0 -473
- package/src/components/approvals/_generated/api.ts +0 -69
- package/src/components/approvals/_generated/component.ts +0 -335
- package/src/components/approvals/_generated/dataModel.ts +0 -60
- package/src/components/approvals/_generated/server.ts +0 -156
- package/src/components/approvals/audit.ts +0 -148
- package/src/components/approvals/client.ts +0 -321
- package/src/components/approvals/constants.ts +0 -51
- package/src/components/approvals/convex.config.ts +0 -10
- package/src/components/approvals/decisions.ts +0 -376
- package/src/components/approvals/functions.ts +0 -27
- package/src/components/approvals/requests.ts +0 -240
- package/src/components/approvals/schema.ts +0 -23
- package/src/components/approvals/validators.ts +0 -272
- package/src/components/approvals/workflow.ts +0 -204
- package/src/components/approvals/workflow_steps.ts +0 -189
- package/src/components/foundation/_generated/api.ts +0 -62
- package/src/components/foundation/_generated/component.ts +0 -29
- package/src/components/foundation/_generated/dataModel.ts +0 -60
- package/src/components/foundation/_generated/server.ts +0 -156
- package/src/components/foundation/client.ts +0 -301
- package/src/components/foundation/convex.config.ts +0 -3
- package/src/components/foundation/functions.ts +0 -10
- package/src/components/foundation/modules/command/__tests__/command.test.ts +0 -106
- package/src/components/foundation/modules/command/command.ts +0 -154
- package/src/components/foundation/modules/observability/__tests__/observability.test.ts +0 -79
- package/src/components/foundation/modules/observability/observability.ts +0 -95
- package/src/components/foundation/modules/query/__tests__/query.test.ts +0 -25
- package/src/components/foundation/modules/query/query.ts +0 -47
- package/src/components/foundation/result.ts +0 -77
- package/src/components/foundation/schema.ts +0 -4
- package/src/components/foundation/telemetry.ts +0 -23
- package/src/errors.ts +0 -25
- package/src/index.ts +0 -56
- package/src/tenancy.ts +0 -110
- package/src/triggers.ts +0 -124
- package/src/zod-table.ts +0 -283
|
@@ -1,62 +0,0 @@
|
|
|
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 client from "../client.js";
|
|
12
|
-
import type * as functions from "../functions.js";
|
|
13
|
-
import type * as modules_command_command from "../modules/command/command.js";
|
|
14
|
-
import type * as modules_observability_observability from "../modules/observability/observability.js";
|
|
15
|
-
import type * as modules_query_query from "../modules/query/query.js";
|
|
16
|
-
import type * as result from "../result.js";
|
|
17
|
-
import type * as telemetry from "../telemetry.js";
|
|
18
|
-
|
|
19
|
-
import type {
|
|
20
|
-
ApiFromModules,
|
|
21
|
-
FilterApi,
|
|
22
|
-
FunctionReference,
|
|
23
|
-
} from "convex/server";
|
|
24
|
-
import { anyApi, componentsGeneric } from "convex/server";
|
|
25
|
-
|
|
26
|
-
const fullApi: ApiFromModules<{
|
|
27
|
-
client: typeof client;
|
|
28
|
-
functions: typeof functions;
|
|
29
|
-
"modules/command/command": typeof modules_command_command;
|
|
30
|
-
"modules/observability/observability": typeof modules_observability_observability;
|
|
31
|
-
"modules/query/query": typeof modules_query_query;
|
|
32
|
-
result: typeof result;
|
|
33
|
-
telemetry: typeof telemetry;
|
|
34
|
-
}> = anyApi as any;
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* A utility for referencing Convex functions in your app's public API.
|
|
38
|
-
*
|
|
39
|
-
* Usage:
|
|
40
|
-
* ```js
|
|
41
|
-
* const myFunctionReference = api.myModule.myFunction;
|
|
42
|
-
* ```
|
|
43
|
-
*/
|
|
44
|
-
export const api: FilterApi<
|
|
45
|
-
typeof fullApi,
|
|
46
|
-
FunctionReference<any, "public">
|
|
47
|
-
> = anyApi as any;
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* A utility for referencing Convex functions in your app's internal API.
|
|
51
|
-
*
|
|
52
|
-
* Usage:
|
|
53
|
-
* ```js
|
|
54
|
-
* const myFunctionReference = internal.myModule.myFunction;
|
|
55
|
-
* ```
|
|
56
|
-
*/
|
|
57
|
-
export const internal: FilterApi<
|
|
58
|
-
typeof fullApi,
|
|
59
|
-
FunctionReference<any, "internal">
|
|
60
|
-
> = anyApi as any;
|
|
61
|
-
|
|
62
|
-
export const components = componentsGeneric() as unknown as {};
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
/* eslint-disable */
|
|
2
|
-
/**
|
|
3
|
-
* Generated `ComponentApi` utility.
|
|
4
|
-
*
|
|
5
|
-
* THIS CODE IS AUTOMATICALLY GENERATED.
|
|
6
|
-
*
|
|
7
|
-
* To regenerate, run `npx convex dev`.
|
|
8
|
-
* @module
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import type { FunctionReference } from "convex/server";
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* A utility for referencing a Convex component's exposed API.
|
|
15
|
-
*
|
|
16
|
-
* Useful when expecting a parameter like `components.myComponent`.
|
|
17
|
-
* Usage:
|
|
18
|
-
* ```ts
|
|
19
|
-
* async function myFunction(ctx: QueryCtx, component: ComponentApi) {
|
|
20
|
-
* return ctx.runQuery(component.someFile.someQuery, { ...args });
|
|
21
|
-
* }
|
|
22
|
-
* ```
|
|
23
|
-
*/
|
|
24
|
-
export type ComponentApi<Name extends string | undefined = string | undefined> =
|
|
25
|
-
{
|
|
26
|
-
functions: {
|
|
27
|
-
status: FunctionReference<"query", "internal", {}, "ready", Name>;
|
|
28
|
-
};
|
|
29
|
-
};
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
/* eslint-disable */
|
|
2
|
-
/**
|
|
3
|
-
* Generated data model types.
|
|
4
|
-
*
|
|
5
|
-
* THIS CODE IS AUTOMATICALLY GENERATED.
|
|
6
|
-
*
|
|
7
|
-
* To regenerate, run `npx convex dev`.
|
|
8
|
-
* @module
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import type {
|
|
12
|
-
DataModelFromSchemaDefinition,
|
|
13
|
-
DocumentByName,
|
|
14
|
-
TableNamesInDataModel,
|
|
15
|
-
SystemTableNames,
|
|
16
|
-
} from "convex/server";
|
|
17
|
-
import type { GenericId } from "convex/values";
|
|
18
|
-
import schema from "../schema.js";
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* The names of all of your Convex tables.
|
|
22
|
-
*/
|
|
23
|
-
export type TableNames = TableNamesInDataModel<DataModel>;
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* The type of a document stored in Convex.
|
|
27
|
-
*
|
|
28
|
-
* @typeParam TableName - A string literal type of the table name (like "users").
|
|
29
|
-
*/
|
|
30
|
-
export type Doc<TableName extends TableNames> = DocumentByName<
|
|
31
|
-
DataModel,
|
|
32
|
-
TableName
|
|
33
|
-
>;
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* An identifier for a document in Convex.
|
|
37
|
-
*
|
|
38
|
-
* Convex documents are uniquely identified by their `Id`, which is accessible
|
|
39
|
-
* on the `_id` field. To learn more, see [Document IDs](https://docs.convex.dev/using/document-ids).
|
|
40
|
-
*
|
|
41
|
-
* Documents can be loaded using `db.get(tableName, id)` in query and mutation functions.
|
|
42
|
-
*
|
|
43
|
-
* IDs are just strings at runtime, but this type can be used to distinguish them from other
|
|
44
|
-
* strings when type checking.
|
|
45
|
-
*
|
|
46
|
-
* @typeParam TableName - A string literal type of the table name (like "users").
|
|
47
|
-
*/
|
|
48
|
-
export type Id<TableName extends TableNames | SystemTableNames> =
|
|
49
|
-
GenericId<TableName>;
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* A type describing your Convex data model.
|
|
53
|
-
*
|
|
54
|
-
* This type includes information about what tables you have, the type of
|
|
55
|
-
* documents stored in those tables, and the indexes defined on them.
|
|
56
|
-
*
|
|
57
|
-
* This type is used to parameterize methods like `queryGeneric` and
|
|
58
|
-
* `mutationGeneric` to make them type-safe.
|
|
59
|
-
*/
|
|
60
|
-
export type DataModel = DataModelFromSchemaDefinition<typeof schema>;
|
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
/* eslint-disable */
|
|
2
|
-
/**
|
|
3
|
-
* Generated utilities for implementing server-side Convex query and mutation functions.
|
|
4
|
-
*
|
|
5
|
-
* THIS CODE IS AUTOMATICALLY GENERATED.
|
|
6
|
-
*
|
|
7
|
-
* To regenerate, run `npx convex dev`.
|
|
8
|
-
* @module
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import type {
|
|
12
|
-
ActionBuilder,
|
|
13
|
-
HttpActionBuilder,
|
|
14
|
-
MutationBuilder,
|
|
15
|
-
QueryBuilder,
|
|
16
|
-
GenericActionCtx,
|
|
17
|
-
GenericMutationCtx,
|
|
18
|
-
GenericQueryCtx,
|
|
19
|
-
GenericDatabaseReader,
|
|
20
|
-
GenericDatabaseWriter,
|
|
21
|
-
} from "convex/server";
|
|
22
|
-
import {
|
|
23
|
-
actionGeneric,
|
|
24
|
-
httpActionGeneric,
|
|
25
|
-
queryGeneric,
|
|
26
|
-
mutationGeneric,
|
|
27
|
-
internalActionGeneric,
|
|
28
|
-
internalMutationGeneric,
|
|
29
|
-
internalQueryGeneric,
|
|
30
|
-
} from "convex/server";
|
|
31
|
-
import type { DataModel } from "./dataModel.js";
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Define a query in this Convex app's public API.
|
|
35
|
-
*
|
|
36
|
-
* This function will be allowed to read your Convex database and will be accessible from the client.
|
|
37
|
-
*
|
|
38
|
-
* @param func - The query function. It receives a {@link QueryCtx} as its first argument.
|
|
39
|
-
* @returns The wrapped query. Include this as an `export` to name it and make it accessible.
|
|
40
|
-
*/
|
|
41
|
-
export const query: QueryBuilder<DataModel, "public"> = queryGeneric;
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Define a query that is only accessible from other Convex functions (but not from the client).
|
|
45
|
-
*
|
|
46
|
-
* This function will be allowed to read from your Convex database. It will not be accessible from the client.
|
|
47
|
-
*
|
|
48
|
-
* @param func - The query function. It receives a {@link QueryCtx} as its first argument.
|
|
49
|
-
* @returns The wrapped query. Include this as an `export` to name it and make it accessible.
|
|
50
|
-
*/
|
|
51
|
-
export const internalQuery: QueryBuilder<DataModel, "internal"> =
|
|
52
|
-
internalQueryGeneric;
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Define a mutation in this Convex app's public API.
|
|
56
|
-
*
|
|
57
|
-
* This function will be allowed to modify your Convex database and will be accessible from the client.
|
|
58
|
-
*
|
|
59
|
-
* @param func - The mutation function. It receives a {@link MutationCtx} as its first argument.
|
|
60
|
-
* @returns The wrapped mutation. Include this as an `export` to name it and make it accessible.
|
|
61
|
-
*/
|
|
62
|
-
export const mutation: MutationBuilder<DataModel, "public"> = mutationGeneric;
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Define a mutation that is only accessible from other Convex functions (but not from the client).
|
|
66
|
-
*
|
|
67
|
-
* This function will be allowed to modify your Convex database. It will not be accessible from the client.
|
|
68
|
-
*
|
|
69
|
-
* @param func - The mutation function. It receives a {@link MutationCtx} as its first argument.
|
|
70
|
-
* @returns The wrapped mutation. Include this as an `export` to name it and make it accessible.
|
|
71
|
-
*/
|
|
72
|
-
export const internalMutation: MutationBuilder<DataModel, "internal"> =
|
|
73
|
-
internalMutationGeneric;
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Define an action in this Convex app's public API.
|
|
77
|
-
*
|
|
78
|
-
* An action is a function which can execute any JavaScript code, including non-deterministic
|
|
79
|
-
* code and code with side-effects, like calling third-party services.
|
|
80
|
-
* They can be run in Convex's JavaScript environment or in Node.js using the "use node" directive.
|
|
81
|
-
* They can interact with the database indirectly by calling queries and mutations using the {@link ActionCtx}.
|
|
82
|
-
*
|
|
83
|
-
* @param func - The action. It receives an {@link ActionCtx} as its first argument.
|
|
84
|
-
* @returns The wrapped action. Include this as an `export` to name it and make it accessible.
|
|
85
|
-
*/
|
|
86
|
-
export const action: ActionBuilder<DataModel, "public"> = actionGeneric;
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Define an action that is only accessible from other Convex functions (but not from the client).
|
|
90
|
-
*
|
|
91
|
-
* @param func - The function. It receives an {@link ActionCtx} as its first argument.
|
|
92
|
-
* @returns The wrapped function. Include this as an `export` to name it and make it accessible.
|
|
93
|
-
*/
|
|
94
|
-
export const internalAction: ActionBuilder<DataModel, "internal"> =
|
|
95
|
-
internalActionGeneric;
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Define an HTTP action.
|
|
99
|
-
*
|
|
100
|
-
* The wrapped function will be used to respond to HTTP requests received
|
|
101
|
-
* by a Convex deployment if the requests matches the path and method where
|
|
102
|
-
* this action is routed. Be sure to route your httpAction in `convex/http.js`.
|
|
103
|
-
*
|
|
104
|
-
* @param func - The function. It receives an {@link ActionCtx} as its first argument
|
|
105
|
-
* and a Fetch API `Request` object as its second.
|
|
106
|
-
* @returns The wrapped function. Import this function from `convex/http.js` and route it to hook it up.
|
|
107
|
-
*/
|
|
108
|
-
export const httpAction: HttpActionBuilder = httpActionGeneric;
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* A set of services for use within Convex query functions.
|
|
112
|
-
*
|
|
113
|
-
* The query context is passed as the first argument to any Convex query
|
|
114
|
-
* function run on the server.
|
|
115
|
-
*
|
|
116
|
-
* If you're using code generation, use the `QueryCtx` type in `convex/_generated/server.d.ts` instead.
|
|
117
|
-
*/
|
|
118
|
-
export type QueryCtx = GenericQueryCtx<DataModel>;
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* A set of services for use within Convex mutation functions.
|
|
122
|
-
*
|
|
123
|
-
* The mutation context is passed as the first argument to any Convex mutation
|
|
124
|
-
* function run on the server.
|
|
125
|
-
*
|
|
126
|
-
* If you're using code generation, use the `MutationCtx` type in `convex/_generated/server.d.ts` instead.
|
|
127
|
-
*/
|
|
128
|
-
export type MutationCtx = GenericMutationCtx<DataModel>;
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* A set of services for use within Convex action functions.
|
|
132
|
-
*
|
|
133
|
-
* The action context is passed as the first argument to any Convex action
|
|
134
|
-
* function run on the server.
|
|
135
|
-
*/
|
|
136
|
-
export type ActionCtx = GenericActionCtx<DataModel>;
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* An interface to read from the database within Convex query functions.
|
|
140
|
-
*
|
|
141
|
-
* The two entry points are {@link DatabaseReader.get}, which fetches a single
|
|
142
|
-
* document by its {@link Id}, or {@link DatabaseReader.query}, which starts
|
|
143
|
-
* building a query.
|
|
144
|
-
*/
|
|
145
|
-
export type DatabaseReader = GenericDatabaseReader<DataModel>;
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* An interface to read from and write to the database within Convex mutation
|
|
149
|
-
* functions.
|
|
150
|
-
*
|
|
151
|
-
* Convex guarantees that all writes within a single mutation are
|
|
152
|
-
* executed atomically, so you never have to worry about partial writes leaving
|
|
153
|
-
* your data in an inconsistent state. See [the Convex Guide](https://docs.convex.dev/understanding/convex-fundamentals/functions#atomicity-and-optimistic-concurrency-control)
|
|
154
|
-
* for the guarantees Convex provides your functions.
|
|
155
|
-
*/
|
|
156
|
-
export type DatabaseWriter = GenericDatabaseWriter<DataModel>;
|
|
@@ -1,301 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Command as CommandKernelClass,
|
|
3
|
-
type CommandExecution,
|
|
4
|
-
type CommandInput,
|
|
5
|
-
type CommandRegistry,
|
|
6
|
-
type CommandResult,
|
|
7
|
-
} from './modules/command/command'
|
|
8
|
-
import {
|
|
9
|
-
Observability,
|
|
10
|
-
type ObservabilityOptions,
|
|
11
|
-
} from './modules/observability/observability'
|
|
12
|
-
import { Query } from './modules/query/query'
|
|
13
|
-
|
|
14
|
-
/** Shape handed to the injected audit writer; classification comes from the operation. */
|
|
15
|
-
export type AuditEntryInput = {
|
|
16
|
-
operation: string
|
|
17
|
-
actorId: string
|
|
18
|
-
aggregate: { type: string; id: string }
|
|
19
|
-
metadata?: Record<string, unknown>
|
|
20
|
-
classification: string
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export type AuditWriter = (
|
|
24
|
-
context: never,
|
|
25
|
-
entry: AuditEntryInput,
|
|
26
|
-
) => Promise<unknown> | unknown
|
|
27
|
-
|
|
28
|
-
type MaybePromise<Value> = Value | Promise<Value>
|
|
29
|
-
type OperationKey<Operations extends CommandRegistry> = Extract<
|
|
30
|
-
keyof Operations,
|
|
31
|
-
string
|
|
32
|
-
>
|
|
33
|
-
|
|
34
|
-
export type AuditedOperation = Readonly<{
|
|
35
|
-
command: Readonly<{ parse: (value: unknown) => unknown }>
|
|
36
|
-
result: Readonly<{ parse: (value: unknown) => unknown }>
|
|
37
|
-
classification: string
|
|
38
|
-
/**
|
|
39
|
-
* Permission slug required to execute this operation. Checked through the
|
|
40
|
-
* Foundation's injected checkPermission BEFORE guards and the handler;
|
|
41
|
-
* declaring a permission without injecting a checker fails closed.
|
|
42
|
-
*/
|
|
43
|
-
permission?: string
|
|
44
|
-
/**
|
|
45
|
-
* Per-operation precondition, after the permission check and the registry
|
|
46
|
-
* default guard, before the handler. Throw to deny — nothing has run yet.
|
|
47
|
-
*/
|
|
48
|
-
guard?: (context: never, command: never) => MaybePromise<void>
|
|
49
|
-
/**
|
|
50
|
-
* Allowlist of aggregate types this operation's audit may reference. When
|
|
51
|
-
* declared, an audit entry whose aggregate.type is not listed throws —
|
|
52
|
-
* the audit vocabulary is enforced, not advisory.
|
|
53
|
-
*/
|
|
54
|
-
aggregates?: readonly string[]
|
|
55
|
-
audit: (
|
|
56
|
-
resolution: Readonly<{ command: never; result: never }>,
|
|
57
|
-
context: never,
|
|
58
|
-
) => MaybePromise<Omit<AuditEntryInput, 'classification'> | null>
|
|
59
|
-
}>
|
|
60
|
-
|
|
61
|
-
/** Registry-wide defaults applied to every operation of one Command. */
|
|
62
|
-
export type CommandDefaults = Readonly<{
|
|
63
|
-
/** Runs before every operation's own guard. Throw to deny. */
|
|
64
|
-
guard?: (context: never) => MaybePromise<void>
|
|
65
|
-
}>
|
|
66
|
-
|
|
67
|
-
/** Injected permission policy: throw to deny. Host semantics, kit ordering. */
|
|
68
|
-
export type PermissionChecker = (
|
|
69
|
-
context: never,
|
|
70
|
-
input: Readonly<{ permission: string; operation: string }>,
|
|
71
|
-
) => MaybePromise<void>
|
|
72
|
-
|
|
73
|
-
export type AuditedRegistry = Readonly<Record<string, AuditedOperation>>
|
|
74
|
-
|
|
75
|
-
export type FoundationOptions = Readonly<{
|
|
76
|
-
observability: ObservabilityOptions &
|
|
77
|
-
Readonly<{
|
|
78
|
-
/**
|
|
79
|
-
* In-transaction audit writer used by Command. Operations that
|
|
80
|
-
* return an audit record get it written here automatically.
|
|
81
|
-
*/
|
|
82
|
-
writeAudit?: AuditWriter
|
|
83
|
-
}>
|
|
84
|
-
/**
|
|
85
|
-
* Permission policy for operations that declare a `permission`. Runs
|
|
86
|
-
* before guards and the handler; throw to deny. An operation with a
|
|
87
|
-
* permission but no injected checker fails closed.
|
|
88
|
-
*/
|
|
89
|
-
checkPermission?: PermissionChecker
|
|
90
|
-
}>
|
|
91
|
-
|
|
92
|
-
type FoundationComponentApi = Readonly<{
|
|
93
|
-
functions: Readonly<{ status: unknown }>
|
|
94
|
-
}>
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Application command protocol bound to a Foundation instance:
|
|
98
|
-
* validate, observe, execute, and audit. `classification` and `audit()`
|
|
99
|
-
* are mandatory per operation — auditing is type-enforced, not opt-in.
|
|
100
|
-
* Obtain it by destructuring the Foundation: `const { Command } = new Foundation(...)`.
|
|
101
|
-
*/
|
|
102
|
-
class BoundCommand<Context, const Operations extends AuditedRegistry> {
|
|
103
|
-
readonly #kernel: CommandKernelClass<Context, Operations>
|
|
104
|
-
readonly #observability: Observability
|
|
105
|
-
readonly #writeAudit: AuditWriter
|
|
106
|
-
readonly #checkPermission: PermissionChecker | undefined
|
|
107
|
-
readonly #defaults: CommandDefaults
|
|
108
|
-
|
|
109
|
-
/** Declared aggregate types per operation — introspectable for tests. */
|
|
110
|
-
readonly aggregates: Readonly<{
|
|
111
|
-
[Key in OperationKey<Operations>]: Operations[Key]['aggregates']
|
|
112
|
-
}>
|
|
113
|
-
|
|
114
|
-
static operation<const Definition extends AuditedOperation>(
|
|
115
|
-
definition: Definition,
|
|
116
|
-
): Definition {
|
|
117
|
-
return definition
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
constructor(
|
|
121
|
-
operations: Operations,
|
|
122
|
-
deps: {
|
|
123
|
-
observability: Observability
|
|
124
|
-
writeAudit: AuditWriter
|
|
125
|
-
checkPermission?: PermissionChecker
|
|
126
|
-
defaults?: CommandDefaults
|
|
127
|
-
},
|
|
128
|
-
) {
|
|
129
|
-
this.#observability = deps.observability
|
|
130
|
-
this.#writeAudit = deps.writeAudit
|
|
131
|
-
this.#checkPermission = deps.checkPermission
|
|
132
|
-
this.#defaults = deps.defaults ?? {}
|
|
133
|
-
this.aggregates = Object.freeze(
|
|
134
|
-
Object.fromEntries(
|
|
135
|
-
Object.entries(operations).map(([operation, definition]) => [
|
|
136
|
-
operation,
|
|
137
|
-
definition.aggregates,
|
|
138
|
-
]),
|
|
139
|
-
),
|
|
140
|
-
) as this['aggregates']
|
|
141
|
-
this.#kernel = new CommandKernelClass<Context, Operations>({
|
|
142
|
-
operations,
|
|
143
|
-
execute: (execution) => this.#execute(execution),
|
|
144
|
-
})
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
exec<const Key extends OperationKey<Operations>>(executor: {
|
|
148
|
-
operation: Key
|
|
149
|
-
handler: (
|
|
150
|
-
context: Context,
|
|
151
|
-
command: CommandInput<Operations, Key>,
|
|
152
|
-
) => MaybePromise<CommandResult<Operations, Key>>
|
|
153
|
-
}) {
|
|
154
|
-
return this.#kernel.exec(executor)
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
async #execute<Key extends OperationKey<Operations>>(
|
|
158
|
-
execution: CommandExecution<Context, Operations, Key>,
|
|
159
|
-
): Promise<CommandResult<Operations, Key>> {
|
|
160
|
-
return this.#observability.observe(
|
|
161
|
-
{
|
|
162
|
-
operation: execution.operation,
|
|
163
|
-
classification: execution.definition.classification,
|
|
164
|
-
},
|
|
165
|
-
async () => {
|
|
166
|
-
// Guards run before the handler: permission → default → operation.
|
|
167
|
-
const permission = execution.definition.permission
|
|
168
|
-
if (permission !== undefined) {
|
|
169
|
-
if (!this.#checkPermission) {
|
|
170
|
-
throw new CommandPermissionError(execution.operation)
|
|
171
|
-
}
|
|
172
|
-
await this.#checkPermission(execution.context as never, {
|
|
173
|
-
permission,
|
|
174
|
-
operation: execution.operation,
|
|
175
|
-
})
|
|
176
|
-
}
|
|
177
|
-
await this.#defaults.guard?.(execution.context as never)
|
|
178
|
-
await execution.definition.guard?.(
|
|
179
|
-
execution.context as never,
|
|
180
|
-
execution.command as never,
|
|
181
|
-
)
|
|
182
|
-
const result = await execution.run()
|
|
183
|
-
const audit = await execution.definition.audit(
|
|
184
|
-
{ command: execution.command, result } as never,
|
|
185
|
-
execution.context as never,
|
|
186
|
-
)
|
|
187
|
-
if (audit) {
|
|
188
|
-
const allowed = execution.definition.aggregates
|
|
189
|
-
if (allowed && !allowed.includes(audit.aggregate.type)) {
|
|
190
|
-
throw new CommandAggregateError(
|
|
191
|
-
execution.operation,
|
|
192
|
-
audit.aggregate.type,
|
|
193
|
-
)
|
|
194
|
-
}
|
|
195
|
-
await this.#writeAudit(execution.context as never, {
|
|
196
|
-
...audit,
|
|
197
|
-
classification: execution.definition.classification,
|
|
198
|
-
})
|
|
199
|
-
}
|
|
200
|
-
return result
|
|
201
|
-
},
|
|
202
|
-
)
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
/** The audit referenced an aggregate type outside the operation's allowlist. */
|
|
207
|
-
class CommandAggregateError extends Error {
|
|
208
|
-
readonly code = 'COMMAND_AGGREGATE_NOT_DECLARED'
|
|
209
|
-
readonly name = 'CommandAggregateError'
|
|
210
|
-
|
|
211
|
-
constructor(operation: string, aggregateType: string) {
|
|
212
|
-
super(
|
|
213
|
-
`Operation "${operation}" audited aggregate type "${aggregateType}" outside its declared aggregates`,
|
|
214
|
-
)
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
/** Fails closed: an operation declared a permission but no checker exists. */
|
|
219
|
-
class CommandPermissionError extends Error {
|
|
220
|
-
readonly code = 'COMMAND_PERMISSION_NOT_CONFIGURED'
|
|
221
|
-
readonly name = 'CommandPermissionError'
|
|
222
|
-
|
|
223
|
-
constructor(operation: string) {
|
|
224
|
-
super(
|
|
225
|
-
`Operation "${operation}" declares a permission but the Foundation has no checkPermission`,
|
|
226
|
-
)
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
export type ApplicationCommand<
|
|
231
|
-
Context,
|
|
232
|
-
Operations extends AuditedRegistry,
|
|
233
|
-
> = BoundCommand<Context, Operations>
|
|
234
|
-
|
|
235
|
-
type CommandConstructor = {
|
|
236
|
-
new <Context, const Operations extends AuditedRegistry>(
|
|
237
|
-
operations: Operations,
|
|
238
|
-
defaults?: CommandDefaults,
|
|
239
|
-
): BoundCommand<Context, Operations>
|
|
240
|
-
operation: (typeof BoundCommand)['operation']
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
/**
|
|
244
|
-
* Host facade for a component that owns no application execution capability.
|
|
245
|
-
* Declared once; the sole source of the command protocol, query kernel, and
|
|
246
|
-
* observability: `const { Command, Query, observability } = new Foundation(...)`.
|
|
247
|
-
*/
|
|
248
|
-
export class Foundation<
|
|
249
|
-
Component extends FoundationComponentApi = FoundationComponentApi,
|
|
250
|
-
> {
|
|
251
|
-
readonly status: Component['functions']['status']
|
|
252
|
-
readonly Command: CommandConstructor
|
|
253
|
-
readonly Query = Query
|
|
254
|
-
readonly observability: Observability
|
|
255
|
-
|
|
256
|
-
constructor(component: Component, options: FoundationOptions) {
|
|
257
|
-
this.status = component.functions.status
|
|
258
|
-
const observability = new Observability(options.observability)
|
|
259
|
-
this.observability = observability
|
|
260
|
-
const writeAudit = options.observability.writeAudit ?? (() => undefined)
|
|
261
|
-
const checkPermission = options.checkPermission
|
|
262
|
-
this.Command = class <
|
|
263
|
-
Context,
|
|
264
|
-
const Operations extends AuditedRegistry,
|
|
265
|
-
> extends BoundCommand<Context, Operations> {
|
|
266
|
-
constructor(operations: Operations, defaults?: CommandDefaults) {
|
|
267
|
-
super(operations, {
|
|
268
|
-
observability,
|
|
269
|
-
writeAudit,
|
|
270
|
-
checkPermission,
|
|
271
|
-
defaults,
|
|
272
|
-
})
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
export type {
|
|
279
|
-
CommandExecution,
|
|
280
|
-
CommandInput,
|
|
281
|
-
CommandRegistry,
|
|
282
|
-
CommandResult,
|
|
283
|
-
Parseable,
|
|
284
|
-
} from './modules/command/command'
|
|
285
|
-
export { Observability } from './modules/observability/observability'
|
|
286
|
-
export type {
|
|
287
|
-
CommandObservation,
|
|
288
|
-
ObservabilityOptions,
|
|
289
|
-
} from './modules/observability/observability'
|
|
290
|
-
export { Query } from './modules/query/query'
|
|
291
|
-
export type { QueryExecution } from './modules/query/query'
|
|
292
|
-
export { executeResultBoundary, projectResult } from './result'
|
|
293
|
-
export type {
|
|
294
|
-
Result,
|
|
295
|
-
ResultBoundary,
|
|
296
|
-
TransactionMetricsContext,
|
|
297
|
-
} from './result'
|
|
298
|
-
export { emitSemanticEvent } from './telemetry'
|
|
299
|
-
|
|
300
|
-
// Component definition as default export: `import foundation from "cvx-kit/components/foundation"` → app.use(foundation)
|
|
301
|
-
export { default } from "./convex.config"
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { v } from 'convex/values'
|
|
2
|
-
|
|
3
|
-
import { query } from './_generated/server'
|
|
4
|
-
|
|
5
|
-
/** Minimal health signal exposed only through the configured host facade. */
|
|
6
|
-
export const status = query({
|
|
7
|
-
args: {},
|
|
8
|
-
returns: v.literal('ready'),
|
|
9
|
-
handler: async () => 'ready' as const,
|
|
10
|
-
})
|