@rotorsoft/act-http 1.3.0 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,109 @@
1
+ import type { Act, Actor, Schema, Schemas } from "@rotorsoft/act";
2
+ import type { IdempotencyStore } from "@rotorsoft/act-ops/idempotency";
3
+ import type { Context, MiddlewareHandler } from "hono";
4
+ import { Hono } from "hono";
5
+ import { type ActorExtractor } from "../api/index.js";
6
+ /**
7
+ * Hono context variables this subpath contributes. The generic on
8
+ * the returned middleware threads it through so routes downstream
9
+ * of {@link authenticated} see `c.get("actor")` typed without a
10
+ * manual cast.
11
+ */
12
+ export type ActMiddlewareVariables = {
13
+ actor: Actor;
14
+ };
15
+ /**
16
+ * Per-call options for {@link hono}. The host supplies the four
17
+ * seams the package can't make decisions on:
18
+ *
19
+ * - `actor` — auth resolver. Receives the Hono context, returns the
20
+ * `Actor` that flows onto `c.get("actor")` for every generated
21
+ * route (via an internal {@link authenticated}).
22
+ * - `stream` — stream-id resolver. Receives the action name, the
23
+ * validated body, and the Hono context; returns the target
24
+ * stream. Singleton aggregates return a constant; per-tenant
25
+ * aggregates pull from headers, path, or body.
26
+ * - `expectedVersion` (optional) — optimistic-concurrency resolver.
27
+ * Hosts typically read it from an `If-Match` header on the
28
+ * request, or from the client's last-known snapshot. Returning
29
+ * `undefined` skips the check for that call — handy when only
30
+ * some actions are concurrency-sensitive.
31
+ * - `idempotency` (optional) — when set, the route honors
32
+ * `Idempotency-Key` via {@link withIdempotency}. The host
33
+ * supplies the `IdempotencyStore`; `keyFrom` defaults to reading
34
+ * the `Idempotency-Key` header. On a duplicate claim, the route
35
+ * responds `409 Conflict` — the contract intentionally doesn't
36
+ * cache the original handler's result, matching the
37
+ * receiver-side "ack the duplicate" semantics.
38
+ * - `basePath` (optional, default `"/api"`) — Hono basePath the
39
+ * routes mount under.
40
+ */
41
+ export type HonoOptions = {
42
+ readonly actor: ActorExtractor;
43
+ readonly stream: (action_name: string, input: unknown, c: Context) => string | Promise<string>;
44
+ readonly expectedVersion?: (action_name: string, input: unknown, c: Context) => number | undefined | Promise<number | undefined>;
45
+ readonly idempotency?: {
46
+ readonly store: IdempotencyStore;
47
+ readonly keyFrom?: (c: Context) => string | undefined;
48
+ };
49
+ readonly basePath?: string;
50
+ };
51
+ /**
52
+ * Build a Hono middleware that runs an {@link ActorExtractor} once
53
+ * per call and stashes the resolved {@link Actor} under
54
+ * `c.set("actor", ...)` so downstream routes can read it via
55
+ * `c.get("actor")`. The generator at {@link hono} uses this
56
+ * internally; it's also exported so hosts that want to compose
57
+ * their own route chain (logging, tracing, custom auth flavors)
58
+ * can wire it directly:
59
+ *
60
+ * ```ts
61
+ * import { Hono } from "hono";
62
+ * import { authenticated, type ActMiddlewareVariables } from "@rotorsoft/act-http/hono";
63
+ *
64
+ * const api = new Hono<{ Variables: ActMiddlewareVariables }>();
65
+ * api.use("*", authenticated((c) => resolveUserFromJwt(c)));
66
+ * api.get("/me", (c) => c.json(c.get("actor"))); // typed
67
+ * ```
68
+ *
69
+ * Errors thrown by the extractor surface as `401 Unauthorized` with
70
+ * the shared {@link ApiError} envelope.
71
+ */
72
+ export declare function authenticated(extractor: ActorExtractor): MiddlewareHandler<{
73
+ Variables: ActMiddlewareVariables;
74
+ }>;
75
+ /**
76
+ * Build a typed Hono REST surface from a built `Act` instance.
77
+ *
78
+ * Walks `app.registry.actions` once and emits one
79
+ * `POST /actions/<actionName>` per action under the configured
80
+ * `basePath` (default `/api`). Each route:
81
+ *
82
+ * 1. Runs the internal {@link authenticated} middleware —
83
+ * `options.actor` resolves the {@link Actor} once and stashes
84
+ * it under `c.get("actor")`.
85
+ * 2. Validates the JSON body against the action's Zod schema via
86
+ * `@hono/zod-validator`. Failures short-circuit with `422`.
87
+ * 3. Resolves the target stream via
88
+ * `options.stream(name, input, c)`.
89
+ * 4. (Optionally) resolves `expectedVersion` for optimistic
90
+ * concurrency.
91
+ * 5. (Optionally) claims the `Idempotency-Key` via
92
+ * {@link withIdempotency} — responds `409` on duplicate.
93
+ * 6. Calls
94
+ * `app.do(name, { stream, actor, expectedVersion? }, input)`.
95
+ * 7. Maps any thrown framework error onto the shared
96
+ * {@link ApiError} envelope via {@link toApiError}, returning
97
+ * the conventional HTTP status (`409` / `412` for concurrency,
98
+ * `422` for validation, `400` for non-retryable, etc.).
99
+ *
100
+ * @param app A built `Act` orchestrator.
101
+ * @param options Auth, stream, expected-version, idempotency, and
102
+ * base-path seams.
103
+ * @returns A Hono app covering every registered action under
104
+ * `<basePath>/actions/<name>`.
105
+ */
106
+ export declare function hono(app: Act<Schemas, Schemas, Schemas, Record<string, Schema>>, options: HonoOptions): Hono<{
107
+ Variables: ActMiddlewareVariables;
108
+ }>;
109
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hono/index.ts"],"names":[],"mappings":"AAgDA,OAAO,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAClE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gCAAgC,CAAC;AACvE,OAAO,KAAK,EAAE,OAAO,EAAE,iBAAiB,EAAE,MAAM,MAAM,CAAC;AACvD,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAC5B,OAAO,EACL,KAAK,cAAc,EAIpB,MAAM,iBAAiB,CAAC;AAEzB;;;;;GAKG;AACH,MAAM,MAAM,sBAAsB,GAAG;IACnC,KAAK,EAAE,KAAK,CAAC;CACd,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,MAAM,WAAW,GAAG;IACxB,QAAQ,CAAC,KAAK,EAAE,cAAc,CAAC;IAC/B,QAAQ,CAAC,MAAM,EAAE,CACf,WAAW,EAAE,MAAM,EACnB,KAAK,EAAE,OAAO,EACd,CAAC,EAAE,OAAO,KACP,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAC9B,QAAQ,CAAC,eAAe,CAAC,EAAE,CACzB,WAAW,EAAE,MAAM,EACnB,KAAK,EAAE,OAAO,EACd,CAAC,EAAE,OAAO,KACP,MAAM,GAAG,SAAS,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;IACtD,QAAQ,CAAC,WAAW,CAAC,EAAE;QACrB,QAAQ,CAAC,KAAK,EAAE,gBAAgB,CAAC;QACjC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC,EAAE,OAAO,KAAK,MAAM,GAAG,SAAS,CAAC;KACvD,CAAC;IACF,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;CAC5B,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,aAAa,CAC3B,SAAS,EAAE,cAAc,GACxB,iBAAiB,CAAC;IAAE,SAAS,EAAE,sBAAsB,CAAA;CAAE,CAAC,CAe1D;AAKD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAgB,IAAI,CAClB,GAAG,EAAE,GAAG,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,EAC3D,OAAO,EAAE,WAAW,GACnB,IAAI,CAAC;IAAE,SAAS,EAAE,sBAAsB,CAAA;CAAE,CAAC,CAyE7C"}
@@ -0,0 +1,191 @@
1
+ /**
2
+ * @packageDocumentation
3
+ * @module act-http/openapi
4
+ *
5
+ * OpenAPI subpath for the auto-generated API epic (#835). Walks the
6
+ * registry of a built `Act` once at function call and returns a
7
+ * valid OpenAPI 3.1 document describing the same action surface the
8
+ * Hono sibling at `@rotorsoft/act-http/hono` serves. The doc is
9
+ * suitable for cross-language client codegen (Go, Python, Swift,
10
+ * Kotlin, .NET), for serving over `/openapi.json` alongside a live
11
+ * API, and for the broader OpenAPI tooling ecosystem (Postman,
12
+ * Swagger UI, Redoc, schemathesis, API gateways).
13
+ *
14
+ * Usage:
15
+ *
16
+ * ```ts
17
+ * import { openapi } from "@rotorsoft/act-http/openapi";
18
+ *
19
+ * const doc = openapi(app, {
20
+ * info: { title: "Wolfdesk API", version: "1.0.0" },
21
+ * servers: [{ url: "https://api.example.com" }],
22
+ * });
23
+ * // doc: OpenAPIDocument — valid OpenAPI 3.1 object
24
+ * ```
25
+ *
26
+ * **Pure data emit.** No runtime dep on Hono or tRPC. The subpath is
27
+ * consumable standalone for hosts that serve their API some other
28
+ * way (Fastify, Lambda, edge) or that only want the doc for client
29
+ * codegen. The emit walks the registry once at function call; cost
30
+ * is linear in `N actions × M Zod fields each`, single-digit
31
+ * milliseconds for a 100-endpoint registry.
32
+ *
33
+ * **Determinism.** Output is stable across runs given the same
34
+ * registry — entries land in `Object.entries(app.registry.actions)`
35
+ * iteration order, schemas come from the action's Zod definition,
36
+ * and the rest of the doc is fixed content. CI can snapshot the
37
+ * result to catch unintended API surface changes.
38
+ *
39
+ * The same shared utilities at `@rotorsoft/act-http/api` underwrite
40
+ * cross-transport consistency: the `ApiError` envelope is referenced
41
+ * once from `components.schemas`, every error response points at it,
42
+ * and the same shape ships from the tRPC and Hono siblings at
43
+ * runtime. A client speaking the doc and the live REST API sees one
44
+ * envelope per framework error.
45
+ *
46
+ * **The doc describes the Hono surface, not tRPC.** tRPC's URL
47
+ * shape (`POST /trpc/<procedure>`, JSON-RPC-style body framing,
48
+ * batching) doesn't model cleanly as OpenAPI operations — and tRPC
49
+ * consumers already share types directly via `typeof router`, so
50
+ * OpenAPI buys them nothing. The path shape this emitter produces
51
+ * (`POST <basePath>/actions/<name>`) matches the
52
+ * `@rotorsoft/act-http/hono` sibling exactly: same default
53
+ * `basePath`, same request/response shapes, same envelope. If the
54
+ * operator overrides `basePath` on the Hono adapter, they pass the
55
+ * same override here so the doc keeps matching the live routes by
56
+ * construction. tRPC and Hono can run side-by-side at different
57
+ * mount points on the same Act instance; the OpenAPI doc covers
58
+ * the REST half only.
59
+ */
60
+ import type { Act, Schema, Schemas } from "@rotorsoft/act";
61
+ /**
62
+ * Minimal OpenAPI 3.1 document type. The emitter targets the 3.1
63
+ * spec; the type is intentionally a structural subset rather than
64
+ * the full surface from `openapi-types` so callers can post-process
65
+ * without fighting the type system. Cast to a fuller type from
66
+ * `openapi-types` when downstream consumers need it.
67
+ */
68
+ export type OpenAPIDocument = {
69
+ readonly openapi: "3.1.0";
70
+ readonly info: OpenAPIInfo;
71
+ readonly servers?: ReadonlyArray<OpenAPIServer>;
72
+ readonly paths: Record<string, OpenAPIPathItem>;
73
+ readonly components: OpenAPIComponents;
74
+ };
75
+ /**
76
+ * Per-call options for {@link openapi}. The host supplies the
77
+ * document-shape fields the registry can't derive (title, version,
78
+ * servers) and toggles for the cross-cutting headers that the live
79
+ * REST API may accept.
80
+ *
81
+ * - `info` — required. `title` and `version` must be non-empty;
82
+ * `info` may carry any other top-level OpenAPI info fields.
83
+ * - `servers` — optional. Each entry's `url` may contain
84
+ * `{variable}` template syntax; bare URLs are validated through
85
+ * `URL`'s parser and reject malformed inputs.
86
+ * - `basePath` — optional, default `/api`. Mirrors the Hono
87
+ * sibling's default so the doc describes the same paths the
88
+ * generated REST surface serves.
89
+ * - `idempotency` — optional, default `false`. When `true`, every
90
+ * mutation operation documents an optional `Idempotency-Key`
91
+ * request header.
92
+ * - `expectedVersion` — optional, default `false`. When `true`,
93
+ * every mutation operation documents an optional `If-Match`
94
+ * request header carrying the expected stream version.
95
+ */
96
+ export type OpenAPIOptions = {
97
+ readonly info: OpenAPIInfo;
98
+ readonly servers?: ReadonlyArray<OpenAPIServer>;
99
+ readonly basePath?: string;
100
+ readonly idempotency?: boolean;
101
+ readonly expectedVersion?: boolean;
102
+ };
103
+ export type OpenAPIInfo = {
104
+ readonly title: string;
105
+ readonly version: string;
106
+ readonly description?: string;
107
+ readonly summary?: string;
108
+ readonly termsOfService?: string;
109
+ readonly contact?: {
110
+ readonly name?: string;
111
+ readonly url?: string;
112
+ readonly email?: string;
113
+ };
114
+ readonly license?: {
115
+ readonly name: string;
116
+ readonly identifier?: string;
117
+ readonly url?: string;
118
+ };
119
+ };
120
+ export type OpenAPIServer = {
121
+ readonly url: string;
122
+ readonly description?: string;
123
+ readonly variables?: Record<string, {
124
+ readonly default: string;
125
+ readonly enum?: ReadonlyArray<string>;
126
+ readonly description?: string;
127
+ }>;
128
+ };
129
+ export type OpenAPIPathItem = {
130
+ readonly post?: OpenAPIOperation;
131
+ };
132
+ export type OpenAPIOperation = {
133
+ readonly operationId: string;
134
+ readonly summary: string;
135
+ readonly tags: ReadonlyArray<string>;
136
+ readonly parameters?: ReadonlyArray<OpenAPIParameter>;
137
+ readonly requestBody?: OpenAPIRequestBody;
138
+ readonly responses: Record<string, OpenAPIResponse>;
139
+ };
140
+ export type OpenAPIParameter = {
141
+ readonly name: string;
142
+ readonly in: "header" | "query" | "path" | "cookie";
143
+ readonly required?: boolean;
144
+ readonly description?: string;
145
+ readonly schema?: Record<string, unknown>;
146
+ };
147
+ export type OpenAPIRequestBody = {
148
+ readonly required?: boolean;
149
+ readonly content: Record<string, {
150
+ readonly schema: Record<string, unknown>;
151
+ }>;
152
+ };
153
+ export type OpenAPIResponse = {
154
+ readonly description: string;
155
+ readonly content?: Record<string, {
156
+ readonly schema: Record<string, unknown>;
157
+ }>;
158
+ } | {
159
+ readonly $ref: string;
160
+ };
161
+ export type OpenAPIComponents = {
162
+ readonly schemas: Record<string, Record<string, unknown>>;
163
+ readonly responses: Record<string, OpenAPIResponse>;
164
+ };
165
+ /**
166
+ * Build an OpenAPI 3.1 document describing a built `Act` instance's
167
+ * action surface.
168
+ *
169
+ * Walks `app.registry.actions` once and emits a `POST
170
+ * <basePath>/actions/<actionName>` operation per action, deriving
171
+ * the request-body schema from each action's Zod definition via
172
+ * `z.toJSONSchema` (Zod 4's native JSON Schema 2020-12 emitter,
173
+ * which is the OpenAPI 3.1 schema dialect — no conversion layer
174
+ * needed). Error responses reference the shared
175
+ * `#/components/responses/ApiError` so a single envelope shape
176
+ * covers every error path.
177
+ *
178
+ * @param app A built `Act` orchestrator. Required.
179
+ * @param options Document-shape fields (`info.title`, `info.version`,
180
+ * servers, base path) and toggles for the cross-cutting headers
181
+ * the live REST API may accept (`Idempotency-Key`, `If-Match`).
182
+ * @returns A valid OpenAPI 3.1 document object — serve as
183
+ * `/openapi.json`, ship to a CDN, write to disk during CI, or
184
+ * pipe to a codegen tool.
185
+ *
186
+ * @throws Error if `info.title` / `info.version` is missing or
187
+ * empty, or if any `servers[].url` fails URL parsing after
188
+ * `{variable}` substitution.
189
+ */
190
+ export declare function openapi(app: Act<Schemas, Schemas, Schemas, Record<string, Schema>>, options: OpenAPIOptions): OpenAPIDocument;
191
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/openapi/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0DG;AACH,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAG3D;;;;;;GAMG;AACH,MAAM,MAAM,eAAe,GAAG;IAC5B,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,QAAQ,CAAC,IAAI,EAAE,WAAW,CAAC;IAC3B,QAAQ,CAAC,OAAO,CAAC,EAAE,aAAa,CAAC,aAAa,CAAC,CAAC;IAChD,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC;IAChD,QAAQ,CAAC,UAAU,EAAE,iBAAiB,CAAC;CACxC,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,MAAM,cAAc,GAAG;IAC3B,QAAQ,CAAC,IAAI,EAAE,WAAW,CAAC;IAC3B,QAAQ,CAAC,OAAO,CAAC,EAAE,aAAa,CAA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,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;KAAE,CAC7C,CAAC;CACH,GACD;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC;AAE9B,MAAM,MAAM,iBAAiB,GAAG;IAC9B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;IAC1D,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC;CACrD,CAAC;AAyJF;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,OAAO,CACrB,GAAG,EAAE,GAAG,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,EAC3D,OAAO,EAAE,cAAc,GACtB,eAAe,CA8BjB"}
package/dist/api/index.js CHANGED
@@ -3,6 +3,7 @@ import {
3
3
  toApiError,
4
4
  withIdempotency
5
5
  } from "../chunk-Z6FVXDDU.js";
