@sincpro/web-framework 3.10.0 → 3.12.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 +103 -8
- package/dist/core/index.d.ts +1 -1
- package/dist/core/index.js +1 -1
- package/dist/core/list.d.ts +1 -1
- package/dist/core/server.d.ts +55 -0
- package/dist/core/server.js +74 -7
- package/dist/errors/index.d.ts +5 -13
- package/dist/errors/index.js +2 -11
- package/dist/errors/narrowed.d.ts +18 -0
- package/dist/errors/narrowed.js +25 -0
- package/dist/errors/server.d.ts +92 -0
- package/dist/errors/server.js +170 -0
- package/dist/errors/uncaught.d.ts +16 -0
- package/dist/errors/uncaught.js +41 -0
- package/dist/react/useRecord.d.ts +15 -1
- package/dist/react/useRecord.js +44 -10
- package/dist/telemetry/index.d.ts +10 -1
- package/dist/telemetry/index.js +3 -1
- package/dist/telemetry/server.d.ts +10 -1
- package/dist/telemetry/server.js +16 -4
- package/dist/web/index.d.ts +2 -2
- package/dist/web/list/EntityList.d.ts +31 -9
- package/dist/web/list/EntityList.js +31 -15
- package/dist/web/list/EntitySearchBar.d.ts +6 -1
- package/dist/web/list/EntitySearchBar.js +2 -2
- package/dist/web/list/presentation.d.ts +14 -0
- package/dist/web/workspace/record.d.ts +8 -1
- package/dist/web/workspace/record.js +3 -3
- package/package.json +21 -7
package/README.md
CHANGED
|
@@ -365,9 +365,9 @@ function InvoiceView({ api, resource }: ViewProps) {
|
|
|
365
365
|
}
|
|
366
366
|
```
|
|
367
367
|
|
|
368
|
-
How a record of an entity LOOKS — as a choice, a row, a card — is said once, around the
|
|
369
|
-
and every place that draws one uses it (`EntityPicker`, `EntityTags`, ⌘K, `EntityList`'s
|
|
370
|
-
board). Each size answers web-ui's props for it, over the default; give only what changes:
|
|
368
|
+
How a record of an entity LOOKS — as a choice, a row, a card, a tile — is said once, around the
|
|
369
|
+
entity, and every place that draws one uses it (`EntityPicker`, `EntityTags`, ⌘K, `EntityList`'s
|
|
370
|
+
rows, board and tiles). Each size answers web-ui's props for it, over the default; give only what changes:
|
|
371
371
|
|
|
372
372
|
```tsx
|
|
373
373
|
export const customers = presented(
|
|
@@ -376,6 +376,7 @@ export const customers = presented(
|
|
|
376
376
|
option: (c) => ({ meta: `NIT ${c.tax_id}`, aside: <Balance value={c.balance} /> }),
|
|
377
377
|
row: (c) => (c.blocked ? { tags: <Tag tone="bad">Blocked</Tag> } : {}),
|
|
378
378
|
card: (c) => ({ subtitle: c.city }),
|
|
379
|
+
tile: (c) => ({ media: { src: c.logo_url }, figure: <Balance value={c.balance} /> }),
|
|
379
380
|
},
|
|
380
381
|
);
|
|
381
382
|
```
|
|
@@ -399,18 +400,25 @@ and where the context lives, once, in `main`; without them, everything runs the
|
|
|
399
400
|
```ts
|
|
400
401
|
// entrypoints/main.tsx
|
|
401
402
|
import { configureProcess, processContext } from "@sincpro/client-framework";
|
|
402
|
-
import { glitchtipReporter } from "@sincpro/web-framework/errors"; // optional
|
|
403
|
+
import { glitchtipReporter, reportUncaught } from "@sincpro/web-framework/errors"; // optional
|
|
403
404
|
import { browserTracer } from "@sincpro/web-framework/telemetry"; // optional
|
|
404
405
|
|
|
405
406
|
const release = "my-app:1.0.0"; // <name>:<version>, the backend's APP_RELEASE
|
|
406
407
|
configureProcess({
|
|
407
408
|
identity: { release }, // + tenant, when the deployment serves one
|
|
408
|
-
|
|
409
|
+
// recordExceptions: false — a failure's message and stack stay in GlitchTip, not in every span
|
|
410
|
+
tracer: browserTracer({ endpoint: settings.otlp, release, recordExceptions: false }),
|
|
409
411
|
reporter: glitchtipReporter({ dsn: settings.glitchtip, release }),
|
|
410
412
|
});
|
|
413
|
+
reportUncaught(); // optional: what nobody caught — a click handler, a promise nobody waited for
|
|
411
414
|
processContext.set({ tenant_id: session.tenant }); // every use case runs with it and sends it
|
|
412
415
|
```
|
|
413
416
|
|
|
417
|
+
`reportUncaught()` listens to `window`'s `error` and `unhandledrejection` and hands each to the
|
|
418
|
+
process's `reportError`: a cancellation or a refusal is not a fault, and a use case's failure
|
|
419
|
+
already reported is not reported again. Without it, only what the process decides is unexpected
|
|
420
|
+
reaches the reporter, as before.
|
|
421
|
+
|
|
414
422
|
```tsx
|
|
415
423
|
function CloseButton({ issueId }: { issueId: string }) {
|
|
416
424
|
const close = useService(project.Issues.close); // run · busy · answer · error
|
|
@@ -488,6 +496,28 @@ const balance = useServiceReading(project.Accounts.balance, [{ member_id: member
|
|
|
488
496
|
});
|
|
489
497
|
```
|
|
490
498
|
|
|
499
|
+
A record screen starts the same way. `readRecordOnServer` reads the record and its model with the
|
|
500
|
+
transport the provider is given — plain JSON (`RecordRead`), `undefined` and one line on the log
|
|
501
|
+
when it fails — and `useRecord` starts `ready` from it (so do `useRecordScreen` and
|
|
502
|
+
`EntityRecord`, by `initial`): the HTML carries the record, not a skeleton, and the browser asks
|
|
503
|
+
nothing for it on mount. Only for the record it was read for; editing, previews, drafts and
|
|
504
|
+
saving stay the browser's, as always. Give the provider the business's `timeZone` and `locale`:
|
|
505
|
+
without them an instant is written on the server's clock and then on the browser's, and the HTML
|
|
506
|
+
does not match:
|
|
507
|
+
|
|
508
|
+
```tsx
|
|
509
|
+
// the Server Component
|
|
510
|
+
import { readRecordOnServer } from "@sincpro/web-framework/core";
|
|
511
|
+
const invoice = await readRecordOnServer(transport, invoices, params.id);
|
|
512
|
+
return <InvoicePage id={params.id} initial={invoice} />;
|
|
513
|
+
|
|
514
|
+
// the page ("use client"), under <FrameworkProvider timeZone="America/La_Paz" locale="es">
|
|
515
|
+
function InvoicePage({ id, initial }: { id: string; initial?: RecordRead<Invoice> }) {
|
|
516
|
+
const invoice = useRecord(invoices, id, { initial }); // ready on the server already
|
|
517
|
+
…
|
|
518
|
+
}
|
|
519
|
+
```
|
|
520
|
+
|
|
491
521
|
The server's use cases are traced like the browser's and their `traceparent` reaches the API —
|
|
492
522
|
the counterpart of `browserTracer`, in its own optional entry. The provider is registered once
|
|
493
523
|
per process, where the SSR framework's own spans land too; the process is configured once per
|
|
@@ -500,7 +530,13 @@ export async function register() {
|
|
|
500
530
|
if (process.env.NEXT_RUNTIME !== "nodejs") return;
|
|
501
531
|
const { registerServerTracing } = await import("@sincpro/web-framework/telemetry/server");
|
|
502
532
|
const endpoint = process.env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT; // none: tracing stays off
|
|
503
|
-
await registerServerTracing({
|
|
533
|
+
await registerServerTracing({
|
|
534
|
+
endpoint,
|
|
535
|
+
release,
|
|
536
|
+
environment,
|
|
537
|
+
oneTreePerRequest: true,
|
|
538
|
+
recordExceptions: false, // the detail is the error tracker's
|
|
539
|
+
});
|
|
504
540
|
}
|
|
505
541
|
|
|
506
542
|
// where the server runs use cases — once per realm; in a browser it does nothing
|
|
@@ -515,6 +551,18 @@ unless the platform registered one first, and a use case with no parent of its o
|
|
|
515
551
|
the active span (client-framework 2.5's `adoptActiveSpan`). Without it — the default — each use
|
|
516
552
|
case's span starts a trace of its own, as before.
|
|
517
553
|
|
|
554
|
+
`recordExceptions: false` (both tracers, client-framework 2.6+) keeps a failed use case's span
|
|
555
|
+
to what the run says — `error.type`, `sincpro.outcome`, an ERROR status — without its exception:
|
|
556
|
+
where errors go to an error tracker with their trace id, the message and the stack are already
|
|
557
|
+
there, under its access rules, and a failure's message can name a customer. Default `true`.
|
|
558
|
+
|
|
559
|
+
`registerServerTracing` also registers the W3C trace-context propagator (no baggage), unless the
|
|
560
|
+
platform registered one: an SSR framework reads and writes `traceparent` only through it. With it
|
|
561
|
+
and `oneTreePerRequest`, a request that arrives with a `traceparent` continues that trace, and
|
|
562
|
+
Next writes the request's own into the page (`experimental.clientTraceMetadata: ["traceparent"]`,
|
|
563
|
+
`<meta name="traceparent">` — on dynamic pages only: a cached page would hand every visitor one
|
|
564
|
+
trace).
|
|
565
|
+
|
|
518
566
|
A page that broke while drawing shows the route's error screen instead of the framework's generic
|
|
519
567
|
one: plain words, the code the server logged it with (Next's `digest`), «try again» and «home»,
|
|
520
568
|
reported once through the process:
|
|
@@ -528,6 +576,48 @@ export default function PortalError({ error, reset }: { error: Error; reset: ()
|
|
|
528
576
|
}
|
|
529
577
|
```
|
|
530
578
|
|
|
579
|
+
What fails on the server reaches GlitchTip too, with its real stack — the browser only ever gets
|
|
580
|
+
Next's generic message and the `digest`. `./errors/server` keeps one Sentry-compatible client per
|
|
581
|
+
process (`@sentry/core`'s server client, not `@sentry/node`, which would set up an OpenTelemetry
|
|
582
|
+
of its own); a request's report carries the same `digest` the visitor sees, its route and its
|
|
583
|
+
trace:
|
|
584
|
+
|
|
585
|
+
```ts
|
|
586
|
+
// instrumentation.ts (Next) — once per process, and each request that failed
|
|
587
|
+
export async function register() {
|
|
588
|
+
if (process.env.NEXT_RUNTIME !== "nodejs") return;
|
|
589
|
+
const { registerServerErrors } = await import("@sincpro/web-framework/errors/server");
|
|
590
|
+
await registerServerErrors({ dsn: process.env.SENTRY_DSN, release, environment }); // none: off
|
|
591
|
+
}
|
|
592
|
+
export async function onRequestError(error, request, context) {
|
|
593
|
+
const { reportRequestError } = await import("@sincpro/web-framework/errors/server");
|
|
594
|
+
reportRequestError(error, {
|
|
595
|
+
method: request.method,
|
|
596
|
+
path: request.path,
|
|
597
|
+
route: context.routePath,
|
|
598
|
+
});
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
// where the server runs use cases: what fails unexpectedly there is reported, with its trace
|
|
602
|
+
import { serverReporter } from "@sincpro/web-framework/errors/server";
|
|
603
|
+
configureServerProcess({ identity: { release }, reporter: serverReporter() });
|
|
604
|
+
```
|
|
605
|
+
|
|
606
|
+
**Readable stacks.** A production stack points at built bundles (`page.js:1:4313`). On the server
|
|
607
|
+
nothing has to be uploaded: build the server with its source maps and run Node with its own
|
|
608
|
+
(`experimental.serverSourceMaps: true` in Next, `NODE_OPTIONS=--enable-source-maps`), and each
|
|
609
|
+
reported frame says where it was in the source (`app/cursos/page.tsx:12`) — Next prints its
|
|
610
|
+
stacks mapped but hands over errors whose `stack` is not, so `./errors/server` maps them itself
|
|
611
|
+
with `node:module`. In the browser the maps must never be served: inject debug IDs at build and
|
|
612
|
+
upload them to GlitchTip with its CLI, then drop them from what is deployed:
|
|
613
|
+
|
|
614
|
+
```sh
|
|
615
|
+
# the application's build (Next: productionBrowserSourceMaps: true)
|
|
616
|
+
glitchtip-cli sourcemaps inject .next/static
|
|
617
|
+
glitchtip-cli sourcemaps upload .next/static --release "$RELEASE" # SENTRY_URL, SENTRY_AUTH_TOKEN, SENTRY_ORG, SENTRY_PROJECT
|
|
618
|
+
find .next/static -name '*.map' -delete
|
|
619
|
+
```
|
|
620
|
+
|
|
531
621
|
## The abstractions
|
|
532
622
|
|
|
533
623
|
Each answers one question, and each comes in the three levels — use the component, drop to the
|
|
@@ -539,15 +629,17 @@ hook and draw your own, or go straight to the loose piece.
|
|
|
539
629
|
| The application's logic | its `Feature`s and `ApplicationService`s (client-framework) | `useService` | `project.Issues.close(…)` |
|
|
540
630
|
| Read through a use case | its `Feature`s (client-framework) | `useServiceReading` | `project.Accounts.balance(…)` |
|
|
541
631
|
| Read it on the server first | the same use case, before the page is drawn | `useServiceReading(…, { initial })` | `readOnServer`, `ServerRead` |
|
|
632
|
+
| A record read on the server | `EntityRecord initial` | `useRecord(…, { initial })` | `readRecordOnServer`, `RecordRead` |
|
|
542
633
|
| What every use case runs with | `processContext`, `project.context` (client-framework) | `useContextValues` | `ContextStore` |
|
|
543
634
|
| What happened, what broke | `./telemetry`, `./errors` (optional) | — | `configureProcess({ tracer, reporter })` |
|
|
544
635
|
| What happened, on the server | `configureServerProcess` (`./telemetry/server`, optional) | — | `registerServerTracing`, `serverTracer` |
|
|
636
|
+
| What broke, on the server | `reportRequestError` (`./errors/server`, optional) | — | `registerServerErrors`, `serverReporter` |
|
|
545
637
|
| A page that broke | `RouteError`, in the route's error boundary | — | `reportError` (client-framework) |
|
|
546
638
|
| What is each field? | `Meta` from the server, or `defineMeta` | — | `textOf`, `viewOf`, `metaFromSchema` |
|
|
547
639
|
| A value outside a model | — | `useTextOf` | `moneyTextOf`, `dayTextOf`, `instantTextOf`, `numberTextOf` |
|
|
548
640
|
| A file or a photo it holds | `EntityList`, `RecordField`: by its widget, as a `FileValue` | — | `attachmentOf`, `storedFileOf`, `renditionUrl` |
|
|
549
641
|
| What is a record, at any size? | `cardOf`, `relationOf` | `useSummaryOf` | `summaryOf` |
|
|
550
|
-
| How does a record look? | `presented(entity, { option, row, card })`
|
|
642
|
+
| How does a record look? | `presented(entity, { option, row, card, tile })` | — | `presentationOf` |
|
|
551
643
|
| N records: search, filter, page | `EntityList`, `EntitySearchBar` | `useListing`, `useGroups` | `transport.list(route, criteria)` |
|
|
552
644
|
| A named slice (to collect, …) | `defineList` + `listView` | `useListing(list)` | `scopeOf` |
|
|
553
645
|
| A record's whole screen | `EntityRecord`, `entityView` without `record` | `useRecordScreen` | `formOf` |
|
|
@@ -561,7 +653,7 @@ hook and draw your own, or go straight to the loose piece.
|
|
|
561
653
|
| Choose several (many2many) | `EntityTags` | `useLookup` (`values`) | `lookupOf` |
|
|
562
654
|
| A relation, from the model alone | `RecordField name="customer_id"` | `useEntityFor` | `relationFieldOf`, `entityFor` |
|
|
563
655
|
| Open it where it belongs | `RelatedRecord`, `entityView`, `EntityList openTargets` | — | `openIn`, `targetsFor`, `OpenInMenu` |
|
|
564
|
-
| Look at the set several ways | `EntityList views` (table, rows, board, sheet)
|
|
656
|
+
| Look at the set several ways | `EntityList views` (table, rows, board, sheet, tiles) | — | web-ui's `List.*` |
|
|
565
657
|
| Cross it by two fields | `EntityList pivot` | `usePivot` | `pivotOf` |
|
|
566
658
|
| Order by several fields | the bar's criteria builder | `listing.orderBy([...])` | criteria's `order` |
|
|
567
659
|
| Act on it (with a dry run) | `ServiceButton` (a use case), `CommandButton`, row commands | `useService`, `useCommand` | `transport.send` |
|
|
@@ -618,6 +710,7 @@ Three levels, one direction, and optional entries — `make doctor` keeps the ar
|
|
|
618
710
|
| `web/workspace` | the composer: `entityView`, `openIn`, `RelatedRecord`, `<SincproApp>` | where it opens, the application context |
|
|
619
711
|
| `telemetry`, `errors` | `browserTracer`, `glitchtipReporter`: built from the application's settings | optional peers, loaded after the first render; nothing else imports them |
|
|
620
712
|
| `telemetry/server` | `registerServerTracing`, `configureServerProcess`: a server's use cases traced | optional peers too; Node only — a browser never loads it |
|
|
713
|
+
| `errors/server` | `registerServerErrors`, `serverReporter`, `reportRequestError`: to GlitchTip | optional peer `@sentry/core`; a browser never loads it |
|
|
621
714
|
|
|
622
715
|
Screens import from `@sincpro/web-framework`. What every platform shares — the transport,
|
|
623
716
|
`entity()`, model definitions, `Feature` and `ApplicationService`, the generated client — is
|
|
@@ -658,8 +751,10 @@ moves to the generated one — is [`docs/ADOPTION.md`](docs/ADOPTION.md).
|
|
|
658
751
|
| `defineMeta`, `metaFromSchema` | A model by hand, or completed from OpenAPI (client-framework) | ✅ |
|
|
659
752
|
| `useService` | A screen runs one of the application's use cases: run, busy, answer, error | ✅ |
|
|
660
753
|
| `readOnServer` | A page read on the server by the browser's use case: plain JSON, a failure left to the browser | ✅ |
|
|
754
|
+
| `readRecordOnServer` | A record screen filled in the server's HTML: `useRecord` starts from it, nothing asked on mount | ✅ |
|
|
661
755
|
| `./telemetry`, `./errors` | Optional: the application's traces to its OTLP endpoint, its errors to GlitchTip | ✅ |
|
|
662
756
|
| `./telemetry/server` | Optional: a rendering server's use cases on its OTLP provider, their trace continued by the API | ✅ |
|
|
757
|
+
| `./errors/server` | Optional: what fails on a rendering server to GlitchTip, with the visitor's digest and trace | ✅ |
|
|
663
758
|
| `RouteError` | A page that broke: plain words, the server's code, retry and home; reported once | ✅ |
|
|
664
759
|
| `selection` | Filters named as criteria 2.0 and the backend read them, the search bar's included | ✅ |
|
|
665
760
|
| `useListing`, `EntityList` | N records: criteria, search bar, builder, paging | ✅ |
|
package/dist/core/index.d.ts
CHANGED
|
@@ -23,6 +23,6 @@ export { detailOf, measuresOf, sumsOf, type Total, totalOf, } from "./measure.js
|
|
|
23
23
|
export { BOOKKEEPING, currencyNow, type DateStyle, dateStyleNow, dayTextOf, defaultFields, fieldAt, fieldMetaOf, type FieldView, installCurrency, installDateStyle, instantTextOf, moneyTextOf, numberTextOf, shortTextOf, sortKeyOf, STAMPS, textOf, valueAt, viewOf, } from "./present.js";
|
|
24
24
|
export { type Related, relatedOf } from "./related.js";
|
|
25
25
|
export { backReference, entityFor, relatedScope, type RelationField, relationFieldOf, } from "./relation.js";
|
|
26
|
-
export { readOnServer, type ServerRead } from "./server.js";
|
|
26
|
+
export { readOnServer, readRecordOnServer, type RecordRead, type ServerRead, } from "./server.js";
|
|
27
27
|
export { type RecordSummary, summaryFieldsOf, summaryOf, } from "./summary.js";
|
|
28
28
|
export { placeOf, stepIn, type Trail } from "./trail.js";
|
package/dist/core/index.js
CHANGED
|
@@ -23,6 +23,6 @@ export { detailOf, measuresOf, sumsOf, totalOf, } from "./measure.js";
|
|
|
23
23
|
export { BOOKKEEPING, currencyNow, dateStyleNow, dayTextOf, defaultFields, fieldAt, fieldMetaOf, installCurrency, installDateStyle, instantTextOf, moneyTextOf, numberTextOf, shortTextOf, sortKeyOf, STAMPS, textOf, valueAt, viewOf, } from "./present.js";
|
|
24
24
|
export { relatedOf } from "./related.js";
|
|
25
25
|
export { backReference, entityFor, relatedScope, relationFieldOf, } from "./relation.js";
|
|
26
|
-
export { readOnServer } from "./server.js";
|
|
26
|
+
export { readOnServer, readRecordOnServer, } from "./server.js";
|
|
27
27
|
export { summaryFieldsOf, summaryOf, } from "./summary.js";
|
|
28
28
|
export { placeOf, stepIn } from "./trail.js";
|
package/dist/core/list.d.ts
CHANGED
|
@@ -56,7 +56,7 @@ export type ListDefinition<T extends AnyRecord = AnyRecord> = {
|
|
|
56
56
|
* Excel». Without it, the ones the entity's search template names, else its display. */
|
|
57
57
|
facets?: readonly string[];
|
|
58
58
|
/** The faces it offers, the first one shown. */
|
|
59
|
-
views?: readonly ("table" | "rows" | "board" | "sheet" | "pivot")[];
|
|
59
|
+
views?: readonly ("table" | "rows" | "board" | "tiles" | "sheet" | "pivot")[];
|
|
60
60
|
/** Grouped from the start — the server counts the groups. */
|
|
61
61
|
groupBy?: readonly GroupLevel[];
|
|
62
62
|
/** The fields «Group by» offers, in this order — among those the model lets split the set. */
|
package/dist/core/server.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { type Draft, type Entity, type ResourceRef, type Transport } from "@sincpro/client-framework";
|
|
2
|
+
import type { AnyRecord, Meta } from "@sincpro/criteria";
|
|
1
3
|
/**
|
|
2
4
|
* A use case's answer read on the server that renders a page, before it is drawn — the very use
|
|
3
5
|
* case the page reads with in the browser (`useServiceReading`), so the page arrives with its
|
|
@@ -42,3 +44,56 @@ export declare function readOnServer<A extends unknown[], R>(useCase: (...args:
|
|
|
42
44
|
* type Props = { initial?: ServerRead<typeof project.Accounts.balance> };
|
|
43
45
|
*/
|
|
44
46
|
export type ServerRead<U extends (...args: never[]) => unknown> = Awaited<ReturnType<U>> | undefined;
|
|
47
|
+
/**
|
|
48
|
+
* One record of an entity and its model, as the server that renders a page read it: what
|
|
49
|
+
* `useRecord(entity, opened, { initial })` starts from — `readRecordOnServer`'s answer.
|
|
50
|
+
*
|
|
51
|
+
* Context: plain JSON, as everything that crosses from the server to the browser. It says what
|
|
52
|
+
* it was read FOR — the entity's key and the record's id — because a screen must never show it
|
|
53
|
+
* under another: `useRecord` takes it only when it opens that same record.
|
|
54
|
+
*/
|
|
55
|
+
export type RecordRead<T> = {
|
|
56
|
+
/** The entity's key (`entity.key`) it was read for. */
|
|
57
|
+
entity: string;
|
|
58
|
+
/** The record's key it was read for — `""` for a record that does not exist yet. */
|
|
59
|
+
id: string;
|
|
60
|
+
/** The record as the server answered it — `null` for a new one: the screen starts it from
|
|
61
|
+
* its model's defaults (and what its reference carries), as in the browser. */
|
|
62
|
+
record: T | null;
|
|
63
|
+
/** Its model's definition, when the server published one. */
|
|
64
|
+
meta: Meta | null;
|
|
65
|
+
/** What the caller kept of an edit between requests (sincpro_framework's `Drafts`). */
|
|
66
|
+
draft: Draft | null;
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* One record and its model, read on the server that renders the page — the same question
|
|
70
|
+
* `useRecord` asks in the browser, through the same transport the `FrameworkProvider` is given —,
|
|
71
|
+
* so its screen arrives FILLED in the HTML, not as a skeleton, and the browser does not ask
|
|
72
|
+
* again on mount.
|
|
73
|
+
*
|
|
74
|
+
* // a route's loader or a Server Component — plain logic, no screen loaded
|
|
75
|
+
* const invoice = await readRecordOnServer(transport, invoices, params.id);
|
|
76
|
+
* return <InvoicePage id={params.id} initial={invoice} />;
|
|
77
|
+
*
|
|
78
|
+
* // the page, rendered there and hydrated in the browser
|
|
79
|
+
* const invoice = useRecord(invoices, id, { initial });
|
|
80
|
+
*
|
|
81
|
+
* `opened` is what the screen opens: a record's id, `""` for a new one — only its model is read,
|
|
82
|
+
* as `useRecord` reads it (the list, one row) — or the reference the screen receives.
|
|
83
|
+
*
|
|
84
|
+
* Context: it asks through the screens' use case (`screensTransport`), as `useRecord` and
|
|
85
|
+
* `readListing` do — traced like the browser's reads, with the process's context — and answers
|
|
86
|
+
* plain JSON (`RecordRead`): a Client Component is handed data, never instances. A read that
|
|
87
|
+
* fails never refuses the page, as `readOnServer`: it answers `undefined` and one WARN line on
|
|
88
|
+
* the process's log — `what` (the entity's key when not given), the failure's type and kind, and
|
|
89
|
+
* never a refusal's message (a 404, a permission) —, the page is drawn without it and the
|
|
90
|
+
* browser reads the record on mount, as a single-page application does. A read the caller
|
|
91
|
+
* aborted (`signal`: nobody waits for the page any more) answers `undefined` without a line.
|
|
92
|
+
*
|
|
93
|
+
* Plain logic, without React or the DOM (`core/`): a route's loader, a Server Component and a
|
|
94
|
+
* script import it from `@sincpro/web-framework/core` without loading any screen.
|
|
95
|
+
*/
|
|
96
|
+
export declare function readRecordOnServer<T extends AnyRecord>(transport: Transport, entity: Entity<T>, opened: string | ResourceRef, options?: {
|
|
97
|
+
what?: string;
|
|
98
|
+
signal?: AbortSignal;
|
|
99
|
+
}): Promise<RecordRead<T> | undefined>;
|
package/dist/core/server.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { describeFailure, logger, outcomeOf } from "@sincpro/client-framework";
|
|
1
|
+
import { describeFailure, logger, outcomeOf, screensTransport, } from "@sincpro/client-framework";
|
|
2
2
|
/**
|
|
3
3
|
* A use case's answer read on the server that renders a page, before it is drawn — the very use
|
|
4
4
|
* case the page reads with in the browser (`useServiceReading`), so the page arrives with its
|
|
@@ -42,13 +42,80 @@ export async function readOnServer(useCase, args, options = {}) {
|
|
|
42
42
|
return JSON.parse(JSON.stringify(answer));
|
|
43
43
|
}
|
|
44
44
|
catch (error) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
45
|
+
notRead(options.what ?? (useCase.name || "a use case"), error);
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/** The one line a read on the server that failed leaves on the process's log (WARN: it always
|
|
50
|
+
* speaks): what was not read, and the failure's type and kind — a refusal never its message. */
|
|
51
|
+
function notRead(what, error) {
|
|
52
|
+
const failure = describeFailure(error, outcomeOf(error));
|
|
53
|
+
logger.warn("read on the server failed: the page is drawn without it", {
|
|
54
|
+
what,
|
|
55
|
+
"error.type": failure.type,
|
|
56
|
+
"error.kind": failure.kind,
|
|
57
|
+
...(failure.message === undefined ? {} : { "error.message": failure.message }),
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* One record and its model, read on the server that renders the page — the same question
|
|
62
|
+
* `useRecord` asks in the browser, through the same transport the `FrameworkProvider` is given —,
|
|
63
|
+
* so its screen arrives FILLED in the HTML, not as a skeleton, and the browser does not ask
|
|
64
|
+
* again on mount.
|
|
65
|
+
*
|
|
66
|
+
* // a route's loader or a Server Component — plain logic, no screen loaded
|
|
67
|
+
* const invoice = await readRecordOnServer(transport, invoices, params.id);
|
|
68
|
+
* return <InvoicePage id={params.id} initial={invoice} />;
|
|
69
|
+
*
|
|
70
|
+
* // the page, rendered there and hydrated in the browser
|
|
71
|
+
* const invoice = useRecord(invoices, id, { initial });
|
|
72
|
+
*
|
|
73
|
+
* `opened` is what the screen opens: a record's id, `""` for a new one — only its model is read,
|
|
74
|
+
* as `useRecord` reads it (the list, one row) — or the reference the screen receives.
|
|
75
|
+
*
|
|
76
|
+
* Context: it asks through the screens' use case (`screensTransport`), as `useRecord` and
|
|
77
|
+
* `readListing` do — traced like the browser's reads, with the process's context — and answers
|
|
78
|
+
* plain JSON (`RecordRead`): a Client Component is handed data, never instances. A read that
|
|
79
|
+
* fails never refuses the page, as `readOnServer`: it answers `undefined` and one WARN line on
|
|
80
|
+
* the process's log — `what` (the entity's key when not given), the failure's type and kind, and
|
|
81
|
+
* never a refusal's message (a 404, a permission) —, the page is drawn without it and the
|
|
82
|
+
* browser reads the record on mount, as a single-page application does. A read the caller
|
|
83
|
+
* aborted (`signal`: nobody waits for the page any more) answers `undefined` without a line.
|
|
84
|
+
*
|
|
85
|
+
* Plain logic, without React or the DOM (`core/`): a route's loader, a Server Component and a
|
|
86
|
+
* script import it from `@sincpro/web-framework/core` without loading any screen.
|
|
87
|
+
*/
|
|
88
|
+
export async function readRecordOnServer(transport, entity, opened, options = {}) {
|
|
89
|
+
const id = typeof opened === "string" ? opened : (opened.id ?? "");
|
|
90
|
+
const screens = screensTransport(transport);
|
|
91
|
+
try {
|
|
92
|
+
if (id === "") {
|
|
93
|
+
// Nothing to read but the model: asked of the list with no rows, as `useRecord` asks it.
|
|
94
|
+
const page = await screens.list(entity.list, { pagination: { limit: 1 } }, options.signal);
|
|
95
|
+
return plain({
|
|
96
|
+
entity: entity.key,
|
|
97
|
+
id,
|
|
98
|
+
record: null,
|
|
99
|
+
meta: page.meta ?? null,
|
|
100
|
+
draft: null,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
const answer = await screens.one(entity.one(id), options.signal);
|
|
104
|
+
return plain({
|
|
105
|
+
entity: entity.key,
|
|
106
|
+
id,
|
|
107
|
+
record: answer.record,
|
|
108
|
+
meta: answer.meta ?? null,
|
|
109
|
+
draft: answer.draft ?? null,
|
|
51
110
|
});
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
if (options.signal?.aborted !== true)
|
|
114
|
+
notRead(options.what ?? entity.key, error);
|
|
52
115
|
return undefined;
|
|
53
116
|
}
|
|
54
117
|
}
|
|
118
|
+
/** Written and read back as JSON: what a server hands a page crosses that way. */
|
|
119
|
+
function plain(read) {
|
|
120
|
+
return JSON.parse(JSON.stringify(read));
|
|
121
|
+
}
|
package/dist/errors/index.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import type { ErrorReporter } from "@sincpro/client-framework";
|
|
2
|
+
import { type SentEvent } from "./narrowed.js";
|
|
3
|
+
export type { SentEvent };
|
|
4
|
+
export { reportUncaught } from "./uncaught.js";
|
|
2
5
|
/**
|
|
3
6
|
* An `ErrorReporter` for the browser, built from the application's own settings: a
|
|
4
7
|
* Sentry-compatible client — GlitchTip, where the backend reports too — with the application's
|
|
@@ -20,7 +23,8 @@ import type { ErrorReporter } from "@sincpro/client-framework";
|
|
|
20
23
|
*
|
|
21
24
|
* What it sends is narrow by default, because in an ERP a request body is an invoice and a
|
|
22
25
|
* customer: no breadcrumbs or default integrations, and every event leaves without cookies,
|
|
23
|
-
* headers, request bodies, query strings or the user's address
|
|
26
|
+
* headers, request bodies, query strings or the user's address — of the person, only their id,
|
|
27
|
+
* what GlitchTip counts «users affected» by. `beforeSend` is the
|
|
24
28
|
* application's, run after that.
|
|
25
29
|
*/
|
|
26
30
|
export type GlitchtipOptions = {
|
|
@@ -34,16 +38,4 @@ export type GlitchtipOptions = {
|
|
|
34
38
|
/** The application's last word on an event: change it, or answer `null` to drop it. */
|
|
35
39
|
beforeSend?: (event: SentEvent) => SentEvent | null;
|
|
36
40
|
};
|
|
37
|
-
/** The parts of an event this reporter narrows; the rest passes as the SDK built it. */
|
|
38
|
-
export type SentEvent = {
|
|
39
|
-
request?: {
|
|
40
|
-
url?: string;
|
|
41
|
-
cookies?: unknown;
|
|
42
|
-
headers?: unknown;
|
|
43
|
-
data?: unknown;
|
|
44
|
-
query_string?: unknown;
|
|
45
|
-
};
|
|
46
|
-
user?: unknown;
|
|
47
|
-
[key: string]: unknown;
|
|
48
|
-
};
|
|
49
41
|
export declare function glitchtipReporter(options: GlitchtipOptions): ErrorReporter;
|
package/dist/errors/index.js
CHANGED
|
@@ -1,15 +1,6 @@
|
|
|
1
|
+
import { narrowed } from "./narrowed.js";
|
|
2
|
+
export { reportUncaught } from "./uncaught.js";
|
|
1
3
|
const WAITING_AT_MOST = 20;
|
|
2
|
-
/** Every event leaves without what may carry a person's or a customer's data. */
|
|
3
|
-
function narrowed(event) {
|
|
4
|
-
const { user: _user, ...rest } = event;
|
|
5
|
-
if (rest.request === undefined)
|
|
6
|
-
return rest;
|
|
7
|
-
const { cookies: _c, headers: _h, data: _d, query_string: _q, ...request } = rest.request;
|
|
8
|
-
return {
|
|
9
|
-
...rest,
|
|
10
|
-
request: { ...request, ...(request.url ? { url: request.url.split("?")[0] } : {}) },
|
|
11
|
-
};
|
|
12
|
-
}
|
|
13
4
|
export function glitchtipReporter(options) {
|
|
14
5
|
let ready = null;
|
|
15
6
|
let failed = false;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What every reported event leaves without — the browser's (`./errors`) and the server's
|
|
3
|
+
* (`./errors/server`) alike: in an ERP a request body is an invoice and a customer.
|
|
4
|
+
*/
|
|
5
|
+
/** The parts of an event the reporters narrow; the rest passes as the SDK built it. */
|
|
6
|
+
export type SentEvent = {
|
|
7
|
+
request?: {
|
|
8
|
+
url?: string;
|
|
9
|
+
cookies?: unknown;
|
|
10
|
+
headers?: unknown;
|
|
11
|
+
data?: unknown;
|
|
12
|
+
query_string?: unknown;
|
|
13
|
+
};
|
|
14
|
+
user?: unknown;
|
|
15
|
+
[key: string]: unknown;
|
|
16
|
+
};
|
|
17
|
+
/** Every event leaves without what may carry a person's or a customer's data. */
|
|
18
|
+
export declare function narrowed(event: SentEvent): SentEvent;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What every reported event leaves without — the browser's (`./errors`) and the server's
|
|
3
|
+
* (`./errors/server`) alike: in an ERP a request body is an invoice and a customer.
|
|
4
|
+
*/
|
|
5
|
+
/** The person's id — what GlitchTip counts «users affected» by, and what already travels as the
|
|
6
|
+
* `user_id` tag —; nothing else of the person (email, name, IP…). */
|
|
7
|
+
function idOf(user) {
|
|
8
|
+
if (typeof user !== "object" || user === null || !("id" in user))
|
|
9
|
+
return undefined;
|
|
10
|
+
const id = user.id;
|
|
11
|
+
return typeof id === "string" || typeof id === "number" ? { id: String(id) } : undefined;
|
|
12
|
+
}
|
|
13
|
+
/** Every event leaves without what may carry a person's or a customer's data. */
|
|
14
|
+
export function narrowed(event) {
|
|
15
|
+
const { user, ...others } = event;
|
|
16
|
+
const person = idOf(user);
|
|
17
|
+
const rest = person === undefined ? others : { ...others, user: person };
|
|
18
|
+
if (rest.request === undefined)
|
|
19
|
+
return rest;
|
|
20
|
+
const { cookies: _c, headers: _h, data: _d, query_string: _q, ...request } = rest.request;
|
|
21
|
+
return {
|
|
22
|
+
...rest,
|
|
23
|
+
request: { ...request, ...(request.url ? { url: request.url.split("?")[0] } : {}) },
|
|
24
|
+
};
|
|
25
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { type ErrorReporter } from "@sincpro/client-framework";
|
|
2
|
+
import { type SentEvent } from "./narrowed.js";
|
|
3
|
+
/**
|
|
4
|
+
* The server's side of `./errors`: what a server that renders pages — Next.js, TanStack Start,
|
|
5
|
+
* Remix, a Node process of one's own — needs so what fails there reaches GlitchTip like what
|
|
6
|
+
* fails in the browser and in the backend.
|
|
7
|
+
*
|
|
8
|
+
* // once per process — Next's instrumentation.ts (no Next here: the application wires it)
|
|
9
|
+
* export async function register() {
|
|
10
|
+
* if (process.env.NEXT_RUNTIME !== "nodejs") return;
|
|
11
|
+
* const { registerServerErrors } = await import("./server.js");
|
|
12
|
+
* await registerServerErrors({ dsn: process.env.SENTRY_DSN, release, environment });
|
|
13
|
+
* }
|
|
14
|
+
*
|
|
15
|
+
* // a request that failed on the server: the page that broke while drawing
|
|
16
|
+
* export async function onRequestError(error, request, context) {
|
|
17
|
+
* const { reportRequestError } = await import("./server.js");
|
|
18
|
+
* reportRequestError(error, { method: request.method, path: request.path, route: context.routePath });
|
|
19
|
+
* }
|
|
20
|
+
*
|
|
21
|
+
* // once per realm, where the server runs use cases
|
|
22
|
+
* configureServerProcess({ identity: { release }, reporter: serverReporter() });
|
|
23
|
+
*
|
|
24
|
+
* Context: one client per PROCESS, kept on `globalThis`, because a server that renders loads
|
|
25
|
+
* this package once per realm (Next: instrumentation, Server Components, the server render of
|
|
26
|
+
* Client Components) and every realm reports through the one `register` built. The client is
|
|
27
|
+
* `@sentry/core`'s server client — not `@sentry/node`, which sets up its own OpenTelemetry,
|
|
28
|
+
* instrumentation and bundler plugins and would clash with `./telemetry/server`. Nothing global
|
|
29
|
+
* besides it: no `Sentry.init`, no handlers on `process`; only what the process decides is
|
|
30
|
+
* unexpected, and the requests the application hands over, reach it.
|
|
31
|
+
*
|
|
32
|
+
* A request's report carries the `digest` — the code the visitor's `RouteError` shows and the
|
|
33
|
+
* browser's own report carries —, the route and the active span's trace: the three reports of
|
|
34
|
+
* one failure (browser, server, trace) find each other. A use case that failed and also broke
|
|
35
|
+
* the page is two events of one issue — the use case's and the request's —, in one trace.
|
|
36
|
+
*
|
|
37
|
+
* Optional on every count, like `./errors`: an entry of its own that nothing else imports,
|
|
38
|
+
* `@sentry/core` an optional peer loaded with `import()`, and nothing throws: without a DSN
|
|
39
|
+
* nothing loads; if the SDK does not load, that is said once and reports are dropped. What it
|
|
40
|
+
* sends is narrowed as the browser's: no cookies, headers, bodies, query strings, and of the
|
|
41
|
+
* person only their id.
|
|
42
|
+
*/
|
|
43
|
+
export type ServerErrorsOptions = {
|
|
44
|
+
/** The application's DSN, from its settings. None — an empty setting — and reporting is off. */
|
|
45
|
+
dsn: string | undefined;
|
|
46
|
+
/** `<name>:<version>` as deployed, the same text as `configureProcess({ identity })`. */
|
|
47
|
+
release?: string;
|
|
48
|
+
/** `production`, `staging`… */
|
|
49
|
+
environment?: string;
|
|
50
|
+
/** Fraction of errors sent, 0 to 1. Default 1. */
|
|
51
|
+
sampling?: number;
|
|
52
|
+
/** The application's last word on an event: change it, or answer `null` to drop it. */
|
|
53
|
+
beforeSend?: (event: SentEvent) => SentEvent | null;
|
|
54
|
+
};
|
|
55
|
+
/** The process's reporting, once registered. */
|
|
56
|
+
export type ServerErrors = {
|
|
57
|
+
/** Sends what is queued now: a script that ends, a test. */
|
|
58
|
+
flush: (timeoutMs?: number) => Promise<boolean>;
|
|
59
|
+
/** Sends what is queued and stops: a server shutting down. */
|
|
60
|
+
close: (timeoutMs?: number) => Promise<boolean>;
|
|
61
|
+
};
|
|
62
|
+
/** What a failed request was, for its report — never its headers, cookies, body or query. */
|
|
63
|
+
export type FailedRequest = {
|
|
64
|
+
/** The code the SSR framework gave the failure (Next's `digest`); read from the error if absent. */
|
|
65
|
+
digest?: string;
|
|
66
|
+
method?: string;
|
|
67
|
+
/** The path asked for; its query string is dropped. */
|
|
68
|
+
path?: string;
|
|
69
|
+
/** The route that drew it (`/cursos/[id]`). */
|
|
70
|
+
route?: string;
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* The process's error reporting: a Sentry-compatible client — GlitchTip — with the application's
|
|
74
|
+
* DSN, kept for every realm. Once per process: a second call, from any realm, answers the
|
|
75
|
+
* first's. It answers `undefined` when reporting stays off — no DSN, or the SDK did not load.
|
|
76
|
+
*/
|
|
77
|
+
export declare function registerServerErrors(options: ServerErrorsOptions): Promise<ServerErrors | undefined>;
|
|
78
|
+
/**
|
|
79
|
+
* An `ErrorReporter` over the process's client — what `configureServerProcess({ reporter })`
|
|
80
|
+
* plugs, so a use case that fails unexpectedly on the server is reported with its trace, as
|
|
81
|
+
* the browser's are. Before `registerServerErrors` (or without a DSN) it reports nothing.
|
|
82
|
+
*/
|
|
83
|
+
export declare function serverReporter(): ErrorReporter;
|
|
84
|
+
/**
|
|
85
|
+
* A request that failed on the server — a page that broke while drawing —, reported with what
|
|
86
|
+
* finds it again: its `digest`, its route, its method and path (without the query), and
|
|
87
|
+
* the trace it failed in. What the server already explained (a refusal, a cancellation) is not
|
|
88
|
+
* a fault and is not reported. One error handed over twice (two phases of one render) is one
|
|
89
|
+
* event: the client marks what it captured. Before `registerServerErrors` (or without a DSN) it
|
|
90
|
+
* does nothing.
|
|
91
|
+
*/
|
|
92
|
+
export declare function reportRequestError(error: unknown, request?: FailedRequest): void;
|