@voltro/protocol 0.69.0 → 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,56 +1,257 @@
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";
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";
4
5
  //#region src/publicApi.ts
5
- var l = (e, t) => t.method ?? (e === "query" ? "GET" : "POST"), u = (e, t) => t.path ?? `/${t.version ?? "v1"}/${e.replace(/\./g, "/")}`, d = (e, t, n) => {
6
- let r = e.publicApi;
7
- if (r === void 0) throw Error(`publicApiRoute: descriptor '${e.name}' has no publicApi annotation`);
8
- let i = l(e.kind, r), a = u(e.name, r), o = i === "GET" ? "query" : "body", s = c.Struct({ [o]: e.input }), d = (r.scopes ?? []).map((e) => m(e)), f = r.stream === "sse" && e.kind === "query" && n !== void 0, p = f ? (e, t) => x((r) => n(e[o], t, (e) => {
9
- r(S(String(e._tag ?? "message"), e));
10
- })) : (e, n) => t(e[o], n);
11
- return b({
12
- method: i,
13
- path: a,
14
- input: s,
15
- output: e.output,
16
- handler: p,
17
- ...f ? { streaming: !0 } : {},
18
- ...d.length > 0 ? { guards: d } : {},
19
- ...r.summary === void 0 ? {} : { summary: r.summary },
20
- ...r.description === void 0 ? {} : { description: r.description },
21
- ...r.deprecated === void 0 ? {} : { deprecated: r.deprecated },
22
- ...r.sunset === void 0 ? {} : { sunset: r.sunset }
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
+ if (t.length === 1) return t[0];
10
+ if (t.length !== e.types.length) return x.Union.make(t);
11
+ }
12
+ return e;
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
+ for (let t of n.types) {
18
+ let n = O(e, t);
19
+ if (n !== e) return n;
20
+ }
21
+ return e;
22
+ }
23
+ if (x.isTupleType(n)) {
24
+ let t = Array.isArray(e) ? e : e === void 0 ? void 0 : [e];
25
+ if (t === void 0) return e;
26
+ let r = n.rest[0]?.type ?? n.elements[0]?.type;
27
+ return r === void 0 ? t : t.map((e) => O(e, r));
28
+ }
29
+ if (typeof e != "string") return e;
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
+ let t = n.literal;
34
+ return typeof t == "number" && D.test(e.trim()) && Number(e) === t || typeof t == "boolean" && String(t) === e ? t : e;
35
+ }
36
+ return e;
37
+ }, k = (e, t) => {
38
+ if (typeof e != "object" || !e || Array.isArray(e)) return e;
39
+ let n = E(x.encodedAST(t.ast));
40
+ if (!x.isTypeLiteral(n)) return e;
41
+ let r = { ...e };
42
+ for (let e of n.propertySignatures) {
43
+ let t = String(e.name);
44
+ t in r && (r[t] = O(r[t], e.type));
45
+ }
46
+ return r;
47
+ }, A = (e) => {
48
+ let t = /* @__PURE__ */ new Set();
49
+ if (e === void 0) return t;
50
+ let n = (e) => {
51
+ if (x.isUnion(e)) {
52
+ for (let t of e.types) n(t);
53
+ return;
54
+ }
55
+ if (x.isTransformation(e)) {
56
+ n(e.from), n(e.to);
57
+ return;
58
+ }
59
+ if (x.isRefinement(e)) {
60
+ n(e.from);
61
+ return;
62
+ }
63
+ if (x.isTypeLiteral(e)) {
64
+ let n = e.propertySignatures.find((e) => e.name === "_tag");
65
+ n !== void 0 && x.isLiteral(n.type) && typeof n.type.literal == "string" && t.add(n.type.literal);
66
+ }
67
+ };
68
+ return n(e.ast), t;
69
+ }, j = (e, t, n) => {
70
+ if (n !== void 0) try {
71
+ let r = b.encodeUnknownSync(n)(e);
72
+ if (typeof r == "object" && r) return {
73
+ error: t,
74
+ ...r
75
+ };
76
+ } catch {}
77
+ let r = { error: t };
78
+ for (let [t, n] of Object.entries(e)) t !== "stack" && t !== "_tag" && (r[t] = n);
79
+ return r;
80
+ }, M = (e, t, n, r = []) => {
81
+ if (typeof e != "object" || !e) return e;
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
23
100
  });
