@unifiedcommerce/core 0.1.1 → 0.2.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 (257) 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 +3 -1
  73. package/src/adapters/console-email.ts +43 -0
  74. package/src/auth/access.ts +187 -0
  75. package/src/auth/auth-schema.ts +139 -0
  76. package/src/auth/middleware.ts +161 -0
  77. package/src/auth/org.ts +41 -0
  78. package/src/auth/permissions.ts +28 -0
  79. package/src/auth/setup.ts +171 -0
  80. package/src/auth/system-actor.ts +19 -0
  81. package/src/auth/types.ts +10 -0
  82. package/src/config/defaults.ts +82 -0
  83. package/src/config/define-config.ts +53 -0
  84. package/src/config/types.ts +301 -0
  85. package/src/generated/plugin-capabilities.d.ts +20 -0
  86. package/src/generated/plugin-manifest.ts +23 -0
  87. package/src/generated/plugin-repositories.d.ts +20 -0
  88. package/src/hooks/checkout-completion.ts +262 -0
  89. package/src/hooks/checkout.ts +677 -0
  90. package/src/hooks/order-emails.ts +62 -0
  91. package/src/index.ts +215 -0
  92. package/src/interfaces/mcp/agent-prompt.ts +174 -0
  93. package/src/interfaces/mcp/context-enrichment.ts +177 -0
  94. package/src/interfaces/mcp/server.ts +47 -0
  95. package/src/interfaces/mcp/tool-builder.ts +261 -0
  96. package/src/interfaces/mcp/tools/analytics.ts +76 -0
  97. package/src/interfaces/mcp/tools/cart.ts +57 -0
  98. package/src/interfaces/mcp/tools/catalog.ts +299 -0
  99. package/src/interfaces/mcp/tools/index.ts +22 -0
  100. package/src/interfaces/mcp/tools/inventory.ts +161 -0
  101. package/src/interfaces/mcp/tools/orders.ts +104 -0
  102. package/src/interfaces/mcp/tools/pricing.ts +94 -0
  103. package/src/interfaces/mcp/tools/promotions.ts +106 -0
  104. package/src/interfaces/mcp/tools/registry.ts +101 -0
  105. package/src/interfaces/mcp/tools/search.ts +42 -0
  106. package/src/interfaces/mcp/tools/webhooks.ts +48 -0
  107. package/src/interfaces/mcp/transport.ts +128 -0
  108. package/src/interfaces/rest/customer-portal.ts +299 -0
  109. package/src/interfaces/rest/index.ts +74 -0
  110. package/src/interfaces/rest/router.ts +333 -0
  111. package/src/interfaces/rest/routes/admin-jobs.ts +58 -0
  112. package/src/interfaces/rest/routes/audit.ts +50 -0
  113. package/src/interfaces/rest/routes/carts.ts +89 -0
  114. package/src/interfaces/rest/routes/catalog.ts +493 -0
  115. package/src/interfaces/rest/routes/checkout.ts +284 -0
  116. package/src/interfaces/rest/routes/inventory.ts +70 -0
  117. package/src/interfaces/rest/routes/media.ts +86 -0
  118. package/src/interfaces/rest/routes/orders.ts +78 -0
  119. package/src/interfaces/rest/routes/payments.ts +60 -0
  120. package/src/interfaces/rest/routes/pricing.ts +57 -0
  121. package/src/interfaces/rest/routes/promotions.ts +93 -0
  122. package/src/interfaces/rest/routes/search.ts +71 -0
  123. package/src/interfaces/rest/routes/webhooks.ts +46 -0
  124. package/src/interfaces/rest/schemas/admin-jobs.ts +40 -0
  125. package/src/interfaces/rest/schemas/audit.ts +46 -0
  126. package/src/interfaces/rest/schemas/carts.ts +125 -0
  127. package/src/interfaces/rest/schemas/catalog.ts +450 -0
  128. package/src/interfaces/rest/schemas/checkout.ts +66 -0
  129. package/src/interfaces/rest/schemas/customer-portal.ts +195 -0
  130. package/src/interfaces/rest/schemas/inventory.ts +138 -0
  131. package/src/interfaces/rest/schemas/media.ts +75 -0
  132. package/src/interfaces/rest/schemas/orders.ts +104 -0
  133. package/src/interfaces/rest/schemas/pricing.ts +80 -0
  134. package/src/interfaces/rest/schemas/promotions.ts +110 -0
  135. package/src/interfaces/rest/schemas/responses.ts +85 -0
  136. package/src/interfaces/rest/schemas/search.ts +58 -0
  137. package/src/interfaces/rest/schemas/shared.ts +62 -0
  138. package/src/interfaces/rest/schemas/webhooks.ts +68 -0
  139. package/src/interfaces/rest/utils.ts +104 -0
  140. package/src/interfaces/rest/webhook-router.ts +50 -0
  141. package/src/kernel/compensation/executor.ts +61 -0
  142. package/src/kernel/compensation/types.ts +26 -0
  143. package/src/kernel/database/adapter.ts +21 -0
  144. package/src/kernel/database/drizzle-db.ts +56 -0
  145. package/src/kernel/database/migrate.ts +76 -0
  146. package/src/kernel/database/plugin-types.ts +34 -0
  147. package/src/kernel/database/schema.ts +49 -0
  148. package/src/kernel/database/scoped-db.ts +68 -0
  149. package/src/kernel/database/tx-context.ts +46 -0
  150. package/src/kernel/error-mapper.ts +15 -0
  151. package/src/kernel/errors.ts +89 -0
  152. package/src/kernel/factory/repository-factory.ts +244 -0
  153. package/src/kernel/hooks/create-context.ts +43 -0
  154. package/src/kernel/hooks/executor.ts +88 -0
  155. package/src/kernel/hooks/registry.ts +74 -0
  156. package/src/kernel/hooks/types.ts +52 -0
  157. package/src/kernel/http-error.ts +44 -0
  158. package/src/kernel/jobs/adapter.ts +36 -0
  159. package/src/kernel/jobs/drizzle-adapter.ts +58 -0
  160. package/src/kernel/jobs/runner.ts +153 -0
  161. package/src/kernel/jobs/schema.ts +46 -0
  162. package/src/kernel/jobs/types.ts +30 -0
  163. package/src/kernel/local-api.ts +187 -0
  164. package/src/kernel/plugin/manifest.ts +271 -0
  165. package/src/kernel/query/executor.ts +184 -0
  166. package/src/kernel/query/registry.ts +46 -0
  167. package/src/kernel/result.ts +33 -0
  168. package/src/kernel/schema/extra-columns.ts +37 -0
  169. package/src/kernel/service-registry.ts +76 -0
  170. package/src/kernel/service-timing.ts +89 -0
  171. package/src/kernel/state-machine/machine.ts +101 -0
  172. package/src/modules/analytics/drizzle-adapter.ts +426 -0
  173. package/src/modules/analytics/hooks.ts +11 -0
  174. package/src/modules/analytics/models.ts +125 -0
  175. package/src/modules/analytics/repository/index.ts +6 -0
  176. package/src/modules/analytics/service.ts +245 -0
  177. package/src/modules/analytics/types.ts +180 -0
  178. package/src/modules/audit/hooks.ts +78 -0
  179. package/src/modules/audit/schema.ts +33 -0
  180. package/src/modules/audit/service.ts +151 -0
  181. package/src/modules/cart/access.ts +27 -0
  182. package/src/modules/cart/matcher.ts +26 -0
  183. package/src/modules/cart/repository/index.ts +234 -0
  184. package/src/modules/cart/schema.ts +42 -0
  185. package/src/modules/cart/schemas.ts +38 -0
  186. package/src/modules/cart/service.ts +541 -0
  187. package/src/modules/catalog/repository/index.ts +772 -0
  188. package/src/modules/catalog/schema.ts +203 -0
  189. package/src/modules/catalog/schemas.ts +104 -0
  190. package/src/modules/catalog/service.ts +1544 -0
  191. package/src/modules/customers/repository/index.ts +327 -0
  192. package/src/modules/customers/schema.ts +64 -0
  193. package/src/modules/customers/service.ts +171 -0
  194. package/src/modules/fulfillment/repository/index.ts +426 -0
  195. package/src/modules/fulfillment/schema.ts +101 -0
  196. package/src/modules/fulfillment/service.ts +555 -0
  197. package/src/modules/fulfillment/types.ts +59 -0
  198. package/src/modules/inventory/repository/index.ts +509 -0
  199. package/src/modules/inventory/schema.ts +94 -0
  200. package/src/modules/inventory/schemas.ts +38 -0
  201. package/src/modules/inventory/service.ts +490 -0
  202. package/src/modules/media/adapter.ts +17 -0
  203. package/src/modules/media/repository/index.ts +274 -0
  204. package/src/modules/media/schema.ts +41 -0
  205. package/src/modules/media/service.ts +151 -0
  206. package/src/modules/orders/repository/index.ts +287 -0
  207. package/src/modules/orders/schema.ts +66 -0
  208. package/src/modules/orders/service.ts +619 -0
  209. package/src/modules/orders/stale-order-cleanup.ts +76 -0
  210. package/src/modules/organization/service.ts +191 -0
  211. package/src/modules/payments/adapter.ts +47 -0
  212. package/src/modules/payments/repository/index.ts +6 -0
  213. package/src/modules/payments/service.ts +107 -0
  214. package/src/modules/pricing/repository/index.ts +291 -0
  215. package/src/modules/pricing/schema.ts +71 -0
  216. package/src/modules/pricing/schemas.ts +38 -0
  217. package/src/modules/pricing/service.ts +494 -0
  218. package/src/modules/promotions/repository/index.ts +325 -0
  219. package/src/modules/promotions/schema.ts +62 -0
  220. package/src/modules/promotions/schemas.ts +38 -0
  221. package/src/modules/promotions/service.ts +598 -0
  222. package/src/modules/search/adapter.ts +57 -0
  223. package/src/modules/search/hooks.ts +12 -0
  224. package/src/modules/search/repository/index.ts +6 -0
  225. package/src/modules/search/service.ts +315 -0
  226. package/src/modules/shipping/calculator.ts +188 -0
  227. package/src/modules/shipping/repository/index.ts +6 -0
  228. package/src/modules/shipping/service.ts +51 -0
  229. package/src/modules/tax/adapter.ts +60 -0
  230. package/src/modules/tax/repository/index.ts +6 -0
  231. package/src/modules/tax/service.ts +53 -0
  232. package/src/modules/webhooks/hook.ts +34 -0
  233. package/src/modules/webhooks/repository/index.ts +278 -0
  234. package/src/modules/webhooks/schema.ts +56 -0
  235. package/src/modules/webhooks/service.ts +117 -0
  236. package/src/modules/webhooks/signing.ts +6 -0
  237. package/src/modules/webhooks/ssrf-guard.ts +71 -0
  238. package/src/modules/webhooks/tasks.ts +52 -0
  239. package/src/modules/webhooks/worker.ts +134 -0
  240. package/src/runtime/commerce.ts +145 -0
  241. package/src/runtime/kernel.ts +426 -0
  242. package/src/runtime/logger.ts +36 -0
  243. package/src/runtime/server.ts +355 -0
  244. package/src/runtime/shutdown.ts +43 -0
  245. package/src/test-utils/create-pglite-adapter.ts +129 -0
  246. package/src/test-utils/create-plugin-test-app.ts +128 -0
  247. package/src/test-utils/create-repository-test-harness.ts +16 -0
  248. package/src/test-utils/create-test-config.ts +190 -0
  249. package/src/test-utils/create-test-kernel.ts +7 -0
  250. package/src/test-utils/create-test-plugin-context.ts +75 -0
  251. package/src/test-utils/rest-api-test-utils.ts +265 -0
  252. package/src/test-utils/test-actors.ts +62 -0
  253. package/src/test-utils/typed-hooks.ts +54 -0
  254. package/src/types/commerce-types.ts +34 -0
  255. package/src/utils/id.ts +3 -0
  256. package/src/utils/logger.ts +18 -0
  257. package/src/utils/pagination.ts +22 -0
