@mettlecast/domain-cli 0.2.60 → 0.2.62

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 (107) hide show
  1. package/dist/builder/build-registry.d.ts +1 -1
  2. package/dist/builder/build-registry.js +1 -36
  3. package/dist/builder/build-types.d.ts +1 -1
  4. package/dist/builder/load-module.d.ts +1 -1
  5. package/dist/cli.js +21 -3
  6. package/dist/commands/add-api.js +2 -2
  7. package/dist/commands/add-domain.js +4 -4
  8. package/dist/commands/add-fixture-factory.js +5 -6
  9. package/dist/commands/build-catalog.d.ts +6 -22
  10. package/dist/commands/build-catalog.js +7 -18
  11. package/dist/commands/build.js +2 -1
  12. package/dist/commands/check-hashes.d.ts +2 -0
  13. package/dist/commands/check-hashes.js +8 -0
  14. package/dist/commands/dev.js +1 -1
  15. package/dist/commands/doctor.js +70 -41
  16. package/dist/commands/explain.js +13 -13
  17. package/dist/commands/generate-openapi.d.ts +10 -1
  18. package/dist/commands/generate-openapi.js +19 -33
  19. package/dist/commands/regenerate-modules-hashes.d.ts +25 -0
  20. package/dist/commands/regenerate-modules-hashes.js +58 -0
  21. package/dist/commands/show.d.ts +2 -3
  22. package/dist/commands/show.js +0 -2
  23. package/dist/commands/test.js +0 -1
  24. package/dist/commands/update-all.d.ts +21 -0
  25. package/dist/commands/update-all.js +62 -0
  26. package/dist/commands/upgrade-backend.js +3 -3
  27. package/dist/commands/validate.js +12 -90
  28. package/dist/server/api-server.d.ts +1 -1
  29. package/dist/server/mount-routes.d.ts +11 -2
  30. package/dist/server/mount-routes.js +20 -8
  31. package/dist/templates/api-skeleton.d.ts +5 -0
  32. package/dist/templates/api-skeleton.js +28 -27
  33. package/dist/templates/claude-md.js +1 -1
  34. package/dist/templates/patterns/api/create-with-event.d.ts +4 -0
  35. package/dist/templates/patterns/api/create-with-event.js +38 -32
  36. package/dist/templates/patterns/api/idempotent-mutation.d.ts +4 -0
  37. package/dist/templates/patterns/api/idempotent-mutation.js +47 -41
  38. package/dist/templates/patterns/api/paginated-list.d.ts +4 -0
  39. package/dist/templates/patterns/api/paginated-list.js +30 -24
  40. package/dist/templates/patterns/api/simple-crud.d.ts +4 -0
  41. package/dist/templates/patterns/api/simple-crud.js +46 -35
  42. package/dist/templates/patterns/api/streaming-list.d.ts +4 -0
  43. package/dist/templates/patterns/api/streaming-list.js +46 -41
  44. package/dist/templates/patterns/api/system-admin.d.ts +4 -0
  45. package/dist/templates/patterns/api/system-admin.js +59 -52
  46. package/dist/templates/patterns/api/webhook-receiver-style.d.ts +4 -0
  47. package/dist/templates/patterns/api/webhook-receiver-style.js +43 -35
  48. package/dist/types.d.ts +100 -0
  49. package/dist/types.js +1 -0
  50. package/dist/utils/file-helpers.d.ts +0 -2
  51. package/dist/utils/file-helpers.js +2 -3
  52. package/dist/utils/manifest.d.ts +1 -0
  53. package/dist/utils/manifest.js +16 -1
  54. package/dist/utils/scaffold-config.d.ts +6 -0
  55. package/dist/utils/scaffold-config.js +2 -0
  56. package/package.json +1 -1
  57. package/src/__tests__/build-registry.test.ts +43 -20
  58. package/src/__tests__/build-types.test.ts +4 -7
  59. package/src/__tests__/builder/walkDomainDir.test.ts +19 -21
  60. package/src/__tests__/commands/add-api.test.ts +12 -10
  61. package/src/__tests__/commands/add-domain.test.ts +8 -5
  62. package/src/__tests__/commands/build-flows.test.ts +55 -0
  63. package/src/__tests__/commands/check-hashes.test.ts +31 -0
  64. package/src/__tests__/commands/create-project.test.ts +5 -5
  65. package/src/__tests__/commands/dev.test.ts +0 -1
  66. package/src/__tests__/commands/regenerate-modules-hashes.test.ts +170 -0
  67. package/src/__tests__/commands/update-all.test.ts +322 -0
  68. package/src/__tests__/doctor.test.ts +73 -0
  69. package/src/__tests__/mount-routes.test.ts +64 -23
  70. package/src/__tests__/package-freshness.test.ts +1 -21
  71. package/src/__tests__/smoke/scaffold.test.ts +13 -15
  72. package/src/__tests__/utils/manifest.test.ts +128 -0
  73. package/src/__tests__/validate.test.ts +21 -103
  74. package/src/builder/build-registry.ts +7 -44
  75. package/src/builder/build-types.ts +1 -1
  76. package/src/cli.ts +23 -3
  77. package/src/commands/add-api.ts +2 -2
  78. package/src/commands/add-domain.ts +4 -4
  79. package/src/commands/add-fixture-factory.ts +5 -6
  80. package/src/commands/build-catalog.ts +13 -35
  81. package/src/commands/build.ts +3 -2
  82. package/src/commands/check-hashes.ts +12 -0
  83. package/src/commands/dev.ts +1 -1
  84. package/src/commands/doctor.ts +72 -41
  85. package/src/commands/explain.ts +13 -13
  86. package/src/commands/generate-openapi.ts +30 -52
  87. package/src/commands/regenerate-modules-hashes.ts +89 -0
  88. package/src/commands/show.ts +2 -5
  89. package/src/commands/test.ts +0 -1
  90. package/src/commands/update-all.ts +79 -0
  91. package/src/commands/upgrade-backend.ts +3 -3
  92. package/src/commands/validate.ts +11 -96
  93. package/src/server/api-server.ts +1 -1
  94. package/src/server/mount-routes.ts +21 -10
  95. package/src/templates/api-skeleton.ts +29 -28
  96. package/src/templates/claude-md.ts +1 -1
  97. package/src/templates/patterns/api/create-with-event.ts +39 -33
  98. package/src/templates/patterns/api/idempotent-mutation.ts +48 -42
  99. package/src/templates/patterns/api/paginated-list.ts +31 -25
  100. package/src/templates/patterns/api/simple-crud.ts +47 -36
  101. package/src/templates/patterns/api/streaming-list.ts +47 -42
  102. package/src/templates/patterns/api/system-admin.ts +60 -53
  103. package/src/templates/patterns/api/webhook-receiver-style.ts +48 -40
  104. package/src/types.ts +128 -0
  105. package/src/utils/file-helpers.ts +2 -5
  106. package/src/utils/manifest.ts +16 -1
  107. package/src/utils/scaffold-config.ts +9 -0
