@unifiedcommerce/core 0.1.0 → 0.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.
Files changed (245) hide show
  1. package/dist/auth/setup.d.ts.map +1 -1
  2. package/dist/auth/setup.js +8 -3
  3. package/dist/config/types.d.ts +3 -1
  4. package/dist/config/types.d.ts.map +1 -1
  5. package/dist/index.d.ts +1 -0
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +1 -0
  8. package/dist/interfaces/mcp/server.d.ts +3 -5
  9. package/dist/interfaces/mcp/server.d.ts.map +1 -1
  10. package/dist/interfaces/mcp/server.js +25 -510
  11. package/dist/interfaces/mcp/tool-builder.d.ts +120 -0
  12. package/dist/interfaces/mcp/tool-builder.d.ts.map +1 -0
  13. package/dist/interfaces/mcp/tool-builder.js +224 -0
  14. package/dist/interfaces/mcp/tools/analytics.d.ts +42 -0
  15. package/dist/interfaces/mcp/tools/analytics.d.ts.map +1 -0
  16. package/dist/interfaces/mcp/tools/analytics.js +70 -0
  17. package/dist/interfaces/mcp/tools/cart.d.ts +14 -0
  18. package/dist/interfaces/mcp/tools/cart.d.ts.map +1 -0
  19. package/dist/interfaces/mcp/tools/cart.js +47 -0
  20. package/dist/interfaces/mcp/tools/catalog.d.ts +53 -0
  21. package/dist/interfaces/mcp/tools/catalog.d.ts.map +1 -0
  22. package/dist/interfaces/mcp/tools/catalog.js +284 -0
  23. package/dist/interfaces/mcp/tools/index.d.ts +3 -0
  24. package/dist/interfaces/mcp/tools/index.d.ts.map +1 -0
  25. package/dist/interfaces/mcp/tools/index.js +20 -0
  26. package/dist/interfaces/mcp/tools/inventory.d.ts +27 -0
  27. package/dist/interfaces/mcp/tools/inventory.d.ts.map +1 -0
  28. package/dist/interfaces/mcp/tools/inventory.js +143 -0
  29. package/dist/interfaces/mcp/tools/orders.d.ts +18 -0
  30. package/dist/interfaces/mcp/tools/orders.d.ts.map +1 -0
  31. package/dist/interfaces/mcp/tools/orders.js +82 -0
  32. package/dist/interfaces/mcp/tools/pricing.d.ts +29 -0
  33. package/dist/interfaces/mcp/tools/pricing.d.ts.map +1 -0
  34. package/dist/interfaces/mcp/tools/pricing.js +90 -0
  35. package/dist/interfaces/mcp/tools/promotions.d.ts +44 -0
  36. package/dist/interfaces/mcp/tools/promotions.d.ts.map +1 -0
  37. package/dist/interfaces/mcp/tools/promotions.js +109 -0
  38. package/dist/interfaces/mcp/tools/registry.d.ts +32 -0
  39. package/dist/interfaces/mcp/tools/registry.d.ts.map +1 -0
  40. package/dist/interfaces/mcp/tools/registry.js +55 -0
  41. package/dist/interfaces/mcp/tools/search.d.ts +14 -0
  42. package/dist/interfaces/mcp/tools/search.d.ts.map +1 -0
  43. package/dist/interfaces/mcp/tools/search.js +39 -0
  44. package/dist/interfaces/mcp/tools/webhooks.d.ts +15 -0
  45. package/dist/interfaces/mcp/tools/webhooks.d.ts.map +1 -0
  46. package/dist/interfaces/mcp/tools/webhooks.js +48 -0
  47. package/dist/interfaces/mcp/transport.d.ts +17 -2
  48. package/dist/interfaces/mcp/transport.d.ts.map +1 -1
  49. package/dist/interfaces/mcp/transport.js +91 -44
  50. package/dist/interfaces/rest/router.d.ts.map +1 -1
  51. package/dist/interfaces/rest/routes/checkout.d.ts.map +1 -1
  52. package/dist/interfaces/rest/routes/checkout.js +1 -1
  53. package/dist/interfaces/rest/routes/promotions.d.ts.map +1 -1
  54. package/dist/interfaces/rest/routes/promotions.js +3 -2
  55. package/dist/kernel/database/adapter.d.ts +8 -0
  56. package/dist/kernel/database/adapter.d.ts.map +1 -1
  57. package/dist/kernel/factory/repository-factory.d.ts.map +1 -1
  58. package/dist/kernel/factory/repository-factory.js +3 -1
  59. package/dist/kernel/local-api.d.ts.map +1 -1
  60. package/dist/kernel/local-api.js +2 -0
  61. package/dist/kernel/plugin/manifest.d.ts +3 -3
  62. package/dist/kernel/plugin/manifest.d.ts.map +1 -1
  63. package/dist/kernel/plugin/manifest.js +36 -7
  64. package/dist/runtime/kernel.d.ts +1 -2
  65. package/dist/runtime/kernel.d.ts.map +1 -1
  66. package/dist/runtime/kernel.js +16 -8
  67. package/dist/runtime/server.d.ts.map +1 -1
  68. package/dist/runtime/server.js +8 -3
  69. package/dist/test-utils/create-pglite-adapter.d.ts.map +1 -1
  70. package/dist/test-utils/create-pglite-adapter.js +7 -6
  71. package/dist/tsconfig.tsbuildinfo +1 -0
  72. package/package.json +2 -2
  73. package/src/adapters/console-email.ts +0 -43
  74. package/src/auth/access.ts +0 -187
  75. package/src/auth/auth-schema.ts +0 -139
  76. package/src/auth/middleware.ts +0 -161
  77. package/src/auth/org.ts +0 -41
  78. package/src/auth/permissions.ts +0 -28
  79. package/src/auth/setup.ts +0 -169
  80. package/src/auth/system-actor.ts +0 -19
  81. package/src/auth/types.ts +0 -10
  82. package/src/config/defaults.ts +0 -82
  83. package/src/config/define-config.ts +0 -53
  84. package/src/config/types.ts +0 -299
  85. package/src/generated/plugin-capabilities.d.ts +0 -20
  86. package/src/generated/plugin-manifest.ts +0 -23
  87. package/src/generated/plugin-repositories.d.ts +0 -20
  88. package/src/hooks/checkout-completion.ts +0 -262
  89. package/src/hooks/checkout.ts +0 -677
  90. package/src/hooks/order-emails.ts +0 -62
  91. package/src/index.ts +0 -214
  92. package/src/interfaces/mcp/agent-prompt.ts +0 -174
  93. package/src/interfaces/mcp/context-enrichment.ts +0 -177
  94. package/src/interfaces/mcp/server.ts +0 -617
  95. package/src/interfaces/mcp/transport.ts +0 -68
  96. package/src/interfaces/rest/customer-portal.ts +0 -299
  97. package/src/interfaces/rest/index.ts +0 -74
  98. package/src/interfaces/rest/router.ts +0 -334
  99. package/src/interfaces/rest/routes/admin-jobs.ts +0 -58
  100. package/src/interfaces/rest/routes/audit.ts +0 -50
  101. package/src/interfaces/rest/routes/carts.ts +0 -89
  102. package/src/interfaces/rest/routes/catalog.ts +0 -493
  103. package/src/interfaces/rest/routes/checkout.ts +0 -283
  104. package/src/interfaces/rest/routes/inventory.ts +0 -70
  105. package/src/interfaces/rest/routes/media.ts +0 -86
  106. package/src/interfaces/rest/routes/orders.ts +0 -78
  107. package/src/interfaces/rest/routes/payments.ts +0 -60
  108. package/src/interfaces/rest/routes/pricing.ts +0 -57
  109. package/src/interfaces/rest/routes/promotions.ts +0 -92
  110. package/src/interfaces/rest/routes/search.ts +0 -71
  111. package/src/interfaces/rest/routes/webhooks.ts +0 -46
  112. package/src/interfaces/rest/schemas/admin-jobs.ts +0 -40
  113. package/src/interfaces/rest/schemas/audit.ts +0 -46
  114. package/src/interfaces/rest/schemas/carts.ts +0 -125
  115. package/src/interfaces/rest/schemas/catalog.ts +0 -450
  116. package/src/interfaces/rest/schemas/checkout.ts +0 -66
  117. package/src/interfaces/rest/schemas/customer-portal.ts +0 -195
  118. package/src/interfaces/rest/schemas/inventory.ts +0 -138
  119. package/src/interfaces/rest/schemas/media.ts +0 -75
  120. package/src/interfaces/rest/schemas/orders.ts +0 -104
  121. package/src/interfaces/rest/schemas/pricing.ts +0 -80
  122. package/src/interfaces/rest/schemas/promotions.ts +0 -110
  123. package/src/interfaces/rest/schemas/responses.ts +0 -85
  124. package/src/interfaces/rest/schemas/search.ts +0 -58
  125. package/src/interfaces/rest/schemas/shared.ts +0 -62
  126. package/src/interfaces/rest/schemas/webhooks.ts +0 -68
  127. package/src/interfaces/rest/utils.ts +0 -104
  128. package/src/interfaces/rest/webhook-router.ts +0 -50
  129. package/src/kernel/compensation/executor.ts +0 -61
  130. package/src/kernel/compensation/types.ts +0 -26
  131. package/src/kernel/database/adapter.ts +0 -13
  132. package/src/kernel/database/drizzle-db.ts +0 -56
  133. package/src/kernel/database/migrate.ts +0 -76
  134. package/src/kernel/database/plugin-types.ts +0 -34
  135. package/src/kernel/database/schema.ts +0 -49
  136. package/src/kernel/database/scoped-db.ts +0 -68
  137. package/src/kernel/database/tx-context.ts +0 -46
  138. package/src/kernel/error-mapper.ts +0 -15
  139. package/src/kernel/errors.ts +0 -89
  140. package/src/kernel/factory/repository-factory.ts +0 -242
  141. package/src/kernel/hooks/create-context.ts +0 -43
  142. package/src/kernel/hooks/executor.ts +0 -88
  143. package/src/kernel/hooks/registry.ts +0 -74
  144. package/src/kernel/hooks/types.ts +0 -52
  145. package/src/kernel/http-error.ts +0 -44
  146. package/src/kernel/jobs/adapter.ts +0 -36
  147. package/src/kernel/jobs/drizzle-adapter.ts +0 -58
  148. package/src/kernel/jobs/runner.ts +0 -153
  149. package/src/kernel/jobs/schema.ts +0 -46
  150. package/src/kernel/jobs/types.ts +0 -30
  151. package/src/kernel/local-api.ts +0 -185
  152. package/src/kernel/plugin/manifest.ts +0 -253
  153. package/src/kernel/query/executor.ts +0 -184
  154. package/src/kernel/query/registry.ts +0 -46
  155. package/src/kernel/result.ts +0 -33
  156. package/src/kernel/schema/extra-columns.ts +0 -37
  157. package/src/kernel/service-registry.ts +0 -76
  158. package/src/kernel/service-timing.ts +0 -89
  159. package/src/kernel/state-machine/machine.ts +0 -101
  160. package/src/modules/analytics/drizzle-adapter.ts +0 -426
  161. package/src/modules/analytics/hooks.ts +0 -11
  162. package/src/modules/analytics/models.ts +0 -125
  163. package/src/modules/analytics/repository/index.ts +0 -6
  164. package/src/modules/analytics/service.ts +0 -245
  165. package/src/modules/analytics/types.ts +0 -180
  166. package/src/modules/audit/hooks.ts +0 -78
  167. package/src/modules/audit/schema.ts +0 -33
  168. package/src/modules/audit/service.ts +0 -151
  169. package/src/modules/cart/access.ts +0 -27
  170. package/src/modules/cart/matcher.ts +0 -26
  171. package/src/modules/cart/repository/index.ts +0 -234
  172. package/src/modules/cart/schema.ts +0 -42
  173. package/src/modules/cart/schemas.ts +0 -38
  174. package/src/modules/cart/service.ts +0 -541
  175. package/src/modules/catalog/repository/index.ts +0 -772
  176. package/src/modules/catalog/schema.ts +0 -203
  177. package/src/modules/catalog/schemas.ts +0 -104
  178. package/src/modules/catalog/service.ts +0 -1544
  179. package/src/modules/customers/repository/index.ts +0 -327
  180. package/src/modules/customers/schema.ts +0 -64
  181. package/src/modules/customers/service.ts +0 -171
  182. package/src/modules/fulfillment/repository/index.ts +0 -426
  183. package/src/modules/fulfillment/schema.ts +0 -101
  184. package/src/modules/fulfillment/service.ts +0 -555
  185. package/src/modules/fulfillment/types.ts +0 -59
  186. package/src/modules/inventory/repository/index.ts +0 -509
  187. package/src/modules/inventory/schema.ts +0 -94
  188. package/src/modules/inventory/schemas.ts +0 -38
  189. package/src/modules/inventory/service.ts +0 -490
  190. package/src/modules/media/adapter.ts +0 -17
  191. package/src/modules/media/repository/index.ts +0 -274
  192. package/src/modules/media/schema.ts +0 -41
  193. package/src/modules/media/service.ts +0 -151
  194. package/src/modules/orders/repository/index.ts +0 -287
  195. package/src/modules/orders/schema.ts +0 -66
  196. package/src/modules/orders/service.ts +0 -619
  197. package/src/modules/orders/stale-order-cleanup.ts +0 -76
  198. package/src/modules/organization/service.ts +0 -191
  199. package/src/modules/payments/adapter.ts +0 -47
  200. package/src/modules/payments/repository/index.ts +0 -6
  201. package/src/modules/payments/service.ts +0 -107
  202. package/src/modules/pricing/repository/index.ts +0 -291
  203. package/src/modules/pricing/schema.ts +0 -71
  204. package/src/modules/pricing/schemas.ts +0 -38
  205. package/src/modules/pricing/service.ts +0 -494
  206. package/src/modules/promotions/repository/index.ts +0 -325
  207. package/src/modules/promotions/schema.ts +0 -62
  208. package/src/modules/promotions/schemas.ts +0 -38
  209. package/src/modules/promotions/service.ts +0 -598
  210. package/src/modules/search/adapter.ts +0 -57
  211. package/src/modules/search/hooks.ts +0 -12
  212. package/src/modules/search/repository/index.ts +0 -6
  213. package/src/modules/search/service.ts +0 -315
  214. package/src/modules/shipping/calculator.ts +0 -188
  215. package/src/modules/shipping/repository/index.ts +0 -6
  216. package/src/modules/shipping/service.ts +0 -51
  217. package/src/modules/tax/adapter.ts +0 -60
  218. package/src/modules/tax/repository/index.ts +0 -6
  219. package/src/modules/tax/service.ts +0 -53
  220. package/src/modules/webhooks/hook.ts +0 -34
  221. package/src/modules/webhooks/repository/index.ts +0 -278
  222. package/src/modules/webhooks/schema.ts +0 -56
  223. package/src/modules/webhooks/service.ts +0 -117
  224. package/src/modules/webhooks/signing.ts +0 -6
  225. package/src/modules/webhooks/ssrf-guard.ts +0 -71
  226. package/src/modules/webhooks/tasks.ts +0 -52
  227. package/src/modules/webhooks/worker.ts +0 -134
  228. package/src/runtime/commerce.ts +0 -145
  229. package/src/runtime/kernel.ts +0 -419
  230. package/src/runtime/logger.ts +0 -36
  231. package/src/runtime/server.ts +0 -349
  232. package/src/runtime/shutdown.ts +0 -43
  233. package/src/test-utils/create-pglite-adapter.ts +0 -129
  234. package/src/test-utils/create-plugin-test-app.ts +0 -128
  235. package/src/test-utils/create-repository-test-harness.ts +0 -16
  236. package/src/test-utils/create-test-config.ts +0 -190
  237. package/src/test-utils/create-test-kernel.ts +0 -7
  238. package/src/test-utils/create-test-plugin-context.ts +0 -75
  239. package/src/test-utils/rest-api-test-utils.ts +0 -265
  240. package/src/test-utils/test-actors.ts +0 -62
  241. package/src/test-utils/typed-hooks.ts +0 -54
  242. package/src/types/commerce-types.ts +0 -34
  243. package/src/utils/id.ts +0 -3
  244. package/src/utils/logger.ts +0 -18
  245. package/src/utils/pagination.ts +0 -22
