appos 0.2.1 → 0.2.2-0
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/bin/auth-schema-7KeUwlcd.mjs +2 -0
- package/dist/bin/concurrently.mjs +2 -0
- package/dist/bin/event-v2sCJkNd.mjs +2 -0
- package/dist/bin/extract-blob-metadata-TqNd9w-6.mjs +2 -0
- package/dist/bin/generate-image-variant-D8H9FxgD.mjs +2 -0
- package/dist/bin/generate-preview-5jLZLX6I.mjs +2 -0
- package/dist/bin/main.mjs +362 -0
- package/dist/bin/purge-attachment-CMlJMNOk.mjs +2 -0
- package/dist/bin/purge-audit-logs-hd6q6vnR.mjs +2 -0
- package/dist/bin/purge-unattached-blobs-BYv5b9R9.mjs +2 -0
- package/dist/bin/track-db-changes-q0Vl7Htm.mjs +2 -0
- package/dist/bin/vite.mjs +2 -0
- package/dist/bin/vitest.mjs +2 -0
- package/dist/bin/workflow-BagSlsMp.mjs +2 -0
- package/dist/bin/youch-handler-Jj6i1XIT.mjs +2 -0
- package/dist/exports/api/_virtual/rolldown_runtime.mjs +1 -0
- package/dist/exports/api/app-context.d.mts +115 -0
- package/dist/exports/api/app-context.mjs +1 -0
- package/dist/exports/api/auth-schema.d.mts +4248 -0
- package/dist/exports/api/auth-schema.mjs +1 -0
- package/dist/exports/api/auth.d.mts +398 -0
- package/dist/exports/api/auth.mjs +1 -0
- package/dist/exports/api/cache.d.mts +44 -0
- package/dist/exports/api/cache.mjs +1 -0
- package/dist/exports/api/config.d.mts +28 -0
- package/dist/exports/api/config.mjs +1 -0
- package/dist/exports/api/container.d.mts +210 -0
- package/dist/exports/api/container.mjs +1 -0
- package/dist/exports/api/database.d.mts +99 -0
- package/dist/exports/api/database.mjs +1 -0
- package/dist/exports/api/event.d.mts +235 -0
- package/dist/exports/api/event.mjs +1 -0
- package/dist/exports/api/i18n.d.mts +34 -0
- package/dist/exports/api/i18n.mjs +1 -0
- package/dist/exports/api/index.d.mts +21 -0
- package/dist/exports/api/index.mjs +1 -0
- package/dist/exports/api/logger.d.mts +21 -0
- package/dist/exports/api/logger.mjs +1 -0
- package/dist/exports/api/mailer.d.mts +70 -0
- package/dist/exports/api/mailer.mjs +1 -0
- package/dist/exports/api/middleware/request-logger.d.mts +24 -0
- package/dist/exports/api/middleware.d.mts +39 -0
- package/dist/exports/api/middleware.mjs +1 -0
- package/dist/exports/api/node_modules/.bun/change-case@5.4.4/node_modules/change-case/dist/index.mjs +1 -0
- package/dist/exports/api/openapi.d.mts +271 -0
- package/dist/exports/api/openapi.mjs +1 -0
- package/dist/exports/api/orm.d.mts +13 -0
- package/dist/exports/api/orm.mjs +1 -0
- package/dist/exports/api/otel.d.mts +40 -0
- package/dist/exports/api/otel.mjs +1 -0
- package/dist/exports/api/packages/appos/src/constants.mjs +1 -0
- package/dist/exports/api/packages/appos/src/instrumentation.d.mts +7 -0
- package/dist/exports/api/packages/appos/src/instrumentation.mjs +1 -0
- package/dist/exports/api/packages/appos/src/web/auth.mjs +1 -0
- package/dist/exports/api/redis.d.mts +34 -0
- package/dist/exports/api/redis.mjs +1 -0
- package/dist/exports/api/storage-schema.d.mts +707 -0
- package/dist/exports/api/storage-schema.mjs +1 -0
- package/dist/exports/api/storage.d.mts +506 -0
- package/dist/exports/api/storage.mjs +1 -0
- package/dist/exports/api/workflow.d.mts +250 -0
- package/dist/exports/api/workflow.mjs +1 -0
- package/dist/exports/api/workflows/_virtual/rolldown_runtime.mjs +1 -0
- package/dist/exports/api/workflows/auth-schema.mjs +1 -0
- package/dist/exports/api/workflows/auth.d.mts +375 -0
- package/dist/exports/api/workflows/cache.d.mts +44 -0
- package/dist/exports/api/workflows/config.d.mts +18 -0
- package/dist/exports/api/workflows/container.d.mts +167 -0
- package/dist/exports/api/workflows/database.d.mts +46 -0
- package/dist/exports/api/workflows/event.d.mts +68 -0
- package/dist/exports/api/workflows/event.mjs +1 -0
- package/dist/exports/api/workflows/extract-blob-metadata.mjs +1 -0
- package/dist/exports/api/workflows/generate-image-variant.d.mts +99 -0
- package/dist/exports/api/workflows/generate-image-variant.mjs +1 -0
- package/dist/exports/api/workflows/generate-preview.mjs +1 -0
- package/dist/exports/api/workflows/index.d.mts +2 -0
- package/dist/exports/api/workflows/index.mjs +1 -0
- package/dist/exports/api/workflows/logger.d.mts +21 -0
- package/dist/exports/api/workflows/mailer.d.mts +70 -0
- package/dist/exports/api/workflows/orm.d.mts +13 -0
- package/dist/exports/api/workflows/purge-attachment.mjs +1 -0
- package/dist/exports/api/workflows/purge-audit-logs.mjs +1 -0
- package/dist/exports/api/workflows/purge-unattached-blobs.mjs +1 -0
- package/dist/exports/api/workflows/redis.mjs +1 -0
- package/dist/exports/api/workflows/storage-schema.d.mts +699 -0
- package/dist/exports/api/workflows/storage.d.mts +396 -0
- package/dist/exports/api/workflows/track-db-changes.d.mts +72 -0
- package/dist/exports/api/workflows/track-db-changes.mjs +1 -0
- package/dist/exports/api/workflows/workflow.d.mts +24 -0
- package/dist/exports/api/workflows/workflow.mjs +1 -0
- package/dist/exports/cli/_virtual/rolldown_runtime.mjs +1 -0
- package/dist/exports/cli/api/auth-schema.mjs +1 -0
- package/dist/exports/cli/api/auth.d.mts +375 -0
- package/dist/exports/cli/api/cache.d.mts +44 -0
- package/dist/exports/cli/api/config.d.mts +18 -0
- package/dist/exports/cli/api/container.d.mts +167 -0
- package/dist/exports/cli/api/database.d.mts +46 -0
- package/dist/exports/cli/api/event.d.mts +68 -0
- package/dist/exports/cli/api/event.mjs +1 -0
- package/dist/exports/cli/api/logger.d.mts +21 -0
- package/dist/exports/cli/api/mailer.d.mts +70 -0
- package/dist/exports/cli/api/orm.d.mts +13 -0
- package/dist/exports/cli/api/redis.mjs +1 -0
- package/dist/exports/cli/api/storage-schema.d.mts +699 -0
- package/dist/exports/cli/api/storage.d.mts +396 -0
- package/dist/exports/cli/api/workflow.d.mts +2 -0
- package/dist/exports/cli/api/workflow.mjs +1 -0
- package/dist/exports/cli/api/workflows/extract-blob-metadata.mjs +1 -0
- package/dist/exports/cli/api/workflows/generate-image-variant.d.mts +63 -0
- package/dist/exports/cli/api/workflows/generate-image-variant.mjs +1 -0
- package/dist/exports/cli/api/workflows/generate-preview.mjs +1 -0
- package/dist/exports/cli/api/workflows/purge-attachment.mjs +1 -0
- package/dist/exports/cli/api/workflows/purge-audit-logs.mjs +1 -0
- package/dist/exports/cli/api/workflows/purge-unattached-blobs.mjs +1 -0
- package/dist/exports/cli/api/workflows/track-db-changes.mjs +1 -0
- package/dist/exports/cli/command.d.mts +54 -0
- package/dist/exports/cli/command.mjs +1 -0
- package/dist/exports/cli/context.d.mts +170 -0
- package/dist/exports/cli/index.d.mts +3 -0
- package/dist/exports/cli/index.mjs +1 -0
- package/dist/exports/devtools/index.d.ts +3 -0
- package/dist/exports/devtools/index.js +1 -0
- package/dist/exports/instrumentation.d.mts +1 -0
- package/dist/exports/instrumentation.mjs +1 -0
- package/dist/exports/tests/_virtual/rolldown_runtime.mjs +1 -0
- package/dist/exports/tests/api.d.mts +86 -0
- package/dist/exports/tests/api.mjs +1 -0
- package/dist/exports/tests/mock.d.mts +1 -0
- package/dist/exports/tests/mock.mjs +1 -0
- package/dist/exports/tests/node_modules/.bun/change-case@5.4.4/node_modules/change-case/dist/index.mjs +1 -0
- package/dist/exports/tests/node_modules/.bun/rate-limit-redis@4.3.1_f1fa5524233c9c60/node_modules/rate-limit-redis/dist/index.mjs +25 -0
- package/dist/exports/tests/packages/appos/src/api/app-context.d.mts +115 -0
- package/dist/exports/tests/packages/appos/src/api/auth-schema.d.mts +4248 -0
- package/dist/exports/tests/packages/appos/src/api/auth-schema.mjs +1 -0
- package/dist/exports/tests/packages/appos/src/api/auth.d.mts +398 -0
- package/dist/exports/tests/packages/appos/src/api/cache.d.mts +44 -0
- package/dist/exports/tests/packages/appos/src/api/config.d.mts +28 -0
- package/dist/exports/tests/packages/appos/src/api/container.d.mts +210 -0
- package/dist/exports/tests/packages/appos/src/api/database.d.mts +99 -0
- package/dist/exports/tests/packages/appos/src/api/database.mjs +1 -0
- package/dist/exports/tests/packages/appos/src/api/event.d.mts +235 -0
- package/dist/exports/tests/packages/appos/src/api/event.mjs +1 -0
- package/dist/exports/tests/packages/appos/src/api/i18n.d.mts +34 -0
- package/dist/exports/tests/packages/appos/src/api/index.d.mts +27 -0
- package/dist/exports/tests/packages/appos/src/api/logger.d.mts +21 -0
- package/dist/exports/tests/packages/appos/src/api/mailer.d.mts +70 -0
- package/dist/exports/tests/packages/appos/src/api/middleware/error-handler.mjs +1 -0
- package/dist/exports/tests/packages/appos/src/api/middleware/health.mjs +1 -0
- package/dist/exports/tests/packages/appos/src/api/middleware/i18n.mjs +1 -0
- package/dist/exports/tests/packages/appos/src/api/middleware/request-logger.d.mts +24 -0
- package/dist/exports/tests/packages/appos/src/api/middleware/request-logger.mjs +1 -0
- package/dist/exports/tests/packages/appos/src/api/middleware/shutdown.mjs +1 -0
- package/dist/exports/tests/packages/appos/src/api/middleware/timeout.mjs +1 -0
- package/dist/exports/tests/packages/appos/src/api/middleware/youch-handler.mjs +1 -0
- package/dist/exports/tests/packages/appos/src/api/middleware.d.mts +39 -0
- package/dist/exports/tests/packages/appos/src/api/middleware.mjs +1 -0
- package/dist/exports/tests/packages/appos/src/api/openapi.d.mts +271 -0
- package/dist/exports/tests/packages/appos/src/api/orm.d.mts +13 -0
- package/dist/exports/tests/packages/appos/src/api/otel.d.mts +40 -0
- package/dist/exports/tests/packages/appos/src/api/redis.d.mts +34 -0
- package/dist/exports/tests/packages/appos/src/api/redis.mjs +1 -0
- package/dist/exports/tests/packages/appos/src/api/server.mjs +1 -0
- package/dist/exports/tests/packages/appos/src/api/storage-schema.d.mts +707 -0
- package/dist/exports/tests/packages/appos/src/api/storage.d.mts +506 -0
- package/dist/exports/tests/packages/appos/src/api/workflow.d.mts +250 -0
- package/dist/exports/tests/packages/appos/src/api/workflow.mjs +1 -0
- package/dist/exports/tests/packages/appos/src/api/workflows/extract-blob-metadata.mjs +1 -0
- package/dist/exports/tests/packages/appos/src/api/workflows/generate-image-variant.d.mts +99 -0
- package/dist/exports/tests/packages/appos/src/api/workflows/generate-image-variant.mjs +1 -0
- package/dist/exports/tests/packages/appos/src/api/workflows/generate-preview.mjs +1 -0
- package/dist/exports/tests/packages/appos/src/api/workflows/purge-attachment.mjs +1 -0
- package/dist/exports/tests/packages/appos/src/api/workflows/purge-audit-logs.mjs +1 -0
- package/dist/exports/tests/packages/appos/src/api/workflows/purge-unattached-blobs.mjs +1 -0
- package/dist/exports/tests/packages/appos/src/api/workflows/track-db-changes.mjs +1 -0
- package/dist/exports/tests/packages/appos/src/constants.mjs +1 -0
- package/dist/exports/tests/packages/appos/src/instrumentation.d.mts +7 -0
- package/dist/exports/tests/packages/appos/src/instrumentation.mjs +1 -0
- package/dist/exports/tests/react.d.mts +2 -0
- package/dist/exports/tests/react.mjs +1 -0
- package/dist/exports/tests/setup.d.mts +1 -0
- package/dist/exports/tests/setup.mjs +1 -0
- package/dist/exports/vendors/date.js +1 -0
- package/dist/exports/vendors/toolkit.js +1 -0
- package/dist/exports/vendors/zod.d.ts +1 -0
- package/dist/exports/vendors/zod.js +1 -0
- package/dist/exports/vite/index.d.mts +19 -0
- package/dist/exports/vite/index.mjs +1 -0
- package/dist/exports/vitest/config.d.mts +1 -0
- package/dist/exports/vitest/config.mjs +1 -0
- package/dist/exports/vitest/globals.d.mts +1 -0
- package/dist/exports/vitest/globals.mjs +1 -0
- package/dist/exports/vitest/index.d.mts +1 -0
- package/dist/exports/vitest/index.mjs +1 -0
- package/dist/exports/web/api/auth.d.ts +125 -0
- package/dist/exports/web/api/database.d.ts +4 -0
- package/dist/exports/web/api/logger.d.ts +1 -0
- package/dist/exports/web/auth.d.ts +2388 -0
- package/dist/exports/web/auth.js +1 -0
- package/dist/exports/web/i18n.d.ts +42 -0
- package/dist/exports/web/i18n.js +1 -0
- package/dist/exports/web/index.d.ts +6 -0
- package/dist/exports/web/index.js +1 -0
- package/package.json +138 -98
- package/build/bin/main.mjs +0 -2
- package/build/exports/cli/index.d.mts +0 -325
- package/build/exports/cli/index.mjs +0 -1
- package/build/exports/instrumentation/execAsync-DaIUcs6_.mjs +0 -1
- package/build/exports/instrumentation/getMachineId-bsd-bB6ipDhm.mjs +0 -1
- package/build/exports/instrumentation/getMachineId-darwin-D1Bx5aCe.mjs +0 -2
- package/build/exports/instrumentation/getMachineId-linux-D_R9Tla0.mjs +0 -1
- package/build/exports/instrumentation/getMachineId-unsupported-BZKPE_Ev.mjs +0 -1
- package/build/exports/instrumentation/getMachineId-win-CmPvIqHL.mjs +0 -1
- package/build/exports/instrumentation/instrumentation.d.mts +0 -1
- package/build/exports/instrumentation/instrumentation.mjs +0 -80
- package/build/exports/server/index.d.mts +0 -327
- package/build/exports/server/index.mjs +0 -219
- package/build/exports/server/react-gPO8Jsy-.mjs +0 -13
- package/build/exports/server/server.node-D_9RYjm9.mjs +0 -210
- package/build/exports/store/index.d.mts +0 -58
- package/build/exports/store/index.mjs +0 -15
- package/build/exports/support/datetime.js +0 -1
- package/build/exports/support/utils.js +0 -1
- package/build/exports/support/zod.d.ts +0 -2
- package/build/exports/support/zod.js +0 -23
- package/build/exports/test/dist-DAsoCGWk.mjs +0 -348
- package/build/exports/test/index.d.mts +0 -3
- package/build/exports/test/index.mjs +0 -1
- package/build/exports/test/magic-string.es-BWgiB2kd.mjs +0 -14
- package/build/exports/test/setup.d.mts +0 -1
- package/build/exports/test/setup.mjs +0 -329
- /package/{build/exports/support/datetime.d.ts → dist/exports/vendors/date.d.ts} +0 -0
- /package/{build/exports/support/utils.d.ts → dist/exports/vendors/toolkit.d.ts} +0 -0
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import { Container } from "./container.mjs";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { DBOS } from "@dbos-inc/dbos-sdk";
|
|
4
|
+
|
|
5
|
+
//#region src/api/workflow.d.ts
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Context available to workflow functions.
|
|
9
|
+
*/
|
|
10
|
+
interface WorkflowContext<TInput = unknown> {
|
|
11
|
+
/**
|
|
12
|
+
* The application container with access to db, mailer, logger, etc.
|
|
13
|
+
*/
|
|
14
|
+
container: Container;
|
|
15
|
+
/**
|
|
16
|
+
* The validated input passed to this workflow.
|
|
17
|
+
*/
|
|
18
|
+
input: TInput;
|
|
19
|
+
/**
|
|
20
|
+
* Execute a checkpointed step within the workflow.
|
|
21
|
+
* Steps are retried on failure and their results are durably stored.
|
|
22
|
+
*
|
|
23
|
+
* @param name - Unique name for this step within the workflow
|
|
24
|
+
* @param fn - The step function to execute
|
|
25
|
+
* @returns The result of the step function
|
|
26
|
+
*/
|
|
27
|
+
step<T>(name: string, fn: () => Promise<T>): Promise<T>;
|
|
28
|
+
/**
|
|
29
|
+
* The unique ID of this workflow execution.
|
|
30
|
+
*/
|
|
31
|
+
workflowId: string;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Context available to scheduled workflow functions.
|
|
35
|
+
*/
|
|
36
|
+
interface ScheduledWorkflowContext {
|
|
37
|
+
/**
|
|
38
|
+
* The application container with access to db, mailer, logger, etc.
|
|
39
|
+
*/
|
|
40
|
+
container: Container;
|
|
41
|
+
/**
|
|
42
|
+
* The time this workflow execution was scheduled for.
|
|
43
|
+
*/
|
|
44
|
+
scheduledTime: Date;
|
|
45
|
+
/**
|
|
46
|
+
* Execute a checkpointed step within the workflow.
|
|
47
|
+
* Steps are retried on failure and their results are durably stored.
|
|
48
|
+
*
|
|
49
|
+
* @param name - Unique name for this step within the workflow
|
|
50
|
+
* @param fn - The step function to execute
|
|
51
|
+
* @returns The result of the step function
|
|
52
|
+
*/
|
|
53
|
+
step<T>(name: string, fn: () => Promise<T>): Promise<T>;
|
|
54
|
+
/**
|
|
55
|
+
* The unique ID of this workflow execution.
|
|
56
|
+
*/
|
|
57
|
+
workflowId: string;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Handle to a running or completed workflow.
|
|
61
|
+
*/
|
|
62
|
+
interface WorkflowHandle<TOutput> {
|
|
63
|
+
/**
|
|
64
|
+
* Get the current status of the workflow.
|
|
65
|
+
*/
|
|
66
|
+
getStatus(): Promise<unknown>;
|
|
67
|
+
/**
|
|
68
|
+
* Wait for the workflow to complete and return its result.
|
|
69
|
+
*/
|
|
70
|
+
getResult(): Promise<TOutput>;
|
|
71
|
+
/**
|
|
72
|
+
* The unique ID of this workflow execution.
|
|
73
|
+
*/
|
|
74
|
+
workflowId: string;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Options for defining a workflow.
|
|
78
|
+
*/
|
|
79
|
+
interface DefineWorkflowOptions<TInput extends z.ZodType, TOutput> {
|
|
80
|
+
/**
|
|
81
|
+
* Optional configuration for the workflow.
|
|
82
|
+
*/
|
|
83
|
+
config?: {
|
|
84
|
+
/**
|
|
85
|
+
* Maximum number of times to attempt recovery after failure.
|
|
86
|
+
*/
|
|
87
|
+
maxRecoveryAttempts?: number;
|
|
88
|
+
};
|
|
89
|
+
/**
|
|
90
|
+
* Zod schema for validating workflow input.
|
|
91
|
+
*/
|
|
92
|
+
input: TInput;
|
|
93
|
+
/**
|
|
94
|
+
* The workflow function to execute.
|
|
95
|
+
*
|
|
96
|
+
* @param ctx - Workflow context with container, input, and step helper
|
|
97
|
+
* @returns The workflow result
|
|
98
|
+
*/
|
|
99
|
+
run: (ctx: WorkflowContext<z.infer<TInput>>) => Promise<TOutput>;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Defines a durable workflow that can be triggered with type-safe input.
|
|
103
|
+
*
|
|
104
|
+
* Workflows are:
|
|
105
|
+
* - Durable: Automatically resume after crashes or restarts
|
|
106
|
+
* - Type-safe: Input and output types are inferred from the schema
|
|
107
|
+
* - Container-aware: Access to db, mailer, logger via ctx.container
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```typescript
|
|
111
|
+
* // api/workflows/send-welcome-email.ts
|
|
112
|
+
* export default defineWorkflow({
|
|
113
|
+
* input: z.object({
|
|
114
|
+
* userId: z.string(),
|
|
115
|
+
* email: z.string().email(),
|
|
116
|
+
* }),
|
|
117
|
+
* async run(ctx) {
|
|
118
|
+
* const user = await ctx.container.db.primary.query.users.findFirst({
|
|
119
|
+
* where: eq(users.id, ctx.input.userId),
|
|
120
|
+
* });
|
|
121
|
+
*
|
|
122
|
+
* await ctx.container.mailer.send({
|
|
123
|
+
* to: ctx.input.email,
|
|
124
|
+
* subject: "Welcome!",
|
|
125
|
+
* });
|
|
126
|
+
*
|
|
127
|
+
* return { sent: true };
|
|
128
|
+
* },
|
|
129
|
+
* });
|
|
130
|
+
*
|
|
131
|
+
* // Triggering from a route:
|
|
132
|
+
* import sendWelcomeEmail from "#api/workflows/send-welcome-email.ts";
|
|
133
|
+
* const handle = await sendWelcomeEmail.start({ userId: "123", email: "..." });
|
|
134
|
+
* ```
|
|
135
|
+
*/
|
|
136
|
+
declare function defineWorkflow<TInput extends z.ZodType, TOutput>(options: DefineWorkflowOptions<TInput, TOutput>): {
|
|
137
|
+
/**
|
|
138
|
+
* The Zod schema for validating input.
|
|
139
|
+
*/
|
|
140
|
+
inputSchema: TInput;
|
|
141
|
+
/**
|
|
142
|
+
* The workflow name (set during registration from filename).
|
|
143
|
+
*/
|
|
144
|
+
readonly name: string | null;
|
|
145
|
+
/**
|
|
146
|
+
* Register this workflow with DBOS. Called by the workflow loader.
|
|
147
|
+
*
|
|
148
|
+
* @param c - The application container
|
|
149
|
+
* @param name - The workflow name (derived from filename)
|
|
150
|
+
* @param dbos - The DBOS instance to use for registration
|
|
151
|
+
*/
|
|
152
|
+
register(c: Container, name: string, dbos: typeof DBOS): void;
|
|
153
|
+
/**
|
|
154
|
+
* Start this workflow with the given input.
|
|
155
|
+
*
|
|
156
|
+
* @param input - Input matching the workflow's input schema
|
|
157
|
+
* @returns A handle to track and retrieve the workflow result
|
|
158
|
+
*/
|
|
159
|
+
start(input: z.infer<TInput>): Promise<WorkflowHandle<TOutput>>;
|
|
160
|
+
};
|
|
161
|
+
/**
|
|
162
|
+
* Options for defining a scheduled workflow.
|
|
163
|
+
*/
|
|
164
|
+
interface DefineScheduledWorkflowOptions {
|
|
165
|
+
/**
|
|
166
|
+
* Cron expression for when to run the workflow.
|
|
167
|
+
* Supports 5-spot (minute hour day month weekday) or
|
|
168
|
+
* 6-spot (second minute hour day month weekday) format.
|
|
169
|
+
*
|
|
170
|
+
* Examples:
|
|
171
|
+
* - "0 9 * * *" - Every day at 9am
|
|
172
|
+
* - "0,30 * * * *" - Every 30 minutes
|
|
173
|
+
* - "0 0 * * * *" - Every hour (6-spot with seconds)
|
|
174
|
+
*/
|
|
175
|
+
crontab: string;
|
|
176
|
+
/**
|
|
177
|
+
* The workflow function to execute on schedule.
|
|
178
|
+
*
|
|
179
|
+
* @param ctx - Workflow context with container, scheduledTime, and step helper
|
|
180
|
+
*/
|
|
181
|
+
run: (ctx: ScheduledWorkflowContext) => Promise<void>;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Defines a scheduled workflow that runs on a cron schedule.
|
|
185
|
+
*
|
|
186
|
+
* @example
|
|
187
|
+
* ```typescript
|
|
188
|
+
* // api/workflows/daily-report.ts
|
|
189
|
+
* export default defineScheduledWorkflow({
|
|
190
|
+
* crontab: "0 9 * * *", // 9am daily
|
|
191
|
+
* async run(ctx) {
|
|
192
|
+
* const stats = await ctx.container.db.primary.query.stats.findMany();
|
|
193
|
+
* await ctx.container.mailer.send({
|
|
194
|
+
* to: "team@company.com",
|
|
195
|
+
* subject: `Daily Report - ${ctx.scheduledTime.toDateString()}`,
|
|
196
|
+
* });
|
|
197
|
+
* },
|
|
198
|
+
* });
|
|
199
|
+
* ```
|
|
200
|
+
*/
|
|
201
|
+
declare function defineScheduledWorkflow(options: DefineScheduledWorkflowOptions): {
|
|
202
|
+
/**
|
|
203
|
+
* The cron expression for this workflow.
|
|
204
|
+
*/
|
|
205
|
+
crontab: string;
|
|
206
|
+
/**
|
|
207
|
+
* The workflow name (set during registration from filename).
|
|
208
|
+
*/
|
|
209
|
+
readonly name: string | null;
|
|
210
|
+
/**
|
|
211
|
+
* Register this workflow with DBOS. Called by the workflow loader.
|
|
212
|
+
*
|
|
213
|
+
* @param c - The application container
|
|
214
|
+
* @param name - The workflow name (derived from filename)
|
|
215
|
+
* @param dbos - The DBOS instance to use for registration
|
|
216
|
+
*/
|
|
217
|
+
register(c: Container, name: string, dbos: typeof DBOS): void;
|
|
218
|
+
};
|
|
219
|
+
/**
|
|
220
|
+
* Options for loading workflows.
|
|
221
|
+
*/
|
|
222
|
+
interface LoadWorkflowsOptions {
|
|
223
|
+
/**
|
|
224
|
+
* The application container.
|
|
225
|
+
*/
|
|
226
|
+
container: Container;
|
|
227
|
+
/**
|
|
228
|
+
* The DBOS instance to use for registration.
|
|
229
|
+
*/
|
|
230
|
+
dbos: typeof DBOS;
|
|
231
|
+
/**
|
|
232
|
+
* Optional custom workflows directory path.
|
|
233
|
+
*/
|
|
234
|
+
workflowsDir?: string;
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Auto-discovers and registers all workflows.
|
|
238
|
+
*
|
|
239
|
+
* Algorithm:
|
|
240
|
+
* 1. Register built-in workflows (extractBlobMetadata, generateImageVariant, etc.)
|
|
241
|
+
* 2. Register scheduled workflows (purgeAuditLogs, purgeUnattachedBlobs)
|
|
242
|
+
* 3. Glob all .ts files in the directory (excluding test files)
|
|
243
|
+
* 4. Import each module's default export
|
|
244
|
+
* 5. If it has a register method, call register(container, name, dbos)
|
|
245
|
+
*
|
|
246
|
+
* @param opts - Load workflows options
|
|
247
|
+
*/
|
|
248
|
+
declare function loadWorkflows(opts: LoadWorkflowsOptions): Promise<void>;
|
|
249
|
+
//#endregion
|
|
250
|
+
export { ScheduledWorkflowContext, WorkflowContext, WorkflowHandle, defineScheduledWorkflow, defineWorkflow, loadWorkflows };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{APPOS_DIR as e,WORKFLOWS_DIR as t}from"./packages/appos/src/constants.mjs";import{basename as n,join as r}from"node:path";import{glob as i}from"node:fs/promises";import{camelCase as a}from"es-toolkit";function o(e){let t=null,n=null,r=null,i=null,a=async i=>{if(!t||!r)throw Error(`Workflow "${n}" not registered`);let a=r,o=a.workflowID;if(!o)throw Error(`DBOS.workflowID is not available in this context`);let s={container:t,workflowId:o,input:i,step:(e,t)=>a.runStep(t,{name:e})};return e.run(s)};return{inputSchema:e.input,get name(){return n},register(o,s,c){t=o,n=s,r=c,i=c.registerWorkflow(a,{name:s,...e.config})},async start(t){if(!i||!n||!r)throw Error(`Workflow not registered. Ensure the worker is started before triggering workflows.`);let a=e.input.parse(t),o=await r.startWorkflow(i)(a);return{workflowId:o.workflowID,getStatus:()=>o.getStatus(),getResult:()=>o.getResult()}}}}function s(e){let t=null,n=null,r=null,i=async(i,a)=>{if(!t||!r)throw Error(`Workflow "${n}" not registered`);let o=r,s=o.workflowID;if(!s)throw Error(`DBOS.workflowID is not available in this context`);let c={container:t,workflowId:s,scheduledTime:i,step:(e,t)=>o.runStep(t,{name:e})};return e.run(c)};return{crontab:e.crontab,get name(){return n},register(a,o,s){t=a,n=o,r=s,s.registerScheduled(s.registerWorkflow(i,{name:o}),{crontab:e.crontab})}}}function c(e){return a(n(e,`.ts`))}async function l(n){let{container:a,dbos:o}=n,s=n.workflowsDir??r(process.cwd(),e,t),l=(e,t)=>{try{e.register(a,t,o)}catch(e){if(!(e instanceof Error)||!e.message.includes(`already registered`))throw e}},{extractBlobMetadata:u}=await import(`./workflows/extract-blob-metadata.mjs`),{generateImageVariant:d}=await import(`./workflows/generate-image-variant.mjs`),{generatePreview:f}=await import(`./workflows/generate-preview.mjs`),{purgeAttachment:p}=await import(`./workflows/purge-attachment.mjs`),{trackDbChanges:m}=await import(`./workflows/track-db-changes.mjs`),{definePurgeAuditLogs:h}=await import(`./workflows/purge-audit-logs.mjs`),{definePurgeUnattachedBlobs:g}=await import(`./workflows/purge-unattached-blobs.mjs`);l(u,`extractBlobMetadata`),l(d,`generateImageVariant`),l(f,`generatePreview`),l(p,`purgeAttachment`),l(m,`trackDbChanges`),l(h(a.auth.auditLog?.purgeCron),`purgeAuditLogs`),l(g(a.storage.primary.purgeCron),`purgeUnattachedBlobs`);let _=await Array.fromAsync(i(`${s}/**/*.ts`,{exclude:[`**/*.test.ts`,`**/*.spec.ts`]}));for(let e of _){let t=await import(e);if(t.default&&typeof t.default==`object`&&`register`in t.default){let n=c(e);l(t.default,n)}}}export{s as defineScheduledWorkflow,o as defineWorkflow,l as loadWorkflows};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var e=Object.defineProperty,t=Object.getOwnPropertyDescriptor,n=Object.getOwnPropertyNames,r=Object.prototype.hasOwnProperty,i=(t,n)=>{let r={};for(var i in t)e(r,i,{get:t[i],enumerable:!0});return n&&e(r,Symbol.toStringTag,{value:`Module`}),r},a=(i,a,o,s)=>{if(a&&typeof a==`object`||typeof a==`function`)for(var c=n(a),l=0,u=c.length,d;l<u;l++)d=c[l],!r.call(i,d)&&d!==o&&e(i,d,{get:(e=>a[e]).bind(null,d),enumerable:!(s=t(a,d))||s.enumerable});return i},o=(e,t,n)=>(a(e,t,`default`),n&&a(n,t,`default`));export{i as __export,o as __reExport};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{sql as e}from"drizzle-orm";import{pgTable as t}from"drizzle-orm/pg-core";function n(){let n=t(`accounts`,t=>({id:t.text(`id`).primaryKey().default(e`uuidv7()`),accessToken:t.text(`access_token`),accessTokenExpiresAt:t.timestamp(`access_token_expires_at`,{mode:`string`,withTimezone:!0}),accountId:t.text(`account_id`).notNull(),createdAt:t.timestamp(`created_at`,{mode:`string`,withTimezone:!0}).default(e`NOW()`).notNull(),idToken:t.text(`id_token`),providerId:t.text(`provider_id`).notNull(),password:t.text(`password`),userId:t.text(`user_id`).notNull().references(()=>l.id,{onDelete:`cascade`}),refreshToken:t.text(`refresh_token`),refreshTokenExpiresAt:t.timestamp(`refresh_token_expires_at`,{mode:`string`,withTimezone:!0}),scope:t.text(`scope`),updatedAt:t.timestamp(`updated_at`,{mode:`string`,withTimezone:!0}).default(e`NOW()`).notNull()}),e=>[]),r=t(`api_keys`,t=>({id:t.text(`id`).primaryKey().default(e`uuidv7()`),name:t.text(`name`),enabled:t.boolean(`enabled`).default(!0),expiresAt:t.timestamp(`expires_at`,{mode:`string`,withTimezone:!0}),key:t.text(`key`).notNull(),lastRefillAt:t.timestamp(`last_refill_at`,{mode:`string`,withTimezone:!0}),lastRequest:t.timestamp(`last_request`,{mode:`string`,withTimezone:!0}),lastUsedAt:t.timestamp(`last_used_at`,{mode:`string`,withTimezone:!0}),metadata:t.text(`metadata`),permissions:t.text(`permissions`),prefix:t.text(`prefix`),rateLimitEnabled:t.boolean(`rate_limit_enabled`).default(!0),rateLimitTimeWindow:t.integer(`rate_limit_time_window`).default(864e5),rateLimitMax:t.integer(`rate_limit_max`).default(10),refillInterval:t.integer(`refill_interval`),refillAmount:t.integer(`refill_amount`),requestCount:t.integer(`request_count`),remaining:t.integer(`remaining`),start:t.text(`start`),userId:t.text(`user_id`).notNull().references(()=>l.id,{onDelete:`cascade`}),createdAt:t.timestamp(`created_at`,{mode:`string`,withTimezone:!0}).default(e`NOW()`).notNull(),updatedAt:t.timestamp(`updated_at`,{mode:`string`,withTimezone:!0}).default(e`NOW()`).notNull()}),e=>[]),i=t(`invitations`,t=>({id:t.text(`id`).primaryKey().default(e`uuidv7()`),email:t.text(`email`).notNull(),expiresAt:t.timestamp(`expires_at`,{mode:`string`,withTimezone:!0}).notNull(),inviterId:t.text(`inviter_id`).notNull().references(()=>l.id,{onDelete:`cascade`}),organizationId:t.text(`organization_id`).notNull().references(()=>o.id,{onDelete:`cascade`}),role:t.text(`role`),status:t.text(`status`).default(`pending`).notNull(),teamId:t.text(`team_id`),createdAt:t.timestamp(`created_at`,{mode:`string`,withTimezone:!0}).default(e`NOW()`).notNull(),updatedAt:t.timestamp(`updated_at`,{mode:`string`,withTimezone:!0}).default(e`NOW()`).notNull()}),e=>[]),a=t(`members`,t=>({id:t.text(`id`).primaryKey().default(e`uuidv7()`),organizationId:t.text(`organization_id`).notNull().references(()=>o.id,{onDelete:`cascade`}),role:t.text(`role`).default(`member`).notNull(),userId:t.text(`user_id`).notNull().references(()=>l.id,{onDelete:`cascade`}),createdAt:t.timestamp(`created_at`,{mode:`string`,withTimezone:!0}).default(e`NOW()`).notNull()}),e=>[]),o=t(`organizations`,t=>({id:t.text(`id`).primaryKey().default(e`uuidv7()`),name:t.text(`name`).notNull(),slug:t.text(`slug`).unique(),logo:t.text(`logo`),createdAt:t.timestamp(`created_at`,{mode:`string`,withTimezone:!0}).default(e`NOW()`).notNull(),updatedAt:t.timestamp(`updated_at`,{mode:`string`,withTimezone:!0}).default(e`NOW()`).notNull(),metadata:t.text(`metadata`)}),e=>[]),s=t(`sessions`,t=>({id:t.text(`id`).primaryKey().default(e`uuidv7()`),activeOrganizationId:t.text(`active_organization_id`).references(()=>o.id,{onDelete:`set null`}),activeTeamId:t.text(`active_team_id`),createdAt:t.timestamp(`created_at`,{mode:`string`,withTimezone:!0}).default(e`NOW()`).notNull(),expiresAt:t.timestamp(`expires_at`,{mode:`string`,withTimezone:!0}).notNull(),impersonatedBy:t.text(`impersonated_by`).references(()=>l.id,{onDelete:`set null`}),ipAddress:t.text(`ip_address`),token:t.text(`token`).notNull().unique(),updatedAt:t.timestamp(`updated_at`,{mode:`string`,withTimezone:!0}).default(e`NOW()`).notNull(),userAgent:t.text(`user_agent`),userId:t.text(`user_id`).notNull().references(()=>l.id,{onDelete:`cascade`})}),e=>[]),c=t(`sso_providers`,t=>({id:t.text(`id`).primaryKey().default(e`uuidv7()`),domain:t.text(`domain`).notNull(),issuer:t.text(`issuer`).notNull(),oidcConfig:t.text(`oidc_config`),organizationId:t.text(`organization_id`).references(()=>o.id,{onDelete:`cascade`}),providerId:t.text(`provider_id`).notNull().unique(),samlConfig:t.text(`saml_config`),userId:t.text(`user_id`).references(()=>l.id,{onDelete:`cascade`})}),e=>[]),l=t(`users`,t=>({id:t.text(`id`).primaryKey().default(e`uuidv7()`),banExpires:t.timestamp(`ban_expires`,{mode:`string`,withTimezone:!0}),banReason:t.text(`ban_reason`),banned:t.boolean(`banned`).default(!1),createdAt:t.timestamp(`created_at`,{mode:`string`,withTimezone:!0}).default(e`NOW()`).notNull(),displayUsername:t.text(`display_username`),email:t.text(`email`).notNull().unique(),emailVerified:t.boolean(`email_verified`).default(!1).notNull(),image:t.text(`image`),isAnonymous:t.boolean(`is_anonymous`),lastLoginMethod:t.text(`last_login_method`),name:t.text(`name`).notNull(),phoneNumber:t.text(`phone_number`).unique(),phoneNumberVerified:t.boolean(`phone_number_verified`),role:t.text(`role`),twoFactorEnabled:t.boolean(`two_factor_enabled`).default(!1),updatedAt:t.timestamp(`updated_at`,{mode:`string`,withTimezone:!0}).default(e`NOW()`).notNull(),username:t.text(`username`).unique()}),e=>[]),u=t(`teams`,t=>({id:t.text(`id`).primaryKey().default(e`uuidv7()`),name:t.text(`name`).notNull(),organizationId:t.text(`organization_id`).notNull().references(()=>o.id,{onDelete:`cascade`}),createdAt:t.timestamp(`created_at`,{mode:`string`,withTimezone:!0}).default(e`NOW()`).notNull(),updatedAt:t.timestamp(`updated_at`,{mode:`string`,withTimezone:!0}).default(e`NOW()`).notNull()}),e=>[]),d=t(`team_members`,t=>({id:t.text(`id`).primaryKey().default(e`uuidv7()`),teamId:t.text(`team_id`).notNull().references(()=>u.id,{onDelete:`cascade`}),userId:t.text(`user_id`).notNull().references(()=>l.id,{onDelete:`cascade`}),createdAt:t.timestamp(`created_at`,{mode:`string`,withTimezone:!0}).default(e`NOW()`).notNull()}),e=>[]),f=t(`two_factors`,t=>({id:t.text(`id`).primaryKey().default(e`uuidv7()`),secret:t.text(`secret`).notNull(),backupCodes:t.text(`backup_codes`).notNull(),userId:t.text(`user_id`).notNull().references(()=>l.id,{onDelete:`cascade`})}),e=>[]),p=t(`verifications`,t=>({id:t.text(`id`).primaryKey().default(e`uuidv7()`),createdAt:t.timestamp(`created_at`,{mode:`string`,withTimezone:!0}).default(e`NOW()`).notNull(),expiresAt:t.timestamp(`expires_at`,{mode:`string`,withTimezone:!0}).notNull(),identifier:t.text(`identifier`).notNull(),updatedAt:t.timestamp(`updated_at`,{mode:`string`,withTimezone:!0}).default(e`NOW()`).notNull(),value:t.text(`value`).notNull()}),e=>[]);return{tables:{accounts:n,apiKeys:r,auditLogs:t(`audit_logs`,t=>({id:t.text(`id`).primaryKey().default(e`uuidv7()`),tableName:t.text(`table_name`),action:t.text(`action`).notNull(),customAction:t.text(`custom_action`),oldData:t.jsonb(`old_data`),newData:t.jsonb(`new_data`),metadata:t.jsonb(`metadata`),organizationId:t.text(`organization_id`).references(()=>o.id,{onDelete:`set null`}),userId:t.text(`user_id`).references(()=>l.id,{onDelete:`set null`}),sessionId:t.text(`session_id`).references(()=>s.id,{onDelete:`set null`}),requestId:t.text(`request_id`),createdAt:t.timestamp(`created_at`,{mode:`string`,withTimezone:!0}).default(e`NOW()`).notNull()}),e=>[]),invitations:i,members:a,organizations:o,sessions:s,ssoProviders:c,teams:u,teamMembers:d,twoFactors:f,users:l,verifications:p},relations:e=>({users:{sessions:e.many.sessions({from:e.users.id,to:e.sessions.userId}),accounts:e.many.accounts({from:e.users.id,to:e.accounts.userId}),apiKeys:e.many.apiKeys({from:e.users.id,to:e.apiKeys.userId}),memberships:e.many.members({from:e.users.id,to:e.members.userId}),invitations:e.many.invitations({from:e.users.id,to:e.invitations.inviterId}),ssoProvider:e.one.ssoProviders({from:e.users.id,to:e.ssoProviders.userId}),twoFactor:e.one.twoFactors({from:e.users.id,to:e.twoFactors.userId})},sessions:{user:e.one.users({from:e.sessions.userId,to:e.users.id})},accounts:{user:e.one.users({from:e.accounts.userId,to:e.users.id})},apiKeys:{user:e.one.users({from:e.apiKeys.userId,to:e.users.id})},organizations:{members:e.many.members({from:e.organizations.id,to:e.members.organizationId}),invitations:e.many.invitations({from:e.organizations.id,to:e.invitations.organizationId}),teams:e.many.teams({from:e.organizations.id,to:e.teams.organizationId})},members:{organization:e.one.organizations({from:e.members.organizationId,to:e.organizations.id}),user:e.one.users({from:e.members.userId,to:e.users.id})},invitations:{organization:e.one.organizations({from:e.invitations.organizationId,to:e.organizations.id}),inviter:e.one.users({from:e.invitations.inviterId,to:e.users.id})},teams:{organization:e.one.organizations({from:e.teams.organizationId,to:e.organizations.id})},ssoProviders:{user:e.one.users({from:e.ssoProviders.userId,to:e.users.id})},verifications:{},twoFactors:{user:e.one.users({from:e.twoFactors.userId,to:e.users.id})},auditLogs:{organization:e.one.organizations({from:e.auditLogs.organizationId,to:e.organizations.id}),user:e.one.users({from:e.auditLogs.userId,to:e.users.id}),session:e.one.sessions({from:e.auditLogs.sessionId,to:e.sessions.id})}})}}export{n as defineAuthSchema};
|
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
import { Database, QualifiedTableNames } from "./database.mjs";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { betterAuth } from "better-auth";
|
|
4
|
+
import { Role, createAccessControl } from "better-auth/plugins/access";
|
|
5
|
+
|
|
6
|
+
//#region src/api/auth.d.ts
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Type for access controller created via createAccessControl().
|
|
10
|
+
* Used for RBAC on both server and client.
|
|
11
|
+
*/
|
|
12
|
+
type AccessController = ReturnType<typeof createAccessControl>;
|
|
13
|
+
/**
|
|
14
|
+
* Type for roles created via ac.newRole().
|
|
15
|
+
* Uses Role type from better-auth for compatibility.
|
|
16
|
+
*/
|
|
17
|
+
type AccessControlRoles = Record<string, Role>;
|
|
18
|
+
/**
|
|
19
|
+
* Type-safe audit log options for defineAuth().
|
|
20
|
+
* Generic over db object to provide autocomplete for excludeTables.
|
|
21
|
+
*
|
|
22
|
+
* @template TDb - Database record type for table name inference
|
|
23
|
+
*/
|
|
24
|
+
type AuditLogOptions<TDb> = {
|
|
25
|
+
/**
|
|
26
|
+
* Tables to exclude from audit logging.
|
|
27
|
+
*/
|
|
28
|
+
excludeTables?: QualifiedTableNames<TDb>[];
|
|
29
|
+
/**
|
|
30
|
+
* Cron expression for purge schedule.
|
|
31
|
+
*
|
|
32
|
+
* @default "0 0 * * *"
|
|
33
|
+
*/
|
|
34
|
+
purgeCron?: string;
|
|
35
|
+
/**
|
|
36
|
+
* Retention period in days. Audit logs older than this are auto-deleted.
|
|
37
|
+
* @default 90
|
|
38
|
+
*/
|
|
39
|
+
retentionDays?: number;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Neutral auth configuration - shared between server and client.
|
|
43
|
+
* Contains ONLY fields that affect both sides (UI + server features).
|
|
44
|
+
*
|
|
45
|
+
* PRESENCE-BASED CONFIG: If a key exists, it's enabled. No redundant `enabled` fields.
|
|
46
|
+
* Server-only fields (appName, session) are passed via DefineAuthOptions.
|
|
47
|
+
*/
|
|
48
|
+
interface AuthConfig {
|
|
49
|
+
/**
|
|
50
|
+
* Base URL where auth server is hosted.
|
|
51
|
+
*
|
|
52
|
+
* @default "" (same origin - client uses relative URLs)
|
|
53
|
+
* @example "http://localhost:8000" for cross-origin
|
|
54
|
+
*/
|
|
55
|
+
baseURL?: string;
|
|
56
|
+
/**
|
|
57
|
+
* Base path for auth routes.
|
|
58
|
+
*
|
|
59
|
+
* @default "/auth"
|
|
60
|
+
*/
|
|
61
|
+
basePath?: string;
|
|
62
|
+
/** Authentication methods - if defined, it's enabled */
|
|
63
|
+
methods?: {
|
|
64
|
+
/** Email/password auth. If defined, enabled. */
|
|
65
|
+
emailPassword?: {
|
|
66
|
+
requireEmailVerification?: boolean;
|
|
67
|
+
minPasswordLength?: number;
|
|
68
|
+
maxPasswordLength?: number;
|
|
69
|
+
};
|
|
70
|
+
/** Magic link auth. If defined, enabled. */
|
|
71
|
+
magicLink?: {
|
|
72
|
+
expiresIn?: number;
|
|
73
|
+
};
|
|
74
|
+
/** Passkey auth. If defined (even as empty object), enabled. */
|
|
75
|
+
passkey?: Record<string, never>;
|
|
76
|
+
/** Phone OTP auth. If defined, enabled. */
|
|
77
|
+
phoneOtp?: {
|
|
78
|
+
otpLength?: number;
|
|
79
|
+
expiresIn?: number;
|
|
80
|
+
};
|
|
81
|
+
/** Email OTP auth. If defined, enabled. */
|
|
82
|
+
emailOtp?: {
|
|
83
|
+
otpLength?: number;
|
|
84
|
+
expiresIn?: number;
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
/** OAuth providers - true = enabled, undefined/false = disabled */
|
|
88
|
+
oauth?: {
|
|
89
|
+
google?: boolean;
|
|
90
|
+
github?: boolean;
|
|
91
|
+
apple?: boolean;
|
|
92
|
+
facebook?: boolean;
|
|
93
|
+
};
|
|
94
|
+
/** Plugins - if defined, it's enabled */
|
|
95
|
+
plugins?: {
|
|
96
|
+
/** Admin plugin. If defined, enabled. Includes RBAC for both server and client. */
|
|
97
|
+
admin?: {
|
|
98
|
+
defaultRole?: string;
|
|
99
|
+
adminRoles?: string[];
|
|
100
|
+
/** Access controller created via createAccessControl() - shared between server and client */
|
|
101
|
+
ac: AccessController;
|
|
102
|
+
/** Role definitions created via ac.newRole() - shared between server and client */
|
|
103
|
+
roles: AccessControlRoles;
|
|
104
|
+
};
|
|
105
|
+
/** API key plugin. If defined, enabled. */
|
|
106
|
+
apiKey?: {
|
|
107
|
+
defaultPrefix?: string;
|
|
108
|
+
defaultKeyLength?: number;
|
|
109
|
+
rateLimit?: {
|
|
110
|
+
maxRequests?: number;
|
|
111
|
+
timeWindow?: number;
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
/** Two-factor plugin. If defined, enabled. Sub-features also presence-based. */
|
|
115
|
+
twoFactor?: {
|
|
116
|
+
issuer?: string;
|
|
117
|
+
totp?: {
|
|
118
|
+
digits?: 6 | 8;
|
|
119
|
+
period?: number;
|
|
120
|
+
};
|
|
121
|
+
otp?: boolean;
|
|
122
|
+
backupCodes?: {
|
|
123
|
+
amount?: number;
|
|
124
|
+
length?: number;
|
|
125
|
+
};
|
|
126
|
+
};
|
|
127
|
+
/** Multi-session plugin. If defined, enabled. */
|
|
128
|
+
multiSession?: {
|
|
129
|
+
maximumSessions?: number;
|
|
130
|
+
};
|
|
131
|
+
/** Username plugin. If defined, enabled. */
|
|
132
|
+
username?: {
|
|
133
|
+
minUsernameLength?: number;
|
|
134
|
+
maxUsernameLength?: number;
|
|
135
|
+
};
|
|
136
|
+
/** Anonymous auth plugin. If defined, enabled. */
|
|
137
|
+
anonymous?: {
|
|
138
|
+
emailDomainName?: string;
|
|
139
|
+
};
|
|
140
|
+
/** SSO plugin. If defined, enabled. Import from @better-auth/sso */
|
|
141
|
+
sso?: {
|
|
142
|
+
providersLimit?: number;
|
|
143
|
+
trustEmailVerified?: boolean;
|
|
144
|
+
domainVerification?: boolean;
|
|
145
|
+
};
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
/** Base hook types for reference */
|
|
149
|
+
type EmailHook = (params: {
|
|
150
|
+
email: string;
|
|
151
|
+
url: string;
|
|
152
|
+
token: string;
|
|
153
|
+
}) => Promise<void>;
|
|
154
|
+
type OtpHook = (params: {
|
|
155
|
+
email: string;
|
|
156
|
+
otp: string;
|
|
157
|
+
}) => Promise<void>;
|
|
158
|
+
type PhoneOtpHook = (params: {
|
|
159
|
+
phoneNumber: string;
|
|
160
|
+
otp: string;
|
|
161
|
+
}) => Promise<void>;
|
|
162
|
+
/**
|
|
163
|
+
* Conditionally required hooks based on config.
|
|
164
|
+
* Uses PRESENCE-BASED detection - if key exists, hook is REQUIRED.
|
|
165
|
+
*/
|
|
166
|
+
type RequiredHooks<T extends AuthConfig> = (T["methods"] extends {
|
|
167
|
+
emailPassword: {
|
|
168
|
+
requireEmailVerification: true;
|
|
169
|
+
};
|
|
170
|
+
} ? {
|
|
171
|
+
sendVerificationEmail: EmailHook;
|
|
172
|
+
} : {
|
|
173
|
+
sendVerificationEmail?: EmailHook;
|
|
174
|
+
}) & (T["methods"] extends {
|
|
175
|
+
emailPassword: object;
|
|
176
|
+
} ? {
|
|
177
|
+
sendResetPasswordEmail: EmailHook;
|
|
178
|
+
} : {
|
|
179
|
+
sendResetPasswordEmail?: EmailHook;
|
|
180
|
+
}) & (T["methods"] extends {
|
|
181
|
+
magicLink: object;
|
|
182
|
+
} ? {
|
|
183
|
+
sendMagicLink: EmailHook;
|
|
184
|
+
} : {
|
|
185
|
+
sendMagicLink?: EmailHook;
|
|
186
|
+
}) & (T["methods"] extends {
|
|
187
|
+
emailOtp: object;
|
|
188
|
+
} ? {
|
|
189
|
+
sendEmailOTP: OtpHook;
|
|
190
|
+
} : {
|
|
191
|
+
sendEmailOTP?: OtpHook;
|
|
192
|
+
}) & (T["methods"] extends {
|
|
193
|
+
phoneOtp: object;
|
|
194
|
+
} ? {
|
|
195
|
+
sendPhoneOTP: PhoneOtpHook;
|
|
196
|
+
} : {
|
|
197
|
+
sendPhoneOTP?: PhoneOtpHook;
|
|
198
|
+
}) & (T["plugins"] extends {
|
|
199
|
+
twoFactor: {
|
|
200
|
+
otp: true;
|
|
201
|
+
};
|
|
202
|
+
} ? {
|
|
203
|
+
send2FAOTP: OtpHook;
|
|
204
|
+
} : {
|
|
205
|
+
send2FAOTP?: OtpHook;
|
|
206
|
+
});
|
|
207
|
+
/**
|
|
208
|
+
* Conditionally required OAuth credentials based on config.
|
|
209
|
+
* If an OAuth provider is enabled in config, its credentials are REQUIRED.
|
|
210
|
+
*/
|
|
211
|
+
type RequiredOAuth<T extends AuthConfig> = (T["oauth"] extends {
|
|
212
|
+
google: true;
|
|
213
|
+
} ? {
|
|
214
|
+
google: {
|
|
215
|
+
clientId: string;
|
|
216
|
+
clientSecret: string;
|
|
217
|
+
};
|
|
218
|
+
} : {
|
|
219
|
+
google?: {
|
|
220
|
+
clientId: string;
|
|
221
|
+
clientSecret: string;
|
|
222
|
+
};
|
|
223
|
+
}) & (T["oauth"] extends {
|
|
224
|
+
github: true;
|
|
225
|
+
} ? {
|
|
226
|
+
github: {
|
|
227
|
+
clientId: string;
|
|
228
|
+
clientSecret: string;
|
|
229
|
+
};
|
|
230
|
+
} : {
|
|
231
|
+
github?: {
|
|
232
|
+
clientId: string;
|
|
233
|
+
clientSecret: string;
|
|
234
|
+
};
|
|
235
|
+
}) & (T["oauth"] extends {
|
|
236
|
+
apple: true;
|
|
237
|
+
} ? {
|
|
238
|
+
apple: {
|
|
239
|
+
clientId: string;
|
|
240
|
+
clientSecret: string;
|
|
241
|
+
};
|
|
242
|
+
} : {
|
|
243
|
+
apple?: {
|
|
244
|
+
clientId: string;
|
|
245
|
+
clientSecret: string;
|
|
246
|
+
};
|
|
247
|
+
}) & (T["oauth"] extends {
|
|
248
|
+
facebook: true;
|
|
249
|
+
} ? {
|
|
250
|
+
facebook: {
|
|
251
|
+
clientId: string;
|
|
252
|
+
clientSecret: string;
|
|
253
|
+
};
|
|
254
|
+
} : {
|
|
255
|
+
facebook?: {
|
|
256
|
+
clientId: string;
|
|
257
|
+
clientSecret: string;
|
|
258
|
+
};
|
|
259
|
+
});
|
|
260
|
+
/** Check if any OAuth provider is enabled */
|
|
261
|
+
type HasOAuthEnabled<T extends AuthConfig> = T["oauth"] extends {
|
|
262
|
+
google: true;
|
|
263
|
+
} | {
|
|
264
|
+
github: true;
|
|
265
|
+
} | {
|
|
266
|
+
apple: true;
|
|
267
|
+
} | {
|
|
268
|
+
facebook: true;
|
|
269
|
+
} ? true : false;
|
|
270
|
+
/** Check if passkey is enabled */
|
|
271
|
+
type HasPasskeyEnabled<T extends AuthConfig> = T["methods"] extends {
|
|
272
|
+
passkey: object;
|
|
273
|
+
} ? true : false;
|
|
274
|
+
/**
|
|
275
|
+
* Server-only session configuration.
|
|
276
|
+
*/
|
|
277
|
+
interface AuthSessionConfig {
|
|
278
|
+
/**
|
|
279
|
+
* Session duration in seconds.
|
|
280
|
+
*
|
|
281
|
+
* @default 604800 (7 days)
|
|
282
|
+
*/
|
|
283
|
+
expiresIn?: number;
|
|
284
|
+
/**
|
|
285
|
+
* How often to update session in seconds.
|
|
286
|
+
*
|
|
287
|
+
* @default 86400 (1 day)
|
|
288
|
+
*/
|
|
289
|
+
updateAge?: number;
|
|
290
|
+
/**
|
|
291
|
+
* Session freshness in seconds for sensitive ops.
|
|
292
|
+
*
|
|
293
|
+
* @default 86400 (1 day)
|
|
294
|
+
*/
|
|
295
|
+
freshAge?: number;
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Server-only passkey configuration (required if passkey is enabled).
|
|
299
|
+
*/
|
|
300
|
+
interface AuthPasskeyConfig {
|
|
301
|
+
/**
|
|
302
|
+
* Relying Party ID - domain for passkey (e.g., "example.com" or "localhost").
|
|
303
|
+
*/
|
|
304
|
+
rpID: string;
|
|
305
|
+
/**
|
|
306
|
+
* Origin URL for passkey verification (e.g., "http://localhost:8000").
|
|
307
|
+
*/
|
|
308
|
+
origin: string;
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Fully type-safe options for defineAuth().
|
|
312
|
+
*
|
|
313
|
+
* Server-only fields:
|
|
314
|
+
* - `appName` - Application name for passkey rpName, TOTP issuer, emails
|
|
315
|
+
* - `auditLog` - Audit logging configuration
|
|
316
|
+
* - `session` - Session duration and freshness settings
|
|
317
|
+
*
|
|
318
|
+
* Conditional requirements:
|
|
319
|
+
* - If config has `oauth.google: true`, then `oauth.google` credentials are REQUIRED
|
|
320
|
+
* - If config has `methods.magicLink` defined, then `hooks.sendMagicLink` is REQUIRED
|
|
321
|
+
*
|
|
322
|
+
* @template T - Auth config type
|
|
323
|
+
* @template TDb - Database record type for type-safe excludeTables
|
|
324
|
+
*/
|
|
325
|
+
type DefineAuthOptions<T extends AuthConfig, TDb = unknown> = {
|
|
326
|
+
/**
|
|
327
|
+
* The application name.
|
|
328
|
+
*/
|
|
329
|
+
appName: string;
|
|
330
|
+
/**
|
|
331
|
+
* Audit logging configuration (server-only). Use qualified table names: "dbName.tableName".
|
|
332
|
+
*/
|
|
333
|
+
auditLog?: AuditLogOptions<TDb>;
|
|
334
|
+
/**
|
|
335
|
+
* The neutral auth configuration.
|
|
336
|
+
*/
|
|
337
|
+
config: T;
|
|
338
|
+
/**
|
|
339
|
+
* Full db object (container.db) for type inference.
|
|
340
|
+
*/
|
|
341
|
+
db: TDb;
|
|
342
|
+
/**
|
|
343
|
+
* Primary database for Better Auth storage.
|
|
344
|
+
*/
|
|
345
|
+
database: Database;
|
|
346
|
+
/**
|
|
347
|
+
* Hooks for email sending and OTP delivery.
|
|
348
|
+
*/
|
|
349
|
+
hooks: RequiredHooks<T>;
|
|
350
|
+
/**
|
|
351
|
+
* Session configuration.
|
|
352
|
+
*/
|
|
353
|
+
session?: AuthSessionConfig;
|
|
354
|
+
} & (HasOAuthEnabled<T> extends true ? {
|
|
355
|
+
oauth: RequiredOAuth<T>;
|
|
356
|
+
} : {
|
|
357
|
+
oauth?: RequiredOAuth<T>;
|
|
358
|
+
}) & (HasPasskeyEnabled<T> extends true ? {
|
|
359
|
+
passkey: AuthPasskeyConfig;
|
|
360
|
+
} : {
|
|
361
|
+
passkey?: AuthPasskeyConfig;
|
|
362
|
+
});
|
|
363
|
+
/**
|
|
364
|
+
* Defines Better Auth instance from neutral config + server dependencies.
|
|
365
|
+
*/
|
|
366
|
+
declare function defineAuth<T extends AuthConfig, TDb extends Record<"primary", Database> & Record<string, Database> = Record<"primary", Database> & Record<string, Database>>(opts: DefineAuthOptions<T, TDb>): ReturnType<typeof betterAuth> & {
|
|
367
|
+
auditLog?: AuditLogOptions<TDb>;
|
|
368
|
+
shouldAudit(tableName: QualifiedTableNames<TDb>): boolean;
|
|
369
|
+
};
|
|
370
|
+
/**
|
|
371
|
+
* The auth instance type.
|
|
372
|
+
*/
|
|
373
|
+
type Auth<TDb extends Record<"primary", Database> & Record<string, Database> = Record<"primary", Database> & Record<string, Database>> = ReturnType<typeof defineAuth<AuthConfig, TDb>>;
|
|
374
|
+
//#endregion
|
|
375
|
+
export { Auth };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Logger } from "./logger.mjs";
|
|
2
|
+
import * as keyv0 from "keyv";
|
|
3
|
+
import { KeyvRedisOptions } from "@keyv/redis";
|
|
4
|
+
|
|
5
|
+
//#region src/api/cache.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* The cache instance type.
|
|
8
|
+
*/
|
|
9
|
+
type Cache = ReturnType<typeof defineCache>;
|
|
10
|
+
/**
|
|
11
|
+
* Options for defining the cache.
|
|
12
|
+
*/
|
|
13
|
+
type DefineCacheOptions = {
|
|
14
|
+
/**
|
|
15
|
+
* Redis URL(s). Single or comma-separated for cluster.
|
|
16
|
+
*/
|
|
17
|
+
url: string;
|
|
18
|
+
/**
|
|
19
|
+
* The logger instance.
|
|
20
|
+
*/
|
|
21
|
+
logger: Logger;
|
|
22
|
+
/**
|
|
23
|
+
* The Keyv Redis options.
|
|
24
|
+
*/
|
|
25
|
+
options?: KeyvRedisOptions;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Define the cache instance using shared Redis client.
|
|
29
|
+
* Connection is lazy - only connects when first cache operation is performed.
|
|
30
|
+
*
|
|
31
|
+
* Algorithm:
|
|
32
|
+
* 1. Create Redis client using defineRedisClient() (lazy connection)
|
|
33
|
+
* 2. Pass client to createKeyv() - connection happens on first use
|
|
34
|
+
*
|
|
35
|
+
* @param opts - The options for defining the cache.
|
|
36
|
+
* @returns The cache instance.
|
|
37
|
+
*/
|
|
38
|
+
declare function defineCache({
|
|
39
|
+
url,
|
|
40
|
+
logger,
|
|
41
|
+
options
|
|
42
|
+
}: DefineCacheOptions): keyv0.Keyv<any>;
|
|
43
|
+
//#endregion
|
|
44
|
+
export { Cache };
|