@revenexx/app-sdk 0.9.0 → 1.1.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/README.md +162 -35
- package/dist/{chunk-2BOFXTTC.js → chunk-P6ZEOMKS.js} +129 -6
- package/dist/chunk-P6ZEOMKS.js.map +1 -0
- package/dist/{errors-D7FGkghT.d.cts → errors-CtrtCOoG.d.cts} +39 -6
- package/dist/{errors-D7FGkghT.d.ts → errors-CtrtCOoG.d.ts} +39 -6
- package/dist/index.cjs +111 -13
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +10 -3
- package/dist/index.d.ts +10 -3
- package/dist/index.js +73 -81
- package/dist/index.js.map +1 -1
- package/dist/router/index.cjs +85 -28
- package/dist/router/index.cjs.map +1 -1
- package/dist/router/index.d.cts +27 -4
- package/dist/router/index.d.ts +27 -4
- package/dist/router/index.js +85 -25
- package/dist/router/index.js.map +1 -1
- package/package.json +1 -6
- package/dist/chunk-2BOFXTTC.js.map +0 -1
- package/dist/chunk-TUTGCI5D.js +0 -41
- package/dist/chunk-TUTGCI5D.js.map +0 -1
- package/dist/instrument.cjs +0 -75
- package/dist/instrument.cjs.map +0 -1
- package/dist/instrument.d.cts +0 -26
- package/dist/instrument.d.ts +0 -26
- package/dist/instrument.js +0 -7
- package/dist/instrument.js.map +0 -1
package/dist/router/index.cjs
CHANGED
|
@@ -36,6 +36,7 @@ __export(router_exports, {
|
|
|
36
36
|
DEFAULT_SETTINGS_TIMEOUT_MS: () => DEFAULT_SETTINGS_TIMEOUT_MS,
|
|
37
37
|
DEFAULT_SETTINGS_TTL_MS: () => DEFAULT_SETTINGS_TTL_MS,
|
|
38
38
|
HttpError: () => HttpError,
|
|
39
|
+
RESERVED_LIST_QUERY_KEYS: () => RESERVED_LIST_QUERY_KEYS,
|
|
39
40
|
actorFrom: () => actorFrom,
|
|
40
41
|
badRequest: () => badRequest,
|
|
41
42
|
conflict: () => conflict,
|
|
@@ -44,7 +45,8 @@ __export(router_exports, {
|
|
|
44
45
|
forbidden: () => forbidden,
|
|
45
46
|
isHttpError: () => isHttpError,
|
|
46
47
|
mountCrud: () => mountCrud,
|
|
47
|
-
notFound: () => notFound
|
|
48
|
+
notFound: () => notFound,
|
|
49
|
+
refusalOf: () => refusalOf
|
|
48
50
|
});
|
|
49
51
|
module.exports = __toCommonJS(router_exports);
|
|
50
52
|
|
|
@@ -284,10 +286,41 @@ function isHttpError(err) {
|
|
|
284
286
|
const e = err;
|
|
285
287
|
return e.__isHttpError === true || e.name === "HttpError" && typeof e.status === "number";
|
|
286
288
|
}
|
|
287
|
-
var notFound = (message = "not found") => new HttpError(404, message);
|
|
288
|
-
var badRequest = (message = "bad request", details) => new HttpError(400, message, details);
|
|
289
|
-
var forbidden = (message = "forbidden") => new HttpError(403, message);
|
|
290
|
-
var conflict = (message = "conflict") => new HttpError(409, message);
|
|
289
|
+
var notFound = (message = "not found", options = {}) => new HttpError(404, message, void 0, { code: "not_found", ...options });
|
|
290
|
+
var badRequest = (message = "bad request", details, options = {}) => new HttpError(400, message, details, { code: "bad_request", ...options });
|
|
291
|
+
var forbidden = (message = "forbidden", options = {}) => new HttpError(403, message, void 0, { code: "forbidden", ...options });
|
|
292
|
+
var conflict = (message = "conflict", options = {}) => new HttpError(409, message, void 0, { code: "conflict", ...options });
|
|
293
|
+
function refusalBody(message, code, details) {
|
|
294
|
+
return {
|
|
295
|
+
error: message,
|
|
296
|
+
message,
|
|
297
|
+
...code ? { code, type: code } : {},
|
|
298
|
+
...details != null ? { details } : {}
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
function refusalOf(err) {
|
|
302
|
+
if (isHttpError(err)) {
|
|
303
|
+
return { status: err.status, code: err.code ?? null, message: err.message, details: err.details ?? null, app: null };
|
|
304
|
+
}
|
|
305
|
+
if (!err || typeof err !== "object") return null;
|
|
306
|
+
const e = err;
|
|
307
|
+
const status = typeof e.code === "number" ? e.code : Number.NaN;
|
|
308
|
+
if (!(status >= 400 && status <= 599) || typeof e.response !== "string") return null;
|
|
309
|
+
let body = {};
|
|
310
|
+
try {
|
|
311
|
+
const parsed = JSON.parse(e.response || "{}");
|
|
312
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) body = parsed;
|
|
313
|
+
} catch {
|
|
314
|
+
}
|
|
315
|
+
const str = (v) => typeof v === "string" && v !== "" ? v : null;
|
|
316
|
+
return {
|
|
317
|
+
status,
|
|
318
|
+
code: str(body.code) ?? str(body.type) ?? str(e.type),
|
|
319
|
+
message: str(body.error) ?? str(body.message) ?? str(e.message) ?? "the call was refused",
|
|
320
|
+
details: body.details ?? null,
|
|
321
|
+
app: str(e.app)
|
|
322
|
+
};
|
|
323
|
+
}
|
|
291
324
|
|
|
292
325
|
// src/router/router.ts
|
|
293
326
|
var splitPath = (path) => path.split("/").filter(Boolean);
|
|
@@ -446,9 +479,9 @@ function createApp(options = {}) {
|
|
|
446
479
|
span.setAttribute("http.response.status_code", status);
|
|
447
480
|
span.setStatus({ code: import_api.SpanStatusCode.ERROR, message: pathExists ? "method not allowed" : "no route" });
|
|
448
481
|
if (pathExists) {
|
|
449
|
-
return res.json(
|
|
482
|
+
return res.json(refusalBody(`method ${method} not allowed for ${path}`, "method_not_allowed"), 405);
|
|
450
483
|
}
|
|
451
|
-
return res.json({
|
|
484
|
+
return res.json({ ...refusalBody(`no route for ${method} ${path}`, "no_route"), routes: app.routes() }, 404);
|
|
452
485
|
}
|
|
453
486
|
span.updateName(`${method} ${matched.route.template}`);
|
|
454
487
|
span.setAttribute("http.route", matched.route.template);
|
|
@@ -488,24 +521,15 @@ function createApp(options = {}) {
|
|
|
488
521
|
span.setAttribute("revenexx.error.detail", detail);
|
|
489
522
|
context.error?.(`${method} ${path} \u2014 ${err.status} ${err.message}: ${detail}`);
|
|
490
523
|
}
|
|
491
|
-
return res.json(
|
|
492
|
-
{
|
|
493
|
-
error: err.message,
|
|
494
|
-
...err.code ? { code: err.code } : {},
|
|
495
|
-
...err.details != null ? { details: err.details } : {}
|
|
496
|
-
},
|
|
497
|
-
err.status
|
|
498
|
-
);
|
|
499
|
-
}
|
|
500
|
-
const msg = err instanceof Error ? err.message : String(err);
|
|
501
|
-
if (/not permitted/.test(msg)) {
|
|
502
|
-
span.setAttribute("http.response.status_code", 403);
|
|
503
|
-
return res.json({ error: msg }, 403);
|
|
524
|
+
return res.json(refusalBody(err.message, err.code, err.details), err.status);
|
|
504
525
|
}
|
|
526
|
+
const upstream = refusalOf(err);
|
|
527
|
+
const msg = upstream ? `refused by ${upstream.app ?? "another app"}: ${upstream.status} ${upstream.code ?? ""} ${upstream.message}`.replace(/\s+/g, " ") : err instanceof Error ? err.message : String(err);
|
|
528
|
+
span.setAttribute("http.response.status_code", 500);
|
|
505
529
|
span.recordException(err instanceof Error ? err : new Error(msg));
|
|
506
530
|
span.setStatus({ code: import_api.SpanStatusCode.ERROR, message: msg });
|
|
507
531
|
context.error?.(`unhandled error on ${method} ${path}: ${msg}`);
|
|
508
|
-
return res.json(
|
|
532
|
+
return res.json(refusalBody("internal error", "internal_error"), 500);
|
|
509
533
|
} finally {
|
|
510
534
|
span.end();
|
|
511
535
|
}
|
|
@@ -526,7 +550,11 @@ function updateFields(patch) {
|
|
|
526
550
|
);
|
|
527
551
|
}
|
|
528
552
|
|
|
553
|
+
// src/query.ts
|
|
554
|
+
var OPS = /* @__PURE__ */ new Set(["eq", "neq", "gt", "gte", "lt", "lte", "like", "ilike", "in", "is"]);
|
|
555
|
+
|
|
529
556
|
// src/router/crud.ts
|
|
557
|
+
var RESERVED_LIST_QUERY_KEYS = Object.freeze(["limit", "offset", "order"]);
|
|
530
558
|
var INT = (v, d) => {
|
|
531
559
|
const n = parseInt(String(v), 10);
|
|
532
560
|
return Number.isFinite(n) ? n : d;
|
|
@@ -544,21 +572,46 @@ function assertJsonFilter(column, value) {
|
|
|
544
572
|
);
|
|
545
573
|
}
|
|
546
574
|
}
|
|
575
|
+
function assertKnownKeys(query, opts) {
|
|
576
|
+
const columns = opts.columns ?? {};
|
|
577
|
+
const extra = opts.queryParams ?? [];
|
|
578
|
+
const known = (k) => Object.prototype.hasOwnProperty.call(columns, k) || RESERVED_LIST_QUERY_KEYS.includes(k) || extra.includes(k);
|
|
579
|
+
const unknown = Object.keys(query).filter((k) => !known(k));
|
|
580
|
+
if (unknown.length === 0) return;
|
|
581
|
+
const allowed = [.../* @__PURE__ */ new Set([...Object.keys(columns), ...RESERVED_LIST_QUERY_KEYS, ...extra])].sort();
|
|
582
|
+
const quoted = unknown.map((k) => `'${k}'`).join(", ");
|
|
583
|
+
throw new HttpError(
|
|
584
|
+
400,
|
|
585
|
+
`unknown list filter ${quoted} \u2014 not a column of this resource. A filter that cannot be applied is refused rather than ignored; see details.allowed.`,
|
|
586
|
+
{ unknown, allowed },
|
|
587
|
+
{ code: "unknown_filter" }
|
|
588
|
+
);
|
|
589
|
+
}
|
|
590
|
+
var OPERATOR_PREFIX = new RegExp(`^(?:not|${[...OPS].join("|")})\\.`);
|
|
591
|
+
function assertNoOperator(column, value) {
|
|
592
|
+
if (!OPERATOR_PREFIX.test(value)) return;
|
|
593
|
+
throw new HttpError(
|
|
594
|
+
400,
|
|
595
|
+
`filter '${column}' carries an operator ('${value.slice(0, value.indexOf(".") + 1)}') \u2014 a list filters by exact value only; operators and value lists are not supported.`,
|
|
596
|
+
{ parameter: column },
|
|
597
|
+
{ code: "unsupported_filter_operator" }
|
|
598
|
+
);
|
|
599
|
+
}
|
|
547
600
|
function parentWhere(c, parent) {
|
|
548
601
|
if (!parent) return {};
|
|
549
602
|
const value = c.params[parent.param];
|
|
550
|
-
if (!value) throw new HttpError(400, `missing path parameter '${parent.param}'
|
|
603
|
+
if (!value) throw new HttpError(400, `missing path parameter '${parent.param}'`, void 0, { code: "missing_parameter" });
|
|
551
604
|
return { [parent.column]: value };
|
|
552
605
|
}
|
|
553
606
|
async function assertParentExists(c, parent) {
|
|
554
607
|
if (!parent?.repo) return;
|
|
555
608
|
const value = c.params[parent.param];
|
|
556
|
-
if (!value) throw new HttpError(400, `missing path parameter '${parent.param}'
|
|
609
|
+
if (!value) throw new HttpError(400, `missing path parameter '${parent.param}'`, void 0, { code: "missing_parameter" });
|
|
557
610
|
if (!await parent.repo.get(value)) throw notFound();
|
|
558
611
|
}
|
|
559
612
|
async function fetchOwned(repo, c, opts) {
|
|
560
613
|
const id = c.params[opts.idParam ?? "id"] ?? "";
|
|
561
|
-
if (!id) throw new HttpError(400, `missing path parameter '${opts.idParam ?? "id"}'
|
|
614
|
+
if (!id) throw new HttpError(400, `missing path parameter '${opts.idParam ?? "id"}'`, void 0, { code: "missing_parameter" });
|
|
562
615
|
const row = await repo.get(id);
|
|
563
616
|
if (!row) throw notFound();
|
|
564
617
|
if (opts.parent) {
|
|
@@ -575,13 +628,15 @@ function mountCrud(app, repo, opts) {
|
|
|
575
628
|
const maxLimit = opts.maxLimit ?? 200;
|
|
576
629
|
if (enabled.has("list")) {
|
|
577
630
|
app.get(base, async (c) => {
|
|
578
|
-
|
|
631
|
+
assertKnownKeys(c.query, opts);
|
|
579
632
|
const where = {};
|
|
580
633
|
for (const [k, v] of Object.entries(c.query)) {
|
|
581
|
-
if (!opts.columns || !(
|
|
634
|
+
if (!opts.columns || !Object.prototype.hasOwnProperty.call(opts.columns, k)) continue;
|
|
635
|
+
assertNoOperator(k, v);
|
|
582
636
|
if (isJsonColumn(opts.columns[k])) assertJsonFilter(k, v);
|
|
583
637
|
where[k] = v;
|
|
584
638
|
}
|
|
639
|
+
await assertParentExists(c, opts.parent);
|
|
585
640
|
Object.assign(where, parentWhere(c, opts.parent));
|
|
586
641
|
const limit = Math.min(Math.max(INT(c.query.limit, defaultLimit), 1), maxLimit);
|
|
587
642
|
const offset = Math.max(INT(c.query.offset, 0), 0);
|
|
@@ -615,7 +670,7 @@ function mountCrud(app, repo, opts) {
|
|
|
615
670
|
if (enabled.has("update")) {
|
|
616
671
|
app.put(idTemplate, async (c) => {
|
|
617
672
|
const { id } = await fetchOwned(repo, c, opts);
|
|
618
|
-
if (updateFields(c.body).length === 0) throw new HttpError(400, EMPTY_UPDATE_MESSAGE);
|
|
673
|
+
if (updateFields(c.body).length === 0) throw new HttpError(400, EMPTY_UPDATE_MESSAGE, void 0, { code: "empty_update" });
|
|
619
674
|
const patch = { ...c.body, ...parentWhere(c, opts.parent) };
|
|
620
675
|
return c.json(await repo.update(id, patch));
|
|
621
676
|
});
|
|
@@ -637,6 +692,7 @@ function mountCrud(app, repo, opts) {
|
|
|
637
692
|
DEFAULT_SETTINGS_TIMEOUT_MS,
|
|
638
693
|
DEFAULT_SETTINGS_TTL_MS,
|
|
639
694
|
HttpError,
|
|
695
|
+
RESERVED_LIST_QUERY_KEYS,
|
|
640
696
|
actorFrom,
|
|
641
697
|
badRequest,
|
|
642
698
|
conflict,
|
|
@@ -645,6 +701,7 @@ function mountCrud(app, repo, opts) {
|
|
|
645
701
|
forbidden,
|
|
646
702
|
isHttpError,
|
|
647
703
|
mountCrud,
|
|
648
|
-
notFound
|
|
704
|
+
notFound,
|
|
705
|
+
refusalOf
|
|
649
706
|
});
|
|
650
707
|
//# sourceMappingURL=index.cjs.map
|