@@ -1,5 +1,10 @@
1
1
  /**
2
2
  * Template for generating API handler skeleton.
3
+ *
4
+ * Action-first contract: HTTP endpoints are declared with `defineAction`
5
+ * + `exposure: { type: 'api', ... }`. The legacy `defineApi` factory was
6
+ * removed in #4689 — the only public HTTP endpoint primitive is now an
7
+ * action whose exposure type is `'api'`.
3
8
  */
4
9
  /**
5
10
  * Convert kebab-case to camelCase.
@@ -19,45 +24,41 @@ function camelCase(s) {
19
24
  export function apiSkeletonTemplate(domainId, apiId, tenancy) {
20
25
  const tenantPath = tenancy === 'required' ? '/v1/tenants/{tenantId}' : '/v1';
21
26
  const outputType = `${camelCase(domainId)}${camelCase(apiId)}Output`;
27
+ // The `auth` posture must be set explicitly per the action-first contract:
28
+ // - 'required' for tenant-scoped or any authenticated endpoint.
29
+ // - 'none' for anonymous endpoints (then add `securityException`).
30
+ // - 'service' for service-to-service only.
31
+ const authPosture = tenancy === 'system' ? 'service' : 'required';
22
32
  return `import { z } from 'zod';
23
- import { defineApi } from '@mettlecast/domain-runtime';
24
- import { createKyClient, initOtel } from '@mettlecast/domain-runtime';
33
+ import { defineAction, initOtel } from '@mettlecast/domain-runtime';
25
34
  import type { Result, AppError } from '@mettlecast/domain-runtime';
26
35
  import { ok, err, notFound } from '@mettlecast/domain-runtime';
27
36
 
28
37
  // Initialise OpenTelemetry tracing for the process. Idempotent —
29
38
  // safe to call at module scope. Sets up OTLP export (when
30
- // OTEL_EXPORTER_OTLP_ENDPOINT is set) or console-export for
31
- // local dev. Auto-instruments outbound HTTP calls via ky.
39
+ // OTEL_EXPORTER_OTLP_ENDPOINT is set) or console-export for local dev.
32
40
  initOtel();
33
41
 
34
- // Shared HTTP client with retry on 503/429. Replace this with
35
- // your own client if you need custom headers or auth.
36
- const http = createKyClient();
37
-
38
42
  const outputSchema = z.object({ ok: z.boolean() }).default({ ok: true });
39
43
 
40
- export const ${camelCase(apiId)} = defineApi({
44
+ export const ${camelCase(apiId)} = defineAction({
41
45
  id: '${apiId}',
42
- path: '${tenantPath}/${domainId}/${apiId}',
43
- method: 'POST',
44
- tenancy: '${tenancy}',
45
- outboundAccess: 'internal',
46
- versions: {
47
- v1: {
48
- status: 'stable',
49
- input: z.object({}).default({}),
50
- output: outputSchema,
51
- handler: async (_input, _ctx): Promise<Result<z.infer<typeof outputSchema>, AppError>> => {
52
- // Replace this with your handler logic.
53
- // Return ok(value) on success, err({ ... }) on failure.
54
- return ok({ ok: true });
55
- },
56
- },
46
+ backendAccess: 'domain',
47
+ exposure: {
48
+ type: 'api',
49
+ path: '${tenantPath}/${domainId}/${apiId}',
50
+ method: 'POST',
51
+ auth: '${authPosture}',
52
+ tenancy: '${tenancy}',
57
53
  },
58
- examples: {
59
- request: {},
60
- response: { ok: true },
54
+ outboundAccess: 'internal',
55
+ idempotent: false,
56
+ input: z.object({}).default({}),
57
+ output: outputSchema,
58
+ handler: async (_input, _ctx): Promise<Result<z.infer<typeof outputSchema>, AppError>> => {
59
+ // Replace this with your handler logic.
60
+ // Return ok(value) on success, err({ ... }) on failure.
61
+ return ok({ ok: true });
61
62
  },
62
63
  });
63
64
  `;
@@ -3,5 +3,5 @@
3
3
  */