@@ -0,0 +1,333 @@
1
+ /**
2
+ * Type-safe route builder for UnifiedCommerce plugins.
3
+ *
4
+ * Wraps @hono/zod-openapi's createRoute() with sensible defaults:
5
+ * - /api prefix prepended automatically
6
+ * - Error responses (400, 401, 403, 404, 422) injected
7
+ * - Path params auto-detected from {id} patterns
8
+ * - POST defaults to 201, others to 200
9
+ * - .auth() enforces login, .permission() enforces specific scope
10
+ * - Handler receives { input, params, query, actor, services, db, logger }
11
+ * - Return value auto-wrapped in { data: result }
12
+ * - Errors auto-caught and mapped to { error: { code, message } }
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * import { router, z } from "@unifiedcommerce/core";
17
+ *
18
+ * const vendors = router("Vendors", "/marketplace/vendors");
19
+ *
20
+ * vendors.get("/").summary("List").auth().handler(async ({ actor, services }) => {
21
+ * return services.vendor.list();
22
+ * });
23
+ *
24
+ * vendors.post("/").summary("Create").permission("marketplace:admin").input(Schema)
25
+ * .handler(async ({ input, services }) => services.vendor.create(input));
26
+ * ```
27
+ */
28
+
29
+ import { createRoute, z } from "@hono/zod-openapi";
30
+ import { ErrorSchema } from "./schemas/shared.js";
31
+ import { mapErrorToResponse, mapErrorToStatus } from "./utils.js";
32
+ import type { PluginRouteRegistration } from "../../kernel/plugin/manifest.js";
33
+ import { createScopedDb } from "../../kernel/database/scoped-db.js";
34
+ import { resolveOrgId } from "../../auth/org.js";
35
+
36
+ // ─── Shared OpenAPI Error Responses ──────────────────────────────────────────
37
+
38
+ const errorResponses = {
39
+ 400: { content: { "application/json": { schema: ErrorSchema } }, description: "Bad request." },
40
+ 401: { content: { "application/json": { schema: ErrorSchema } }, description: "Unauthorized." },
41
+ 403: { content: { "application/json": { schema: ErrorSchema } }, description: "Forbidden." },
42
+ 404: { content: { "application/json": { schema: ErrorSchema } }, description: "Not found." },
43
+ 422: { content: { "application/json": { schema: ErrorSchema } }, description: "Validation error." },
44
+ } as const;
45
+
46
+ function wrapJson(schema: z.ZodType) {
47
+ return { content: { "application/json": { schema } }, description: "Success" };
48
+ }
49
+
50
+ function extractPathParams(path: string): string[] {
51
+ return [...path.matchAll(/\{(\w+)\}/g)].map((m) => m[1]!);
52
+ }
53
+
54
+ // ─── Handler Context ─────────────────────────────────────────────────────────
55
+
56
+ export interface RouteHandlerContext {
57
+ /** Validated request body (from .input()). Undefined if no .input() was called. */
58
+ input: unknown;
59
+ /** Validated query parameters (from .query()). Empty object if no .query(). */
60
+ query: Record<string, unknown>;
61
+ /** Path parameters, auto-extracted from {id} segments. */
62
+ params: Record<string, string>;
63
+ /** Authenticated actor. Guaranteed non-null if .auth() or .permission() was called. */
64
+ actor: { userId: string; role: string; permissions: string[]; vendorId?: string | null; [key: string]: unknown } | null;
65
+ /** Resolved organization ID. Derived from actor.organizationId, falls back to DEFAULT_ORG_ID. */
66
+ orgId: string;
67
+ /** Kernel services (orders, cart, inventory, etc.) */
68
+ services: Record<string, unknown>;
69
+ /** Drizzle database instance */
70
+ db: unknown;
71
+ /** Per-request structured logger */
72
+ logger: unknown;
73
+ /** Request ID */
74
+ requestId: string;
75
+ /** Raw Hono context (escape hatch) */
76
+ raw: unknown;
77
+ }
78
+
79
+ // ─── Route Chain ─────────────────────────────────────────────────────────────
80
+
81
+ class RouteChain {
82
+ private _summary = "";
83
+ private _description = "";
84
+ private _input: z.ZodType | undefined;
85
+ private _query: z.ZodType | undefined;
86
+ private _params: z.ZodType | undefined;
87
+ private _requireAuth = false;
88
+ private _requiredPermission: string | undefined;
89
+ constructor(
90
+ private method: string,
91
+ private fullPath: string,
92
+ private tag: string,
93
+ private routesList: PluginRouteRegistration[],
94
+ private pluginCtx?: { services: Record<string, unknown>; db: unknown },
95
+ ) {
96
+ const paramNames = extractPathParams(fullPath);
97
+ if (paramNames.length > 0) {
98
+ this._params = z.object(
99
+ Object.fromEntries(paramNames.map((n) => [n, z.uuid()])),
100
+ );
101
+ }
102
+ }
103
+
104
+ /** Set the OpenAPI summary for this route. */
105
+ summary(text: string) { this._summary = text; return this; }
106
+
107
+ /** Set the OpenAPI description for this route. */
108
+ description(text: string) { this._description = text; return this; }
109
+
110
+ /** Set the request body schema. Only for POST/PATCH/PUT. */
111
+ input(schema: z.ZodType) { this._input = schema; return this; }
112
+
113
+ /** Set the query parameter schema. */
114
+ query(schema: z.ZodType) { this._query = schema; return this; }
115
+
116
+ /** Override the auto-detected path parameter schema. */
117
+ params(schema: z.ZodType) { this._params = schema; return this; }
118
+
119
+ /**
120
+ * Require authentication. The handler's `actor` is guaranteed non-null.
121
+ * Returns 401 if the request has no authenticated actor.
122
+ */
123
+ auth() { this._requireAuth = true; return this; }
124
+
125
+ /**
126
+ * Require a specific permission scope. Implies .auth().
127
+ * Returns 401 if not authenticated, 403 if the actor lacks the permission.
128
+ *
129
+ * Permission scopes should be declared in the plugin's `permissions` manifest.
130
+ * The wildcard `*:*` always passes.
131
+ *
132
+ * @example
133
+ * ```typescript
134
+ * vendors.post("/").permission("marketplace:admin").handler(...)
135
+ * wishlist.post("/").permission("wishlist:write").handler(...)
136
+ * ```
137
+ */
138
+ permission(scope: string) {
139
+ this._requireAuth = true;
140
+ this._requiredPermission = scope;
141
+ return this;
142
+ }
143
+
144
+ /**
145
+ * Register the handler for this route.
146
+ *
147
+ * The handler receives a context object with typed input, params, query,
148
+ * actor, services, and db. Return data is auto-wrapped in { data: result }.
149
+ * Thrown errors are auto-caught and mapped to { error: { code, message } }.
150
+ */
151
+ handler(fn: (ctx: RouteHandlerContext) => Promise<unknown>): void {
152
+ const request: Record<string, unknown> = {};
153
+ if (this._input) request.body = { ...wrapJson(this._input), required: true };
154
+ if (this._query) request.query = this._query;
155
+ if (this._params) request.params = this._params;
156
+
157
+ const status = this.method === "post" ? 201 : 200;
158
+
159
+ const routeConfig = createRoute({
160
+ method: this.method as "get",
161
+ path: this.fullPath,
162
+ tags: [this.tag],
163
+ summary: this._summary,
164
+ ...(this._description ? { description: this._description } : {}),
165
+ ...(Object.keys(request).length > 0 ? { request } : {}),
166
+ responses: {
167
+ [status]: wrapJson(z.object({ data: z.any() })),
168
+ ...errorResponses,
169
+ },
170
+ });
171
+
172
+ const inputSchema = this._input;
173
+ const querySchema = this._query;
174
+ const pathParams = extractPathParams(this.fullPath);
175
+ const successStatus = status;
176
+ const requireAuth = this._requireAuth;
177
+ const requiredPermission = this._requiredPermission;
178
+ const pluginCtx = this.pluginCtx;
179
+
180
+ const honoHandler = async (c: unknown) => {
181
+ const ctx = c as {
182
+ req: {
183
+ valid: (target: string) => unknown;
184
+ param: (name: string) => string;
185
+ };
186
+ get: (key: string) => unknown;
187
+ json: (data: unknown, status?: number) => unknown;
188
+ };
189
+
190
+ try {
191
+ // ─── Auth + Permission Check ─────────────────────────────────
192
+ // .permission() implies .auth() — both are enforced here.
193
+ // Order: auth first (401), then permission (403).
194
+ const actor = ctx.get("actor") as RouteHandlerContext["actor"];
195
+
196
+ if ((requireAuth || requiredPermission) && !actor) {
197
+ return ctx.json({ error: { code: "UNAUTHORIZED", message: "Authentication required." } }, 401);
198
+ }
199
+
200
+ if (requiredPermission) {
201
+ const perms = actor?.permissions ?? [];
202
+ if (!perms.includes(requiredPermission) && !perms.includes("*:*")) {
203
+ return ctx.json({
204
+ error: { code: "FORBIDDEN", message: `Permission '${requiredPermission}' is required.` },
205
+ }, 403);
206
+ }
207
+ }
208
+
209
+ // ─── Extract context ─────────────────────────────────────────
210
+ const params: Record<string, string> = {};
211
+ for (const name of pathParams) {
212
+ params[name] = ctx.req.param(name);
213
+ }
214
+
215
+ // Use plugin context if provided (plugin routes), fallback to kernel on Hono context (core routes)
216
+ const kernel = pluginCtx ?? (ctx.get("kernel") as {
217
+ services?: Record<string, unknown>;
218
+ db?: unknown;
219
+ } | undefined);
220
+
221
+ // Scope the database to the actor's organization.
222
+ // Plugin route handlers receive a db that auto-filters queries
223
+ // and auto-stamps inserts with the actor's organizationId.
224
+ const rawDb = kernel?.db;
225
+ const orgId = resolveOrgId(actor);
226
+ const scopedDb = rawDb ? createScopedDb(rawDb as Record<string, unknown>, orgId) : rawDb;
227
+
228
+ const handlerCtx: RouteHandlerContext = {
229
+ input: inputSchema ? ctx.req.valid("json") : undefined,
230
+ query: querySchema ? (ctx.req.valid("query") as Record<string, unknown>) : {},
231
+ params,
232
+ actor,
233
+ orgId,
234
+ services: kernel?.services ?? {},
235
+ db: scopedDb,
236
+ logger: ctx.get("logger"),
237
+ requestId: (ctx.get("requestId") as string) ?? "",
238
+ raw: c,
239
+ };
240
+
241
+ const result = await fn(handlerCtx);
242
+
243
+ if (result === undefined || result === null) {
244
+ return ctx.json({ data: null }, successStatus);
245
+ }
246
+ return ctx.json({ data: result }, successStatus);
247
+ } catch (error: unknown) {
248
+ try {
249
+ return ctx.json(
250
+ mapErrorToResponse(error),
251
+ mapErrorToStatus(error) as number,
252
+ );
253
+ } catch {
254
+ // Fallback if mapErrorToResponse/mapErrorToStatus themselves throw
255
+ return ctx.json(
256
+ { error: { code: "INTERNAL_ERROR", message: "An unexpected error occurred." } },
257
+ 500,
258
+ );
259
+ }
260
+ }
261
+ };
262
+
263
+ this.routesList.push({ openapi: routeConfig, handler: honoHandler as (...args: unknown[]) => unknown });
264
+ }
265
+ }
266
+
267
+ // ─── Router ──────────────────────────────────────────────────────────────────
268
+
269
+ class RouterImpl {
270
+ private _routes: PluginRouteRegistration[] = [];
271
+ private _prefix: string;
272
+ private _pluginCtx: { services: Record<string, unknown>; db: unknown } | undefined;
273
+
274
+ constructor(
275
+ private tag: string,
276
+ prefix: string,
277
+ pluginCtx?: { services?: Record<string, unknown>; database?: { db: unknown } },
278
+ ) {
279
+ // Normalize: ensure /api prefix, strip trailing slash
280
+ let p = prefix.startsWith("/api") ? prefix : `/api${prefix}`;
281
+ p = p.replace(/\/+$/, ""); // remove trailing slashes
282
+ this._prefix = p;
283
+
284
+ // If plugin context is provided, wire services + db for handler access
285
+ if (pluginCtx) {
286
+ this._pluginCtx = {
287
+ services: pluginCtx.services ?? {},
288
+ db: pluginCtx.database?.db,
289
+ };
290
+ }
291
+ }
292
+
293
+ /** @internal — used by RouteChain to wire plugin context into handlers */
294
+ get pluginContext() { return this._pluginCtx; }
295
+
296
+ private resolvePath(path: string): string {
297
+ // "/" means "the resource root" → just the prefix, no trailing slash
298
+ if (path === "/") return this._prefix;
299
+ return (this._prefix + path).replace(/\/\/+/g, "/");
300
+ }
301
+
302
+ get(path: string) { return new RouteChain("get", this.resolvePath(path), this.tag, this._routes, this._pluginCtx); }
303
+ post(path: string) { return new RouteChain("post", this.resolvePath(path), this.tag, this._routes, this._pluginCtx); }
304
+ patch(path: string) { return new RouteChain("patch", this.resolvePath(path), this.tag, this._routes, this._pluginCtx); }
305
+ delete(path: string) { return new RouteChain("delete", this.resolvePath(path), this.tag, this._routes, this._pluginCtx); }
306
+ put(path: string) { return new RouteChain("put", this.resolvePath(path), this.tag, this._routes, this._pluginCtx); }
307
+
308
+ /** Returns all registered routes as PluginRouteRegistration[] */
309
+ routes(): PluginRouteRegistration[] { return this._routes; }
310
+ }
311
+
312
+ /**
313
+ * Create a typed route group for plugin routes.
314
+ *
315
+ * @param tag — OpenAPI tag for Swagger UI grouping
316
+ * @param prefix — Path prefix without /api (e.g., "/marketplace/vendors"). /api is prepended automatically.
317
+ * @param ctx — Optional PluginContext. When provided, handler receives { services, db } from the plugin.
318
+ *
319
+ * @example
320
+ * ```typescript
321
+ * // In plugin routes callback:
322
+ * routes: (ctx) => {
323
+ * const r = router("Vendors", "/marketplace/vendors", ctx);
324
+ * r.get("/").summary("List").handler(async ({ services }) => {
325
+ * return (services as VendorServices).vendor.list();
326
+ * });
327
+ * return r.routes();
328
+ * }
329
+ * ```
330
+ */
331
+ export function router(tag: string, prefix: string, ctx?: { services?: Record<string, unknown>; database?: { db: unknown } }): RouterImpl {
332
+ return new RouterImpl(tag, prefix, ctx);
333
+ }
@@ -0,0 +1,58 @@
1
+ import { OpenAPIHono } from "@hono/zod-openapi";
2
+ import { eq, and, desc } from "drizzle-orm";
3
+ import type { PgDatabase, PgQueryResultHKT } from "drizzle-orm/pg-core";
4
+ import type { Kernel } from "../../../runtime/kernel.js";
5
+ import { commerceJobs } from "../../../kernel/jobs/schema.js";
6
+ import { listFailedJobsRoute, retryJobRoute } from "../schemas/admin-jobs.js";
7
+ import { type AppEnv, requirePerm } from "../utils.js";
8
+ import { resolveOrgId } from "../../../auth/org.js";
9
+
10
+ type Db = PgDatabase<PgQueryResultHKT, Record<string, unknown>>;
11
+
12
+ export function adminJobRoutes(kernel: Kernel) {
13
+ const router = new OpenAPIHono<AppEnv>();
14
+ const db = kernel.database.db as Db;
15
+
16
+ router.use("/jobs/failed", requirePerm("jobs:admin"));
17
+
18
+ router.openapi(listFailedJobsRoute, async (c) => {
19
+ const actor = c.get("actor");
20
+ const orgId = resolveOrgId(actor);
21
+ const limit = Math.min(Number(c.req.query("limit") ?? "50"), 100);
22
+ const conditions = [eq(commerceJobs.status, "failed")];
23
+ // Scope to org unless wildcard admin
24
+ if (!actor?.permissions?.includes("*:*")) {
25
+ conditions.push(eq(commerceJobs.organizationId, orgId));
26
+ }
27
+ const failed = await db.select()
28
+ .from(commerceJobs)
29
+ .where(and(...conditions))
30
+ .orderBy(desc(commerceJobs.completedAt))
31
+ .limit(limit);
32
+ return c.json({ data: failed });
33
+ });
34
+
35
+ router.use("/jobs/:id/retry", requirePerm("jobs:admin"));
36
+
37
+ // @ts-expect-error -- openapi handler union return type
38
+ router.openapi(retryJobRoute, async (c) => {
39
+ const actor = c.get("actor");
40
+ const orgId = resolveOrgId(actor);
41
+ const id = c.req.param("id");
42
+ const conditions = [eq(commerceJobs.id, id)];
43
+ // Scope to org unless wildcard admin
44
+ if (!actor?.permissions?.includes("*:*")) {
45
+ conditions.push(eq(commerceJobs.organizationId, orgId));
46
+ }
47
+ const result = await db.update(commerceJobs)
48
+ .set({ status: "pending", attempts: 0, error: null, waitUntil: null, updatedAt: new Date() })
49
+ .where(and(...conditions))
50
+ .returning();
51
+ if (result.length === 0) {
52
+ return c.json({ error: { code: "NOT_FOUND", message: "Job not found." } }, 404);
53
+ }
54
+ return c.json({ data: { retried: true } });
55
+ });
56
+
57
+ return router;
58
+ }
@@ -0,0 +1,50 @@
1
+ import { OpenAPIHono } from "@hono/zod-openapi";
2
+ import type { Kernel } from "../../../runtime/kernel.js";
3
+ import { listAuditRoute, listEntityAuditRoute } from "../schemas/audit.js";
4
+ import { type AppEnv, requirePerm } from "../utils.js";
5
+ import { resolveOrgId } from "../../../auth/org.js";
6
+
7
+ export function auditRoutes(kernel: Kernel) {
8
+ const router = new OpenAPIHono<AppEnv>();
9
+
10
+ /**
11
+ * GET /api/audit
12
+ * List audit entries with optional filters.
13
+ */
14
+ router.use("/", requirePerm("audit:read"));
15
+
16
+ router.openapi(listAuditRoute, async (c) => {
17
+ const actor = c.get("actor");
18
+ const orgId = resolveOrgId(actor);
19
+ const entries = await kernel.services.audit.list({
20
+ organizationId: orgId,
21
+ entityType: c.req.query("entityType"),
22
+ entityId: c.req.query("entityId"),
23
+ event: c.req.query("event"),
24
+ actorId: c.req.query("actorId"),
25
+ from: c.req.query("from") ? new Date(c.req.query("from")!) : undefined,
26
+ to: c.req.query("to") ? new Date(c.req.query("to")!) : undefined,
27
+ limit: c.req.query("limit") ? Number(c.req.query("limit")) : 50,
28
+ });
29
+ return c.json({ data: entries });
30
+ });
31
+
32
+ /**
33
+ * GET /api/audit/:entityType/:entityId
34
+ * List audit history for a specific entity.
35
+ */
36
+ router.use("/:entityType/:entityId", requirePerm("audit:read"));
37
+
38
+ router.openapi(listEntityAuditRoute, async (c) => {
39
+ const actor = c.get("actor");
40
+ const orgId = resolveOrgId(actor);
41
+ const entries = await kernel.services.audit.listForEntity({
42
+ organizationId: orgId,
43
+ entityType: c.req.param("entityType"),
44
+ entityId: c.req.param("entityId"),
45
+ });
46
+ return c.json({ data: entries });
47
+ });
48
+
49
+ return router;
50
+ }
@@ -0,0 +1,89 @@
1
+ import { OpenAPIHono } from "@hono/zod-openapi";
2
+ import type { Kernel } from "../../../runtime/kernel.js";
3
+ import type { CreateCartInput, AddCartItemInput } from "../../../modules/cart/schemas.js";
4
+ import { createCartRoute, addCartItemRoute, updateCartItemQuantityRoute, getCartRoute, removeCartItemRoute } from "../schemas/carts.js";
5
+ import { type AppEnv, mapErrorToResponse, mapErrorToStatus } from "../utils.js";
6
+
7
+ export function cartRoutes(kernel: Kernel) {
8
+ const router = new OpenAPIHono<AppEnv>();
9
+
10
+ // @ts-expect-error -- openapi() enforces strict response typing but our handler
11
+ // returns union responses (201 | 400 | 422). The route definition documents the
12
+ // contract; the handler returns dynamic status.
13
+ router.openapi(createCartRoute, async (c) => {
14
+ const actor = c.get("actor");
15
+ const result = await kernel.services.cart.create(c.req.valid("json") as CreateCartInput, actor);
16
+ if (!result.ok)
17
+ return c.json(
18
+ mapErrorToResponse(result.error),
19
+ mapErrorToStatus(result.error),
20
+ );
21
+ return c.json({ data: result.value }, 201);
22
+ });
23
+
24
+ // @ts-expect-error -- openapi handler union return type
25
+ router.openapi(getCartRoute, async (c) => {
26
+ const actor = c.get("actor");
27
+ const result = await kernel.services.cart.getById(c.req.param("id"), actor);
28
+ if (!result.ok)
29
+ return c.json(
30
+ mapErrorToResponse(result.error),
31
+ mapErrorToStatus(result.error),
32
+ );
33
+ return c.json({ data: result.value });
34
+ });
35
+
36
+ // @ts-expect-error -- openapi() enforces strict response typing but our handler
37
+ // returns union responses (201 | 400 | 422). The route definition documents the
38
+ // contract; the handler returns dynamic status.
39
+ router.openapi(addCartItemRoute, async (c) => {
40
+ const result = await kernel.services.cart.addItem(
41
+ { ...c.req.valid("json"), cartId: c.req.param("id") } as AddCartItemInput,
42
+ c.get("actor"),
43
+ );
44
+ if (!result.ok)
45
+ return c.json(
46
+ mapErrorToResponse(result.error),
47
+ mapErrorToStatus(result.error),
48
+ );
49
+ return c.json({ data: result.value }, 201);
50
+ });
51
+
52
+ // @ts-expect-error -- openapi() enforces strict response typing but our handler
53
+ // returns union responses (200 | 400 | 422). The route definition documents the
54
+ // contract; the handler returns dynamic status.
55
+ router.openapi(updateCartItemQuantityRoute, async (c) => {
56
+ const body = c.req.valid("json");
57
+ const result = await kernel.services.cart.updateQuantity(
58
+ {
59
+ cartId: c.req.param("id"),
60
+ itemId: c.req.param("itemId"),
61
+ quantity: body.quantity,
62
+ },
63
+ c.get("actor"),
64
+ );
65
+ if (!result.ok)
66
+ return c.json(
67
+ mapErrorToResponse(result.error),
68
+ mapErrorToStatus(result.error),
69
+ );
70
+ return c.json({ data: result.value });
71
+ });
72
+
73
+ // @ts-expect-error -- openapi handler union return type
74
+ router.openapi(removeCartItemRoute, async (c) => {
75
+ const result = await kernel.services.cart.removeItem(
76
+ c.req.param("id"),
77
+ c.req.param("itemId"),
78
+ c.get("actor"),
79
+ );
80
+ if (!result.ok)
81
+ return c.json(
82
+ mapErrorToResponse(result.error),
83
+ mapErrorToStatus(result.error),
84
+ );
85
+ return c.json({ data: { deleted: true } });
86
+ });
87
+
88
+ return router;
89
+ }