@unifiedcommerce/core 0.0.4 → 0.1.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.
Files changed (179) hide show
  1. package/dist/auth/auth-schema.d.ts +92 -0
  2. package/dist/auth/auth-schema.d.ts.map +1 -1
  3. package/dist/auth/auth-schema.js +7 -0
  4. package/dist/auth/setup.d.ts.map +1 -1
  5. package/dist/auth/setup.js +3 -1
  6. package/package.json +1 -2
  7. package/src/adapters/console-email.ts +0 -43
  8. package/src/auth/access.ts +0 -187
  9. package/src/auth/auth-schema.ts +0 -131
  10. package/src/auth/middleware.ts +0 -161
  11. package/src/auth/org.ts +0 -41
  12. package/src/auth/permissions.ts +0 -28
  13. package/src/auth/setup.ts +0 -165
  14. package/src/auth/system-actor.ts +0 -19
  15. package/src/auth/types.ts +0 -10
  16. package/src/config/defaults.ts +0 -82
  17. package/src/config/define-config.ts +0 -53
  18. package/src/config/types.ts +0 -299
  19. package/src/generated/plugin-capabilities.d.ts +0 -20
  20. package/src/generated/plugin-manifest.ts +0 -23
  21. package/src/generated/plugin-repositories.d.ts +0 -20
  22. package/src/hooks/checkout-completion.ts +0 -262
  23. package/src/hooks/checkout.ts +0 -677
  24. package/src/hooks/order-emails.ts +0 -62
  25. package/src/index.ts +0 -214
  26. package/src/interfaces/mcp/agent-prompt.ts +0 -174
  27. package/src/interfaces/mcp/context-enrichment.ts +0 -177
  28. package/src/interfaces/mcp/server.ts +0 -617
  29. package/src/interfaces/mcp/transport.ts +0 -68
  30. package/src/interfaces/rest/customer-portal.ts +0 -299
  31. package/src/interfaces/rest/index.ts +0 -74
  32. package/src/interfaces/rest/router.ts +0 -334
  33. package/src/interfaces/rest/routes/admin-jobs.ts +0 -58
  34. package/src/interfaces/rest/routes/audit.ts +0 -50
  35. package/src/interfaces/rest/routes/carts.ts +0 -89
  36. package/src/interfaces/rest/routes/catalog.ts +0 -493
  37. package/src/interfaces/rest/routes/checkout.ts +0 -283
  38. package/src/interfaces/rest/routes/inventory.ts +0 -70
  39. package/src/interfaces/rest/routes/media.ts +0 -86
  40. package/src/interfaces/rest/routes/orders.ts +0 -78
  41. package/src/interfaces/rest/routes/payments.ts +0 -60
  42. package/src/interfaces/rest/routes/pricing.ts +0 -57
  43. package/src/interfaces/rest/routes/promotions.ts +0 -92
  44. package/src/interfaces/rest/routes/search.ts +0 -71
  45. package/src/interfaces/rest/routes/webhooks.ts +0 -46
  46. package/src/interfaces/rest/schemas/admin-jobs.ts +0 -40
  47. package/src/interfaces/rest/schemas/audit.ts +0 -46
  48. package/src/interfaces/rest/schemas/carts.ts +0 -125
  49. package/src/interfaces/rest/schemas/catalog.ts +0 -450
  50. package/src/interfaces/rest/schemas/checkout.ts +0 -66
  51. package/src/interfaces/rest/schemas/customer-portal.ts +0 -195
  52. package/src/interfaces/rest/schemas/inventory.ts +0 -138
  53. package/src/interfaces/rest/schemas/media.ts +0 -75
  54. package/src/interfaces/rest/schemas/orders.ts +0 -104
  55. package/src/interfaces/rest/schemas/pricing.ts +0 -80
  56. package/src/interfaces/rest/schemas/promotions.ts +0 -110
  57. package/src/interfaces/rest/schemas/responses.ts +0 -85
  58. package/src/interfaces/rest/schemas/search.ts +0 -58
  59. package/src/interfaces/rest/schemas/shared.ts +0 -62
  60. package/src/interfaces/rest/schemas/webhooks.ts +0 -68
  61. package/src/interfaces/rest/utils.ts +0 -104
  62. package/src/interfaces/rest/webhook-router.ts +0 -50
  63. package/src/kernel/compensation/executor.ts +0 -61
  64. package/src/kernel/compensation/types.ts +0 -26
  65. package/src/kernel/database/adapter.ts +0 -13
  66. package/src/kernel/database/drizzle-db.ts +0 -56
  67. package/src/kernel/database/migrate.ts +0 -76
  68. package/src/kernel/database/plugin-types.ts +0 -34
  69. package/src/kernel/database/schema.ts +0 -49
  70. package/src/kernel/database/scoped-db.ts +0 -68
  71. package/src/kernel/database/tx-context.ts +0 -46
  72. package/src/kernel/error-mapper.ts +0 -15
  73. package/src/kernel/errors.ts +0 -89
  74. package/src/kernel/factory/repository-factory.ts +0 -242
  75. package/src/kernel/hooks/create-context.ts +0 -43
  76. package/src/kernel/hooks/executor.ts +0 -88
  77. package/src/kernel/hooks/registry.ts +0 -74
  78. package/src/kernel/hooks/types.ts +0 -52
  79. package/src/kernel/http-error.ts +0 -44
  80. package/src/kernel/jobs/adapter.ts +0 -36
  81. package/src/kernel/jobs/drizzle-adapter.ts +0 -58
  82. package/src/kernel/jobs/runner.ts +0 -153
  83. package/src/kernel/jobs/schema.ts +0 -46
  84. package/src/kernel/jobs/types.ts +0 -30
  85. package/src/kernel/local-api.ts +0 -185
  86. package/src/kernel/plugin/manifest.ts +0 -253
  87. package/src/kernel/query/executor.ts +0 -184
  88. package/src/kernel/query/registry.ts +0 -46
  89. package/src/kernel/result.ts +0 -33
  90. package/src/kernel/schema/extra-columns.ts +0 -37
  91. package/src/kernel/service-registry.ts +0 -76
  92. package/src/kernel/service-timing.ts +0 -89
  93. package/src/kernel/state-machine/machine.ts +0 -101
  94. package/src/modules/analytics/drizzle-adapter.ts +0 -426
  95. package/src/modules/analytics/hooks.ts +0 -11
  96. package/src/modules/analytics/models.ts +0 -125
  97. package/src/modules/analytics/repository/index.ts +0 -6
  98. package/src/modules/analytics/service.ts +0 -245
  99. package/src/modules/analytics/types.ts +0 -180
  100. package/src/modules/audit/hooks.ts +0 -78
  101. package/src/modules/audit/schema.ts +0 -33
  102. package/src/modules/audit/service.ts +0 -151
  103. package/src/modules/cart/access.ts +0 -27
  104. package/src/modules/cart/matcher.ts +0 -26
  105. package/src/modules/cart/repository/index.ts +0 -234
  106. package/src/modules/cart/schema.ts +0 -42
  107. package/src/modules/cart/schemas.ts +0 -38
  108. package/src/modules/cart/service.ts +0 -541
  109. package/src/modules/catalog/repository/index.ts +0 -772
  110. package/src/modules/catalog/schema.ts +0 -203
  111. package/src/modules/catalog/schemas.ts +0 -104
  112. package/src/modules/catalog/service.ts +0 -1544
  113. package/src/modules/customers/repository/index.ts +0 -327
  114. package/src/modules/customers/schema.ts +0 -64
  115. package/src/modules/customers/service.ts +0 -171
  116. package/src/modules/fulfillment/repository/index.ts +0 -426
  117. package/src/modules/fulfillment/schema.ts +0 -101
  118. package/src/modules/fulfillment/service.ts +0 -555
  119. package/src/modules/fulfillment/types.ts +0 -59
  120. package/src/modules/inventory/repository/index.ts +0 -509
  121. package/src/modules/inventory/schema.ts +0 -94
  122. package/src/modules/inventory/schemas.ts +0 -38
  123. package/src/modules/inventory/service.ts +0 -490
  124. package/src/modules/media/adapter.ts +0 -17
  125. package/src/modules/media/repository/index.ts +0 -274
  126. package/src/modules/media/schema.ts +0 -41
  127. package/src/modules/media/service.ts +0 -151
  128. package/src/modules/orders/repository/index.ts +0 -287
  129. package/src/modules/orders/schema.ts +0 -66
  130. package/src/modules/orders/service.ts +0 -619
  131. package/src/modules/orders/stale-order-cleanup.ts +0 -76
  132. package/src/modules/organization/service.ts +0 -191
  133. package/src/modules/payments/adapter.ts +0 -47
  134. package/src/modules/payments/repository/index.ts +0 -6
  135. package/src/modules/payments/service.ts +0 -107
  136. package/src/modules/pricing/repository/index.ts +0 -291
  137. package/src/modules/pricing/schema.ts +0 -71
  138. package/src/modules/pricing/schemas.ts +0 -38
  139. package/src/modules/pricing/service.ts +0 -494
  140. package/src/modules/promotions/repository/index.ts +0 -325
  141. package/src/modules/promotions/schema.ts +0 -62
  142. package/src/modules/promotions/schemas.ts +0 -38
  143. package/src/modules/promotions/service.ts +0 -598
  144. package/src/modules/search/adapter.ts +0 -57
  145. package/src/modules/search/hooks.ts +0 -12
  146. package/src/modules/search/repository/index.ts +0 -6
  147. package/src/modules/search/service.ts +0 -315
  148. package/src/modules/shipping/calculator.ts +0 -188
  149. package/src/modules/shipping/repository/index.ts +0 -6
  150. package/src/modules/shipping/service.ts +0 -51
  151. package/src/modules/tax/adapter.ts +0 -60
  152. package/src/modules/tax/repository/index.ts +0 -6
  153. package/src/modules/tax/service.ts +0 -53
  154. package/src/modules/webhooks/hook.ts +0 -34
  155. package/src/modules/webhooks/repository/index.ts +0 -278
  156. package/src/modules/webhooks/schema.ts +0 -56
  157. package/src/modules/webhooks/service.ts +0 -117
  158. package/src/modules/webhooks/signing.ts +0 -6
  159. package/src/modules/webhooks/ssrf-guard.ts +0 -71
  160. package/src/modules/webhooks/tasks.ts +0 -52
  161. package/src/modules/webhooks/worker.ts +0 -134
  162. package/src/runtime/commerce.ts +0 -145
  163. package/src/runtime/kernel.ts +0 -419
  164. package/src/runtime/logger.ts +0 -36
  165. package/src/runtime/server.ts +0 -349
  166. package/src/runtime/shutdown.ts +0 -43
  167. package/src/test-utils/create-pglite-adapter.ts +0 -129
  168. package/src/test-utils/create-plugin-test-app.ts +0 -128
  169. package/src/test-utils/create-repository-test-harness.ts +0 -16
  170. package/src/test-utils/create-test-config.ts +0 -190
  171. package/src/test-utils/create-test-kernel.ts +0 -7
  172. package/src/test-utils/create-test-plugin-context.ts +0 -75
  173. package/src/test-utils/rest-api-test-utils.ts +0 -265
  174. package/src/test-utils/test-actors.ts +0 -62
  175. package/src/test-utils/typed-hooks.ts +0 -54
  176. package/src/types/commerce-types.ts +0 -34
  177. package/src/utils/id.ts +0 -3
  178. package/src/utils/logger.ts +0 -18
  179. package/src/utils/pagination.ts +0 -22