package/src/auth/setup.ts DELETED
@@ -1,169 +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, jwt, bearer } 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
- | ReturnType<typeof jwt>
76
- | ReturnType<typeof bearer>
77
- > = [
78
- organization({
79
- roles: (config.auth?.roles ?? {}) as unknown as Record<
80
- string,
81
- Role | undefined
82
- >,
83
- }),
84
- bearer(),
85
- jwt(),
86
- ];
87
-
88
- if (config.auth?.twoFactor?.enabled) {
89
- plugins.push(twoFactor({ issuer: config.storeName ?? "UnifiedCommerce" }));
90
- }
91
-
92
- if (config.auth?.apiKeys?.enabled) {
93
- plugins.push(apiKey());
94
- }
95
-
96
- if (config.auth?.phoneAuth) {
97
- plugins.push(phoneNumber({
98
- sendOTP: config.auth.phoneAuth.sendOTP,
99
- verifyOTP: config.auth.phoneAuth.verifyOTP,
100
- otpLength: config.auth.phoneAuth.otpLength ?? 6,
101
- expiresIn: config.auth.phoneAuth.expiresIn ?? 300,
102
- signUpOnVerification: config.auth.phoneAuth.signUpOnVerification ?? {
103
- getTempEmail: (phone: string) => `${phone.replace(/\+/g, "")}@phone.local`,
104
- },
105
- }));
106
- }
107
-
108
- // API key support can be attached via external plugin package in newer better-auth versions.
109
-
110
- try {
111
- const auth = betterAuth({
112
- database: drizzleAdapter(db.db as Record<string, unknown>, {
113
- provider: resolveAuthDbProvider(db.provider),
114
- schema: authSchema,
115
- }),
116
- trustedOrigins: config.auth?.trustedOrigins ?? [],
117
- emailAndPassword: {
118
- enabled: true,
119
- requireEmailVerification: config.auth?.requireEmailVerification ?? true,
120
- sendResetPassword: async ({ user, url }: AuthEmailPayload) => {
121
- if (!config.email) return;
122
- await config.email.send({
123
- template: "password-reset",
124
- to: user.email,
125
- data: { resetUrl: url, userName: user.name },
126
- });
127
- },
128
- sendVerificationEmail: async ({ user, url }: AuthEmailPayload) => {
129
- if (!config.email) return;
130
- await config.email.send({
131
- template: "email-verification",
132
- to: user.email,
133
- data: { verifyUrl: url, userName: user.name },
134
- });
135
- },
136
- },
137
- socialProviders: config.auth?.socialProviders ?? {},
138
- session: {
139
- expiresIn: config.auth?.sessionDuration ?? 60 * 60 * 24 * 7,
140
- updateAge: 60 * 60 * 24,
141
- cookieCache: {
142
- enabled: true,
143
- maxAge: 60 * 5, // 5 minute cookie cache for performance
144
- },
145
- },
146
- advanced: {
147
- cookiePrefix: "uc",
148
- useSecureCookies: process.env.NODE_ENV === "production",
149
- },
150
- plugins,
151
- user: {
152
- additionalFields: {
153
- vendorId: { type: "string", required: false },
154
- posOperatorPin: { type: "string", required: false },
155
- },
156
- },
157
- });
158
- // Better Auth's plugin-extended return type doesn't structurally overlap
159
- // with our simplified AuthInstance interface. The double-cast bridges the
160
- // generic plugin union with our narrowed AuthInstance shape.
161
- return auth as unknown as AuthInstance;
162
- } catch (error) {
163
- const message =
164
- error instanceof Error
165
- ? error.message
166
- : "Unknown better-auth initialization error.";
167
- throw new Error(`Failed to initialize authentication: ${message}`);
168
- }
169
- }
@@ -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
- }
@@ -1,20 +0,0 @@
1
- /* eslint-disable */
2
- // AUTO-GENERATED by scripts/generate-plugin-types.mjs
3
-
4
- export interface PluginCapabilityRegistryShape {
5
- "appointments": Record<string, unknown>;
6
- "cubejs": Record<string, unknown>;
7
- "gift-cards": Record<string, unknown>;
8
- "loyalty": Record<string, unknown>;
9
- "marketplace": Record<string, unknown>;
10
- "notifications": Record<string, unknown>;
11
- "pos": Record<string, unknown>;
12
- "pos-restaurant": Record<string, unknown>;
13
- "procurement": Record<string, unknown>;
14
- "production": Record<string, unknown>;
15
- "reviews": Record<string, unknown>;
16
- "scheduled-orders": Record<string, unknown>;
17
- "uom": Record<string, unknown>;
18
- "warehouse": Record<string, unknown>;
19
- "wishlist": Record<string, unknown>;
20
- }
@@ -1,23 +0,0 @@
1
- /* eslint-disable */
2
- // AUTO-GENERATED by scripts/generate-plugin-types.mjs
3
- // DO NOT EDIT MANUALLY.
4
-
5
- export const resolvedPluginManifest = [
6
- { id: "appointments", directory: "plugin-appointments" },
7
- { id: "cubejs", directory: "plugin-cubejs" },
8
- { id: "gift-cards", directory: "plugin-gift-cards" },
9
- { id: "loyalty", directory: "plugin-loyalty" },
10
- { id: "marketplace", directory: "plugin-marketplace" },
11
- { id: "notifications", directory: "plugin-notifications" },
12
- { id: "pos", directory: "plugin-pos" },
13
- { id: "pos-restaurant", directory: "plugin-pos-restaurant" },
14
- { id: "procurement", directory: "plugin-procurement" },
15
- { id: "production", directory: "plugin-production" },
16
- { id: "reviews", directory: "plugin-reviews" },
17
- { id: "scheduled-orders", directory: "plugin-scheduled-orders" },
18
- { id: "uom", directory: "plugin-uom" },
19
- { id: "warehouse", directory: "plugin-warehouse" },
20
- { id: "wishlist", directory: "plugin-wishlist" },
21
- ] as const;
22
-
23
- export type ResolvedPluginId = (typeof resolvedPluginManifest)[number]["id"];
@@ -1,20 +0,0 @@
1
- /* eslint-disable */
2
- // AUTO-GENERATED by scripts/generate-plugin-types.mjs
3
-
4
- export interface PluginRepositoryRegistryShape {
5
- "appointments": Record<string, unknown>;
6
- "cubejs": Record<string, unknown>;
7
- "gift-cards": Record<string, unknown>;
8
- "loyalty": Record<string, unknown>;
9
- "marketplace": Record<string, unknown>;
10
- "notifications": Record<string, unknown>;
11
- "pos": Record<string, unknown>;
12
- "pos-restaurant": Record<string, unknown>;
13
- "procurement": Record<string, unknown>;
14
- "production": Record<string, unknown>;
15
- "reviews": Record<string, unknown>;
16
- "scheduled-orders": Record<string, unknown>;
17
- "uom": Record<string, unknown>;
18
- "warehouse": Record<string, unknown>;
19
- "wishlist": Record<string, unknown>;
20
- }