6
+ import "../chunk-PZ5AY32C.js";
6
7
  export {
7
8
  ERROR_MAP,
8
9
  toApiError,
@@ -0,0 +1,10 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __export = (target, all) => {
3
+ for (var name in all)
4
+ __defProp(target, name, { get: all[name], enumerable: true });
5
+ };
6
+
7
+ export {
8
+ __export
9
+ };
10
+ //# sourceMappingURL=chunk-PZ5AY32C.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -0,0 +1,192 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/hono/index.ts
21
+ var hono_exports = {};
22
+ __export(hono_exports, {
23
+ authenticated: () => authenticated,
24
+ hono: () => hono
25
+ });
26
+ module.exports = __toCommonJS(hono_exports);
27
+
28
+ // ../../node_modules/.pnpm/@hono+zod-validator@0.8.0_hono@4.12.23_zod@4.4.3/node_modules/@hono/zod-validator/dist/index.js
29
+ var import_validator = require("hono/validator");
30
+ function zValidatorFunction(target, schema, hook, options) {
31
+ return (0, import_validator.validator)(target, async (value, c) => {
32
+ let validatorValue = value;
33
+ if (target === "header" && "_def" in schema || target === "header" && "_zod" in schema) {
34
+ const schemaKeys = Object.keys("in" in schema ? schema.in.shape : schema.shape);
35
+ const caseInsensitiveKeymap = Object.fromEntries(schemaKeys.map((key) => [key.toLowerCase(), key]));
36
+ validatorValue = Object.fromEntries(Object.entries(value).map(([key, value$1]) => [caseInsensitiveKeymap[key] || key, value$1]));
37
+ }
38
+ const result = options && options.validationFunction ? await options.validationFunction(schema, validatorValue) : await schema.safeParseAsync(validatorValue);
39
+ if (hook) {
40
+ const hookResult = await hook({
41
+ data: validatorValue,
42
+ ...result,
43
+ target
44
+ }, c);
45
+ if (hookResult) {
46
+ if (hookResult instanceof Response) return hookResult;
47
+ if ("response" in hookResult) return hookResult.response;
48
+ }
49
+ }
50
+ if (!result.success) return c.json(result, 400);
51
+ return result.data;
52
+ });
53
+ }
54
+ var zValidator = zValidatorFunction;
55
+
56
+ // src/hono/index.ts
57
+ var import_hono = require("hono");
58
+
59
+ // src/api/errors.ts
60
+ var import_act = require("@rotorsoft/act");
61
+ var ERROR_MAP = {
62
+ ValidationError: { status: 422, code: "VALIDATION" },
63
+ InvariantError: { status: 409, code: "INVARIANT" },
64
+ ConcurrencyError: { status: 412, code: "CONCURRENCY" },
65
+ StreamClosedError: { status: 410, code: "STREAM_CLOSED" },
66
+ NonRetryableError: { status: 400, code: "NON_RETRYABLE" }
67
+ };
68
+ var lookup_known = (err) => {
69
+ if (err instanceof import_act.ValidationError) return { name: "ValidationError" };
70
+ if (err instanceof import_act.InvariantError) return { name: "InvariantError" };
71
+ if (err instanceof import_act.ConcurrencyError) return { name: "ConcurrencyError" };
72
+ if (err instanceof import_act.StreamClosedError) return { name: "StreamClosedError" };
73
+ if (err instanceof import_act.NonRetryableError) return { name: "NonRetryableError" };
74
+ return null;
75
+ };
76
+ function toApiError(err) {
77
+ const known = lookup_known(err);
78
+ if (known) {
79
+ const entry = ERROR_MAP[known.name];
80
+ return {
81
+ status: entry.status,
82
+ body: {
83
+ error: known.name,
84
+ detail: err.message,
85
+ code: entry.code
86
+ }
87
+ };
88
+ }
89
+ if (err instanceof Error) {
90
+ return {
91
+ status: 500,
92
+ body: { error: "InternalError", detail: err.message, code: "INTERNAL" }
93
+ };
94
+ }
95
+ return {
96
+ status: 500,
97
+ body: { error: "InternalError", code: "INTERNAL" }
98
+ };
99
+ }
100
+
101
+ // src/api/idempotency.ts
102
+ async function withIdempotency(store, key, handler) {
103
+ const fresh = await store.claim(key);
104
+ if (!fresh) {
105
+ return { deduped: true };
106
+ }
107
+ return { deduped: false, result: await handler() };
108
+ }
109
+
110
+ // src/hono/index.ts
111
+ function authenticated(extractor) {
112
+ return async (c, next) => {
113
+ try {
114
+ const actor = await extractor(c);
115
+ c.set("actor", actor);
116
+ await next();
117
+ } catch (err) {
118
+ const body = {
119
+ error: "Unauthorized",
120
+ detail: err instanceof Error ? err.message : void 0,
121
+ code: "UNAUTHORIZED"
122
+ };
123
+ return c.json(body, 401);
124
+ }
125
+ };
126
+ }
127
+ var default_key_from = (c) => c.req.header("idempotency-key");
128
+ function hono(app, options) {
129
+ const base_path = options.basePath ?? "/api";
130
+ const api = new import_hono.Hono().basePath(
131
+ base_path
132
+ );
133
+ api.use("*", authenticated(options.actor));
134
+ const key_from = options.idempotency?.keyFrom ?? default_key_from;
135
+ for (const [action_name, state] of Object.entries(app.registry.actions)) {
136
+ const schema = state.actions[action_name];
137
+ api.post(
138
+ `/actions/${action_name}`,
139
+ zValidator("json", schema),
140
+ async (c) => {
141
+ try {
142
+ const input = c.req.valid("json");
143
+ const actor = c.get("actor");
144
+ const stream = await options.stream(action_name, input, c);
145
+ const expected_version = options.expectedVersion ? await options.expectedVersion(action_name, input, c) : void 0;
146
+ const target = expected_version === void 0 ? { stream, actor } : { stream, actor, expectedVersion: expected_version };
147
+ if (options.idempotency) {
148
+ const key = key_from(c);
149
+ if (!key) {
150
+ const body = {
151
+ error: "BadRequest",
152
+ detail: "Idempotency-Key required",
153
+ code: "BAD_REQUEST"
154
+ };
155
+ return c.json(body, 400);
156
+ }
157
+ const outcome = await withIdempotency(
158
+ options.idempotency.store,
159
+ key,
160
+ () => app.do(action_name, target, input)
161
+ );
162
+ if (outcome.deduped) {
163
+ const body = {
164
+ error: "Conflict",
165
+ detail: "Idempotency-Key already used; original result not cached",
166
+ code: "CONFLICT"
167
+ };
168
+ return c.json(body, 409);
169
+ }
170
+ return c.json(outcome.result);
171
+ }
172
+ const snapshots = await app.do(
173
+ action_name,
174
+ target,
175
+ input
176
+ );
177
+ return c.json(snapshots);
178
+ } catch (err) {
179
+ const { status, body } = toApiError(err);
180
+ return c.json(body, status);
181
+ }
182
+ }
183
+ );
184
+ }
185
+ return api;
186
+ }
187
+ // Annotate the CommonJS export names for ESM import in node:
188
+ 0 && (module.exports = {
189
+ authenticated,
190
+ hono
191
+ });
192
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/hono/index.ts","../../../../node_modules/.pnpm/@hono+zod-validator@0.8.0_hono@4.12.23_zod@4.4.3/node_modules/@hono/zod-validator/src/index.ts","../../src/api/errors.ts","../../src/api/idempotency.ts"],"sourcesContent":["/**\n * @packageDocumentation\n * @module act-http/hono\n *\n * Hono subpath for the auto-generated API epic (#835). Walks the\n * registry of a built `Act` once at function call and emits a Hono\n * app with one `POST /actions/<name>` route per registered action,\n * Zod-validated bodies, and the shared error envelope.\n *\n * Usage:\n *\n * ```ts\n * import { hono } from \"@rotorsoft/act-http/hono\";\n *\n * const api = hono(app, {\n * actor: (c) => ({ id: c.req.header(\"x-user-id\")!, name: \"...\" }),\n * stream: (action, input, c) => `tenant-${c.req.header(\"x-tenant\")}`,\n * });\n *\n * // Mounts at /api by default — pass `basePath` to override.\n * // POST /api/actions/OpenTicket body: { title } → 200 Snapshot[] | 4xx ApiError\n * ```\n *\n * The route shape matches the flat tRPC sibling at\n * `@rotorsoft/act-http/trpc`: one endpoint per action, no\n * state-name tier. Action names are unique across the registry —\n * the framework enforces no duplicates — so an extra namespace\n * level would be overhead. Queries live in a follow-up; this\n * slice is mutations only.\n *\n * **Actor lives on the context.** The generator wires an internal\n * middleware that runs the host's\n * {@link HonoOptions.actor | actor extractor} once per call and\n * stashes the resolved {@link Actor} under `c.get(\"actor\")`. Every\n * generated route reads from there — auth resolves at one seam,\n * downstream middleware (logging, tracing) sees the same actor.\n * The middleware factory is also exported as {@link authenticated}\n * for hosts that want to compose it into their own Hono chain\n * alongside hand-written routes.\n *\n * Composes the shared utilities at `@rotorsoft/act-http/api`:\n * {@link ActorExtractor} for the auth seam, {@link toApiError} for\n * the uniform error → status / code mapping, {@link withIdempotency}\n * for `Idempotency-Key` dedup. Three transports (tRPC, this one,\n * OpenAPI) ride the same utilities so a client speaking two of\n * them never sees two shapes for the same framework error.\n */\nimport { zValidator } from \"@hono/zod-validator\";\nimport type { Act, Actor, Schema, Schemas } from \"@rotorsoft/act\";\nimport type { IdempotencyStore } from \"@rotorsoft/act-ops/idempotency\";\nimport type { Context, MiddlewareHandler } from \"hono\";\nimport { Hono } from \"hono\";\nimport {\n type ActorExtractor,\n type ApiError,\n toApiError,\n withIdempotency,\n} from \"../api/index.js\";\n\n/**\n * Hono context variables this subpath contributes. The generic on\n * the returned middleware threads it through so routes downstream\n * of {@link authenticated} see `c.get(\"actor\")` typed without a\n * manual cast.\n */\nexport type ActMiddlewareVariables = {\n actor: Actor;\n};\n\n/**\n * Per-call options for {@link hono}. The host supplies the four\n * seams the package can't make decisions on:\n *\n * - `actor` — auth resolver. Receives the Hono context, returns the\n * `Actor` that flows onto `c.get(\"actor\")` for every generated\n * route (via an internal {@link authenticated}).\n * - `stream` — stream-id resolver. Receives the action name, the\n * validated body, and the Hono context; returns the target\n * stream. Singleton aggregates return a constant; per-tenant\n * aggregates pull from headers, path, or body.\n * - `expectedVersion` (optional) — optimistic-concurrency resolver.\n * Hosts typically read it from an `If-Match` header on the\n * request, or from the client's last-known snapshot. Returning\n * `undefined` skips the check for that call — handy when only\n * some actions are concurrency-sensitive.\n * - `idempotency` (optional) — when set, the route honors\n * `Idempotency-Key` via {@link withIdempotency}. The host\n * supplies the `IdempotencyStore`; `keyFrom` defaults to reading\n * the `Idempotency-Key` header. On a duplicate claim, the route\n * responds `409 Conflict` — the contract intentionally doesn't\n * cache the original handler's result, matching the\n * receiver-side \"ack the duplicate\" semantics.\n * - `basePath` (optional, default `\"/api\"`) — Hono basePath the\n * routes mount under.\n */\nexport type HonoOptions = {\n readonly actor: ActorExtractor;\n readonly stream: (\n action_name: string,\n input: unknown,\n c: Context\n ) => string | Promise<string>;\n readonly expectedVersion?: (\n action_name: string,\n input: unknown,\n c: Context\n ) => number | undefined | Promise<number | undefined>;\n readonly idempotency?: {\n readonly store: IdempotencyStore;\n readonly keyFrom?: (c: Context) => string | undefined;\n };\n readonly basePath?: string;\n};\n\n/**\n * Build a Hono middleware that runs an {@link ActorExtractor} once\n * per call and stashes the resolved {@link Actor} under\n * `c.set(\"actor\", ...)` so downstream routes can read it via\n * `c.get(\"actor\")`. The generator at {@link hono} uses this\n * internally; it's also exported so hosts that want to compose\n * their own route chain (logging, tracing, custom auth flavors)\n * can wire it directly:\n *\n * ```ts\n * import { Hono } from \"hono\";\n * import { authenticated, type ActMiddlewareVariables } from \"@rotorsoft/act-http/hono\";\n *\n * const api = new Hono<{ Variables: ActMiddlewareVariables }>();\n * api.use(\"*\", authenticated((c) => resolveUserFromJwt(c)));\n * api.get(\"/me\", (c) => c.json(c.get(\"actor\"))); // typed\n * ```\n *\n * Errors thrown by the extractor surface as `401 Unauthorized` with\n * the shared {@link ApiError} envelope.\n */\nexport function authenticated(\n extractor: ActorExtractor\n): MiddlewareHandler<{ Variables: ActMiddlewareVariables }> {\n return async (c, next) => {\n try {\n const actor = await extractor(c);\n c.set(\"actor\", actor);\n await next();\n } catch (err) {\n const body: ApiError = {\n error: \"Unauthorized\",\n detail: err instanceof Error ? err.message : undefined,\n code: \"UNAUTHORIZED\",\n };\n return c.json(body, 401);\n }\n };\n}\n\nconst default_key_from = (c: Context): string | undefined =>\n c.req.header(\"idempotency-key\");\n\n/**\n * Build a typed Hono REST surface from a built `Act` instance.\n *\n * Walks `app.registry.actions` once and emits one\n * `POST /actions/<actionName>` per action under the configured\n * `basePath` (default `/api`). Each route:\n *\n * 1. Runs the internal {@link authenticated} middleware —\n * `options.actor` resolves the {@link Actor} once and stashes\n * it under `c.get(\"actor\")`.\n * 2. Validates the JSON body against the action's Zod schema via\n * `@hono/zod-validator`. Failures short-circuit with `422`.\n * 3. Resolves the target stream via\n * `options.stream(name, input, c)`.\n * 4. (Optionally) resolves `expectedVersion` for optimistic\n * concurrency.\n * 5. (Optionally) claims the `Idempotency-Key` via\n * {@link withIdempotency} — responds `409` on duplicate.\n * 6. Calls\n * `app.do(name, { stream, actor, expectedVersion? }, input)`.\n * 7. Maps any thrown framework error onto the shared\n * {@link ApiError} envelope via {@link toApiError}, returning\n * the conventional HTTP status (`409` / `412` for concurrency,\n * `422` for validation, `400` for non-retryable, etc.).\n *\n * @param app A built `Act` orchestrator.\n * @param options Auth, stream, expected-version, idempotency, and\n * base-path seams.\n * @returns A Hono app covering every registered action under\n * `<basePath>/actions/<name>`.\n */\nexport function hono(\n app: Act<Schemas, Schemas, Schemas, Record<string, Schema>>,\n options: HonoOptions\n): Hono<{ Variables: ActMiddlewareVariables }> {\n const base_path = options.basePath ?? \"/api\";\n const api = new Hono<{ Variables: ActMiddlewareVariables }>().basePath(\n base_path\n );\n api.use(\"*\", authenticated(options.actor));\n\n const key_from = options.idempotency?.keyFrom ?? default_key_from;\n\n for (const [action_name, state] of Object.entries(app.registry.actions)) {\n const schema = state.actions[action_name] as Schema;\n api.post(\n `/actions/${action_name}`,\n zValidator(\"json\", schema as never),\n async (c) => {\n try {\n const input = c.req.valid(\"json\" as never) as unknown;\n const actor = c.get(\"actor\");\n const stream = await options.stream(action_name, input, c);\n const expected_version = options.expectedVersion\n ? await options.expectedVersion(action_name, input, c)\n : undefined;\n const target =\n expected_version === undefined\n ? { stream, actor }\n : { stream, actor, expectedVersion: expected_version };\n\n if (options.idempotency) {\n const key = key_from(c);\n if (!key) {\n const body: ApiError = {\n error: \"BadRequest\",\n detail: \"Idempotency-Key required\",\n code: \"BAD_REQUEST\",\n };\n return c.json(body, 400);\n }\n const outcome = await withIdempotency(\n options.idempotency.store,\n key,\n () =>\n app.do(action_name as never, target as never, input as never)\n );\n if (outcome.deduped) {\n const body: ApiError = {\n error: \"Conflict\",\n detail:\n \"Idempotency-Key already used; original result not cached\",\n code: \"CONFLICT\",\n };\n return c.json(body, 409);\n }\n return c.json(outcome.result);\n }\n\n const snapshots = await app.do(\n action_name as never,\n target as never,\n input as never\n );\n return c.json(snapshots);\n } catch (err) {\n const { status, body } = toApiError(err);\n // Hono types narrow `status` to its known status-number union;\n // `toApiError` returns a numeric status from a closed set that\n // matches but TypeScript can't prove it through the union.\n return c.json(body, status as 400 | 409 | 410 | 412 | 422 | 500);\n }\n }\n );\n }\n\n return api;\n}\n","/* eslint-disable @typescript-eslint/no-explicit-any */\nimport type { Context, Env, Input, MiddlewareHandler, TypedResponse, ValidationTargets } from 'hono'\nimport { validator } from 'hono/validator'\nimport type * as v3 from 'zod/v3'\nimport type { ZodSafeParseResult as v4ZodSafeParseResult } from 'zod/v4'\nimport type * as v4 from 'zod/v4/core'\nimport type { InferInput } from './utils'\n\ntype ZodSchema = v3.ZodType | v4.$ZodType\ntype ZodError<T extends ZodSchema> = T extends v4.$ZodType\n ? v4.$ZodError<v4.output<T>>\n : v3.ZodError\ntype ZodSafeParseResult<T, T2, T3 extends ZodSchema> = T3 extends v4.$ZodType\n ? v4ZodSafeParseResult<T>\n : v3.SafeParseReturnType<T, T2>\ntype zInput<T> = T extends v3.ZodType ? v3.input<T> : T extends v4.$ZodType ? v4.input<T> : never\ntype zOutput<T> = T extends v3.ZodType ? v3.output<T> : T extends v4.$ZodType ? v4.output<T> : never\ntype zInfer<T> = T extends v3.ZodType ? v3.infer<T> : T extends v4.$ZodType ? v4.infer<T> : never\n\n// Failure variant of `safeParseAsync`'s return value.\n// Mirrors the runtime body returned by `c.json(result, 400)` on validation failure.\ntype ZodValidatorFailureBody<T extends ZodSchema> = T extends v4.$ZodType\n ? Extract<v4ZodSafeParseResult<v4.output<T>>, { success: false }>\n : T extends v3.ZodType\n ? Extract<v3.SafeParseReturnType<v3.input<T>, v3.output<T>>, { success: false }>\n : never\n\ntype ZodValidatorFailureResponse<T extends ZodSchema> = Response &\n TypedResponse<ZodValidatorFailureBody<T>, 400, 'json'>\n\nexport type Hook<\n T,\n E extends Env,\n P extends string,\n Target extends keyof ValidationTargets = keyof ValidationTargets,\n O = {},\n Schema extends ZodSchema = any,\n> = (\n result: ({ success: true; data: T } | { success: false; error: ZodError<Schema>; data: T }) & {\n target: Target\n },\n c: Context<E, P>\n) => Response | void | TypedResponse<O> | Promise<Response | void | TypedResponse<O>>\n\ntype HasUndefined<T> = undefined extends T ? true : false\n\ntype ExtractValidationResponse<VF> = VF extends (value: any, c: any) => infer R\n ? R extends Promise<infer PR>\n ? PR extends TypedResponse<infer T, infer S, infer F>\n ? Response & TypedResponse<T, S, F>\n : PR extends Response\n ? PR\n : PR extends undefined\n ? never\n : never\n : R extends TypedResponse<infer T, infer S, infer F>\n ? Response & TypedResponse<T, S, F>\n : R extends Response\n ? R\n : R extends undefined\n ? never\n : never\n : never\n\ntype ValidationFunctionOption<T extends ZodSchema, Target extends keyof ValidationTargets> = {\n validationFunction: (\n schema: T,\n value: ValidationTargets[Target]\n ) => ZodSafeParseResult<any, any, T> | Promise<ZodSafeParseResult<any, any, T>>\n}\n\ntype DefaultInput<Target extends keyof ValidationTargets, In, Out> = {\n in: HasUndefined<In> extends true\n ? {\n [K in Target]?: [In] extends [ValidationTargets[K]] ? In : InferInput<In, K>\n }\n : {\n [K in Target]: [In] extends [ValidationTargets[K]] ? In : InferInput<In, K>\n }\n out: { [K in Target]: Out }\n}\n\n// without hook (with or without options)\nfunction zValidatorFunction<\n T extends ZodSchema,\n Target extends keyof ValidationTargets,\n E extends Env,\n P extends string,\n In = zInput<T>,\n Out = zOutput<T>,\n I extends Input = DefaultInput<Target, In, Out>,\n V extends I = I,\n>(\n target: Target,\n schema: T,\n hook?: undefined,\n options?: ValidationFunctionOption<T, Target>\n): MiddlewareHandler<E, P, V, ZodValidatorFailureResponse<T>>\n\n// with hook and options\nfunction zValidatorFunction<\n T extends ZodSchema,\n Target extends keyof ValidationTargets,\n E extends Env,\n P extends string,\n HookFn extends Hook<InferredValue, E, P, Target, {}, T>,\n In = zInput<T>,\n Out = zOutput<T>,\n I extends Input = DefaultInput<Target, In, Out>,\n V extends I = I,\n InferredValue = zInfer<T>,\n>(\n target: Target,\n schema: T,\n hook?: HookFn,\n options?: ValidationFunctionOption<T, Target>\n): MiddlewareHandler<E, P, V, ExtractValidationResponse<HookFn>>\n\n// implementation\nfunction zValidatorFunction<\n T extends ZodSchema,\n Target extends keyof ValidationTargets,\n E extends Env,\n P extends string,\n HookFn extends Hook<InferredValue, E, P, Target, {}, T>,\n In = zInput<T>,\n Out = zOutput<T>,\n I extends Input = DefaultInput<Target, In, Out>,\n V extends I = I,\n InferredValue = zInfer<T>,\n>(\n target: Target,\n schema: T,\n hook?: HookFn,\n options?: ValidationFunctionOption<T, Target>\n):\n | MiddlewareHandler<E, P, V, ZodValidatorFailureResponse<T>>\n | MiddlewareHandler<E, P, V, ExtractValidationResponse<HookFn>> {\n // @ts-expect-error not typed well\n return validator(target, async (value: ValidationTargets[Target], c) => {\n let validatorValue = value\n\n // in case where our `target` === `header`, Hono parses all of the headers into lowercase.\n // this might not match the Zod schema, so we want to make sure that we account for that when parsing the schema.\n if ((target === 'header' && '_def' in schema) || (target === 'header' && '_zod' in schema)) {\n // create an object that maps lowercase schema keys to lowercase\n // @ts-expect-error the schema is a Zod Schema\n const schemaKeys = Object.keys('in' in schema ? schema.in.shape : schema.shape)\n const caseInsensitiveKeymap = Object.fromEntries(\n schemaKeys.map((key) => [key.toLowerCase(), key])\n )\n\n validatorValue = Object.fromEntries(\n Object.entries(value).map(([key, value]) => [caseInsensitiveKeymap[key] || key, value])\n )\n }\n\n const result =\n options && options.validationFunction\n ? await options.validationFunction(schema, validatorValue)\n : // @ts-expect-error z4.$ZodType has safeParseAsync\n await schema.safeParseAsync(validatorValue)\n\n if (hook) {\n const hookResult = await hook({ data: validatorValue, ...result, target }, c)\n if (hookResult) {\n if (hookResult instanceof Response) {\n return hookResult\n }\n\n if ('response' in hookResult) {\n return hookResult.response\n }\n }\n }\n\n if (!result.success) {\n return c.json(result, 400)\n }\n\n return result.data as zInfer<T>\n })\n}\n\nexport const zValidator: typeof zValidatorFunction = zValidatorFunction\n","import {\n ConcurrencyError,\n InvariantError,\n NonRetryableError,\n StreamClosedError,\n ValidationError,\n} from \"@rotorsoft/act\";\n\n/**\n * Uniform error envelope shipped over the wire by every act-http\n * transport. Hosts get the same shape from REST, tRPC, and OpenAPI —\n * a client that talks to two transports doesn't have to invent two\n * error parsers.\n *\n * - `error` — the framework error name (`\"ValidationError\"`,\n * `\"InvariantError\"`, …). Stable identifier, safe to switch on.\n * - `detail` — the framework's message text. Human-readable; not\n * parsed by clients.\n * - `code` — a machine-readable status code from {@link ERROR_MAP}\n * for clients that prefer enum-style branching over name strings.\n */\nexport type ApiError = {\n error: string;\n detail?: string;\n code?: string;\n};\n\n/**\n * Status + code pair for one known framework error.\n */\nexport type ErrorMapEntry = {\n status: number;\n code: string;\n};\n\n/**\n * The single table that maps framework error types to HTTP status\n * codes and machine-readable codes. One table, three consumers\n * (Hono, tRPC, OpenAPI) — cross-transport consistency by\n * construction.\n *\n * Operators wanting different mappings wrap the generated transport\n * rather than mutating this — the consistency is the load-bearing\n * property, not the specific status codes.\n */\nexport const ERROR_MAP = {\n ValidationError: { status: 422, code: \"VALIDATION\" },\n InvariantError: { status: 409, code: \"INVARIANT\" },\n ConcurrencyError: { status: 412, code: \"CONCURRENCY\" },\n StreamClosedError: { status: 410, code: \"STREAM_CLOSED\" },\n NonRetryableError: { status: 400, code: \"NON_RETRYABLE\" },\n} as const satisfies Record<string, ErrorMapEntry>;\n\nconst lookup_known = (\n err: unknown\n): { name: keyof typeof ERROR_MAP } | null => {\n if (err instanceof ValidationError) return { name: \"ValidationError\" };\n if (err instanceof InvariantError) return { name: \"InvariantError\" };\n if (err instanceof ConcurrencyError) return { name: \"ConcurrencyError\" };\n if (err instanceof StreamClosedError) return { name: \"StreamClosedError\" };\n if (err instanceof NonRetryableError) return { name: \"NonRetryableError\" };\n return null;\n};\n\n/**\n * Translate an unknown thrown value into the canonical\n * {@link ApiError} envelope plus HTTP status. Each transport's error\n * boundary calls this once and forwards the result to the wire.\n *\n * Known framework errors map per {@link ERROR_MAP}. Everything else\n * surfaces as a 500 with `code: \"INTERNAL\"`; the `detail` field is\n * populated when the throw was an `Error` instance, omitted\n * otherwise (a thrown string or object doesn't get to leak its\n * payload to the client).\n */\nexport function toApiError(err: unknown): { status: number; body: ApiError } {\n const known = lookup_known(err);\n if (known) {\n const entry = ERROR_MAP[known.name];\n return {\n status: entry.status,\n body: {\n error: known.name,\n detail: (err as Error).message,\n code: entry.code,\n },\n };\n }\n if (err instanceof Error) {\n return {\n status: 500,\n body: { error: \"InternalError\", detail: err.message, code: \"INTERNAL\" },\n };\n }\n return {\n status: 500,\n body: { error: \"InternalError\", code: \"INTERNAL\" },\n };\n}\n","import type { IdempotencyStore } from \"@rotorsoft/act-ops/idempotency\";\n\n/**\n * Result of a {@link withIdempotency} call.\n *\n * - `{ deduped: false, result }` — the claim was fresh; the handler\n * ran and produced `result`.\n * - `{ deduped: true }` — the claim was already taken; the handler\n * was *not* invoked. The caller decides how to respond (typically\n * a 2xx with no body, matching the receiver-side convention).\n *\n * Note: the contract does not cache the previous response. A\n * duplicate call returns the deduped marker only — replaying the\n * original handler's output would require a response-caching\n * adapter, which is out of scope here. The receiver-side convention\n * (and the convention the generated transports follow) is \"ack the\n * duplicate; do nothing else.\"\n */\nexport type IdempotencyResult<T> =\n | { deduped: false; result: T }\n | { deduped: true };\n\n/**\n * Wrap an action handler so the framework honors `Idempotency-Key`\n * dedup. Acquires the key via {@link IdempotencyStore.claim}, runs\n * the handler exactly when the claim was fresh, and skips the\n * handler entirely on a duplicate.\n *\n * Reuses the contract `@rotorsoft/act-ops/idempotency` already\n * defines for the receiver-side `Idempotency-Key` story. A single\n * `IdempotencyStore` implementation covers both halves of the \"Act\n * over the wire\" surface — receiver and generated API.\n */\nexport async function withIdempotency<T>(\n store: IdempotencyStore,\n key: string,\n handler: () => Promise<T>\n): Promise<IdempotencyResult<T>> {\n const fresh = await store.claim(key);\n if (!fresh) {\n return { deduped: true };\n }\n return { deduped: false, result: await handler() };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;;ACuHA,SAAS,mBAYP,QACA,QACA,MACA,SAGgE;AAEhE,aAAO,4BAAU,QAAQ,OAAO,OAAkC,MAAM;AACtE,QAAI,iBAAiB;AAIrB,QAAK,WAAW,YAAY,UAAU,UAAY,W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