4
4
  export function claudeMdTemplate(id) {
5
5
  const titleName = id.replace(/-/g, ' ').replace(/\b\w/g, c => c.toUpperCase());
6
- return `# ${titleName} Domain — Ownership\n\nYou are the domain owner for \`${id}\`.\n\n## Responsibilities\n\n- All source files under \`domains/${id}/\` are owned by you.\n- API handlers must use \`defineApi\` from \`@mettlecast/domain-runtime\`.\n- Cross-domain calls must use \`ctx.actions.call()\`, never direct imports.\n- AWS SDK must never be imported directly — use \`ctx\` helpers.\n\n## Scaffold Rules\n\nSee the root \`CLAUDE.md\` for full scaffold rules, anti-patterns, and workflow commands.\n\n## Domain Config\n\nSee \`domains/${id}/domain.config.ts\` for the domain definition.\n`;
6
+ return `# ${titleName} Domain — Ownership\n\nYou are the domain owner for \`${id}\`.\n\n## Responsibilities\n\n- All source files under \`domains/${id}/\` are owned by you.\n- Public HTTP endpoints must use \`defineAction({ exposure: { type: 'api', ... } })\` from \`@mettlecast/domain-runtime\`. The legacy \`defineApi\` factory is no longer scaffolded for public handlers.\n- Internal-only callable handlers must use \`defineAction({ exposure: { type: 'internal' } })\`.\n- Cross-domain calls must use \`ctx.actions.call()\`, never direct imports.\n- AWS SDK must never be imported directly — use \`ctx\` helpers.\n\n## Scaffold Rules\n\nSee the root \`CLAUDE.md\` for full scaffold rules, anti-patterns, and workflow commands.\n\n## Domain Config\n\nSee \`domains/${id}/domain.config.ts\` for the domain definition.\n`;
7
7
  }