24
- }, f = (e) => e.filter((e) => e.descriptor.publicApi !== void 0).map((e) => d(e.descriptor, e.invoke, e.subscribe)).sort((e, t) => e.path.localeCompare(t.path)), p = () => (e) => {
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, {
134
+ strict: !1,
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));
161
+ })) : async (n, i) => {
162
+ let o;
163
+ try {
164
+ o = await t(n.value, i);
165
+ } catch (t) {
166
+ throw ee(t, e, a, c, s, M(i));
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
204
+ };
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 }
222
+ });
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) => {
25
224
  if (e.subject.type === "anonymous") return {
26
225
  status: 401,
27
226
  message: e.subject.credentialRejected ?? "Authentication required"
28
227
  };
29
- }, m = (e) => (t) => {
228
+ }, R = (e) => (t) => {
30
229
  let n = t.subject.scopes;
31
230
  if (!(n && (n.includes(e) || n.includes("admin:full")))) return {
32
231
  status: 403,
33
232
  message: `Missing required scope: ${e}`
34
233
  };
35
- }, h = (e) => (t) => {
234
+ }, z = (e) => (t) => {
36
235
  let n = t.subject.scopes;
37
236
  if (!(n && (n.includes("admin:full") || e.some((e) => n.includes(e))))) return {
38
237
  status: 403,
39
238
  message: `Missing required scope: one of ${e.join(", ")}`
40
239
  };
41
- }, g = (e, t = {}) => ({
240
+ }, B = (e, t = {}) => ({
42
241
  __voltroRestResponse: !0,
43
242
  status: t.status ?? 200,
44
243
  body: e,
45
- ...t.headers === void 0 ? {} : { headers: t.headers }
46
- }), _ = (e) => typeof e == "object" && !!e && e.__voltroRestResponse === !0, v = 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 {
47
247
  status;
48
248
  body;
49
249
  headers;
50
- constructor(e, t, n) {
51
- 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;
52
253
  }
53
- }, y = (e, t, n) => new v(e, t, n), b = (e) => {
254
+ }, U = (e, t, n) => new H(e, t, n), W = (e) => {
54
255
  if (e.version === void 0) return e;
55
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', …`);
56
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.`);
@@ -58,13 +259,13 @@ var l = (e, t) => t.method ?? (e === "query" ? "GET" : "POST"), u = (e, t) => t.
58
259
  ...e,
59
260
  path: `/${e.version}${e.path}`
60
261
  };
61
- }, x = (e, t = {}) => ({
262
+ }, G = (e, t = {}) => ({
62
263
  __voltroRestStream: !0,
63
264
  stream: {
64
265
  subscribe: e,
65
266
  ...t.keepAliveMs === void 0 ? {} : { keepAliveMs: t.keepAliveMs }
66
267
  }
67
- }), S = (e, t) => `event: ${e}\n${(typeof t == "string" ? t : JSON.stringify(t)).split("\n").map((e) => `data: ${e}`).join("\n")}\n\n`, C = (e) => typeof e == "object" && !!e && e.__voltroRestStream === !0, w = (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 = {}) => ({
68
269
  __voltroRestBytes: !0,
69
270
  byteStream: {
70
271
  stream: e,
@@ -74,23 +275,49 @@ var l = (e, t) => t.method ?? (e === "query" ? "GET" : "POST"), u = (e, t) => t.
74
275
  ...t.contentType === void 0 ? {} : { contentType: t.contentType },
75
276
  ...t.status === void 0 ? {} : { status: t.status },
76
277
  ...t.headers === void 0 ? {} : { headers: t.headers }
77
- }), T = (e) => typeof e == "object" && !!e && e.__voltroRestBytes === !0, E = async (e) => {
278
+ }), Y = (e) => typeof e == "object" && !!e && e.__voltroRestBytes === !0, ie = async (e) => {
78
279
  let t = await globalThis.crypto.subtle.digest("SHA-1", new TextEncoder().encode(e));
79
280
  return [...new Uint8Array(t)].map((e) => e.toString(16).padStart(2, "0")).join("");
80
- }, D = (e, t, n) => ({
281
+ }, X = (e, t, n, r = "application/json; charset=utf-8") => ({
81
282
  status: e,
82
- contentType: "application/json; charset=utf-8",
283
+ contentType: r,
83
284
  body: JSON.stringify(t),
84
285
  ...n ? { headers: n } : {}
85
- }), O = (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) => {
86
310
  if (e.length === 0) return;
87
311
  let t = new TextDecoder().decode(e);
88
312
  if (t.trim() !== "") return JSON.parse(t);
89
- }, k = (e) => {
313
+ }, oe = (e) => {
90
314
  let t = {};
91
- for (let [n, r] of new URLSearchParams(e)) t[n] = r;
315
+ for (let [n, r] of new URLSearchParams(e)) {
316
+ let e = t[n];
317
+ e === void 0 ? t[n] = r : Array.isArray(e) ? e.push(r) : t[n] = [e, r];
318
+ }
92
319
  return t;
93
- }, A = (e, t) => {
320
+ }, Q = (e, t) => {
94
321
  let n = e.split("/").filter((e) => e.length > 0), r = t.split("/").filter((e) => e.length > 0), i = {};
95
322
  for (let e = 0; e < n.length; e++) {
96
323
  let t = n[e];
@@ -104,153 +331,173 @@ var l = (e, t) => t.method ?? (e === "query" ? "GET" : "POST"), u = (e, t) => t.
104
331
  }
105
332
  }
106
333
  return i;
107
- }, j = (e, t) => ({
108
- query: k(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),
109
346
  params: t,
110
- body: O(e.rawBody)
111
- }), M = /* @__PURE__ */ new Set([
347
+ body: ae(e.rawBody)
348
+ }), $ = /* @__PURE__ */ new Set([
112
349
  "POST",
113
350
  "PUT",
114
351
  "PATCH",
115
352
  "DELETE"
116
- ]), N = (l, u) => {
117
- let d = l.input ? c.decodeUnknown(l.input) : void 0, f = c.encode(l.output), p = {}, m = (e) => e.replace(/\/$/, "").split("/").length, h = m(l.path);
118
- return l.deprecated !== void 0 && (p.Deprecation = "true"), l.sunset !== void 0 && (p.Sunset = l.sunset), {
353
+ ]), le = (e, n, r, i, a) => {
354
+ if (e instanceof H) return X(e.status, e.body, {
355
+ ...i,
356
+ ...e.headers
357
+ }, e.contentType);
358
+ if (typeof e == "object" && e) {
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
+ }
366
+ }
367
+ return s({
368
+ error: e,
369
+ source: "rest",
370
+ name: `${r.method} ${r.path}`,
371
+ fields: { status: 500 }
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), {
119
379
  method: "*",
120
- path: l.path,
121
- ...l.maxBodyBytes === void 0 ? {} : { maxBodyBytes: l.maxBodyBytes },
122
- handle: async (c) => {
123
- if (!l.path.endsWith("/*") && m(c.path) !== h) return D(404, { error: "Not Found" }, p);
124
- let y = c.method.toUpperCase();
125
- if (!(y === l.method || y === "HEAD" && l.method === "GET")) return D(405, {
126
- error: "Method Not Allowed",
127
- allow: l.method
128
- }, {
129
- ...p,
130
- Allow: l.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
131
389
  });
132
- if (l.sunset !== void 0) {
133
- let e = Date.parse(l.sunset);
134
- if (!Number.isNaN(e) && Date.now() >= e) return D(410, {
135
- error: "Gone",
136
- ...l.version === void 0 ? {} : { version: l.version },
137
- ...l.deprecated === void 0 ? {} : { replacement: l.deprecated }
138
- }, p);
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 });
139
396
  }
140
- let b;
141
- if (d) {
142
- let e = j(c, A(l.path, c.path)), t = await s.runPromise(d(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) => ({
143
400
  ok: !0,
144
401
  value: e
145
- })), s.catchAll((e) => s.succeed({
402
+ })), v.catchAll((e) => v.succeed({
146
403
  ok: !1,
147
404
  cause: e
148
405
  }))));
149
- if (!t.ok) return D(400, {
150
- error: "Invalid request",
151
- detail: String(t.cause)
152
- }, p);
153
- b = t.value;
406
+ if (!t.ok) return Z(r, d, 400, "InvalidRequest", "Invalid request", { detail: String(t.cause) }, { ...c });
407
+ p = t.value;
154
408
  }
155
- let x;
409
+ let m;
156
410
  try {
157
- x = u.resolveSubject ? await u.resolveSubject(c.headers) : e(c.headers["x-tenant"] ?? null);
411
+ m = a.resolveSubject ? await a.resolveSubject(d.headers) : e(d.headers["x-tenant"] ?? null);
158
412
  } catch (e) {
159
413
  if (e instanceof t || typeof e == "object" && e && e._tag === "Unauthenticated") {
160
414
  let t = e.reason;
161
- return D(401, { error: t ?? "Authentication required" }, p);
415
+ return Z(r, d, 401, "Unauthorized", t ?? "Authentication required", {}, { ...c });
162
416
  }
163
417
  throw e;
164
418
  }
165
- let S = u.buildContext ? await u.buildContext(x, c.headers) : void 0, w = typeof S == "object" && S && "store" in S ? S.store : u.store, O = {
166
- subject: x,
167
- headers: c.headers,
168
- store: w,
169
- method: y,
170
- path: c.path,
171
- query: c.query,
172
- rawBody: c.rawBody,
173
- ...S === void 0 ? {} : { app: S }
419
+ let h;
420
+ try {
421
+ h = a.buildContext ? await a.buildContext(m, d.headers) : void 0;
422
+ } catch (e) {
423
+ return le(e, m, r, c, d);
424
+ }
425
+ let y = typeof h == "object" && h && "store" in h ? h.store : a.store, b = {
426
+ subject: m,
427
+ headers: d.headers,
428
+ store: y,
429
+ method: f,
430
+ path: d.path,
431
+ query: d.query,
432
+ rawBody: d.rawBody,
433
+ ...h === void 0 ? {} : { app: h }
174
434
  };
175
- if (l.guards) for (let e of l.guards) {
176
- let t = await e(O);
177
- if (t) return D(t.status, { error: t.message }, p);
435
+ if (r.guards) for (let e of r.guards) {
436
+ let t = await e(b);
437
+ if (t) return Z(r, d, t.status, t.status === 401 ? "Unauthorized" : t.status === 403 ? "Forbidden" : "GuardRejected", t.message, {}, { ...c });
178
438
  }
179
- let k = u.idempotency, N = k && M.has(l.method) ? c.headers[k.header.toLowerCase()] : void 0, P = k && N ? r(x.tenantId, l.method, l.path) : "";
180
- if (k && N) {
181
- let e = await i(k.store, P, N, k.ttlMs, Date.now());
182
- if (e.kind === "replay") return D(e.response.status, e.response.body, {
183
- ...p,
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) : "";
440
+ if (x && S) {
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,
184
444
  "Idempotency-Replayed": "true"
185
445
  });
186
- if (e.kind === "conflict") return D(409, { error: "A request with this Idempotency-Key is already being processed" }, p);
446
+ if (e.kind === "conflict") return Z(r, d, 409, "Conflict", "A request with this Idempotency-Key is already being processed", {}, { ...c });
187
447
  }
188
448
  try {
189
- let e = await l.handler(b, O);
190
- if (C(e) || T(e)) return k && N && await a(k.store, P, N), T(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) ? {
191
451
  status: e.status ?? 200,
192
452
  headers: {
193
- ...p,
453
+ ...c,
194
454
  ...e.headers
195
455
  },
196
456
  byteStream: e.byteStream,
197
457
  ...e.contentType === void 0 ? {} : { contentType: e.contentType }
198
458
  } : {
199
459
  status: 200,
200
- headers: p,
460
+ headers: c,
201
461
  stream: e.stream
202
462
  };
203
- let t = _(e) ? e : g(e), r = await s.runPromise(f(t.body)), i = {
204
- ...p,
463
+ let t = V(e) ? e : B(e), n = await v.runPromise(s(t.body)), i = {
464
+ ...c,
205
465
  ...t.headers
206
466
  };
207
- if (k && N && await n(k.store, P, N, {
467
+ if (x && S && await _(x.store, C, S, {
208
468
  status: t.status,
209
- body: r
210
- }, Date.now()), l.etag === !0 && l.method === "GET") {
211
- let e = `W/"${await E(JSON.stringify(r))}"`, n = c.headers["if-none-match"];
212
- 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) ? {
213
473
  status: 304,
214
474
  headers: {
215
- ...p,
475
+ ...c,
216
476
  ETag: e
217
477
  }
218
- } : D(t.status, r, {
478
+ } : X(t.status, n, {
219
479
  ...i,
220
480
  ETag: e
221
- });
481
+ }, t.contentType);
222
482
  }
223
483
  return t.status === 204 || t.status === 304 ? {
224
484
  status: t.status,
225
485
  headers: i
226
- } : D(t.status, r, i);
486
+ } : X(t.status, n, i, t.contentType);
227
487
  } catch (e) {
228
- if (k && N && await a(k.store, P, N), e instanceof v) return D(e.status, e.body, {
229
- ...p,
230
- ...e.headers
231
- });
232
- if (typeof e == "object" && e && typeof e.status == "number") {
233
- let t = e;
234
- return D(t.status, { error: t.message ?? "Error" }, p);
235
- }
236
- return o({
237
- error: e,
238
- source: "rest",
239
- name: `${l.method} ${l.path}`,
240
- fields: { status: 500 }
241
- }), D(500, { error: "Internal Server Error" }, p);
488
+ return x && S && await g(x.store, C, S), le(e, m, r, c, d);
242
489
  }
243
490
  }
244
491
  };
245
- }, P = (e, t = {}) => {
492
+ }, de = (e, t = {}) => {
246
493
  if (t.idempotency !== void 0) {
247
- for (let t of e) if (t.streaming === !0 && M.has(t.method)) throw Error(`REST route ${t.method} ${t.path} declares \`streaming: true\` on a method the idempotency binding claims (${[...M].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.`);
248
495
  }
249
- return e.map((e) => N(e, t));
250
- }, F = async (e, t) => {
496
+ return e.map((e) => ue(e, t));
497
+ }, fe = async (e, t) => {
251
498
  let n = await e[0].handle(t);
252
499
  for (let r = 1; r < e.length && n.status === 405; r++) n = await e[r].handle(t);
253
500
  return n;
254
501
  };
255
502
  //#endregion
256
- export { v as RestRouteError, w as bytes, f as collectPublicApiRoutes, b as defineRestRoute, l as derivePublicMethod, u as derivePublicPath, F as dispatchSharedPath, T as isRestByteResponse, _ as isRestResponse, C as isRestStreamResponse, A as matchPathParams, d as publicApiRoute, h as requireAnyScope, p as requireAuthenticated, m as requireScope, g as response, y as restError, P as restRoutesToHttpRoutes, x as sse, S 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 };