@sincpro/web-framework 0.4.2 → 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 +82 -41
- package/dist/codegen/cli.d.ts +2 -0
- package/dist/codegen/cli.js +82 -0
- package/dist/codegen/client.d.ts +14 -0
- package/dist/codegen/client.js +178 -0
- package/dist/codegen/domain.d.ts +14 -0
- package/dist/codegen/domain.js +195 -0
- package/dist/codegen/entities.d.ts +14 -0
- package/dist/codegen/entities.js +158 -0
- package/dist/codegen/index.d.ts +11 -0
- package/dist/codegen/index.js +9 -0
- package/dist/codegen/metamodel.d.ts +89 -0
- package/dist/codegen/metamodel.js +1 -0
- package/dist/codegen/names.d.ts +40 -0
- package/dist/codegen/names.js +114 -0
- package/dist/codegen/rest.d.ts +31 -0
- package/dist/codegen/rest.js +27 -0
- package/dist/core/cache.js +7 -0
- package/dist/core/contract.d.ts +34 -0
- package/dist/core/contract.js +41 -0
- package/dist/core/discovery.d.ts +47 -0
- package/dist/core/discovery.js +68 -0
- package/dist/core/edit.d.ts +35 -6
- package/dist/core/edit.js +57 -4
- package/dist/core/entity.d.ts +77 -9
- package/dist/core/entity.js +51 -6
- package/dist/core/index.d.ts +5 -4
- package/dist/core/index.js +5 -4
- package/dist/core/memory.d.ts +3 -0
- package/dist/core/memory.js +9 -0
- package/dist/core/meta.d.ts +7 -0
- package/dist/core/meta.js +1 -1
- package/dist/core/metas.d.ts +4 -5
- package/dist/core/relation.d.ts +11 -1
- package/dist/core/relation.js +30 -2
- package/dist/core/rest.d.ts +23 -0
- package/dist/core/rest.js +78 -3
- package/dist/core/schema.d.ts +5 -12
- package/dist/core/trail.js +1 -4
- package/dist/core/transport.d.ts +11 -0
- package/dist/core/transport.js +9 -0
- package/dist/react/index.d.ts +2 -1
- package/dist/react/index.js +1 -0
- package/dist/react/useCommand.d.ts +15 -5
- package/dist/react/useCommand.js +7 -4
- package/dist/react/useGroups.js +1 -2
- package/dist/react/useHistory.js +1 -1
- package/dist/react/useListing.d.ts +5 -4
- package/dist/react/useListing.js +9 -8
- package/dist/react/useLookup.js +2 -3
- package/dist/react/useNameOf.d.ts +13 -0
- package/dist/react/useNameOf.js +44 -0
- package/dist/react/usePivot.js +0 -1
- package/dist/react/useRecord.d.ts +2 -1
- package/dist/react/useRecord.js +15 -3
- package/dist/react/useRelated.js +2 -2
- package/dist/web/list/EntityList.d.ts +5 -4
- package/dist/web/list/EntityList.js +1 -1
- package/dist/web/list/EntitySearchBar.js +2 -2
- package/dist/web/list/presentation.d.ts +3 -3
- package/dist/web/list/presentation.js +1 -0
- package/dist/web/list/rowCommands.d.ts +16 -12
- package/dist/web/list/rowCommands.js +3 -2
- package/dist/web/list/search.js +1 -1
- package/dist/web/record/CommandButton.d.ts +9 -6
- package/dist/web/record/edit.js +19 -7
- package/dist/web/record/lines.js +23 -10
- package/dist/web/record/opening.js +1 -1
- package/dist/web/workspace/composer.js +1 -4
- package/package.json +18 -7
package/README.md
CHANGED
|
@@ -150,6 +150,12 @@ export const transport = restTransport({
|
|
|
150
150
|
});
|
|
151
151
|
```
|
|
152
152
|
|
|
153
|
+
The definition of every model — its fields, operators, hints, widgets, formulas — is read from
|
|
154
|
+
the service's discovery document (`/openapi.json` by default; `document:` names another, `null`
|
|
155
|
+
reads none), once: each answer says only its `model_version`, and another version reads the
|
|
156
|
+
document again, revalidated by its `ETag`. A form computes a published formula itself and asks the
|
|
157
|
+
server for a preview only when a change reaches something it cannot answer.
|
|
158
|
+
|
|
153
159
|
**3. The entities** — declared once: where each is read and written, and its key in the
|
|
154
160
|
workspace (`domain:kind`). `summary` only when its fields are not the conventional ones.
|
|
155
161
|
|
|
@@ -386,50 +392,85 @@ Three folders, one direction — `make doctor` keeps the arrows:
|
|
|
386
392
|
| `web/record` | one record: `RecordField`, `factsOf`, `RelatedList`, `EntityPicker`, `cardOf` | works outside a workspace (no composer) |
|
|
387
393
|
| `web/workspace` | the composer: `entityView`, `openIn`, `RelatedRecord`, `<SincproApp>` | where it opens, the application context |
|
|
388
394
|
|
|
389
|
-
Everything is imported from `@sincpro/web-framework`.
|
|
395
|
+
Everything is imported from `@sincpro/web-framework`. Code that draws nothing — a script, a
|
|
396
|
+
test in Node, a generated client — imports `@sincpro/web-framework/core`, which loads no React.
|
|
397
|
+
|
|
398
|
+
A fourth folder, `codegen`, is a build step: no screen loads it (`make doctor`).
|
|
399
|
+
|
|
400
|
+
### From the contract
|
|
401
|
+
|
|
402
|
+
A service built on sincpro_framework exports what it publishes — its models and every operation,
|
|
403
|
+
what each one is for its entity and how each wire reaches it — as one client contract per
|
|
404
|
+
bounded context. `sincpro-web generate` writes, from it, what the screens used to declare by
|
|
405
|
+
hand:
|
|
406
|
+
|
|
407
|
+
```bash
|
|
408
|
+
python -m sincpro_framework.entrypoints export-client-contract app.http:gateways contracts/
|
|
409
|
+
npx sincpro-web generate contracts/project.contract.json --out src/generated/project --base /v1
|
|
410
|
+
```
|
|
411
|
+
|
|
412
|
+
| File | What it holds |
|
|
413
|
+
| --------------- | --------------------------------------------------------------------------------------------------- |
|
|
414
|
+
| `domain.ts` | the DTOs — every model, every operation's input and answer — with the server's own docs. Types only |
|
|
415
|
+
| `client.ts` | every operation as data, and `clientOf(transport)`: a typed method per page, record and write |
|
|
416
|
+
| `entities.ts` | an `entity<T, C>()` per model a screen lists: its routes and Commands, read off what each one is |
|
|
417
|
+
| `contract.json` | the snapshot they came from: `metasOf(contract, "project")` preloads the definitions |
|
|
418
|
+
|
|
419
|
+
What is generated is never edited; a screen extends it (`presented(issues, { row })`, a type
|
|
420
|
+
with `&`). Keep the folder out of prettier and eslint, and let CI regenerate and compare it.
|
|
421
|
+
At runtime nothing is closed: the transport keeps reading the live definitions. How an
|
|
422
|
+
application adopts it — where each file goes, what is written by hand, how a hand-written screen
|
|
423
|
+
moves to the generated one — is [`docs/ADOPCION.md`](docs/ADOPCION.md) (in Spanish).
|
|
390
424
|
|
|
391
425
|
## Where it stands
|
|
392
426
|
|
|
393
|
-
| Piece | What it solves
|
|
394
|
-
| --------------------------------------- |
|
|
395
|
-
| `restTransport`, `memoryTransport` | The backend, or the same engine in memory
|
|
396
|
-
| `defineMeta`, `metaFromSchema` | A model by hand, or completed from OpenAPI
|
|
397
|
-
| `useListing`, `EntityList` | N records: criteria, search bar, builder, paging
|
|
398
|
-
| `useRecord`, `factsOf`, `RecordLines` | One record: facts, relations with their real total
|
|
399
|
-
| Preloaded definitions | `FrameworkProvider metas`: the
|
|
400
|
-
| `useLookup`, `EntityPicker` | Literal search: a many2one select, ⌘K
|
|
401
|
-
| `useRecord` + `RecordField`, `dirtyOf` | Editing: hints evaluated live, preview, save with version (409), server drafts
|
|
402
|
-
| `useCommand`, `CommandButton` | An entity's actions, with a dry run before what destroys
|
|
403
|
-
| `
|
|
404
|
-
| `
|
|
405
|
-
|
|
|
406
|
-
| `
|
|
407
|
-
|
|
|
408
|
-
| `
|
|
409
|
-
| `
|
|
410
|
-
| `
|
|
411
|
-
| `
|
|
412
|
-
|
|
|
413
|
-
| `
|
|
414
|
-
|
|
|
415
|
-
| `
|
|
416
|
-
| `
|
|
417
|
-
| `
|
|
418
|
-
| `
|
|
419
|
-
| `
|
|
420
|
-
| `
|
|
421
|
-
| `
|
|
422
|
-
| `
|
|
423
|
-
| `
|
|
424
|
-
| `
|
|
425
|
-
| `
|
|
426
|
-
| `
|
|
427
|
-
|
|
|
428
|
-
| `
|
|
429
|
-
| `
|
|
430
|
-
| `
|
|
431
|
-
| `
|
|
432
|
-
| `
|
|
427
|
+
| Piece | What it solves | Status |
|
|
428
|
+
| --------------------------------------- | -------------------------------------------------------------------------------------------- | ------ |
|
|
429
|
+
| `restTransport`, `memoryTransport` | The backend, or the same engine in memory | ✅ |
|
|
430
|
+
| `defineMeta`, `metaFromSchema` | A model by hand, or completed from OpenAPI | ✅ |
|
|
431
|
+
| `useListing`, `EntityList` | N records: criteria, search bar, builder, paging | ✅ |
|
|
432
|
+
| `useRecord`, `factsOf`, `RecordLines` | One record: facts, relations with their real total | ✅ |
|
|
433
|
+
| Preloaded definitions | `FrameworkProvider metas`: the models of the discovery document, or of an exported metamodel | ✅ |
|
|
434
|
+
| `useLookup`, `EntityPicker` | Literal search: a many2one select, ⌘K | ✅ |
|
|
435
|
+
| `useRecord` + `RecordField`, `dirtyOf` | Editing: hints evaluated live, preview, save with version (409), server drafts | ✅ |
|
|
436
|
+
| `useCommand`, `CommandButton` | An entity's actions, with a dry run before what destroys | ✅ |
|
|
437
|
+
| `entity<T, C>` (`CommandBodies`) | Typed Commands: a name the entity does not declare, or a wrong body, does not compile | ✅ |
|
|
438
|
+
| `entity<T, C, A>` (`CommandAnswers`) | What each Command answers, typed: `run(…)` returns it with no type argument written | ✅ |
|
|
439
|
+
| `readRoute`, `Transport.read` | A GET of the context's own (`/me`), neither a page nor a record | ✅ |
|
|
440
|
+
| A key alone (`EntityID`), `useNameOf` | A key that names whose record it is, drawn as a many2one: a picker, and the name when read | ✅ |
|
|
441
|
+
| `versionsOf`, `onVersionDrift` | A snapshot behind the server is told once per model — never an error | ✅ |
|
|
442
|
+
| `sincpro-web generate` | A context's DTOs, client and entities out of its exported client contract | ✅ |
|
|
443
|
+
| `metasOf`, `bodyFor` (`target`) | Definitions preloaded from that snapshot; a Command's record written where its body names it | ✅ |
|
|
444
|
+
| `useHistory` | Business events in `Record.Activity` (comments: backend first) | ✅ |
|
|
445
|
+
| `entityView` | An entity as a composer view: list, record, ⌘K search | ✅ |
|
|
446
|
+
| `<SincproApp>` | The application context in one place, the workspace dressed as web-ui (`webUiSkin`) | ✅ |
|
|
447
|
+
| `useRecordInWorkspace` | A record tells the workspace its real name and its unsaved work (composer 1.0) | ✅ |
|
|
448
|
+
| Paging by pages, ceiling on «more» | One page on screen, walked by the server's cursor: a million rows never freeze it | ✅ |
|
|
449
|
+
| `cachedTransport` | Repeated reads from memory, forgotten when a write touches the entity (on in `SincproApp`) | ✅ |
|
|
450
|
+
| `EntitySearchBar` | The search bar + criteria builder as a loose piece, for any `List` | ✅ |
|
|
451
|
+
| `EntityList` views | The same set as table, rows or board | ✅ |
|
|
452
|
+
| `useRelated`, `RelatedList` | A record's relation as a live list: searched and paged by the server, inside a form | ✅ |
|
|
453
|
+
| `EntityPicker` chooser | «see all…» opens the whole set, searched on the server: a many2one over millions | ✅ |
|
|
454
|
+
| `useRecord(entity, "")`, `create` | A new record: the model's defaults, saved to `create` (or `edit`), then it IS that record | ✅ |
|
|
455
|
+
| `retryKeys`, `Idempotency-Key` | A Command or a create retried after a lost answer runs once, not twice | ✅ |
|
|
456
|
+
| `entity.newRef()`, «New» in the list | The record's form with no id, in a page or a window; replaced by the record once created | ✅ |
|
|
457
|
+
| `EntityPicker criteria` | A many2one narrowed by another field: the invoices OF the chosen customer | ✅ |
|
|
458
|
+
| `chooserIn`, `entity.chooserRef()` | «see all…» as the composer's chooser: a row answers (`api.pick`) | ✅ |
|
|
459
|
+
| `EntityPicker` / `EntityTags` in a view | Open and choose in the workspace by themselves; the chooser carries the picker's cut | ✅ |
|
|
460
|
+
| `newEntityId`, `edit` on a new record | A new record born under a UUID v7 minted here: its draft and its save go through `Edit` | ✅ |
|
|
461
|
+
| `useGroups`, «Group by» | Groups counted by the server over the whole set; a group's rows read when it is opened | ✅ |
|
|
462
|
+
| `defineList`, `listView` | A named list (a fixed scope, its columns and filters) as data, and as a workspace view | ✅ |
|
|
463
|
+
| `EntityPicker` `browse`, `columns` | The chooser always a click away, as a table searched literally by the server | ✅ |
|
|
464
|
+
| `useFollowed` | A detail panel follows the record the person is on, without knowing the list | ✅ |
|
|
465
|
+
| `commands`, `useRowCommands` | An entity's Commands on each row, idempotent, offered only where `when` holds | ✅ |
|
|
466
|
+
| `summaryOf`, `cardOf`, `relationOf` | Any record at any size: name, description, state, type, picture, who made and changed it | ✅ |
|
|
467
|
+
| `openIn`, `RelatedRecord` | A related record opens in a modal; «Abrir en…» for the rest; never a modal over a modal | ✅ |
|
|
468
|
+
| Literal search everywhere | Picker: the first ten, the server's search as you type; tables: «Buscar «X»» on Enter | ✅ |
|
|
469
|
+
| `EntityTags` | A many2many as chips over thousands: the server's search, faces, the chosen never again | ✅ |
|
|
470
|
+
| `RecordField` + `entities` | A many2one / one2many drawn from the model alone, opened where the view says | ✅ |
|
|
471
|
+
| `EntityRecord`, `formOf` | Any entity's record screen without writing it | ✅ |
|
|
472
|
+
| `Trail`, `Record.Pager` | Previous / next from a record, across the server's pages | ✅ |
|
|
473
|
+
| `EntityList sums` | Each group says how much it adds up, summed by the server over all of it | ✅ |
|
|
433
474
|
|
|
434
475
|
What it does **not** do: mobile (`sincpro_mobile` has its own core), sessions and login (the
|
|
435
476
|
application's), routing (composer's), string-named widget registries, technical logs on a
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { clientFileOf } from "./client.js";
|
|
5
|
+
import { domainFileOf } from "./domain.js";
|
|
6
|
+
import { entitiesFileOf } from "./entities.js";
|
|
7
|
+
import { aliasOf } from "./names.js";
|
|
8
|
+
/**
|
|
9
|
+
* `sincpro-web generate <contract.json> --out <dir> [--domain <name>] [--base <path>]` — what an
|
|
10
|
+
* application's `make client` runs, once per bounded context, over the client contract
|
|
11
|
+
* sincpro_framework exported (`export-client-contract`):
|
|
12
|
+
*
|
|
13
|
+
* - `domain.ts`: the DTOs, only types;
|
|
14
|
+
* - `client.ts`: every operation as data, and `clientOf(transport)`;
|
|
15
|
+
* - `entities.ts`: an `entity<T, C>()` per model a screen can list;
|
|
16
|
+
* - `contract.json`: the snapshot they were written from, for `metasOf`.
|
|
17
|
+
*
|
|
18
|
+
* A bare metamodel (`export_metamodel`, no bindings) writes `domain.ts` and `contract.json` only.
|
|
19
|
+
*
|
|
20
|
+
* Context: it writes only into `--out`, and the same bytes for the same contract, so `make
|
|
21
|
+
* verify-client` regenerates and fails on any difference. The folder is the generator's: keep
|
|
22
|
+
* it out of the application's prettier and eslint, or a formatter rewrites what was generated.
|
|
23
|
+
*/
|
|
24
|
+
const USAGE = "usage: sincpro-web generate <contract.json> --out <dir> [--domain <name>] [--base <path>]";
|
|
25
|
+
/** JSON with every key sorted: the snapshot reads the same however the server ordered it. */
|
|
26
|
+
function canonical(value) {
|
|
27
|
+
if (Array.isArray(value))
|
|
28
|
+
return value.map(canonical);
|
|
29
|
+
if (value === null || typeof value !== "object")
|
|
30
|
+
return value;
|
|
31
|
+
return Object.fromEntries(Object.keys(value)
|
|
32
|
+
.sort()
|
|
33
|
+
.map((key) => [key, canonical(value[key])]));
|
|
34
|
+
}
|
|
35
|
+
/** The command, its source and its flags — or `undefined` when they do not read as usage says. */
|
|
36
|
+
function parsed(args) {
|
|
37
|
+
const [command, source, ...rest] = args;
|
|
38
|
+
if (command !== "generate" || source === undefined)
|
|
39
|
+
return undefined;
|
|
40
|
+
const flags = new Map();
|
|
41
|
+
for (let at = 0; at < rest.length; at += 2) {
|
|
42
|
+
const flag = rest[at];
|
|
43
|
+
const value = rest[at + 1];
|
|
44
|
+
if (flag === undefined || value === undefined || !flag.startsWith("--"))
|
|
45
|
+
return undefined;
|
|
46
|
+
flags.set(flag, value);
|
|
47
|
+
}
|
|
48
|
+
const out = flags.get("--out");
|
|
49
|
+
const known = new Set(["--out", "--domain", "--base"]);
|
|
50
|
+
if (out === undefined || [...flags.keys()].some((one) => !known.has(one)))
|
|
51
|
+
return undefined;
|
|
52
|
+
const domain = flags.get("--domain");
|
|
53
|
+
const base = flags.get("--base");
|
|
54
|
+
return {
|
|
55
|
+
source,
|
|
56
|
+
out,
|
|
57
|
+
options: {
|
|
58
|
+
...(domain === undefined ? {} : { domain }),
|
|
59
|
+
...(base === undefined ? {} : { base }),
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
function main(args) {
|
|
64
|
+
const asked = parsed(args);
|
|
65
|
+
if (asked === undefined) {
|
|
66
|
+
console.error(USAGE);
|
|
67
|
+
return 2;
|
|
68
|
+
}
|
|
69
|
+
const metamodel = JSON.parse(readFileSync(asked.source, "utf8"));
|
|
70
|
+
const files = { "domain.ts": domainFileOf(metamodel) };
|
|
71
|
+
if (metamodel.bindings !== undefined) {
|
|
72
|
+
files["client.ts"] = clientFileOf(metamodel, asked.options);
|
|
73
|
+
files["entities.ts"] = entitiesFileOf(metamodel, asked.options);
|
|
74
|
+
}
|
|
75
|
+
files["contract.json"] = `${JSON.stringify(canonical(metamodel), null, 2)}\n`;
|
|
76
|
+
mkdirSync(asked.out, { recursive: true });
|
|
77
|
+
for (const [name, source] of Object.entries(files))
|
|
78
|
+
writeFileSync(join(asked.out, name), source);
|
|
79
|
+
console.log(`✓ ${aliasOf(metamodel)} → ${asked.out} (${Object.keys(files).join(", ")})`);
|
|
80
|
+
return 0;
|
|
81
|
+
}
|
|
82
|
+
process.exitCode = main(process.argv.slice(2));
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Metamodel } from "./metamodel.js";
|
|
2
|
+
import { type GenerateOptions } from "./rest.js";
|
|
3
|
+
/**
|
|
4
|
+
* The TypeScript of the client of `metamodel`, as the text of one file next to its `domain.ts`.
|
|
5
|
+
*
|
|
6
|
+
* 1. Every operation, by its local name, as data: identity, role, `query`, entities, targets
|
|
7
|
+
* and the binding of each wire that publishes it.
|
|
8
|
+
* 2. Per operation REST reaches, how a transport calls it (`callOf`): a page, a record, a write
|
|
9
|
+
* — or none, said in the client's own doc.
|
|
10
|
+
* 3. The routes of the writes, read once (`writeRoute`).
|
|
11
|
+
* Final: `clientOf(transport)`, a method per operation reached, named by `methodOf`; two with
|
|
12
|
+
* one name are refused, naming both.
|
|
13
|
+
*/
|
|
14
|
+
export declare function clientFileOf(metamodel: Metamodel, options?: GenerateOptions): string;
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { aliasOf, claimed, deprecationOf, docOf, DOMAIN_MODULE, FRAMEWORK_CORE, headerOf, identifierOf, localOf, methodOf, recordLiteral, sorted, } from "./names.js";
|
|
2
|
+
import { ownerOf, restOf, WRITES, } from "./rest.js";
|
|
3
|
+
/**
|
|
4
|
+
* The client of a bounded context as TypeScript: every operation as data, and a method for each
|
|
5
|
+
* one REST reaches the way a `Transport` sends — over `@sincpro/web-framework/core`, so a script,
|
|
6
|
+
* a test or a hook of its own calls the service typed, with no screen.
|
|
7
|
+
*
|
|
8
|
+
* Context: a method only writes down the route the contract published and hands the call to the
|
|
9
|
+
* transport, which keeps the rules — the `Idempotency-Key`, the `Problem`, the model's version.
|
|
10
|
+
* A GET is a page (`list`), one record (`one`), or — a Query of the context's own, `GET /me` —
|
|
11
|
+
* a read of its answer as it came (`readRoute`, over a transport that has `read`). A GET's
|
|
12
|
+
* keys in its path are required parameters; every other field is an optional one.
|
|
13
|
+
*/
|
|
14
|
+
/** A search's fields a page asks with, not the screen: the criteria, and its cursor inside it. */
|
|
15
|
+
const PAGED = new Set(["criteria", "cursor"]);
|
|
16
|
+
/** A GET's parameters: the path's keys, and the rest of its input but what a page asks with. */
|
|
17
|
+
function askedOf(operation, rest, paged) {
|
|
18
|
+
const path = [...rest.fields].sort();
|
|
19
|
+
const query = Object.keys(operation.input.properties ?? {})
|
|
20
|
+
.filter((one) => !path.includes(one) && !(paged && PAGED.has(one)))
|
|
21
|
+
.sort();
|
|
22
|
+
return { path, query };
|
|
23
|
+
}
|
|
24
|
+
function callOf(metamodel, operation, rest) {
|
|
25
|
+
if (rest === undefined)
|
|
26
|
+
return { kind: "none", why: "REST does not publish it" };
|
|
27
|
+
if (WRITES.has(rest.method))
|
|
28
|
+
return { kind: "send" };
|
|
29
|
+
const owner = ownerOf(metamodel, operation);
|
|
30
|
+
if (operation.role === "search" && owner !== undefined && owner in metamodel.models)
|
|
31
|
+
return { kind: "list", entity: owner, ...askedOf(operation, rest, true) };
|
|
32
|
+
const field = owner === undefined ? undefined : operation.targets[owner];
|
|
33
|
+
const record = rest.fields.length === 1 && rest.fields[0] === field;
|
|
34
|
+
if (operation.role === "get" && owner !== undefined && field !== undefined && record)
|
|
35
|
+
return { kind: "one", entity: owner, field };
|
|
36
|
+
// Any other GET — a Query of the context's own, `GET /me` — is read as it answers.
|
|
37
|
+
return { kind: "read", ...askedOf(operation, rest, false) };
|
|
38
|
+
}
|
|
39
|
+
/** A parameter list's TypeScript: the path's keys required, the rest optional, all as text. */
|
|
40
|
+
function paramsOf(asked) {
|
|
41
|
+
const union = (names) => names.map((one) => JSON.stringify(one)).join(" | ");
|
|
42
|
+
if (asked.path.length === 0 && asked.query.length === 0)
|
|
43
|
+
return { signature: "", route: (path) => JSON.stringify(path) };
|
|
44
|
+
const required = asked.path.length === 0 ? "" : `Readonly<Record<${union(asked.path)}, string>>`;
|
|
45
|
+
const optional = asked.query.length === 0 ? "" : `Partial<Record<${union(asked.query)}, string>>`;
|
|
46
|
+
const type = [required, optional].filter((one) => one !== "").join(" & ");
|
|
47
|
+
return {
|
|
48
|
+
signature: `params${asked.path.length === 0 ? "?" : ""}: ${type}`,
|
|
49
|
+
route: (path) => `listRoute(${JSON.stringify(path)}, params)`,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/** The data of one operation: what it is, and how each wire reaches it. */
|
|
53
|
+
function dataOf(metamodel, identity, operation, rest) {
|
|
54
|
+
const lines = [
|
|
55
|
+
` identity: ${JSON.stringify(identity)},`,
|
|
56
|
+
` role: ${JSON.stringify(operation.role)},`,
|
|
57
|
+
` query: ${operation.query},`,
|
|
58
|
+
` entities: ${JSON.stringify(operation.entities)},`,
|
|
59
|
+
` targets: ${recordLiteral(operation.targets)},`,
|
|
60
|
+
];
|
|
61
|
+
if (rest !== undefined)
|
|
62
|
+
lines.push(` rest: { method: ${JSON.stringify(rest.method)}, path: ${JSON.stringify(rest.path)} },`);
|
|
63
|
+
const rpc = metamodel.bindings?.rpc?.[identity];
|
|
64
|
+
if (rpc !== undefined)
|
|
65
|
+
lines.push(` rpc: { method: ${JSON.stringify(rpc.method)} },`);
|
|
66
|
+
const grpc = metamodel.bindings?.grpc?.[identity];
|
|
67
|
+
if (grpc !== undefined)
|
|
68
|
+
lines.push(` grpc: { method: ${JSON.stringify(grpc.method)} },`);
|
|
69
|
+
const mcp = metamodel.bindings?.mcp?.[identity];
|
|
70
|
+
if (mcp !== undefined)
|
|
71
|
+
lines.push(` mcp: { tool: ${JSON.stringify(mcp.tool)} },`);
|
|
72
|
+
return ` ${localOf(metamodel, identity)}: {\n${lines.join("\n")}\n },`;
|
|
73
|
+
}
|
|
74
|
+
function methodSourceOf(local, method, call, rest, doc) {
|
|
75
|
+
if (call.kind === "list") {
|
|
76
|
+
const entity = `dto.${identifierOf(call.entity)}`;
|
|
77
|
+
const params = paramsOf(call);
|
|
78
|
+
const signature = params.signature === "" ? "" : `, ${params.signature}`;
|
|
79
|
+
return `${doc} ${method}: (criteria: Criteria<${entity}>${signature}) =>\n transport.list<${entity}>(${params.route(rest.path)}, criteria),`;
|
|
80
|
+
}
|
|
81
|
+
if (call.kind === "read") {
|
|
82
|
+
const params = paramsOf(call);
|
|
83
|
+
return `${doc} ${method}: (${params.signature}) =>\n readRoute<dto.${local}Answer>(transport, ${params.route(rest.path)}),`;
|
|
84
|
+
}
|
|
85
|
+
if (call.kind === "one") {
|
|
86
|
+
const path = rest.path.replace(`{${call.field}}`, "${encodeURIComponent(id)}");
|
|
87
|
+
return `${doc} ${method}: (id: string) =>\n transport.one<dto.${identifierOf(call.entity)}>(\`${path}\`),`;
|
|
88
|
+
}
|
|
89
|
+
const id = rest.fields.includes("id")
|
|
90
|
+
? "body.id === undefined ? undefined : String(body.id)"
|
|
91
|
+
: "undefined";
|
|
92
|
+
return [
|
|
93
|
+
`${doc} ${method}: (body: dto.${local}, options?: SendOptions) =>`,
|
|
94
|
+
` transport.send<dto.${local}Answer>(`,
|
|
95
|
+
` ${JSON.stringify(rest.method)},`,
|
|
96
|
+
` pathFor(routes.${method}, ${id}, body),`,
|
|
97
|
+
` body,`,
|
|
98
|
+
` undefined,`,
|
|
99
|
+
` options,`,
|
|
100
|
+
` ),`,
|
|
101
|
+
].join("\n");
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* The TypeScript of the client of `metamodel`, as the text of one file next to its `domain.ts`.
|
|
105
|
+
*
|
|
106
|
+
* 1. Every operation, by its local name, as data: identity, role, `query`, entities, targets
|
|
107
|
+
* and the binding of each wire that publishes it.
|
|
108
|
+
* 2. Per operation REST reaches, how a transport calls it (`callOf`): a page, a record, a write
|
|
109
|
+
* — or none, said in the client's own doc.
|
|
110
|
+
* 3. The routes of the writes, read once (`writeRoute`).
|
|
111
|
+
* Final: `clientOf(transport)`, a method per operation reached, named by `methodOf`; two with
|
|
112
|
+
* one name are refused, naming both.
|
|
113
|
+
*/
|
|
114
|
+
export function clientFileOf(metamodel, options = {}) {
|
|
115
|
+
const claim = claimed();
|
|
116
|
+
const operations = sorted(metamodel.operations);
|
|
117
|
+
const data = [];
|
|
118
|
+
const routes = [];
|
|
119
|
+
const methods = [];
|
|
120
|
+
const unreached = [];
|
|
121
|
+
let lists = false;
|
|
122
|
+
let paged = false;
|
|
123
|
+
let reads = false;
|
|
124
|
+
let sends = false;
|
|
125
|
+
for (const [identity, operation] of operations) {
|
|
126
|
+
const local = localOf(metamodel, identity);
|
|
127
|
+
const rest = restOf(metamodel, identity, options);
|
|
128
|
+
data.push(dataOf(metamodel, identity, operation, rest));
|
|
129
|
+
const call = callOf(metamodel, operation, rest);
|
|
130
|
+
if (call.kind === "none" || rest === undefined) {
|
|
131
|
+
unreached.push(`\`${local}\`: ${call.kind === "none" ? call.why : ""}.`);
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
const method = methodOf(local);
|
|
135
|
+
claim(method, `operation ${identity}`);
|
|
136
|
+
const doc = docOf([operation.description, deprecationOf(metamodel, operation)], " ");
|
|
137
|
+
methods.push(methodSourceOf(local, method, call, rest, doc));
|
|
138
|
+
if (call.kind === "list" || call.kind === "read") {
|
|
139
|
+
paged ||= call.kind === "list";
|
|
140
|
+
reads ||= call.kind === "read";
|
|
141
|
+
lists ||= call.path.length + call.query.length > 0;
|
|
142
|
+
}
|
|
143
|
+
if (call.kind === "send") {
|
|
144
|
+
sends = true;
|
|
145
|
+
routes.push(` ${method}: writeRoute(${JSON.stringify(`${rest.method} ${rest.path}`)}),`);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
const core = [
|
|
149
|
+
...(lists ? ["listRoute"] : []),
|
|
150
|
+
...(sends ? ["pathFor"] : []),
|
|
151
|
+
...(reads ? ["readRoute"] : []),
|
|
152
|
+
...(sends ? ["type SendOptions"] : []),
|
|
153
|
+
"type Transport",
|
|
154
|
+
...(sends ? ["writeRoute"] : []),
|
|
155
|
+
];
|
|
156
|
+
const imports = [
|
|
157
|
+
...(paged ? ['import type { Criteria } from "@sincpro/criteria";'] : []),
|
|
158
|
+
`import { ${core.join(", ")} } from "${FRAMEWORK_CORE}";`,
|
|
159
|
+
"",
|
|
160
|
+
`import type * as dto from "${DOMAIN_MODULE}";`,
|
|
161
|
+
].join("\n");
|
|
162
|
+
const client = [
|
|
163
|
+
"/**",
|
|
164
|
+
` * «${aliasOf(metamodel)}» over a transport: a method for each operation REST reaches as a`,
|
|
165
|
+
" * page, a record, a read of its own or a write.",
|
|
166
|
+
...(unreached.length === 0
|
|
167
|
+
? []
|
|
168
|
+
: [" *", " * Without a method here:", ...unreached.map((one) => ` * - ${one}`)]),
|
|
169
|
+
" */",
|
|
170
|
+
].join("\n");
|
|
171
|
+
return `${[
|
|
172
|
+
headerOf("The client", metamodel),
|
|
173
|
+
imports,
|
|
174
|
+
`/** Every operation of «${aliasOf(metamodel)}», as data: what it is, and how each wire reaches it. */\nexport const operations = {\n${data.join("\n")}\n} as const;`,
|
|
175
|
+
...(routes.length === 0 ? [] : [`const routes = {\n${routes.join("\n")}\n};`]),
|
|
176
|
+
`${client}\nexport function clientOf(transport: Transport) {\n return {\n${methods.join("\n")}\n };\n}`,
|
|
177
|
+
].join("\n\n")}\n`;
|
|
178
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Metamodel } from "./metamodel.js";
|
|
2
|
+
/**
|
|
3
|
+
* The TypeScript of every DTO of `metamodel`, as the text of one file.
|
|
4
|
+
*
|
|
5
|
+
* 1. The models, by name: an entity's contract, a related model by its own name.
|
|
6
|
+
* 2. Each operation, by its local name (its identity without `<context>.`): its input as
|
|
7
|
+
* `<Name>` and its answer as `<Name>Answer`.
|
|
8
|
+
* 3. The shared definitions those reach — and the ones those reach, until none is new —, but
|
|
9
|
+
* never one a package of the stack already types (`Criteria`).
|
|
10
|
+
* Final: a header that says where it came from and not to edit it, the imports, then models,
|
|
11
|
+
* definitions and operations, each sorted by name; two of them with one TypeScript name are
|
|
12
|
+
* refused, naming both.
|
|
13
|
+
*/
|
|
14
|
+
export declare function domainFileOf(metamodel: Metamodel): string;
|