@@ -1,6 +1,10 @@
1
1
  /**
2
2
  * Create-with-event API pattern template.
3
3
  * Produces: POST handler + publishes a domain event after mutation.
4
+ *
5
+ * Action-first: HTTP endpoints are declared with `defineAction` +
6
+ * `exposure: { type: 'api', ... }`. The legacy `defineApi` factory was
7
+ * removed in #4689.
4
8
  */
5
9
  export declare function createWithEventTemplate(domain: string, id: string, tenancy: string): string;
6
10
  /**
@@ -1,6 +1,10 @@
1
1
  /**
2
2
  * Create-with-event API pattern template.
3
3
  * Produces: POST handler + publishes a domain event after mutation.
4
+ *
5
+ * Action-first: HTTP endpoints are declared with `defineAction` +
6
+ * `exposure: { type: 'api', ... }`. The legacy `defineApi` factory was
7
+ * removed in #4689.
4
8
  */
5
9
  function camelCase(s) {
6
10
  return s.replace(/-([a-z])/g, (_, c) => c.toUpperCase());
@@ -8,9 +12,9 @@ function camelCase(s) {
8
12
  export function createWithEventTemplate(domain, id, tenancy) {
9
13
  const varName = camelCase(id);
10
14
  const tenantPath = tenancy === 'required' ? '/v1/tenants/{tenantId}' : '/v1';
15
+ const authPosture = tenancy === 'system' ? 'service' : 'required';
11
16
  return `import { z } from 'zod';
12
- import { defineApi } from '@mettlecast/domain-runtime';
13
- import { createKyClient, initOtel } from '@mettlecast/domain-runtime';
17
+ import { defineAction, initOtel } from '@mettlecast/domain-runtime';
14
18
  import type { Result, AppError } from '@mettlecast/domain-runtime';
15
19
  import { ok, err, notFound } from '@mettlecast/domain-runtime';
16
20
 
@@ -31,43 +35,45 @@ const ${varName}Output = z.object({
31
35
  status: 'created',
32
36
  });
33
37
 
34
- // ── API definition ───────────────────────────────────────────
38
+ // ── Action definition (API exposure) ─────────────────────────
35
39
 
36
- export const ${varName} = defineApi({
40
+ export const ${varName} = defineAction({
37
41
  id: '${id}',
38
- path: '${tenantPath}/${domain}/${id}',
39
- tenancy: '${tenancy}',
42
+ backendAccess: 'domain',
43
+ exposure: {
44
+ type: 'api',
45
+ path: '${tenantPath}/${domain}/${id}',
46
+ method: 'POST',
47
+ auth: '${authPosture}',
48
+ tenancy: '${tenancy}',
49
+ },
40
50
  outboundAccess: 'internal',
41
- versions: {
42
- v1: {
43
- status: 'stable',
44
- input: ${varName}Input,
45
- output: ${varName}Output,
46
- handler: async (input, ctx) => {
47
- const entityId = crypto.randomUUID();
51
+ idempotent: false,
52
+ input: ${varName}Input,
53
+ output: ${varName}Output,
54
+ handler: async (input, ctx) => {
55
+ const entityId = crypto.randomUUID();
48
56
 
49
- // ── Persist entity ────────────────────────────────────
50
- await ctx.store.put(entityId, { id: entityId, name: input.name });
57
+ // ── Persist entity ────────────────────────────────────
58
+ await ctx.store.put(entityId, { id: entityId, name: input.name });
51
59
 
52
- // ── Publish domain event ──────────────────────────────
53
- await ctx.publish(
54
- '${domain}.${id}.created',
55
- {
56
- id: entityId,
57
- name: input.name,
58
- tenantId: ctx.tenant.id,
59
- },
60
- 1,
61
- );
60
+ // ── Publish domain event ──────────────────────────────
61
+ await ctx.publish(
62
+ '${domain}.${id}.created',
63
+ {
64
+ id: entityId,
65
+ name: input.name,
66
+ tenantId: ctx.tenant.id,
67
+ },
68
+ 1,
69
+ );
62
70
 
63
- await ctx.audit.log('${id}.create', entityId, { tenantId: ctx.tenant.id });
71
+ await ctx.audit.log('${id}.create', entityId, { tenantId: ctx.tenant.id });
64
72
 
65
- return {
66
- id: entityId,
67
- status: 'created',
68
- };
69
- },
70
- },
73
+ return {
74
+ id: entityId,
75
+ status: 'created',
76
+ };
71
77
  },
72
78
  });
73
79
  `;
@@ -1,6 +1,10 @@
1
1
  /**
2
2
  * Idempotent-mutation API pattern template.
3
3
  * Produces: PUT with idempotency key middleware.
4
+ *
5
+ * Action-first: HTTP endpoints are declared with `defineAction` +
6
+ * `exposure: { type: 'api', ... }`. The legacy `defineApi` factory was
7
+ * removed in #4689.
4
8
  */
5
9
  export declare function idempotentMutationTemplate(domain: string, id: string, tenancy: string): string;
6
10
  /**
@@ -1,6 +1,10 @@
1
1
  /**
2
2
  * Idempotent-mutation API pattern template.
3
3
  * Produces: PUT with idempotency key middleware.
4
+ *
5
+ * Action-first: HTTP endpoints are declared with `defineAction` +
6
+ * `exposure: { type: 'api', ... }`. The legacy `defineApi` factory was
7
+ * removed in #4689.
4
8
  */
5
9
  function camelCase(s) {
6
10
  return s.replace(/-([a-z])/g, (_, c) => c.toUpperCase());
@@ -8,9 +12,9 @@ function camelCase(s) {
8
12
  export function idempotentMutationTemplate(domain, id, tenancy) {
9
13
  const varName = camelCase(id);
10
14
  const tenantPath = tenancy === 'required' ? '/v1/tenants/{tenantId}' : '/v1';
15
+ const authPosture = tenancy === 'system' ? 'service' : 'required';
11
16
  return `import { z } from 'zod';
12
- import { defineApi } from '@mettlecast/domain-runtime';
13
- import { createKyClient, initOtel } from '@mettlecast/domain-runtime';
17
+ import { defineAction, initOtel } from '@mettlecast/domain-runtime';
14
18
  import type { Result, AppError } from '@mettlecast/domain-runtime';
15
19
  import { ok, err, notFound } from '@mettlecast/domain-runtime';
16
20
 
@@ -36,53 +40,55 @@ const ${varName}Output = z.object({
36
40
  idempotencyKey: '00000000-0000-0000-0000-000000000000',
37
41
  });
38
42
 
39
- // ── API definition ───────────────────────────────────────────
43
+ // ── Action definition (API exposure) ─────────────────────────
40
44
 
41
- export const ${varName} = defineApi({
45
+ export const ${varName} = defineAction({
42
46
  id: '${id}',
43
- path: '${tenantPath}/${domain}/${id}',
44
- tenancy: '${tenancy}',
47
+ backendAccess: 'domain',
48
+ exposure: {
49
+ type: 'api',
50
+ path: '${tenantPath}/${domain}/${id}',
51
+ method: 'PUT',
52
+ auth: '${authPosture}',
53
+ tenancy: '${tenancy}',
54
+ },
45
55
  outboundAccess: 'internal',
46
- versions: {
47
- v1: {
48
- status: 'stable',
49
- input: ${varName}Input,
50
- output: ${varName}Output,
51
- handler: async (input, ctx) => {
52
- // ── Check for duplicate (idempotency key = input.id) ──
53
- const existing = await ctx.store.get('IDEMPOTENT#' + input.id);
56
+ idempotent: true,
57
+ input: ${varName}Input,
58
+ output: ${varName}Output,
59
+ handler: async (input, ctx) => {
60
+ // ── Check for duplicate (idempotency key = input.id) ──
61
+ const existing = await ctx.store.get('IDEMPOTENT#' + input.id);
54
62
 
55
- if (existing) {
56
- await ctx.audit.log('${id}.idempotent.duplicate', input.id);
57
- return {
58
- id: input.id,
59
- status: 'already-processed',
60
- idempotencyKey: input.id,
61
- };
62
- }
63
+ if (existing) {
64
+ await ctx.audit.log('${id}.idempotent.duplicate', input.id);
65
+ return {
66
+ id: input.id,
67
+ status: 'already-processed',
68
+ idempotencyKey: input.id,
69
+ };
70
+ }
63
71
 
64
- // ── Apply mutation ────────────────────────────────────
65
- await ctx.store.put(
66
- '${id.toUpperCase()}#' + input.id,
67
- { id: input.id, payload: input.payload, createdAt: new Date().toISOString() },
68
- );
72
+ // ── Apply mutation ────────────────────────────────────
73
+ await ctx.store.put(
74
+ '${id.toUpperCase()}#' + input.id,
75
+ { id: input.id, payload: input.payload, createdAt: new Date().toISOString() },
76
+ );
69
77
 
70
- // ── Record idempotency token with 24h TTL ─────────────
71
- await ctx.store.put(
72
- 'IDEMPOTENT#' + input.id,
73
- { entityId: input.id },
74
- { ttl: Math.floor(Date.now() / 1000) + 86400 },
75
- );
78
+ // ── Record idempotency token with 24h TTL ─────────────
79
+ await ctx.store.put(
80
+ 'IDEMPOTENT#' + input.id,
81
+ { entityId: input.id },
82
+ { ttl: Math.floor(Date.now() / 1000) + 86400 },
83
+ );
76
84
 
77
- await ctx.audit.log('${id}.idempotent.applied', input.id, { tenantId: ctx.tenant.id });
85
+ await ctx.audit.log('${id}.idempotent.applied', input.id, { tenantId: ctx.tenant.id });
78
86
 
79
- return {
80
- id: input.id,
81
- status: 'applied',
82
- idempotencyKey: input.id,
83
- };
84
- },
85
- },
87
+ return {
88
+ id: input.id,
89
+ status: 'applied',
90
+ idempotencyKey: input.id,
91
+ };
86
92
  },
87
93
  });
88
94
  `;
@@ -1,6 +1,10 @@
1
1
  /**
2
2
  * Paginated-list API pattern template.
3
3
  * Produces: GET with cursor/limit, pagination contract, EMF metrics.
4
+ *
5
+ * Action-first: HTTP endpoints are declared with `defineAction` +
6
+ * `exposure: { type: 'api', ... }`. The legacy `defineApi` factory was
7
+ * removed in #4689.
4
8
  */
5
9
  export declare function paginatedListTemplate(domain: string, id: string, tenancy: string): string;
6
10
  /**
@@ -1,6 +1,10 @@
1
1
  /**
2
2
  * Paginated-list API pattern template.
3
3
  * Produces: GET with cursor/limit, pagination contract, EMF metrics.
4
+ *
5
+ * Action-first: HTTP endpoints are declared with `defineAction` +
6
+ * `exposure: { type: 'api', ... }`. The legacy `defineApi` factory was
7
+ * removed in #4689.
4
8
  */
5
9
  function camelCase(s) {
6
10
  return s.replace(/-([a-z])/g, (_, c) => c.toUpperCase());
@@ -8,9 +12,9 @@ function camelCase(s) {
8
12
  export function paginatedListTemplate(domain, id, tenancy) {
9
13
  const varName = camelCase(id);
10
14
  const tenantPath = tenancy === 'required' ? '/v1/tenants/{tenantId}' : '/v1';
15
+ const authPosture = tenancy === 'system' ? 'service' : 'required';
11
16
  return `import { z } from 'zod';
12
- import { defineApi } from '@mettlecast/domain-runtime';
13
- import { createKyClient, initOtel } from '@mettlecast/domain-runtime';
17
+ import { defineAction, initOtel } from '@mettlecast/domain-runtime';
14
18
  import type { Result, AppError } from '@mettlecast/domain-runtime';
15
19
  import { ok, err, notFound } from '@mettlecast/domain-runtime';
16
20
 
@@ -35,34 +39,36 @@ const PageOutput = z.object({
35
39
  total: z.number().int().optional(),
36
40
  }).default({ items: [], nextCursor: undefined, total: undefined });
37
41
 
38
- // ── API definition ───────────────────────────────────────────
42
+ // ── Action definition (API exposure) ─────────────────────────
39
43
 
40
- export const ${varName} = defineApi({
44
+ export const ${varName} = defineAction({
41
45
  id: '${id}',
42
- path: '${tenantPath}/${domain}/${id}',
43
- tenancy: '${tenancy}',
46
+ backendAccess: 'domain',
47
+ exposure: {
48
+ type: 'api',
49
+ path: '${tenantPath}/${domain}/${id}',
50
+ method: 'GET',
51
+ auth: '${authPosture}',
52
+ tenancy: '${tenancy}',
53
+ },
44
54
  outboundAccess: 'internal',
45
- versions: {
46
- v1: {
47
- status: 'stable',
48
- input: PageInput,
49
- output: PageOutput,
50
- handler: async (input, ctx) => {
51
- const { cursor, limit, filter } = input;
55
+ idempotent: true,
56
+ input: PageInput,
57
+ output: PageOutput,
58
+ handler: async (input, ctx) => {
59
+ const { cursor, limit, filter } = input;
52
60
 
53
- await ctx.audit.log('${id}.list', ctx.tenant.id, { cursor, limit });
61
+ await ctx.audit.log('${id}.list', ctx.tenant.id, { cursor, limit });
54
62
 
55
- // ── paginated data fetch ──────────────────────────────
56
- const items: Array<{ id: string }> = [];
57
- const nextCursor = items.length === limit ? btoa(items[items.length - 1].id) : undefined;
63
+ // ── paginated data fetch ──────────────────────────────
64
+ const items: Array<{ id: string }> = [];
65
+ const nextCursor = items.length === limit ? btoa(items[items.length - 1].id) : undefined;
58
66
 
59
- return {
60
- items,
61
- nextCursor,
62
- total: undefined,
63
- };
64
- },
65
- },
67
+ return {
68
+ items,
69
+ nextCursor,
70
+ total: undefined,
71
+ };
66
72
  },
67
73
  });
68
74
  `;
@@ -1,6 +1,10 @@
1
1
  /**
2
2
  * Simple CRUD API pattern template.
3
3
  * Produces: GET / POST / PUT / DELETE with Zod schemas and audit logging.
4
+ *
5
+ * Action-first: HTTP endpoints are declared with `defineAction` +
6
+ * `exposure: { type: 'api', ... }`. The legacy `defineApi` factory was
7
+ * removed in #4689.
4
8
  */
5
9
  export declare function simpleCrudTemplate(domain: string, id: string, tenancy: string): string;
6
10
  export declare function simpleCrudListTemplate(domain: string, id: string, tenancy: string): string;
@@ -1,6 +1,10 @@
1
1
  /**
2
2
  * Simple CRUD API pattern template.
3
3
  * Produces: GET / POST / PUT / DELETE with Zod schemas and audit logging.
4
+ *
5
+ * Action-first: HTTP endpoints are declared with `defineAction` +
6
+ * `exposure: { type: 'api', ... }`. The legacy `defineApi` factory was
7
+ * removed in #4689.
4
8
  */
5
9
  function camelCase(s) {
6
10
  return s.replace(/-([a-z])/g, (_, c) => c.toUpperCase());
@@ -8,9 +12,9 @@ function camelCase(s) {
8
12
  export function simpleCrudTemplate(domain, id, tenancy) {
9
13
  const varName = camelCase(id);
10
14
  const tenantPath = tenancy === 'required' ? '/v1/tenants/{tenantId}' : '/v1';
15
+ const authPosture = tenancy === 'system' ? 'service' : 'required';
11
16
  return `import { z } from 'zod';
12
- import { defineApi } from '@mettlecast/domain-runtime';
13
- import { createKyClient, initOtel } from '@mettlecast/domain-runtime';
17
+ import { defineAction, initOtel } from '@mettlecast/domain-runtime';
14
18
  import type { Result, AppError } from '@mettlecast/domain-runtime';
15
19
  import { ok, err, notFound } from '@mettlecast/domain-runtime';
16
20
 
@@ -37,28 +41,30 @@ const ${varName}ListOutput = z.object({
37
41
  nextCursor: z.string().optional(),
38
42
  }).default({ items: [], nextCursor: undefined });
39
43
 
40
- // ── API definition ───────────────────────────────────────────
44
+ // ── Action definition (API exposure) ─────────────────────────
41
45
 
42
- export const ${varName} = defineApi({
46
+ export const ${varName} = defineAction({
43
47
  id: '${id}',
44
- path: '${tenantPath}/${domain}/${id}',
45
- tenancy: '${tenancy}',
48
+ backendAccess: 'domain',
49
+ exposure: {
50
+ type: 'api',
51
+ path: '${tenantPath}/${domain}/${id}',
52
+ method: 'POST',
53
+ auth: '${authPosture}',
54
+ tenancy: '${tenancy}',
55
+ },
46
56
  outboundAccess: 'internal',
47
- versions: {
48
- v1: {
49
- status: 'stable',
50
- input: ${varName}Input,
51
- output: ${varName}Output,
52
- handler: async (input, ctx) => {
53
- // Create / Read / Update / Delete based on method
54
- await ctx.audit.log('${id}.v1', ctx.tenant.id, { input });
55
- return {
56
- id: input.id ?? crypto.randomUUID(),
57
- createdAt: new Date().toISOString(),
58
- updatedAt: new Date().toISOString(),
59
- };
60
- },
61
- },
57
+ idempotent: false,
58
+ input: ${varName}Input,
59
+ output: ${varName}Output,
60
+ handler: async (input, ctx) => {
61
+ // Create / Read / Update / Delete based on method
62
+ await ctx.audit.log('${id}.v1', ctx.tenant.id, { input });
63
+ return {
64
+ id: input.id ?? crypto.randomUUID(),
65
+ createdAt: new Date().toISOString(),
66
+ updatedAt: new Date().toISOString(),
67
+ };
62
68
  },
63
69
  });
64
70
  `;
@@ -66,8 +72,11 @@ export const ${varName} = defineApi({
66
72
  export function simpleCrudListTemplate(domain, id, tenancy) {
67
73
  const varName = camelCase(id);
68
74
  const tenantPath = tenancy === 'required' ? '/v1/tenants/{tenantId}' : '/v1';
75
+ const authPosture = tenancy === 'system' ? 'service' : 'required';
69
76
  return `import { z } from 'zod';
70
- import { defineApi } from '@mettlecast/domain-runtime';
77
+ import { defineAction, initOtel } from '@mettlecast/domain-runtime';
78
+
79
+ initOtel();
71
80
 
72
81
  const ${varName}ListInput = z.object({
73
82
  cursor: z.string().optional(),
@@ -89,21 +98,23 @@ const ${varName}ListOutput = z.object({
89
98
  nextCursor: z.string().optional(),
90
99
  }).default({ items: [], nextCursor: undefined });
91
100
 
92
- export const ${varName}List = defineApi({
101
+ export const ${varName}List = defineAction({
93
102
  id: '${id}-list',
94
- path: '${tenantPath}/${domain}/${id}-list',
95
- tenancy: '${tenancy}',
103
+ backendAccess: 'domain',
104
+ exposure: {
105
+ type: 'api',
106
+ path: '${tenantPath}/${domain}/${id}-list',
107
+ method: 'GET',
108
+ auth: '${authPosture}',
109
+ tenancy: '${tenancy}',
110
+ },
96
111
  outboundAccess: 'internal',
97
- versions: {
98
- v1: {
99
- status: 'stable',
100
- input: ${varName}ListInput,
101
- output: ${varName}ListOutput,
102
- handler: async (input, ctx) => {
103
- await ctx.audit.log('${id}-list.v1', ctx.tenant.id);
104
- return { items: [], nextCursor: undefined };
105
- },
106
- },
112
+ idempotent: true,
113
+ input: ${varName}ListInput,
114
+ output: ${varName}ListOutput,
115
+ handler: async (input, ctx) => {
116
+ await ctx.audit.log('${id}-list.v1', ctx.tenant.id);
117
+ return { items: [], nextCursor: undefined };
107
118
  },
108
119
  });
109
120
  `;
@@ -19,6 +19,10 @@
19
19
  * - API Gateway HTTP API with 2.0 payload format (not REST API)
20
20
  * - Lambda runtime v20+
21
21
  *
22
+ * Action-first: HTTP endpoints are declared with `defineAction` +
23
+ * `exposure: { type: 'api', ... }`. The legacy `defineApi` factory was
24
+ * removed in #4689.
25
+ *
22
26
  * @param domain - Domain ID in kebab-case
23
27
  * @param apiId - API ID in kebab-case
24
28
  * @param itemName - Singular item name (e.g. 'ticket', 'member')