@unifiedcommerce/core 0.2.0 → 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 (186) hide show
  1. package/package.json +2 -1
  2. package/src/adapters/console-email.ts +43 -0
  3. package/src/auth/access.ts +187 -0
  4. package/src/auth/auth-schema.ts +139 -0
  5. package/src/auth/middleware.ts +161 -0
  6. package/src/auth/org.ts +41 -0
  7. package/src/auth/permissions.ts +28 -0
  8. package/src/auth/setup.ts +171 -0
  9. package/src/auth/system-actor.ts +19 -0
  10. package/src/auth/types.ts +10 -0
  11. package/src/config/defaults.ts +82 -0
  12. package/src/config/define-config.ts +53 -0
  13. package/src/config/types.ts +301 -0
  14. package/src/generated/plugin-capabilities.d.ts +20 -0
  15. package/src/generated/plugin-manifest.ts +23 -0
  16. package/src/generated/plugin-repositories.d.ts +20 -0
  17. package/src/hooks/checkout-completion.ts +262 -0
  18. package/src/hooks/checkout.ts +677 -0
  19. package/src/hooks/order-emails.ts +62 -0
  20. package/src/index.ts +215 -0
  21. package/src/interfaces/mcp/agent-prompt.ts +174 -0
  22. package/src/interfaces/mcp/context-enrichment.ts +177 -0
  23. package/src/interfaces/mcp/server.ts +47 -0
  24. package/src/interfaces/mcp/tool-builder.ts +261 -0
  25. package/src/interfaces/mcp/tools/analytics.ts +76 -0
  26. package/src/interfaces/mcp/tools/cart.ts +57 -0
  27. package/src/interfaces/mcp/tools/catalog.ts +299 -0
  28. package/src/interfaces/mcp/tools/index.ts +22 -0
  29. package/src/interfaces/mcp/tools/inventory.ts +161 -0
  30. package/src/interfaces/mcp/tools/orders.ts +104 -0
  31. package/src/interfaces/mcp/tools/pricing.ts +94 -0
  32. package/src/interfaces/mcp/tools/promotions.ts +106 -0
  33. package/src/interfaces/mcp/tools/registry.ts +101 -0
  34. package/src/interfaces/mcp/tools/search.ts +42 -0
  35. package/src/interfaces/mcp/tools/webhooks.ts +48 -0
  36. package/src/interfaces/mcp/transport.ts +128 -0
  37. package/src/interfaces/rest/customer-portal.ts +299 -0
  38. package/src/interfaces/rest/index.ts +74 -0
  39. package/src/interfaces/rest/router.ts +333 -0
  40. package/src/interfaces/rest/routes/admin-jobs.ts +58 -0
  41. package/src/interfaces/rest/routes/audit.ts +50 -0
  42. package/src/interfaces/rest/routes/carts.ts +89 -0
  43. package/src/interfaces/rest/routes/catalog.ts +493 -0
  44. package/src/interfaces/rest/routes/checkout.ts +284 -0
  45. package/src/interfaces/rest/routes/inventory.ts +70 -0
  46. package/src/interfaces/rest/routes/media.ts +86 -0
  47. package/src/interfaces/rest/routes/orders.ts +78 -0
  48. package/src/interfaces/rest/routes/payments.ts +60 -0
  49. package/src/interfaces/rest/routes/pricing.ts +57 -0
  50. package/src/interfaces/rest/routes/promotions.ts +93 -0
  51. package/src/interfaces/rest/routes/search.ts +71 -0
  52. package/src/interfaces/rest/routes/webhooks.ts +46 -0
  53. package/src/interfaces/rest/schemas/admin-jobs.ts +40 -0
  54. package/src/interfaces/rest/schemas/audit.ts +46 -0
  55. package/src/interfaces/rest/schemas/carts.ts +125 -0
  56. package/src/interfaces/rest/schemas/catalog.ts +450 -0
  57. package/src/interfaces/rest/schemas/checkout.ts +66 -0
  58. package/src/interfaces/rest/schemas/customer-portal.ts +195 -0
  59. package/src/interfaces/rest/schemas/inventory.ts +138 -0
  60. package/src/interfaces/rest/schemas/media.ts +75 -0
  61. package/src/interfaces/rest/schemas/orders.ts +104 -0
  62. package/src/interfaces/rest/schemas/pricing.ts +80 -0
  63. package/src/interfaces/rest/schemas/promotions.ts +110 -0
  64. package/src/interfaces/rest/schemas/responses.ts +85 -0
  65. package/src/interfaces/rest/schemas/search.ts +58 -0
  66. package/src/interfaces/rest/schemas/shared.ts +62 -0
  67. package/src/interfaces/rest/schemas/webhooks.ts +68 -0
  68. package/src/interfaces/rest/utils.ts +104 -0
  69. package/src/interfaces/rest/webhook-router.ts +50 -0
  70. package/src/kernel/compensation/executor.ts +61 -0
  71. package/src/kernel/compensation/types.ts +26 -0
  72. package/src/kernel/database/adapter.ts +21 -0
  73. package/src/kernel/database/drizzle-db.ts +56 -0
  74. package/src/kernel/database/migrate.ts +76 -0
  75. package/src/kernel/database/plugin-types.ts +34 -0
  76. package/src/kernel/database/schema.ts +49 -0
  77. package/src/kernel/database/scoped-db.ts +68 -0
  78. package/src/kernel/database/tx-context.ts +46 -0
  79. package/src/kernel/error-mapper.ts +15 -0
  80. package/src/kernel/errors.ts +89 -0
  81. package/src/kernel/factory/repository-factory.ts +244 -0
  82. package/src/kernel/hooks/create-context.ts +43 -0
  83. package/src/kernel/hooks/executor.ts +88 -0
  84. package/src/kernel/hooks/registry.ts +74 -0
  85. package/src/kernel/hooks/types.ts +52 -0
  86. package/src/kernel/http-error.ts +44 -0
  87. package/src/kernel/jobs/adapter.ts +36 -0
  88. package/src/kernel/jobs/drizzle-adapter.ts +58 -0
  89. package/src/kernel/jobs/runner.ts +153 -0
  90. package/src/kernel/jobs/schema.ts +46 -0
  91. package/src/kernel/jobs/types.ts +30 -0
  92. package/src/kernel/local-api.ts +187 -0
  93. package/src/kernel/plugin/manifest.ts +271 -0
  94. package/src/kernel/query/executor.ts +184 -0
  95. package/src/kernel/query/registry.ts +46 -0
  96. package/src/kernel/result.ts +33 -0
  97. package/src/kernel/schema/extra-columns.ts +37 -0
  98. package/src/kernel/service-registry.ts +76 -0
  99. package/src/kernel/service-timing.ts +89 -0
  100. package/src/kernel/state-machine/machine.ts +101 -0
  101. package/src/modules/analytics/drizzle-adapter.ts +426 -0
  102. package/src/modules/analytics/hooks.ts +11 -0
  103. package/src/modules/analytics/models.ts +125 -0
  104. package/src/modules/analytics/repository/index.ts +6 -0
  105. package/src/modules/analytics/service.ts +245 -0
  106. package/src/modules/analytics/types.ts +180 -0
  107. package/src/modules/audit/hooks.ts +78 -0
  108. package/src/modules/audit/schema.ts +33 -0
  109. package/src/modules/audit/service.ts +151 -0
  110. package/src/modules/cart/access.ts +27 -0
  111. package/src/modules/cart/matcher.ts +26 -0
  112. package/src/modules/cart/repository/index.ts +234 -0
  113. package/src/modules/cart/schema.ts +42 -0
  114. package/src/modules/cart/schemas.ts +38 -0
  115. package/src/modules/cart/service.ts +541 -0
  116. package/src/modules/catalog/repository/index.ts +772 -0
  117. package/src/modules/catalog/schema.ts +203 -0
  118. package/src/modules/catalog/schemas.ts +104 -0
  119. package/src/modules/catalog/service.ts +1544 -0
  120. package/src/modules/customers/repository/index.ts +327 -0
  121. package/src/modules/customers/schema.ts +64 -0
  122. package/src/modules/customers/service.ts +171 -0
  123. package/src/modules/fulfillment/repository/index.ts +426 -0
  124. package/src/modules/fulfillment/schema.ts +101 -0
  125. package/src/modules/fulfillment/service.ts +555 -0
  126. package/src/modules/fulfillment/types.ts +59 -0
  127. package/src/modules/inventory/repository/index.ts +509 -0
  128. package/src/modules/inventory/schema.ts +94 -0
  129. package/src/modules/inventory/schemas.ts +38 -0
  130. package/src/modules/inventory/service.ts +490 -0
  131. package/src/modules/media/adapter.ts +17 -0
  132. package/src/modules/media/repository/index.ts +274 -0
  133. package/src/modules/media/schema.ts +41 -0
  134. package/src/modules/media/service.ts +151 -0
  135. package/src/modules/orders/repository/index.ts +287 -0
  136. package/src/modules/orders/schema.ts +66 -0
  137. package/src/modules/orders/service.ts +619 -0
  138. package/src/modules/orders/stale-order-cleanup.ts +76 -0
  139. package/src/modules/organization/service.ts +191 -0
  140. package/src/modules/payments/adapter.ts +47 -0
  141. package/src/modules/payments/repository/index.ts +6 -0
  142. package/src/modules/payments/service.ts +107 -0
  143. package/src/modules/pricing/repository/index.ts +291 -0
  144. package/src/modules/pricing/schema.ts +71 -0
  145. package/src/modules/pricing/schemas.ts +38 -0
  146. package/src/modules/pricing/service.ts +494 -0
  147. package/src/modules/promotions/repository/index.ts +325 -0
  148. package/src/modules/promotions/schema.ts +62 -0
  149. package/src/modules/promotions/schemas.ts +38 -0
  150. package/src/modules/promotions/service.ts +598 -0
  151. package/src/modules/search/adapter.ts +57 -0
  152. package/src/modules/search/hooks.ts +12 -0
  153. package/src/modules/search/repository/index.ts +6 -0
  154. package/src/modules/search/service.ts +315 -0
  155. package/src/modules/shipping/calculator.ts +188 -0
  156. package/src/modules/shipping/repository/index.ts +6 -0
  157. package/src/modules/shipping/service.ts +51 -0
  158. package/src/modules/tax/adapter.ts +60 -0
  159. package/src/modules/tax/repository/index.ts +6 -0
  160. package/src/modules/tax/service.ts +53 -0
  161. package/src/modules/webhooks/hook.ts +34 -0
  162. package/src/modules/webhooks/repository/index.ts +278 -0
  163. package/src/modules/webhooks/schema.ts +56 -0
  164. package/src/modules/webhooks/service.ts +117 -0
  165. package/src/modules/webhooks/signing.ts +6 -0
  166. package/src/modules/webhooks/ssrf-guard.ts +71 -0
  167. package/src/modules/webhooks/tasks.ts +52 -0
  168. package/src/modules/webhooks/worker.ts +134 -0
  169. package/src/runtime/commerce.ts +145 -0
  170. package/src/runtime/kernel.ts +426 -0
  171. package/src/runtime/logger.ts +36 -0
  172. package/src/runtime/server.ts +355 -0
  173. package/src/runtime/shutdown.ts +43 -0
  174. package/src/test-utils/create-pglite-adapter.ts +129 -0
  175. package/src/test-utils/create-plugin-test-app.ts +128 -0
  176. package/src/test-utils/create-repository-test-harness.ts +16 -0
  177. package/src/test-utils/create-test-config.ts +190 -0
  178. package/src/test-utils/create-test-kernel.ts +7 -0
  179. package/src/test-utils/create-test-plugin-context.ts +75 -0
  180. package/src/test-utils/rest-api-test-utils.ts +265 -0
  181. package/src/test-utils/test-actors.ts +62 -0
  182. package/src/test-utils/typed-hooks.ts +54 -0
  183. package/src/types/commerce-types.ts +34 -0
  184. package/src/utils/id.ts +3 -0
  185. package/src/utils/logger.ts +18 -0
  186. 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
+ });