package/src/auth/org.ts DELETED
@@ -1,41 +0,0 @@
1
- import { OrganizationService } from "../modules/organization/service.js";
2
-
3
- /**
4
- * Deterministic ID for the default organization.
5
- * Auto-created on kernel boot / test setup for single-tenant deployments.
6
- */
7
- export const DEFAULT_ORG_ID = "org_default";
8
-
9
- /**
10
- * Extracts the organization ID from an actor.
11
- * Falls back to the default org when the actor is null or has no org set.
12
- */
13
- export function resolveOrgId(actor: unknown): string {
14
- if (actor != null && typeof actor === "object" && "organizationId" in actor) {
15
- const orgId = (actor as { organizationId: unknown }).organizationId;
16
- if (typeof orgId === "string") return orgId;
17
- }
18
- return DEFAULT_ORG_ID;
19
- }
20
-
21
- /**
22
- * Ensures the default organization row exists in the database.
23
- * Idempotent — safe to call multiple times (no-ops if the row exists).
24
- *
25
- * Uses OrganizationService which creates both the org row and
26
- * (when auth is available) a member record for the creator.
27
- *
28
- * Accepts `unknown` so callers never need casts — the function
29
- * handles the Drizzle type narrowing internally.
30
- */
31
- export async function ensureDefaultOrg(
32
- db: unknown,
33
- storeName = "Default Store",
34
- ): Promise<void> {
35
- const orgService = new OrganizationService(db);
36
- await orgService.create({
37
- id: DEFAULT_ORG_ID,
38
- name: storeName,
39
- slug: "default",
40
- });
41
- }
@@ -1,28 +0,0 @@
1
- import { CommerceForbiddenError } from "../kernel/errors.js";
2
- import type { Actor } from "./types.js";
3
-
4
- export function assertPermission(actor: Actor | null, required: string): void {
5
- if (!actor) {
6
- throw new CommerceForbiddenError("Authentication required.");
7
- }
8
-
9
- if (actor.permissions.includes("*:*")) return;
10
-
11
- const [resource] = required.split(":");
12
- if (resource && actor.permissions.includes(`${resource}:*`)) return;
13
- if (actor.permissions.includes(required)) return;
14
-
15
- throw new CommerceForbiddenError(
16
- `Permission "${required}" is required. Your role "${actor.role}" does not include this permission.`,
17
- );
18
- }
19
-
20
- export function assertOwnership(actor: Actor | null, resourceOwnerId: string | null): void {
21
- if (!actor) {
22
- throw new CommerceForbiddenError("Authentication required.");
23
- }
24
- if (actor.permissions.includes("*:*")) return;
25
- if (actor.userId !== resourceOwnerId) {
26
- throw new CommerceForbiddenError("You do not have access to this resource.");
27
- }
28
- }
package/src/auth/setup.ts DELETED
@@ -1,165 +0,0 @@
1
- import { drizzleAdapter } from "@better-auth/drizzle-adapter";
2
- import { betterAuth } from "better-auth";
3
- import type { Role } from "better-auth/plugins/access";
4
- import { apiKey } from "@better-auth/api-key";
5
- import { organization, twoFactor, phoneNumber } from "better-auth/plugins";
6
- import type { CommerceConfig } from "../config/types.js";
7
- import type { DatabaseAdapter } from "../kernel/database/adapter.js";
8
- import * as authSchema from "./auth-schema.js";
9
-
10
- type BetterAuthDbProvider = "pg" | "mysql" | "sqlite";
11
-
12
- function resolveAuthDbProvider(provider: string): BetterAuthDbProvider {
13
- if (
14
- provider === "postgres" ||
15
- provider === "postgresql" ||
16
- provider === "pg"
17
- ) {
18
- return "pg";
19
- }
20
- if (provider === "mysql") {
21
- return "mysql";
22
- }
23
- if (provider === "sqlite") {
24
- return "sqlite";
25
- }
26
- throw new Error(
27
- `Unsupported auth database provider "${provider}". Expected one of: postgres, mysql, sqlite.`,
28
- );
29
- }
30
-
31
- interface AuthEmailPayload {
32
- user: {
33
- email: string;
34
- name: string | null;
35
- };
36
- url: string;
37
- }
38
-
39
- export interface AuthInstance {
40
- handler(request: Request): Promise<Response>;
41
- api: {
42
- getSession(input: { headers: Headers }): Promise<unknown>;
43
- getActiveMemberRole?: (input: { headers: Headers }) => Promise<unknown>;
44
- verifyApiKey?: (input: {
45
- body: { key: string; permissions?: Record<string, string[]> };
46
- }) => Promise<{
47
- valid: boolean;
48
- error: { message: string; code: string } | null;
49
- key: Record<string, unknown> | null;
50
- }>;
51
- createApiKey?: (input: {
52
- body: {
53
- name?: string;
54
- permissions?: Record<string, string[]>;
55
- userId?: string;
56
- };
57
- headers?: Headers;
58
- }) => Promise<{ key: string; id: string }>;
59
- /** Allow access to other Better Auth API methods added by plugins */
60
- [key: string]: unknown;
61
- };
62
- options?: Record<string, unknown>;
63
- $context?: Promise<unknown>;
64
- }
65
-
66
- export function createAuth(
67
- db: DatabaseAdapter,
68
- config: CommerceConfig,
69
- ): AuthInstance {
70
- const plugins: Array<
71
- | ReturnType<typeof organization>
72
- | ReturnType<typeof twoFactor>
73
- | ReturnType<typeof apiKey>
74
- | ReturnType<typeof phoneNumber>
75
- > = [
76
- organization({
77
- roles: (config.auth?.roles ?? {}) as unknown as Record<
78
- string,
79
- Role | undefined
80
- >,
81
- }),
82
- ];
83
-
84
- if (config.auth?.twoFactor?.enabled) {
85
- plugins.push(twoFactor({ issuer: config.storeName ?? "UnifiedCommerce" }));
86
- }
87
-
88
- if (config.auth?.apiKeys?.enabled) {
89
- plugins.push(apiKey());
90
- }
91
-
92
- if (config.auth?.phoneAuth) {
93
- plugins.push(phoneNumber({
94
- sendOTP: config.auth.phoneAuth.sendOTP,
95
- verifyOTP: config.auth.phoneAuth.verifyOTP,
96
- otpLength: config.auth.phoneAuth.otpLength ?? 6,
97
- expiresIn: config.auth.phoneAuth.expiresIn ?? 300,
98
- signUpOnVerification: config.auth.phoneAuth.signUpOnVerification ?? {
99
- getTempEmail: (phone: string) => `${phone.replace(/\+/g, "")}@phone.local`,
100
- },
101
- }));
102
- }
103
-
104
- // API key support can be attached via external plugin package in newer better-auth versions.
105
-
106
- try {
107
- const auth = betterAuth({
108
- database: drizzleAdapter(db.db as Record<string, unknown>, {
109
- provider: resolveAuthDbProvider(db.provider),
110
- schema: authSchema,
111
- }),
112
- trustedOrigins: config.auth?.trustedOrigins ?? [],
113
- emailAndPassword: {
114
- enabled: true,
115
- requireEmailVerification: config.auth?.requireEmailVerification ?? true,
116
- sendResetPassword: async ({ user, url }: AuthEmailPayload) => {
117
- if (!config.email) return;
118
- await config.email.send({
119
- template: "password-reset",
120
- to: user.email,
121
- data: { resetUrl: url, userName: user.name },
122
- });
123
- },
124
- sendVerificationEmail: async ({ user, url }: AuthEmailPayload) => {
125
- if (!config.email) return;
126
- await config.email.send({
127
- template: "email-verification",
128
- to: user.email,
129
- data: { verifyUrl: url, userName: user.name },
130
- });
131
- },
132
- },
133
- socialProviders: config.auth?.socialProviders ?? {},
134
- session: {
135
- expiresIn: config.auth?.sessionDuration ?? 60 * 60 * 24 * 7,
136
- updateAge: 60 * 60 * 24,
137
- cookieCache: {
138
- enabled: true,
139
- maxAge: 60 * 5, // 5 minute cookie cache for performance
140
- },
141
- },
142
- advanced: {
143
- cookiePrefix: "uc",
144
- useSecureCookies: process.env.NODE_ENV === "production",
145
- },
146
- plugins,
147
- user: {
148
- additionalFields: {
149
- vendorId: { type: "string", required: false },
150
- posOperatorPin: { type: "string", required: false },
151
- },
152
- },
153
- });
154
- // Better Auth's plugin-extended return type doesn't structurally overlap
155
- // with our simplified AuthInstance interface. The double-cast bridges the
156
- // generic plugin union with our narrowed AuthInstance shape.
157
- return auth as unknown as AuthInstance;
158
- } catch (error) {
159
- const message =
160
- error instanceof Error
161
- ? error.message
162
- : "Unknown better-auth initialization error.";
163
- throw new Error(`Failed to initialize authentication: ${message}`);
164
- }
165
- }
@@ -1,19 +0,0 @@
1
- import type { Actor } from "./types.js";
2
- import { DEFAULT_ORG_ID } from "./org.js";
3
-
4
- /**
5
- * Creates a system actor for internal operations (webhooks, jobs, compensation chains).
6
- * System actors have full permissions and are scoped to a specific organization.
7
- */
8
- export function createSystemActor(orgId: string = DEFAULT_ORG_ID): Actor {
9
- return {
10
- type: "api_key",
11
- userId: "system:internal",
12
- email: null,
13
- name: "System",
14
- vendorId: null,
15
- organizationId: orgId,
16
- role: "system",
17
- permissions: ["*:*"],
18
- };
19
- }
package/src/auth/types.ts DELETED
@@ -1,10 +0,0 @@
1
- export interface Actor {
2
- type: "user" | "api_key";
3
- userId: string;
4
- email: string | null;
5
- name: string;
6
- vendorId: string | null;
7
- organizationId: string | null;
8
- role: string;
9
- permissions: string[];
10
- }
@@ -1,82 +0,0 @@
1
- import type { CommerceConfig } from "./types.js";
2
-
3
- export const defaultConfig: Partial<CommerceConfig> = {
4
- version: "0.0.1",
5
- auth: {
6
- requireEmailVerification: true,
7
- sessionDuration: 60 * 60 * 24 * 7,
8
- twoFactor: { enabled: false },
9
- apiKeys: { enabled: false },
10
- posPin: { enabled: false },
11
- roles: {
12
- owner: { permissions: ["*:*"] },
13
- admin: { permissions: ["*:*"] },
14
- manager: {
15
- permissions: [
16
- "catalog:create",
17
- "catalog:update",
18
- "catalog:delete",
19
- "catalog:read",
20
- "inventory:read",
21
- "inventory:adjust",
22
- "orders:read",
23
- "orders:update",
24
- "cart:create",
25
- "cart:update",
26
- "customers:read:self",
27
- "customers:update:self",
28
- ],
29
- },
30
- customer: {
31
- permissions: [
32
- "catalog:read",
33
- "cart:create",
34
- "cart:read",
35
- "cart:update",
36
- "orders:create",
37
- "orders:read:own",
38
- "customers:read:self",
39
- "customers:update:self",
40
- ],
41
- },
42
- },
43
- customerPermissions: [
44
- "catalog:read",
45
- "cart:create",
46
- "cart:read",
47
- "cart:update",
48
- "orders:create",
49
- "orders:read:own",
50
- "customers:read:self",
51
- "customers:update:self",
52
- ],
53
- },
54
- cart: {
55
- ttlMinutes: 60 * 24 * 7,
56
- hooks: {},
57
- },
58
- checkout: {
59
- hooks: {
60
- beforeCreate: [],
61
- afterCreate: [],
62
- },
63
- },
64
- orders: {
65
- hooks: {
66
- beforeCreate: [],
67
- afterCreate: [],
68
- beforeStatusChange: [],
69
- afterStatusChange: [],
70
- beforeDelete: [],
71
- },
72
- },
73
- inventory: {
74
- hooks: {
75
- afterAdjust: [],
76
- },
77
- },
78
- mcp: {
79
- enabled: true,
80
- capabilities: ["catalog:read", "orders:read", "inventory:read"],
81
- },
82
- };
@@ -1,53 +0,0 @@
1
- import { defaultConfig } from "./defaults.js";
2
- import type { CommerceConfig, DefineConfigInput } from "./types.js";
3
- import { _resetRegisteredPlugins } from "../kernel/plugin/manifest.js";
4
-
5
- function isRecord(value: unknown): value is Record<string, unknown> {
6
- return Boolean(value) && typeof value === "object" && !Array.isArray(value);
7
- }
8
-
9
- function merge<T extends object>(base: T, next: Partial<T>): T {
10
- const output: Record<string, unknown> = {
11
- ...(base as Record<string, unknown>),
12
- };
13
- for (const [key, value] of Object.entries(next as Record<string, unknown>)) {
14
- if (value === undefined) continue;
15
- const baseValue = output[key];
16
- if (
17
- isRecord(value) &&
18
- isRecord(baseValue)
19
- ) {
20
- output[key] = merge(baseValue, value);
21
- } else {
22
- output[key] = value;
23
- }
24
- }
25
- return output as T;
26
- }
27
-
28
- /**
29
- * Builds the final CommerceConfig by:
30
- * 1. Merging user input with defaults
31
- * 2. Applying all plugins (each is a config transform function)
32
- * 3. Freezing the result to prevent runtime mutation
33
- */
34
- export async function defineConfig(
35
- input: DefineConfigInput,
36
- ): Promise<CommerceConfig> {
37
- let config = merge(defaultConfig as CommerceConfig, input);
38
-
39
- // Merge top-level `schema` into `customSchemas` before plugins run
40
- if (config.schema?.length) {
41
- config = {
42
- ...config,
43
- customSchemas: [...(config.customSchemas ?? []), ...config.schema],
44
- };
45
- }
46
-
47
- _resetRegisteredPlugins();
48
- for (const plugin of config.plugins ?? []) {
49
- config = await plugin(config);
50
- }
51
-
52
- return Object.freeze(config);
53
- }
@@ -1,299 +0,0 @@
1
- import type { Hono, MiddlewareHandler } from "hono";
2
- import type { Actor } from "../auth/types.js";
3
- import type { BeforeHook, AfterHook } from "../kernel/hooks/types.js";
4
- import type { PaymentAdapter } from "../modules/payments/adapter.js";
5
- import type { StorageAdapter } from "../modules/media/adapter.js";
6
- import type { DatabaseAdapter } from "../kernel/database/adapter.js";
7
- import type { TaxAdapter } from "../modules/tax/adapter.js";
8
- import type { SearchAdapter } from "../modules/search/adapter.js";
9
- import type { JobsAdapter } from "../kernel/jobs/adapter.js";
10
- import type { TaskDefinition } from "../kernel/jobs/types.js";
11
-
12
- export interface RoleDefinition {
13
- permissions: string[];
14
- }
15
-
16
- export type FieldType = "text" | "number" | "boolean" | "date" | "json" | "relation" | "select";
17
-
18
- export interface EntityFieldDefinition {
19
- name: string;
20
- type: FieldType;
21
- unit?: string;
22
- schema?: unknown;
23
- target?: string;
24
- options?: string[];
25
- }
26
-
27
- export interface EntityVariantConfig {
28
- enabled: boolean;
29
- optionTypes?: string[];
30
- }
31
-
32
- export interface EntityHooks {
33
- beforeCreate?: BeforeHook<unknown>[];
34
- afterCreate?: AfterHook<unknown>[];
35
- beforeUpdate?: BeforeHook<unknown>[];
36
- afterUpdate?: AfterHook<unknown>[];
37
- beforeDelete?: BeforeHook<unknown>[];
38
- afterDelete?: AfterHook<unknown>[];
39
- beforeRead?: BeforeHook<unknown>[];
40
- afterRead?: AfterHook<unknown>[];
41
- beforeList?: BeforeHook<unknown>[];
42
- afterList?: AfterHook<unknown>[];
43
- }
44
-
45
- export interface EntityConfig {
46
- fields: EntityFieldDefinition[];
47
- variants: EntityVariantConfig;
48
- fulfillment: string;
49
- hooks?: EntityHooks;
50
- }
51
-
52
- export interface AuthConfig {
53
- requireEmailVerification?: boolean;
54
- sessionDuration?: number;
55
- socialProviders?: Record<string, { clientId: string; clientSecret: string }>;
56
- twoFactor?: { enabled: boolean; requiredForRoles?: string[] };
57
- apiKeys?: {
58
- enabled: boolean;
59
- /** Default permissions for API keys that don't specify their own. */
60
- defaultPermissions?: string[];
61
- };
62
- posPin?: { enabled: boolean };
63
- roles?: Record<string, RoleDefinition>;
64
- customerPermissions?: string[];
65
- /** Origins allowed for CSRF protection (Better Auth `trustedOrigins`). */
66
- trustedOrigins?: string[];
67
- /** Enable a config-driven dev API key. OFF by default. Only for local development. */
68
- enableDevKey?: boolean;
69
- /** Custom dev key value. Must be set alongside enableDevKey. */
70
- devKey?: string;
71
- /**
72
- * Phone number OTP authentication via Better Auth's phoneNumber plugin.
73
- * When configured, users can sign in/up with phone + OTP instead of email/password.
74
- * You provide the SMS delivery callback; Better Auth handles OTP generation,
75
- * storage, expiry, brute force protection, and session creation.
76
- */
77
- phoneAuth?: {
78
- /** Send OTP to the phone number. Implement with Twilio, AWS SNS, or any SMS gateway. */
79
- sendOTP: (params: { phoneNumber: string; code: string }, ctx: unknown) => void | Promise<void>;
80
- /** Optional custom OTP verification (e.g., Twilio Verify). Overrides internal logic. */
81
- verifyOTP?: (params: { phoneNumber: string; code: string }, ctx: unknown) => boolean | Promise<boolean>;
82
- /** OTP length. Default: 6. */
83
- otpLength?: number;
84
- /** OTP expiry in seconds. Default: 300 (5 minutes). */
85
- expiresIn?: number;
86
- /** Auto-create user on first OTP verification. Default: generates temp email from phone. */
87
- signUpOnVerification?: {
88
- getTempEmail: (phoneNumber: string) => string;
89
- getTempName?: (phoneNumber: string) => string;
90
- };
91
- };
92
- }
93
-
94
- export interface CartConfig {
95
- ttlMinutes?: number;
96
- hooks?: {
97
- beforeAddItem?: BeforeHook<unknown>[];
98
- afterAddItem?: AfterHook<unknown>[];
99
- beforeRemoveItem?: BeforeHook<unknown>[];
100
- afterRemoveItem?: AfterHook<unknown>[];
101
- beforeUpdateQuantity?: BeforeHook<unknown>[];
102
- afterUpdateQuantity?: AfterHook<unknown>[];
103
- };
104
- }
105
-
106
- export interface CheckoutConfig {
107
- hooks?: {
108
- beforeCreate?: BeforeHook<unknown>[];
109
- afterCreate?: AfterHook<unknown>[];
110
- };
111
- }
112
-
113
- export interface OrdersConfig {
114
- hooks?: {
115
- beforeCreate?: BeforeHook<unknown>[];
116
- afterCreate?: AfterHook<unknown>[];
117
- beforeStatusChange?: BeforeHook<unknown>[];
118
- afterStatusChange?: AfterHook<unknown>[];
119
- afterGet?: AfterHook<unknown>[];
120
- beforeDelete?: BeforeHook<unknown>[];
121
- };
122
- /**
123
- * Extend the order state machine with custom transitions.
124
- * New states (e.g., "payment_initiated", "shipped", "delivered", "defaulted")
125
- * are added to the default machine. Existing transitions are preserved.
126
- * See extendOrderStateMachine() for the merge logic.
127
- */
128
- customTransitions?: Record<string, string[]>;
129
- }
130
-
131
- export interface InventoryConfig {
132
- hooks?: {
133
- afterAdjust?: AfterHook<unknown>[];
134
- };
135
- }
136
-
137
- export interface ShippingConfig {
138
- type: "flat" | "weight_based";
139
- flatRate: number;
140
- freeShippingThreshold?: number;
141
- brackets: Array<{ upToGrams: number; cost: number }>;
142
- fallbackCost: number;
143
- }
144
-
145
- export interface TaxConfig {
146
- adapter?: TaxAdapter;
147
- defaultFromAddress?: {
148
- country: string;
149
- postalCode: string;
150
- state?: string;
151
- city?: string;
152
- line1?: string;
153
- };
154
- }
155
-
156
- export interface AnalyticsConfig {
157
- customSchemaPath?: string;
158
- models?: unknown[];
159
- }
160
-
161
- export interface SearchConfig {
162
- adapter?: SearchAdapter;
163
- defaultFacets?: string[];
164
- }
165
-
166
- export interface MCPTool {
167
- name: string;
168
- description: string;
169
- inputSchema?: Record<string, unknown>;
170
- handler: (params: unknown) => Promise<unknown>;
171
- }
172
-
173
- export interface MCPResource {
174
- uri: string;
175
- name: string;
176
- description: string;
177
- mimeType: string;
178
- handler: () => Promise<{ content: Array<{ type: "text"; text: string }> }>;
179
- }
180
-
181
- /**
182
- * A CommercePlugin is a config transform function (PayloadCMS pattern).
183
- * Receives the current config, returns the modified config.
184
- * All plugins — simple or complex — are just functions.
185
- *
186
- * Use `defineCommercePlugin()` for a structured way to build plugins,
187
- * or write a raw transform function for full control.
188
- */
189
- export type CommercePlugin = (
190
- config: CommerceConfig,
191
- ) => CommerceConfig | Promise<CommerceConfig>;
192
-
193
- export interface CommerceConfig {
194
- storeName?: string;
195
- version?: string;
196
- database: {
197
- provider: "postgresql";
198
- options?: Record<string, unknown>;
199
- };
200
- databaseAdapter?: DatabaseAdapter;
201
- auth?: AuthConfig;
202
- entities?: Record<string, EntityConfig>;
203
- cart?: CartConfig;
204
- checkout?: CheckoutConfig;
205
- orders?: OrdersConfig;
206
- inventory?: InventoryConfig;
207
- shipping?: ShippingConfig;
208
- payments?: PaymentAdapter[];
209
- storage?: StorageAdapter;
210
- email?: {
211
- send(input: {
212
- template: string;
213
- to: string;
214
- data?: Record<string, unknown>;
215
- }): Promise<void>;
216
- };
217
- tax?: TaxConfig;
218
- analytics?: AnalyticsConfig;
219
- search?: SearchConfig;
220
- mcp?: {
221
- enabled?: boolean;
222
- capabilities?: string[];
223
- };
224
- jobs?: {
225
- adapter?: JobsAdapter;
226
- tasks?: TaskDefinition[];
227
- autorun?: {
228
- enabled: boolean;
229
- intervalMs?: number;
230
- };
231
- };
232
- /**
233
- * Additional Drizzle table definitions — new tables or extended core tables.
234
- * Each entry is an object of `{ exportName: pgTable(...) }`.
235
- *
236
- * These are merged with core schema by `buildSchema(config)` and must also
237
- * be listed in the app's `drizzle.config.ts` for `db:push` / `db:generate`.
238
- *
239
- * Plugins push into this array automatically via `defineCommercePlugin({ schema })`.
240
- * Apps can also add entries directly — no plugin wrapper needed:
241
- *
242
- * ```ts
243
- * import { reviewsTable } from "./schema/reviews.js";
244
- * import { extendedProducts } from "./schema/extended-products.js";
245
- *
246
- * defineConfig({
247
- * schema: [
248
- * { reviewsTable },
249
- * { extendedProducts },
250
- * ],
251
- * // ...
252
- * });
253
- * ```
254
- */
255
- schema?: Array<Record<string, unknown>>;
256
- /** @internal Merged from `schema` + plugin schemas. Use `schema` instead. */
257
- customSchemas?: Array<Record<string, unknown>>;
258
- hooks?: Record<string, Array<(...args: unknown[]) => unknown>>;
259
- plugins?: CommercePlugin[];
260
- middleware?: MiddlewareHandler[];
261
- routes?: (app: Hono, kernel: unknown) => void;
262
- mcpTools?: (kernel: unknown) => MCPTool[];
263
- /** Log level for structured logging. Default: "info". */
264
- logLevel?: "fatal" | "error" | "warn" | "info" | "debug" | "trace";
265
- /**
266
- * Expose the OpenAPI spec (`/api/doc`) and Swagger UI (`/api/reference`).
267
- * Default: `true` in development, `false` in production.
268
- */
269
- exposeOpenApiSpec?: boolean;
270
- /** Rate limiting overrides. */
271
- rateLimits?: {
272
- /** Requests per minute for general API. Default: 100. */
273
- api?: number;
274
- /** Requests per minute for auth endpoints. Default: 10. */
275
- auth?: number;
276
- /** Requests per minute for checkout. Default: 5. */
277
- checkout?: number;
278
- };
279
- }
280
-
281
- export interface DefineConfigInput extends CommerceConfig {}
282
-
283
- export interface AuthSessionLike {
284
- user: {
285
- id: string;
286
- email?: string | null;
287
- name?: string | null;
288
- vendorId?: string | null;
289
- };
290
- session: {
291
- activeOrganizationId?: string | null;
292
- activeOrganizationRole?: string | null;
293
- };
294
- }
295
-
296
- export interface KernelFactoryContext {
297
- config: CommerceConfig;
298
- actor: Actor | null;
299
- }