@voltro/protocol 0.69.1 → 0.70.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.
package/dist/rest.js CHANGED
@@ -1,74 +1,74 @@
1
1
  import { F as e, P as t } from "./scopes-DVtiKKsm.js";
2
- import { c as n, l as r, o as i, s as a, t as o } from "./serverErrorBus-BTnTpMqj.js";
3
- import { Effect as s, Schema as c } from "effect";
4
- import * as l from "effect/SchemaAST";
2
+ import { _ as n, a as r, b as i, c as a, d as o, f as s, i as c, l, n as u, o as d, r as f, s as p, t as m, u as h, v as g, y as _ } from "./restFormat-Bnngx-zI.js";
3
+ import { Effect as v, ParseResult as y, Schema as b } from "effect";
4
+ import * as x from "effect/SchemaAST";
5
5
  //#region src/publicApi.ts
6
- var u = (e, t) => t.method ?? (e === "query" ? "GET" : "POST"), d = (e, t) => t.path ?? `/${t.version ?? "v1"}/${e.replace(/\./g, "/")}`, f = (e) => {
7
- if (l.isUnion(e)) {
8
- let t = e.types.filter((e) => !l.isUndefinedKeyword(e));
6
+ var S = (e, t) => t.method ?? (e === "query" ? "GET" : "POST"), C = (e, t) => w(t.path ?? `/${t.version ?? "v1"}/${e.replace(/\./g, "/")}`), w = (e) => e.replace(/\{([A-Za-z0-9_]+)\}/g, ":$1"), T = (e) => [...e.matchAll(/:([A-Za-z0-9_]+)/g)].map((e) => e[1]), E = (e) => {
7
+ if (x.isUnion(e)) {
8
+ let t = e.types.filter((e) => !x.isUndefinedKeyword(e));
9
9
  if (t.length === 1) return t[0];
10
- if (t.length !== e.types.length) return l.Union.make(t);
10
+ if (t.length !== e.types.length) return x.Union.make(t);
11
11
  }
12
12
  return e;
13
- }, p = /^-?(\d+\.?\d*|\.\d+)(e[+-]?\d+)?$/i, m = (e, t) => {
14
- let n = f(t);
15
- if (l.isRefinement(n) || l.isTransformation(n)) return m(e, n.from);
16
- if (l.isUnion(n)) {
13
+ }, D = /^-?(\d+\.?\d*|\.\d+)(e[+-]?\d+)?$/i, O = (e, t) => {
14
+ let n = E(t);
15
+ if (x.isRefinement(n) || x.isTransformation(n)) return O(e, n.from);
16
+ if (x.isUnion(n)) {
17
17
  for (let t of n.types) {
18
- let n = m(e, t);
18
+ let n = O(e, t);
19
19
  if (n !== e) return n;
20
20
  }
21
21
  return e;
22
22
  }
23
- if (l.isTupleType(n)) {
23
+ if (x.isTupleType(n)) {
24
24
  let t = Array.isArray(e) ? e : e === void 0 ? void 0 : [e];
25
25
  if (t === void 0) return e;
26
26
  let r = n.rest[0]?.type ?? n.elements[0]?.type;
27
- return r === void 0 ? t : t.map((e) => m(e, r));
27
+ return r === void 0 ? t : t.map((e) => O(e, r));
28
28
  }
29
29
  if (typeof e != "string") return e;
30
- if (l.isNumberKeyword(n)) return p.test(e.trim()) ? Number(e) : e;
31
- if (l.isBooleanKeyword(n)) return e === "true" || e !== "false" && e;
32
- if (l.isLiteral(n)) {
30
+ if (x.isNumberKeyword(n)) return D.test(e.trim()) ? Number(e) : e;
31
+ if (x.isBooleanKeyword(n)) return e === "true" || e !== "false" && e;
32
+ if (x.isLiteral(n)) {
33
33
  let t = n.literal;
34
- return typeof t == "number" && p.test(e.trim()) && Number(e) === t || typeof t == "boolean" && String(t) === e ? t : e;
34
+ return typeof t == "number" && D.test(e.trim()) && Number(e) === t || typeof t == "boolean" && String(t) === e ? t : e;
35
35
  }
36
36
  return e;
37
- }, h = (e, t) => {
37
+ }, k = (e, t) => {
38
38
  if (typeof e != "object" || !e || Array.isArray(e)) return e;
39
- let n = f(l.encodedAST(t.ast));
40
- if (!l.isTypeLiteral(n)) return e;
39
+ let n = E(x.encodedAST(t.ast));
40
+ if (!x.isTypeLiteral(n)) return e;
41
41
  let r = { ...e };
42
42
  for (let e of n.propertySignatures) {
43
43
  let t = String(e.name);
44
- t in r && (r[t] = m(r[t], e.type));
44
+ t in r && (r[t] = O(r[t], e.type));
45
45
  }
46
46
  return r;
47
- }, g = (e) => {
47
+ }, A = (e) => {
48
48
  let t = /* @__PURE__ */ new Set();
49
49
  if (e === void 0) return t;
50
50
  let n = (e) => {
51
- if (l.isUnion(e)) {
51
+ if (x.isUnion(e)) {
52
52
  for (let t of e.types) n(t);
53
53
  return;
54
54
  }
55
- if (l.isTransformation(e)) {
55
+ if (x.isTransformation(e)) {
56
56
  n(e.from), n(e.to);
57
57
  return;
58
58
  }
59
- if (l.isRefinement(e)) {
59
+ if (x.isRefinement(e)) {
60
60
  n(e.from);
61
61
  return;
62
62
  }
63
- if (l.isTypeLiteral(e)) {
63
+ if (x.isTypeLiteral(e)) {
64
64
  let n = e.propertySignatures.find((e) => e.name === "_tag");
65
- n !== void 0 && l.isLiteral(n.type) && typeof n.type.literal == "string" && t.add(n.type.literal);
65
+ n !== void 0 && x.isLiteral(n.type) && typeof n.type.literal == "string" && t.add(n.type.literal);
66
66
  }
67
67
  };
68
68
  return n(e.ast), t;
69
- }, _ = (e, t, n) => {
69
+ }, j = (e, t, n) => {
70
70
  if (n !== void 0) try {
71
- let r = c.encodeUnknownSync(n)(e);
71
+ let r = b.encodeUnknownSync(n)(e);
72
72
  if (typeof r == "object" && r) return {
73
73
  error: t,
74
74
  ...r
@@ -77,69 +77,181 @@ var u = (e, t) => t.method ?? (e === "query" ? "GET" : "POST"), d = (e, t) => t.
77
77
  let r = { error: t };
78
78
  for (let [t, n] of Object.entries(e)) t !== "stack" && t !== "_tag" && (r[t] = n);
79
79
  return r;
80
- }, v = (e, t, n) => {
80
+ }, M = (e, t, n, r = []) => {
81
81
  if (typeof e != "object" || !e) return e;
82
- let r = e._tag;
83
- return typeof r != "string" || r === "ScopeError" || !g(t.error).has(r) ? e : new E(n.errorStatus?.[r] ?? 422, _(e, r, t.error));
84
- }, y = (e, t, n) => {
85
- let r = e.publicApi;
86
- if (r === void 0) throw Error(`publicApiRoute: descriptor '${e.name}' has no publicApi annotation`);
87
- let i = u(e.kind, r), a = d(e.name, r), o = i === "GET" ? "query" : "body", s = c.Struct({ [o]: e.input }), l = i === "GET" ? c.transform(c.Struct({ query: c.Unknown }), s, {
82
+ let i = e._tag;
83
+ if (typeof i != "string" || i === "ScopeError") return e;
84
+ if (A(t.error).has(i)) return new H(n.errorStatus?.[i] ?? 422, j(e, i, t.error));
85
+ let a = r.find((e) => A(e.schema).has(i));
86
+ if (a === void 0) return e;
87
+ let o = n.errorStatus?.[i] ?? a.rest?.status ?? 422, s = a.rest?.headers?.(e);
88
+ return new H(o, j(e, i, a.schema), s);
89
+ }, ee = (e, t, n, r, i, a) => {
90
+ let o = M(e, t, n, r);
91
+ if (!(o instanceof H) || i.errors === "tagged" && i.encodeError === void 0) return o;
92
+ let { error: s, _tag: l, ...u } = o.body, d = typeof u.message == "string" ? u.message : typeof u.reason == "string" ? u.reason : "", f = c(i, {
93
+ status: o.status,
94
+ tag: s,
95
+ message: d,
96
+ fields: u
97
+ }, {
98
+ ...a,
99
+ status: o.status
100
+ });
101
+ return new H(o.status, f.body, {
102
+ ...o.headers ?? {},
103
+ ...f.headers ?? {}
104
+ }, f.contentType);
105
+ }, N = (e, t) => {
106
+ if (e === void 0) return;
107
+ let n = x.isUnion(e.ast) ? e.ast.types : [e.ast];
108
+ for (let e of n) {
109
+ let n = b.make(e);
110
+ if (A(n).has(t)) return n;
111
+ }
112
+ }, P = (e) => e.split(".").map((e, t) => t === 0 ? e : e.charAt(0).toUpperCase() + e.slice(1)).join("").replace(/[^A-Za-z0-9_]/g, "_"), te = (e) => {
113
+ let t = E(x.encodedAST(e.ast));
114
+ return x.isTypeLiteral(t) ? new Set(t.propertySignatures.map((e) => String(e.name))) : void 0;
115
+ }, ne = (e, t) => e === t || JSON.stringify(e) === JSON.stringify(t), re = (e, t) => {
116
+ let n = E(x.encodedAST(t.ast)), r = {}, i = x.isTypeLiteral(n) ? new Map(n.propertySignatures.map((e) => [String(e.name), e.type])) : /* @__PURE__ */ new Map();
117
+ for (let [t, n] of Object.entries(e)) {
118
+ let e = i.get(t);
119
+ r[t] = e === void 0 ? n : O(n, e);
120
+ }
121
+ return r;
122
+ }, F = (e, t, n, i = {}) => {
123
+ let a = e.publicApi;
124
+ if (a === void 0) throw Error(`publicApiRoute: descriptor '${e.name}' has no publicApi annotation`);
125
+ let s = l(i.profile, a.format), c = i.pluginErrors ?? [], u = S(e.kind, a), d = (e) => s.naming === "snake_case" ? o(e) : e, f = C(e.name, a), p = f.replace(/:([A-Za-z0-9_]+)/g, (e, t) => `:${d(t)}`), m = T(p), g = h(e.input, s), _ = te(g);
126
+ if (_ !== void 0) {
127
+ for (let t of m) if (!_.has(t)) throw Error(`publicApi: descriptor '${e.name}' — path '${f}' binds ':${t}', and the input declares no field of that name` + (_.size > 0 ? ` (fields: ${[..._].join(", ")})` : " (the input declares no fields)"));
128
+ }
129
+ let x = h(e.output, s), w = b.Struct({ value: g }), E = b.Struct({
130
+ query: b.Unknown,
131
+ params: b.Unknown,
132
+ body: b.Unknown
133
+ }), D = b.transformOrFail(E, w, {
88
134
  strict: !1,
89
- decode: (t) => ({ query: h(t.query, e.input) }),
90
- encode: (e) => e
91
- }) : s, f = [...e.openAccess === void 0 ? [x()] : [], ...(r.scopes ?? []).map((e) => S(e))], p = r.stream === "sse" && e.kind === "query" && n !== void 0, m = p ? (e, t) => k((r) => n(e[o], t, (e) => {
92
- r(A(String(e._tag ?? "message"), e));
135
+ decode: (e, t, n) => {
136
+ let r = u === "GET" ? k(e.query, g) : e.body ?? {}, i = typeof e.params == "object" && e.params !== null ? re(e.params, g) : {};
137
+ if (typeof r != "object" || !r || Array.isArray(r)) return y.succeed({ value: r });
138
+ let a = r, o = Object.keys(i).filter((e) => e in a && !ne(a[e], i[e]));
139
+ if (o.length > 0) {
140
+ let t = u === "GET" ? "query string" : "body";
141
+ return y.fail(new y.Type(n, e, `${o.map((e) => `'${e}'`).join(", ")} ${o.length === 1 ? "is" : "are"} bound by the path and sent with another value in the ${t}`));
142
+ }
143
+ return y.succeed({ value: {
144
+ ...a,
145
+ ...i
146
+ } });
147
+ },
148
+ encode: (e) => y.succeed(e)
149
+ }), O = [...e.openAccess === void 0 ? [L()] : [], ...(a.scopes ?? []).map((e) => R(e))], j = (() => {
150
+ let t = e.source;
151
+ return typeof t == "string" ? t : Array.isArray(t) ? t[0] : void 0;
152
+ })(), M = (t, n = 200) => ({
153
+ tag: e.name,
154
+ method: t.method,
155
+ path: t.path,
156
+ query: t.query,
157
+ source: j,
158
+ status: n
159
+ }), F = a.stream === "sse" && e.kind === "query" && n !== void 0, I = b.encodeUnknown(x), z = F ? (e, t) => G((r) => n(e.value, t, (e) => {
160
+ r(K(String(e._tag ?? "message"), e));
93
161
  })) : async (n, i) => {
162
+ let o;
94
163
  try {
95
- return await t(n[o], i);
164
+ o = await t(n.value, i);
96
165
  } catch (t) {
97
- throw v(t, e, r);
166
+ throw ee(t, e, a, c, s, M(i));
98
167
  }
168
+ let l = await v.runPromise(I(o)), u = r(s, l, M(i));
169
+ return B(u.body, {
170
+ ...u.headers === void 0 ? {} : { headers: u.headers },
171
+ ...u.contentType === void 0 ? {} : { contentType: u.contentType }
172
+ });
173
+ }, V = e.error, H = [...A(V)], U = [];
174
+ for (let e of H) {
175
+ let t = N(V, e);
176
+ t !== void 0 && U.push({
177
+ status: a.errorStatus?.[e] ?? 422,
178
+ tag: e,
179
+ schema: t
180
+ });
181
+ }
182
+ for (let e of c) for (let t of A(e.schema)) {
183
+ if (H.includes(t)) continue;
184
+ let n = a.errorStatus?.[t] ?? e.rest?.status;
185
+ if (n === void 0) continue;
186
+ let r = N(e.schema, t) ?? e.schema;
187
+ U.push({
188
+ status: n,
189
+ tag: t,
190
+ schema: r
191
+ });
192
+ }
193
+ let q = a.security === !1 ? "none" : a.security ?? (e.openAccess === void 0 ? "required" : "optional"), J = {
194
+ tag: e.name,
195
+ kind: e.kind,
196
+ version: a.version ?? "v1",
197
+ security: q,
198
+ input: g,
199
+ pathParams: m,
200
+ output: x,
201
+ source: j,
202
+ errorResponses: U,
203
+ format: s
99
204
  };
100
- return O({
101
- method: i,
102
- path: a,
103
- input: l,
104
- output: e.output,
105
- handler: m,
106
- ...p ? { streaming: !0 } : {},
107
- guards: f,
108
- ...r.summary === void 0 ? {} : { summary: r.summary },
109
- ...r.description === void 0 ? {} : { description: r.description },
110
- ...r.deprecated === void 0 ? {} : { deprecated: r.deprecated },
111
- ...r.sunset === void 0 ? {} : { sunset: r.sunset }
205
+ return W({
206
+ method: u,
207
+ path: p,
208
+ input: D,
209
+ output: b.Unknown,
210
+ handler: z,
211
+ ...F ? { streaming: !0 } : {},
212
+ guards: O,
213
+ operationId: a.operationId ?? P(e.name),
214
+ tags: a.tags ?? [e.name.split(".")[0] ?? e.name],
215
+ projection: J,
216
+ ...s.etag && u === "GET" && !F ? { etag: !0 } : {},
217
+ ...a.example === void 0 ? {} : { example: a.example },
218
+ ...a.summary === void 0 ? {} : { summary: a.summary },
219
+ ...a.description === void 0 ? {} : { description: a.description },
220
+ ...a.deprecated === void 0 ? {} : { deprecated: a.deprecated },
221
+ ...a.sunset === void 0 ? {} : { sunset: a.sunset }
112
222
  });
113
- }, b = (e) => e.filter((e) => e.descriptor.publicApi !== void 0).map((e) => y(e.descriptor, e.invoke, e.subscribe)).sort((e, t) => e.path.localeCompare(t.path)), x = () => (e) => {
223
+ }, I = (e, t = {}) => e.filter((e) => e.descriptor.publicApi !== void 0).map((e) => F(e.descriptor, e.invoke, e.subscribe, t)).sort((e, t) => e.path.localeCompare(t.path)), L = () => (e) => {
114
224
  if (e.subject.type === "anonymous") return {
115
225
  status: 401,
116
226
  message: e.subject.credentialRejected ?? "Authentication required"
117
227
  };
118
- }, S = (e) => (t) => {
228
+ }, R = (e) => (t) => {
119
229
  let n = t.subject.scopes;
120
230
  if (!(n && (n.includes(e) || n.includes("admin:full")))) return {
121
231
  status: 403,
122
232
  message: `Missing required scope: ${e}`
123
233
  };
124
- }, C = (e) => (t) => {
234
+ }, z = (e) => (t) => {
125
235
  let n = t.subject.scopes;
126
236
  if (!(n && (n.includes("admin:full") || e.some((e) => n.includes(e))))) return {
127
237
  status: 403,
128
238
  message: `Missing required scope: one of ${e.join(", ")}`
129
239
  };
130
- }, w = (e, t = {}) => ({
240
+ }, B = (e, t = {}) => ({
131
241
  __voltroRestResponse: !0,
132
242
  status: t.status ?? 200,
133
243
  body: e,
134
- ...t.headers === void 0 ? {} : { headers: t.headers }
135
- }), T = (e) => typeof e == "object" && !!e && e.__voltroRestResponse === !0, E = class extends Error {
244
+ ...t.headers === void 0 ? {} : { headers: t.headers },
245
+ ...t.contentType === void 0 ? {} : { contentType: t.contentType }
246
+ }), V = (e) => typeof e == "object" && !!e && e.__voltroRestResponse === !0, H = class extends Error {
136
247
  status;
137
248
  body;
138
249
  headers;
139
- constructor(e, t, n) {
140
- super(`REST ${e}`), this.name = "RestRouteError", this.status = e, this.body = t, this.headers = n;
250
+ contentType;
251
+ constructor(e, t, n, r) {
252
+ super(`REST ${e}`), this.name = "RestRouteError", this.status = e, this.body = t, this.headers = n, this.contentType = r;
141
253
  }
142
- }, D = (e, t, n) => new E(e, t, n), O = (e) => {
254
+ }, U = (e, t, n) => new H(e, t, n), W = (e) => {
143
255
  if (e.version === void 0) return e;
144
256
  if (!/^v\d+$/.test(e.version)) throw Error(`defineRestRoute(${e.method} ${e.path}): version "${e.version}" must be 'v' followed by digits — 'v1', 'v2', …`);
145
257
  if ((/* @__PURE__ */ RegExp("^/v\\d+(/|$)")).test(e.path)) throw Error(`defineRestRoute(${e.method} ${e.path}): the path already starts with a version segment AND declares version: '${e.version}'. Pick one spelling — either bake the version into the path, or declare version: and keep the path bare ('/customers'). Both would mount '/${e.version}${e.path}', which is never what the author meant.`);
@@ -147,13 +259,13 @@ var u = (e, t) => t.method ?? (e === "query" ? "GET" : "POST"), d = (e, t) => t.
147
259
  ...e,
148
260
  path: `/${e.version}${e.path}`
149
261
  };
150
- }, k = (e, t = {}) => ({
262
+ }, G = (e, t = {}) => ({
151
263
  __voltroRestStream: !0,
152
264
  stream: {
153
265
  subscribe: e,
154
266
  ...t.keepAliveMs === void 0 ? {} : { keepAliveMs: t.keepAliveMs }
155
267
  }
156
- }), A = (e, t) => `event: ${e}\n${(typeof t == "string" ? t : JSON.stringify(t)).split("\n").map((e) => `data: ${e}`).join("\n")}\n\n`, j = (e) => typeof e == "object" && !!e && e.__voltroRestStream === !0, M = (e, t = {}) => ({
268
+ }), K = (e, t) => `event: ${e}\n${(typeof t == "string" ? t : JSON.stringify(t)).split("\n").map((e) => `data: ${e}`).join("\n")}\n\n`, q = (e) => typeof e == "object" && !!e && e.__voltroRestStream === !0, J = (e, t = {}) => ({
157
269
  __voltroRestBytes: !0,
158
270
  byteStream: {
159
271
  stream: e,
@@ -163,26 +275,49 @@ var u = (e, t) => t.method ?? (e === "query" ? "GET" : "POST"), d = (e, t) => t.
163
275
  ...t.contentType === void 0 ? {} : { contentType: t.contentType },
164
276
  ...t.status === void 0 ? {} : { status: t.status },
165
277
  ...t.headers === void 0 ? {} : { headers: t.headers }
166
- }), N = (e) => typeof e == "object" && !!e && e.__voltroRestBytes === !0, P = async (e) => {
278
+ }), Y = (e) => typeof e == "object" && !!e && e.__voltroRestBytes === !0, ie = async (e) => {
167
279
  let t = await globalThis.crypto.subtle.digest("SHA-1", new TextEncoder().encode(e));
168
280
  return [...new Uint8Array(t)].map((e) => e.toString(16).padStart(2, "0")).join("");
169
- }, F = (e, t, n) => ({
281
+ }, X = (e, t, n, r = "application/json; charset=utf-8") => ({
170
282
  status: e,
171
- contentType: "application/json; charset=utf-8",
283
+ contentType: r,
172
284
  body: JSON.stringify(t),
173
285
  ...n ? { headers: n } : {}
174
- }), I = (e) => {
286
+ }), Z = (e, t, n, r, i, a, o) => {
287
+ let s = e.projection;
288
+ if (s === void 0) return X(n, {
289
+ error: i,
290
+ ...a
291
+ }, o);
292
+ let l = {
293
+ tag: s.tag,
294
+ method: t.method,
295
+ path: t.path,
296
+ query: t.query,
297
+ source: s.source,
298
+ status: n
299
+ }, u = c(s.format, {
300
+ status: n,
301
+ tag: r,
302
+ message: i,
303
+ fields: a
304
+ }, l);
305
+ return X(n, u.body, {
306
+ ...o,
307
+ ...u.headers ?? {}
308
+ }, u.contentType);
309
+ }, ae = (e) => {
175
310
  if (e.length === 0) return;
176
311
  let t = new TextDecoder().decode(e);
177
312
  if (t.trim() !== "") return JSON.parse(t);
178
- }, L = (e) => {
313
+ }, oe = (e) => {
179
314
  let t = {};
180
315
  for (let [n, r] of new URLSearchParams(e)) {
181
316
  let e = t[n];
182
317
  e === void 0 ? t[n] = r : Array.isArray(e) ? e.push(r) : t[n] = [e, r];
183
318
  }
184
319
  return t;
185
- }, R = (e, t) => {
320
+ }, Q = (e, t) => {
186
321
  let n = e.split("/").filter((e) => e.length > 0), r = t.split("/").filter((e) => e.length > 0), i = {};
187
322
  for (let e = 0; e < n.length; e++) {
188
323
  let t = n[e];
@@ -196,163 +331,173 @@ var u = (e, t) => t.method ?? (e === "query" ? "GET" : "POST"), d = (e, t) => t.
196
331
  }
197
332
  }
198
333
  return i;
199
- }, z = (e, t) => ({
200
- query: L(e.query),
334
+ }, se = (e, t) => {
335
+ let n = e.path.split("/"), r = n[n.length - 1] === "*", i = r ? n.slice(0, -1) : n, a = t.split("/");
336
+ if (a.length < i.length) return !1;
337
+ for (let e = 0; e < i.length; e++) {
338
+ let t = i[e], n = a[e];
339
+ if (t.startsWith(":")) {
340
+ if (n.length === 0) return !1;
341
+ } else if (t !== n) return !1;
342
+ }
343
+ return a.length === i.length ? !0 : e.match === "exact" && !r ? !1 : a.slice(i.length).some((e) => e.length > 0);
344
+ }, ce = (e, t) => ({
345
+ query: oe(e.query),
201
346
  params: t,
202
- body: I(e.rawBody)
203
- }), B = /* @__PURE__ */ new Set([
347
+ body: ae(e.rawBody)
348
+ }), $ = /* @__PURE__ */ new Set([
204
349
  "POST",
205
350
  "PUT",
206
351
  "PATCH",
207
352
  "DELETE"
208
- ]), V = (e, n, r, i) => {
209
- if (e instanceof E) return F(e.status, e.body, {
353
+ ]), le = (e, n, r, i, a) => {
354
+ if (e instanceof H) return X(e.status, e.body, {
210
355
  ...i,
211
356
  ...e.headers
212
- });
357
+ }, e.contentType);
213
358
  if (typeof e == "object" && e) {
214
- let r = e;
215
- if (typeof r.status == "number") return F(r.status, { error: typeof r.message == "string" ? r.message : "Error" }, i);
216
- if (r._tag === "Unauthenticated" || e instanceof t) return F(401, { error: typeof r.reason == "string" ? r.reason : "Authentication required" }, i);
217
- if (r._tag === "ScopeError") return F(n.type === "anonymous" ? 401 : 403, { error: typeof r.message == "string" ? r.message : "Forbidden" }, i);
359
+ let o = e;
360
+ if (typeof o.status == "number") return Z(r, a, o.status, typeof o._tag == "string" ? o._tag : "Error", typeof o.message == "string" ? o.message : "Error", {}, { ...i });
361
+ if (o._tag === "Unauthenticated" || e instanceof t) return Z(r, a, 401, "Unauthorized", typeof o.reason == "string" ? o.reason : "Authentication required", {}, { ...i });
362
+ if (o._tag === "ScopeError") {
363
+ let e = n.type === "anonymous" ? 401 : 403;
364
+ return Z(r, a, e, e === 401 ? "Unauthorized" : "Forbidden", typeof o.message == "string" ? o.message : "Forbidden", {}, { ...i });
365
+ }
218
366
  }
219
- return o({
367
+ return s({
220
368
  error: e,
221
369
  source: "rest",
222
370
  name: `${r.method} ${r.path}`,
223
371
  fields: { status: 500 }
224
- }), F(500, { error: "Internal Server Error" }, i);
225
- }, H = (o, l) => {
226
- let u = o.input ? c.decodeUnknown(o.input) : void 0, d = c.encode(o.output), f = {}, p = (e) => e.replace(/\/$/, "").split("/").length, m = p(o.path);
227
- return o.deprecated !== void 0 && (f.Deprecation = "true"), o.sunset !== void 0 && (f.Sunset = o.sunset), {
372
+ }), {
373
+ ...Z(r, a, 500, "InternalServerError", "Internal Server Error", {}, { ...i }),
374
+ cause: e
375
+ };
376
+ }, ue = (r, a) => {
377
+ let o = r.input ? b.decodeUnknown(r.input) : void 0, s = b.encode(r.output), c = {}, l = (e) => e.replace(/\/$/, "").split("/").length, u = l(r.path);
378
+ return r.deprecated !== void 0 && (c.Deprecation = "true"), r.sunset !== void 0 && (c.Sunset = r.sunset), {
228
379
  method: "*",
229
- path: o.path,
230
- ...o.maxBodyBytes === void 0 ? {} : { maxBodyBytes: o.maxBodyBytes },
231
- handle: async (c) => {
232
- if (!o.path.endsWith("/*") && p(c.path) !== m) return F(404, { error: "Not Found" }, f);
233
- let h = c.method.toUpperCase();
234
- if (!(h === o.method || h === "HEAD" && o.method === "GET")) return F(405, {
235
- error: "Method Not Allowed",
236
- allow: o.method
237
- }, {
238
- ...f,
239
- Allow: o.method
380
+ path: r.path,
381
+ match: r.path.endsWith("/*") ? "prefix" : "exact",
382
+ ...r.maxBodyBytes === void 0 ? {} : { maxBodyBytes: r.maxBodyBytes },
383
+ handle: async (d) => {
384
+ if (!r.path.endsWith("/*") && l(d.path) !== u) return Z(r, d, 404, "NotFound", "Not Found", {}, { ...c });
385
+ let f = d.method.toUpperCase();
386
+ if (!(f === r.method || f === "HEAD" && r.method === "GET")) return Z(r, d, 405, "MethodNotAllowed", "Method Not Allowed", { allow: r.method }, {
387
+ ...c,
388
+ Allow: r.method
240
389
  });
241
- if (o.sunset !== void 0) {
242
- let e = Date.parse(o.sunset);
243
- if (!Number.isNaN(e) && Date.now() >= e) return F(410, {
244
- error: "Gone",
245
- ...o.version === void 0 ? {} : { version: o.version },
246
- ...o.deprecated === void 0 ? {} : { replacement: o.deprecated }
247
- }, f);
390
+ if (r.sunset !== void 0) {
391
+ let e = Date.parse(r.sunset);
392
+ if (!Number.isNaN(e) && Date.now() >= e) return Z(r, d, 410, "Gone", "Gone", {
393
+ ...r.version === void 0 ? {} : { version: r.version },
394
+ ...r.deprecated === void 0 ? {} : { replacement: r.deprecated }
395
+ }, { ...c });
248
396
  }
249
- let g;
250
- if (u) {
251
- let e = z(c, R(o.path, c.path)), t = await s.runPromise(u(e).pipe(s.map((e) => ({
397
+ let p;
398
+ if (o) {
399
+ let e = ce(d, Q(r.path, d.path)), t = await v.runPromise(o(e).pipe(v.map((e) => ({
252
400
  ok: !0,
253
401
  value: e
254
- })), s.catchAll((e) => s.succeed({
402
+ })), v.catchAll((e) => v.succeed({
255
403
  ok: !1,
256
404
  cause: e
257
405
  }))));
258
- if (!t.ok) return F(400, {
259
- error: "Invalid request",
260
- detail: String(t.cause)
261
- }, f);
262
- g = t.value;
406
+ if (!t.ok) return Z(r, d, 400, "InvalidRequest", "Invalid request", { detail: String(t.cause) }, { ...c });
407
+ p = t.value;
263
408
  }
264
- let _;
409
+ let m;
265
410
  try {
266
- _ = l.resolveSubject ? await l.resolveSubject(c.headers) : e(c.headers["x-tenant"] ?? null);
411
+ m = a.resolveSubject ? await a.resolveSubject(d.headers) : e(d.headers["x-tenant"] ?? null);
267
412
  } catch (e) {
268
413
  if (e instanceof t || typeof e == "object" && e && e._tag === "Unauthenticated") {
269
414
  let t = e.reason;
270
- return F(401, { error: t ?? "Authentication required" }, f);
415
+ return Z(r, d, 401, "Unauthorized", t ?? "Authentication required", {}, { ...c });
271
416
  }
272
417
  throw e;
273
418
  }
274
- let v;
419
+ let h;
275
420
  try {
276
- v = l.buildContext ? await l.buildContext(_, c.headers) : void 0;
421
+ h = a.buildContext ? await a.buildContext(m, d.headers) : void 0;
277
422
  } catch (e) {
278
- return V(e, _, o, f);
423
+ return le(e, m, r, c, d);
279
424
  }
280
- let y = typeof v == "object" && v && "store" in v ? v.store : l.store, b = {
281
- subject: _,
282
- headers: c.headers,
425
+ let y = typeof h == "object" && h && "store" in h ? h.store : a.store, b = {
426
+ subject: m,
427
+ headers: d.headers,
283
428
  store: y,
284
- method: h,
285
- path: c.path,
286
- query: c.query,
287
- rawBody: c.rawBody,
288
- ...v === void 0 ? {} : { app: v }
429
+ method: f,
430
+ path: d.path,
431
+ query: d.query,
432
+ rawBody: d.rawBody,
433
+ ...h === void 0 ? {} : { app: h }
289
434
  };
290
- if (o.guards) for (let e of o.guards) {
435
+ if (r.guards) for (let e of r.guards) {
291
436
  let t = await e(b);
292
- if (t) return F(t.status, { error: t.message }, f);
437
+ if (t) return Z(r, d, t.status, t.status === 401 ? "Unauthorized" : t.status === 403 ? "Forbidden" : "GuardRejected", t.message, {}, { ...c });
293
438
  }
294
- let x = l.idempotency, S = x && B.has(o.method) ? c.headers[x.header.toLowerCase()] : void 0, C = x && S ? r(_.tenantId, o.method, o.path) : "";
439
+ let x = a.idempotency, S = x && $.has(r.method) ? d.headers[x.header.toLowerCase()] : void 0, C = x && S ? i(m.tenantId, r.method, r.path) : "";
295
440
  if (x && S) {
296
- let e = await i(x.store, C, S, x.ttlMs, Date.now());
297
- if (e.kind === "replay") return F(e.response.status, e.response.body, {
298
- ...f,
441
+ let e = await n(x.store, C, S, x.ttlMs, Date.now());
442
+ if (e.kind === "replay") return X(e.response.status, e.response.body, {
443
+ ...c,
299
444
  "Idempotency-Replayed": "true"
300
445
  });
301
- if (e.kind === "conflict") return F(409, { error: "A request with this Idempotency-Key is already being processed" }, f);
446
+ if (e.kind === "conflict") return Z(r, d, 409, "Conflict", "A request with this Idempotency-Key is already being processed", {}, { ...c });
302
447
  }
303
448
  try {
304
- let e = await o.handler(g, b);
305
- if (j(e) || N(e)) return x && S && await a(x.store, C, S), N(e) ? {
449
+ let e = await r.handler(p, b);
450
+ if (q(e) || Y(e)) return x && S && await g(x.store, C, S), Y(e) ? {
306
451
  status: e.status ?? 200,
307
452
  headers: {
308
- ...f,
453
+ ...c,
309
454
  ...e.headers
310
455
  },
311
456
  byteStream: e.byteStream,
312
457
  ...e.contentType === void 0 ? {} : { contentType: e.contentType }
313
458
  } : {
314
459
  status: 200,
315
- headers: f,
460
+ headers: c,
316
461
  stream: e.stream
317
462
  };
318
- let t = T(e) ? e : w(e), r = await s.runPromise(d(t.body)), i = {
319
- ...f,
463
+ let t = V(e) ? e : B(e), n = await v.runPromise(s(t.body)), i = {
464
+ ...c,
320
465
  ...t.headers
321
466
  };
322
- if (x && S && await n(x.store, C, S, {
467
+ if (x && S && await _(x.store, C, S, {
323
468
  status: t.status,
324
- body: r
325
- }, Date.now()), o.etag === !0 && o.method === "GET") {
326
- let e = `W/"${await P(JSON.stringify(r))}"`, n = c.headers["if-none-match"];
327
- return n !== void 0 && n.split(",").some((t) => t.trim() === e) ? {
469
+ body: n
470
+ }, Date.now()), r.etag === !0 && r.method === "GET") {
471
+ let e = `W/"${await ie(JSON.stringify(n))}"`, r = d.headers["if-none-match"];
472
+ return r !== void 0 && r.split(",").some((t) => t.trim() === e) ? {
328
473
  status: 304,
329
474
  headers: {
330
- ...f,
475
+ ...c,
331
476
  ETag: e
332
477
  }
333
- } : F(t.status, r, {
478
+ } : X(t.status, n, {
334
479
  ...i,
335
480
  ETag: e
336
- });
481
+ }, t.contentType);
337
482
  }
338
483
  return t.status === 204 || t.status === 304 ? {
339
484
  status: t.status,
340
485
  headers: i
341
- } : F(t.status, r, i);
486
+ } : X(t.status, n, i, t.contentType);
342
487
  } catch (e) {
343
- return x && S && await a(x.store, C, S), V(e, _, o, f);
488
+ return x && S && await g(x.store, C, S), le(e, m, r, c, d);
344
489
  }
345
490
  }
346
491
  };
347
- }, U = (e, t = {}) => {
492
+ }, de = (e, t = {}) => {
348
493
  if (t.idempotency !== void 0) {
349
- for (let t of e) if (t.streaming === !0 && B.has(t.method)) throw Error(`REST route ${t.method} ${t.path} declares \`streaming: true\` on a method the idempotency binding claims (${[...B].join("/")}). A stream cannot complete an idempotency claim — there is no replayable body to cache — so a retried request would 409 until the claim's TTL. Either serve the stream on GET, or scope the idempotency binding away from this app's streaming routes.`);
494
+ for (let t of e) if (t.streaming === !0 && $.has(t.method)) throw Error(`REST route ${t.method} ${t.path} declares \`streaming: true\` on a method the idempotency binding claims (${[...$].join("/")}). A stream cannot complete an idempotency claim — there is no replayable body to cache — so a retried request would 409 until the claim's TTL. Either serve the stream on GET, or scope the idempotency binding away from this app's streaming routes.`);
350
495
  }
351
- return e.map((e) => H(e, t));
352
- }, W = async (e, t) => {
496
+ return e.map((e) => ue(e, t));
497
+ }, fe = async (e, t) => {
353
498
  let n = await e[0].handle(t);
354
499
  for (let r = 1; r < e.length && n.status === 405; r++) n = await e[r].handle(t);
355
500
  return n;
356
501
  };
357
502
  //#endregion
358
- export { E as RestRouteError, M as bytes, h as coerceQueryObject, m as coerceQueryValue, b as collectPublicApiRoutes, g as declaredErrorTags, O as defineRestRoute, u as derivePublicMethod, d as derivePublicPath, W as dispatchSharedPath, N as isRestByteResponse, T as isRestResponse, j as isRestStreamResponse, R as matchPathParams, v as projectedFailure, y as publicApiRoute, C as requireAnyScope, x as requireAuthenticated, S as requireScope, w as response, D as restError, U as restRoutesToHttpRoutes, k as sse, A as sseFrame };
503
+ export { m as JSONAPI_MEDIA_TYPE, u as PROBLEM_JSON, f as REST_PROFILE_NAMES, H as RestRouteError, J as bytes, k as coerceQueryObject, O as coerceQueryValue, I as collectPublicApiRoutes, A as declaredErrorTags, W as defineRestRoute, S as derivePublicMethod, C as derivePublicPath, fe as dispatchSharedPath, c as encodeRestError, r as encodeRestSuccess, N as errorMemberSchema, Y as isRestByteResponse, V as isRestResponse, q as isRestStreamResponse, Q as matchPathParams, d as nextLink, w as normalizePathParams, P as operationIdOf, T as pathParamNames, p as problemDetails, a as problemTypeOf, M as projectedFailure, F as publicApiRoute, z as requireAnyScope, L as requireAuthenticated, R as requireScope, l as resolveRestFormat, B as response, U as restError, de as restRoutesToHttpRoutes, h as restViewSchema, se as routeAcceptsPath, G as sse, K as sseFrame, o as toSnakeCase };