@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,93 @@
1
+ import { OpenAPIHono } from "@hono/zod-openapi";
2
+ import { rateLimiter } from "hono-rate-limiter";
3
+ import type { Kernel } from "../../../runtime/kernel.js";
4
+ import { createPromotionRoute, validatePromotionRoute, deactivatePromotionRoute, listActivePromotionsRoute } from "../schemas/promotions.js";
5
+ import { type AppEnv, mapErrorToResponse, mapErrorToStatus, requirePerm } from "../utils.js";
6
+ import { resolveOrgId } from "../../../auth/org.js";
7
+
8
+ export function promotionRoutes(kernel: Kernel) {
9
+ const router = new OpenAPIHono<AppEnv>();
10
+
11
+ // Rate limit promo code validation — prevents brute-force code enumeration
12
+ router.use(
13
+ "/validate",
14
+ rateLimiter({
15
+ windowMs: 60 * 1000,
16
+ limit: 10,
17
+ keyGenerator: (c) => {
18
+ // Node.js Request has a socket property; Hono types it as unknown.
19
+ const socket = (c.req.raw as { socket?: { remoteAddress?: string } }).socket;
20
+ return socket?.remoteAddress ?? "unknown";
21
+ },
22
+ }),
23
+ );
24
+
25
+ router.use("/", requirePerm("promotions:manage"));
26
+
27
+ // @ts-expect-error -- openapi() enforces strict response typing but our handler
28
+ // returns union responses (201 | 400 | 422). The route definition documents the
29
+ // contract; the handler returns dynamic status.
30
+ router.openapi(createPromotionRoute, async (c) => {
31
+ const body = c.req.valid("json");
32
+ const actor = c.get("actor");
33
+ const result = await kernel.services.promotions.create(body, actor);
34
+ if (!result.ok) {
35
+ return c.json(mapErrorToResponse(result.error), mapErrorToStatus(result.error));
36
+ }
37
+ return c.json({ data: result.value }, 201);
38
+ });
39
+
40
+ // List active promotions — requires auth
41
+ router.use("/active", requirePerm("promotions:read"));
42
+
43
+ // @ts-expect-error -- openapi handler union return type
44
+ router.openapi(listActivePromotionsRoute, async (_c) => {
45
+ const actor = _c.get("actor");
46
+ const result = await kernel.services.promotions.listActive(actor);
47
+ if (!result.ok) {
48
+ return _c.json(mapErrorToResponse(result.error), mapErrorToStatus(result.error));
49
+ }
50
+ return _c.json({ data: result.value });
51
+ });
52
+
53
+ // @ts-expect-error -- openapi() enforces strict response typing but our handler
54
+ // returns union responses (200 | 400 | 404). The route definition documents the
55
+ // contract; the handler returns dynamic status.
56
+ router.openapi(validatePromotionRoute, async (c) => {
57
+ const payload = c.req.valid("json");
58
+ const actor = c.get("actor");
59
+ const orgId = resolveOrgId(actor);
60
+
61
+ const result = await kernel.services.promotions.validate(payload.code, {
62
+ orgId,
63
+ currency: payload.currency,
64
+ subtotal: payload.subtotal,
65
+ lineItems: payload.lineItems,
66
+ ...(payload.customerId !== undefined ? { customerId: payload.customerId } : {}),
67
+ ...(payload.customerGroupIds !== undefined ? { customerGroupIds: payload.customerGroupIds } : {}),
68
+ });
69
+
70
+ if (!result.ok) {
71
+ return c.json(mapErrorToResponse(result.error), mapErrorToStatus(result.error));
72
+ }
73
+
74
+ return c.json({ data: result.value });
75
+ });
76
+
77
+ router.use("/:id/deactivate", requirePerm("promotions:manage"));
78
+
79
+ // @ts-expect-error -- openapi() enforces strict response typing but our handler
80
+ // returns union responses (200 | 400 | 404). The route definition documents the
81
+ // contract; the handler returns dynamic status.
82
+ router.openapi(deactivatePromotionRoute, async (c) => {
83
+ const actor = c.get("actor");
84
+ const orgId = resolveOrgId(actor);
85
+ const result = await kernel.services.promotions.deactivate(orgId, c.req.param("id"));
86
+ if (!result.ok) {
87
+ return c.json(mapErrorToResponse(result.error), mapErrorToStatus(result.error));
88
+ }
89
+ return c.json({ data: result.value });
90
+ });
91
+
92
+ return router;
93
+ }
@@ -0,0 +1,71 @@
1
+ import { OpenAPIHono } from "@hono/zod-openapi";
2
+ import type { Kernel } from "../../../runtime/kernel.js";
3
+ import { searchRoute, suggestRoute } from "../schemas/search.js";
4
+ import { type AppEnv, mapErrorToResponse, mapErrorToStatus } from "../utils.js";
5
+
6
+ export function searchRoutes(kernel: Kernel) {
7
+ const router = new OpenAPIHono<AppEnv>();
8
+
9
+ // @ts-expect-error -- openapi handler union return type
10
+ router.openapi(searchRoute, async (c) => {
11
+ const q = c.req.query("q") ?? "";
12
+ const type = c.req.query("type");
13
+ const category = c.req.query("category");
14
+ const brand = c.req.query("brand");
15
+ const status = c.req.query("status");
16
+ const page = Number.parseInt(c.req.query("page") ?? "1", 10);
17
+ const limit = Number.parseInt(c.req.query("limit") ?? "20", 10);
18
+ const facets = (c.req.query("facets") ?? "")
19
+ .split(",")
20
+ .map((facet) => facet.trim())
21
+ .filter(Boolean);
22
+
23
+ const result = await kernel.services.search.query({
24
+ query: q,
25
+ page,
26
+ limit,
27
+ filters: {
28
+ ...(type ? { type } : {}),
29
+ ...(category ? { category } : {}),
30
+ ...(brand ? { brand } : {}),
31
+ ...(status ? { status } : {}),
32
+ },
33
+ ...(facets.length > 0 ? { facets } : {}),
34
+ }, { actor: c.get("actor"), tx: null, requestId: "" });
35
+
36
+ if (!result.ok) {
37
+ return c.json(mapErrorToResponse(result.error), mapErrorToStatus(result.error));
38
+ }
39
+
40
+ return c.json({
41
+ data: result.value.hits,
42
+ meta: {
43
+ total: result.value.total,
44
+ page: result.value.page,
45
+ limit: result.value.limit,
46
+ facets: result.value.facets,
47
+ },
48
+ });
49
+ });
50
+
51
+ // @ts-expect-error -- openapi handler union return type
52
+ router.openapi(suggestRoute, async (c) => {
53
+ const prefix = c.req.query("prefix") ?? "";
54
+ const type = c.req.query("type");
55
+ const limit = Number.parseInt(c.req.query("limit") ?? "10", 10);
56
+
57
+ const result = await kernel.services.search.suggest({
58
+ prefix,
59
+ ...(type ? { type } : {}),
60
+ limit,
61
+ }, { actor: c.get("actor"), tx: null, requestId: "" });
62
+
63
+ if (!result.ok) {
64
+ return c.json(mapErrorToResponse(result.error), mapErrorToStatus(result.error));
65
+ }
66
+
67
+ return c.json({ data: result.value });
68
+ });
69
+
70
+ return router;
71
+ }
@@ -0,0 +1,46 @@
1
+ import { OpenAPIHono } from "@hono/zod-openapi";
2
+ import type { Kernel } from "../../../runtime/kernel.js";
3
+ import { createWebhookEndpointRoute, listWebhookEndpointsRoute, deleteWebhookEndpointRoute } from "../schemas/webhooks.js";
4
+ import { type AppEnv, mapErrorToResponse, mapErrorToStatus, requirePerm } from "../utils.js";
5
+
6
+ export function webhookRoutes(kernel: Kernel) {
7
+ const router = new OpenAPIHono<AppEnv>();
8
+
9
+ router.use("/*", requirePerm("webhooks:manage"));
10
+
11
+ // @ts-expect-error -- openapi handler union return type
12
+ router.openapi(createWebhookEndpointRoute, async (c) => {
13
+ const body = c.req.valid("json") as Parameters<typeof kernel.services.webhooks.createEndpoint>[0];
14
+ // Generate a default secret if none provided (Zod schema marks it optional for API convenience)
15
+ if (!body.secret) {
16
+ body.secret = `whsec_${crypto.randomUUID().replace(/-/g, "")}`;
17
+ }
18
+ const actor = c.get("actor");
19
+ const result = await kernel.services.webhooks.createEndpoint(body, actor);
20
+ if (!result.ok) {
21
+ return c.json(mapErrorToResponse(result.error), mapErrorToStatus(result.error));
22
+ }
23
+ return c.json({ data: result.value }, 201);
24
+ });
25
+
26
+ // @ts-expect-error -- openapi handler union return type
27
+ router.openapi(listWebhookEndpointsRoute, async (c) => {
28
+ const result = await kernel.services.webhooks.listEndpoints();
29
+ if (!result.ok) {
30
+ return c.json(mapErrorToResponse(result.error), mapErrorToStatus(result.error));
31
+ }
32
+ const sanitized = result.value.map(({ secret: _secret, ...rest }) => rest);
33
+ return c.json({ data: sanitized });
34
+ });
35
+
36
+ // @ts-expect-error -- openapi handler union return type
37
+ router.openapi(deleteWebhookEndpointRoute, async (c) => {
38
+ const result = await kernel.services.webhooks.deleteEndpoint(c.req.param("id"));
39
+ if (!result.ok) {
40
+ return c.json(mapErrorToResponse(result.error), mapErrorToStatus(result.error));
41
+ }
42
+ return c.json({ data: { deleted: true } });
43
+ });
44
+
45
+ return router;
46
+ }
@@ -0,0 +1,40 @@
1
+ import { z, createRoute } from "@hono/zod-openapi";
2
+ import { JobListResponse } from "./responses.js";
3
+
4
+ // ─── Route Definitions ──────────────────────────────────────────────────────
5
+
6
+ export const listFailedJobsRoute = createRoute({
7
+ method: "get",
8
+ path: "/jobs/failed",
9
+ tags: ["Admin Jobs"],
10
+ summary: "List failed jobs",
11
+ request: {
12
+ query: z.object({
13
+ limit: z.string().optional(),
14
+ }),
15
+ },
16
+ responses: {
17
+ 200: {
18
+ content: { "application/json": { schema: JobListResponse } },
19
+ description: "Failed jobs",
20
+ },
21
+ },
22
+ });
23
+
24
+ export const retryJobRoute = createRoute({
25
+ method: "post",
26
+ path: "/jobs/{id}/retry",
27
+ tags: ["Admin Jobs"],
28
+ summary: "Retry a failed job",
29
+ request: {
30
+ params: z.object({
31
+ id: z.string().min(1).openapi({ example: "job-uuid" }),
32
+ }),
33
+ },
34
+ responses: {
35
+ 200: {
36
+ content: { "application/json": { schema: z.object({ data: z.object({ retried: z.literal(true) }) }) } },
37
+ description: "Job retried",
38
+ },
39
+ },
40
+ });
@@ -0,0 +1,46 @@
1
+ import { z, createRoute } from "@hono/zod-openapi";
2
+
3
+ // ─── Route Definitions ──────────────────────────────────────────────────────
4
+
5
+ export const listAuditRoute = createRoute({
6
+ method: "get",
7
+ path: "/",
8
+ tags: ["Audit"],
9
+ summary: "List audit entries",
10
+ request: {
11
+ query: z.object({
12
+ entityType: z.string().optional(),
13
+ entityId: z.string().optional(),
14
+ event: z.string().optional(),
15
+ actorId: z.string().optional(),
16
+ from: z.string().optional(),
17
+ to: z.string().optional(),
18
+ limit: z.string().optional(),
19
+ }),
20
+ },
21
+ responses: {
22
+ 200: {
23
+ content: { "application/json": { schema: z.object({ data: z.array(z.record(z.string(), z.unknown())) }) } },
24
+ description: "Audit entries",
25
+ },
26
+ },
27
+ });
28
+
29
+ export const listEntityAuditRoute = createRoute({
30
+ method: "get",
31
+ path: "/{entityType}/{entityId}",
32
+ tags: ["Audit"],
33
+ summary: "List audit history for a specific entity",
34
+ request: {
35
+ params: z.object({
36
+ entityType: z.string().min(1).openapi({ example: "order" }),
37
+ entityId: z.string().min(1).openapi({ example: "550e8400-e29b-41d4-a716-446655440000" }),
38
+ }),
39
+ },
40
+ responses: {
41
+ 200: {
42
+ content: { "application/json": { schema: z.object({ data: z.array(z.record(z.string(), z.unknown())) }) } },
43
+ description: "Entity audit history",
44
+ },
45
+ },
46
+ });
@@ -0,0 +1,125 @@
1
+ import { z, createRoute } from "@hono/zod-openapi";
2
+ import { ErrorSchema, errorResponses, UuidParamSchema, DeletedResponseSchema } from "./shared.js";
3
+ import { CartResponse } from "./responses.js";
4
+ import {
5
+ CreateCartBodySchema,
6
+ AddCartItemBodySchema,
7
+ UpdateCartItemQuantityBodySchema,
8
+ } from "../../../modules/cart/schemas.js";
9
+
10
+ export { CreateCartBodySchema, AddCartItemBodySchema, UpdateCartItemQuantityBodySchema };
11
+
12
+ // ─── Response Schemas ───────────────────────────────────────────────────────
13
+
14
+ export const CartResponseSchema = CartResponse;
15
+
16
+ // ─── Path Params ────────────────────────────────────────────────────────────
17
+
18
+ const CartIdParam = z.object({
19
+ id: z.uuid().openapi({ example: "550e8400-e29b-41d4-a716-446655440000" }),
20
+ });
21
+
22
+ const CartItemParams = z.object({
23
+ id: z.uuid().openapi({ example: "550e8400-e29b-41d4-a716-446655440000" }),
24
+ itemId: z.uuid().openapi({ example: "660e8400-e29b-41d4-a716-446655440000" }),
25
+ });
26
+
27
+ // ─── Route Definitions ──────────────────────────────────────────────────────
28
+
29
+ export const getCartRoute = createRoute({
30
+ method: "get",
31
+ path: "/{id}",
32
+ tags: ["Carts"],
33
+ summary: "Get a cart by ID",
34
+ request: { params: CartIdParam },
35
+ responses: {
36
+ 200: {
37
+ content: { "application/json": { schema: CartResponseSchema } },
38
+ description: "Cart retrieved.",
39
+ },
40
+ ...errorResponses,
41
+ },
42
+ });
43
+
44
+ export const removeCartItemRoute = createRoute({
45
+ method: "delete",
46
+ path: "/{id}/items/{itemId}",
47
+ tags: ["Carts"],
48
+ summary: "Remove an item from a cart",
49
+ request: { params: CartItemParams },
50
+ responses: {
51
+ 200: {
52
+ content: { "application/json": { schema: DeletedResponseSchema } },
53
+ description: "Item removed.",
54
+ },
55
+ ...errorResponses,
56
+ },
57
+ });
58
+
59
+ export const createCartRoute = createRoute({
60
+ method: "post",
61
+ path: "/",
62
+ tags: ["Carts"],
63
+ summary: "Create a new cart",
64
+ request: {
65
+ body: {
66
+ content: {
67
+ "application/json": { schema: CreateCartBodySchema },
68
+ },
69
+ required: true,
70
+ },
71
+ },
72
+ responses: {
73
+ 201: {
74
+ content: { "application/json": { schema: CartResponseSchema } },
75
+ description: "Cart created successfully.",
76
+ },
77
+ ...errorResponses,
78
+ },
79
+ });
80
+
81
+ export const addCartItemRoute = createRoute({
82
+ method: "post",
83
+ path: "/{id}/items",
84
+ tags: ["Carts"],
85
+ summary: "Add an item to a cart",
86
+ request: {
87
+ params: CartIdParam,
88
+ body: {
89
+ content: {
90
+ "application/json": { schema: AddCartItemBodySchema },
91
+ },
92
+ required: true,
93
+ },
94
+ },
95
+ responses: {
96
+ 201: {
97
+ content: { "application/json": { schema: CartResponseSchema } },
98
+ description: "Item added to cart.",
99
+ },
100
+ ...errorResponses,
101
+ },
102
+ });
103
+
104
+ export const updateCartItemQuantityRoute = createRoute({
105
+ method: "patch",
106
+ path: "/{id}/items/{itemId}",
107
+ tags: ["Carts"],
108
+ summary: "Update the quantity of a cart line item",
109
+ request: {
110
+ params: CartItemParams,
111
+ body: {
112
+ content: {
113
+ "application/json": { schema: UpdateCartItemQuantityBodySchema },
114
+ },
115
+ required: true,
116
+ },
117
+ },
118
+ responses: {
119
+ 200: {
120
+ content: { "application/json": { schema: CartResponseSchema } },
121
+ description: "Cart item quantity updated.",
122
+ },
123
+ ...errorResponses,
124
+ },
125
+ });