@sincpro/web-framework 1.1.0 → 2.0.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 +179 -78
- package/dist/core/cache.d.ts +1 -2
- package/dist/core/cache.js +8 -5
- package/dist/core/explain.d.ts +15 -0
- package/dist/core/{transport.js → explain.js} +4 -26
- package/dist/core/form.d.ts +1 -1
- package/dist/core/form.js +1 -0
- package/dist/core/index.d.ts +9 -15
- package/dist/core/index.js +8 -14
- package/dist/core/list.d.ts +5 -4
- package/dist/core/list.js +1 -0
- package/dist/core/lookup.d.ts +1 -1
- package/dist/core/lookup.js +1 -0
- package/dist/core/present.js +1 -1
- package/dist/core/relation.d.ts +1 -1
- package/dist/core/relation.js +1 -1
- package/dist/core/summary.d.ts +1 -6
- package/dist/core/trail.d.ts +1 -1
- package/dist/core/trail.js +1 -0
- package/dist/errors/index.d.ts +48 -0
- package/dist/errors/index.js +56 -0
- package/dist/react/context.d.ts +1 -3
- package/dist/react/context.js +1 -2
- package/dist/react/index.d.ts +2 -0
- package/dist/react/index.js +2 -0
- package/dist/react/scope.d.ts +1 -1
- package/dist/react/scope.js +1 -1
- package/dist/react/useCommand.d.ts +1 -1
- package/dist/react/useCommand.js +1 -2
- package/dist/react/useContextValues.d.ts +15 -0
- package/dist/react/useContextValues.js +19 -0
- package/dist/react/useHistory.d.ts +1 -1
- package/dist/react/useHistory.js +1 -0
- package/dist/react/useListing.d.ts +4 -4
- package/dist/react/useListing.js +6 -6
- package/dist/react/useLookup.d.ts +1 -1
- package/dist/react/useLookup.js +1 -0
- package/dist/react/useNameOf.d.ts +1 -1
- package/dist/react/useNameOf.js +1 -1
- package/dist/react/useRecord.d.ts +1 -2
- package/dist/react/useRecord.js +1 -4
- package/dist/react/useRelated.d.ts +1 -1
- package/dist/react/useRelated.js +1 -0
- package/dist/react/useService.d.ts +38 -0
- package/dist/react/useService.js +66 -0
- package/dist/react/useSummary.d.ts +1 -1
- package/dist/react/useSummary.js +1 -0
- package/dist/telemetry/index.d.ts +45 -0
- package/dist/telemetry/index.js +55 -0
- package/dist/web/Refused.js +1 -1
- package/dist/web/list/EntityList.d.ts +1 -1
- package/dist/web/list/EntityList.js +3 -3
- package/dist/web/list/EntitySearchBar.d.ts +2 -1
- package/dist/web/list/EntitySearchBar.js +17 -11
- package/dist/web/list/presentation.d.ts +1 -1
- package/dist/web/list/presentation.js +1 -0
- package/dist/web/list/rowCommands.d.ts +1 -1
- package/dist/web/list/rowCommands.js +1 -2
- package/dist/web/record/CommandButton.d.ts +1 -1
- package/dist/web/record/CommandButton.js +1 -0
- package/dist/web/record/EntityPicker.d.ts +1 -1
- package/dist/web/record/EntityPicker.js +1 -0
- package/dist/web/record/EntityTags.d.ts +1 -1
- package/dist/web/record/EntityTags.js +1 -0
- package/dist/web/record/RelatedList.d.ts +1 -1
- package/dist/web/record/RelatedList.js +1 -0
- package/dist/web/record/control.d.ts +1 -1
- package/dist/web/record/control.js +1 -1
- package/dist/web/record/edit.js +1 -0
- package/dist/web/record/opening.d.ts +1 -1
- package/dist/web/record/opening.js +1 -0
- package/dist/web/workspace/SincproApp.d.ts +1 -2
- package/dist/web/workspace/SincproApp.js +1 -0
- package/dist/web/workspace/composer.d.ts +1 -2
- package/dist/web/workspace/composer.js +2 -2
- package/dist/web/workspace/open.d.ts +1 -1
- package/dist/web/workspace/open.js +1 -0
- package/dist/web/workspace/record.d.ts +1 -1
- package/dist/web/workspace/record.js +1 -1
- package/dist/web/workspace/workspace.d.ts +1 -1
- package/dist/web/workspace/workspace.js +1 -0
- package/package.json +43 -13
- package/dist/codegen/cli.d.ts +0 -2
- package/dist/codegen/cli.js +0 -82
- package/dist/codegen/client.d.ts +0 -14
- package/dist/codegen/client.js +0 -178
- package/dist/codegen/domain.d.ts +0 -14
- package/dist/codegen/domain.js +0 -195
- package/dist/codegen/entities.d.ts +0 -14
- package/dist/codegen/entities.js +0 -158
- package/dist/codegen/index.d.ts +0 -11
- package/dist/codegen/index.js +0 -9
- package/dist/codegen/metamodel.d.ts +0 -89
- package/dist/codegen/metamodel.js +0 -1
- package/dist/codegen/names.d.ts +0 -40
- package/dist/codegen/names.js +0 -114
- package/dist/codegen/rest.d.ts +0 -31
- package/dist/codegen/rest.js +0 -27
- package/dist/core/contract.d.ts +0 -34
- package/dist/core/contract.js +0 -41
- package/dist/core/discovery.d.ts +0 -47
- package/dist/core/discovery.js +0 -68
- package/dist/core/entity.d.ts +0 -227
- package/dist/core/entity.js +0 -187
- package/dist/core/ids.d.ts +0 -17
- package/dist/core/ids.js +0 -52
- package/dist/core/memory.d.ts +0 -46
- package/dist/core/memory.js +0 -108
- package/dist/core/meta.d.ts +0 -121
- package/dist/core/meta.js +0 -130
- package/dist/core/metas.d.ts +0 -17
- package/dist/core/metas.js +0 -7
- package/dist/core/rest.d.ts +0 -49
- package/dist/core/rest.js +0 -143
- package/dist/core/retry.d.ts +0 -24
- package/dist/core/retry.js +0 -25
- package/dist/core/schema.d.ts +0 -39
- package/dist/core/schema.js +0 -128
- package/dist/core/transport.d.ts +0 -90
- package/dist/core/zone.d.ts +0 -27
- package/dist/core/zone.js +0 -75
package/dist/core/meta.d.ts
DELETED
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
import type { Expression, FieldMeta, FieldType, Meta, Operator } from "@sincpro/criteria";
|
|
2
|
-
/**
|
|
3
|
-
* A model definition written by hand, in the shape sincpro_framework publishes.
|
|
4
|
-
*
|
|
5
|
-
* Context: everything in this package reads a `Meta` — the labels, the columns, the controls,
|
|
6
|
-
* the search box. A service built with sincpro_framework sends one with every answer. A domain
|
|
7
|
-
* that has none (another backend, rows a screen already holds, a prototype
|
|
8
|
-
* with no server yet) declares it here, and from then on it is indistinguishable: the same
|
|
9
|
-
* list, the same record, the same criteria. `describeRows` from `@sincpro/criteria` guesses
|
|
10
|
-
* one from rows; this one is written on purpose, with the words a person reads.
|
|
11
|
-
*
|
|
12
|
-
* const tickets = defineMeta({
|
|
13
|
-
* aggregate: "Ticket", identity: "id", display: "title", search: ["title"],
|
|
14
|
-
* name: { es: "Tickets" },
|
|
15
|
-
* fields: {
|
|
16
|
-
* id: field.text({ readonly: true }),
|
|
17
|
-
* title: field.text({ label: { es: "Título" }, required: true }),
|
|
18
|
-
* stage: field.choice(["new", "done"], { labels: { new: { es: "Nuevo" } } }),
|
|
19
|
-
* opened_at: field.datetime({ label: { es: "Abierto" } }),
|
|
20
|
-
* },
|
|
21
|
-
* });
|
|
22
|
-
*/
|
|
23
|
-
/** A word in several languages, `{default?, es?, en?, …}`; a bare string is the default. */
|
|
24
|
-
export type Words = string | Readonly<Record<string, string>>;
|
|
25
|
-
export type FieldOptions = {
|
|
26
|
-
label?: Words;
|
|
27
|
-
help?: Words;
|
|
28
|
-
required?: boolean;
|
|
29
|
-
readonly?: boolean;
|
|
30
|
-
/** Holds no value sometimes. A nullable field is never sortable: keyset paging over it
|
|
31
|
-
* loses the rows that hold NULL, which is why the framework refuses to order by one. */
|
|
32
|
-
nullable?: boolean;
|
|
33
|
-
default?: unknown;
|
|
34
|
-
/** Form hints over the record, as sincpro_framework's `Presentation` publishes them. */
|
|
35
|
-
readonlyWhen?: Expression;
|
|
36
|
-
requiredWhen?: Expression;
|
|
37
|
-
visibleWhen?: Expression;
|
|
38
|
-
};
|
|
39
|
-
/** The field builders: one per type the framework publishes. */
|
|
40
|
-
export declare const field: {
|
|
41
|
-
text: (options?: FieldOptions) => FieldMeta;
|
|
42
|
-
integer: (options?: FieldOptions) => FieldMeta;
|
|
43
|
-
/** A decimal travels as text so no digit is lost (`"10.50"`): pass `exact: true`. */
|
|
44
|
-
number: (options?: FieldOptions & {
|
|
45
|
-
exact?: boolean;
|
|
46
|
-
}) => {
|
|
47
|
-
exact?: boolean;
|
|
48
|
-
type: FieldType;
|
|
49
|
-
nullable: boolean;
|
|
50
|
-
choices?: unknown[];
|
|
51
|
-
choice_labels?: Record<string, Record<string, string>>;
|
|
52
|
-
label?: Record<string, string>;
|
|
53
|
-
help?: Record<string, string>;
|
|
54
|
-
groupable?: boolean;
|
|
55
|
-
aggregates?: string[];
|
|
56
|
-
computed?: boolean;
|
|
57
|
-
owner?: string | null;
|
|
58
|
-
readonly?: boolean;
|
|
59
|
-
required?: boolean;
|
|
60
|
-
default?: unknown;
|
|
61
|
-
readonly_when?: Expression | null;
|
|
62
|
-
required_when?: Expression | null;
|
|
63
|
-
visible_when?: Expression | null;
|
|
64
|
-
sortable: boolean;
|
|
65
|
-
ops: Operator[];
|
|
66
|
-
widget?: string;
|
|
67
|
-
affects?: string[];
|
|
68
|
-
judged_by?: string[];
|
|
69
|
-
holds?: Expression | null;
|
|
70
|
-
computes?: import("@sincpro/criteria").Formula | null;
|
|
71
|
-
navigable?: boolean;
|
|
72
|
-
total?: string | null;
|
|
73
|
-
many?: boolean;
|
|
74
|
-
relation?: string | null;
|
|
75
|
-
identified_by?: string | null;
|
|
76
|
-
parent_field?: string | null;
|
|
77
|
-
related_field?: string | null;
|
|
78
|
-
definition?: Meta | null;
|
|
79
|
-
kind: import("@sincpro/criteria").FieldKind;
|
|
80
|
-
};
|
|
81
|
-
boolean: (options?: FieldOptions) => FieldMeta;
|
|
82
|
-
date: (options?: FieldOptions) => FieldMeta;
|
|
83
|
-
datetime: (options?: FieldOptions) => FieldMeta;
|
|
84
|
-
tags: (options?: FieldOptions) => FieldMeta;
|
|
85
|
-
/** An enum: the values in order, and the word each one is read as. */
|
|
86
|
-
choice: (values: readonly string[], options?: FieldOptions & {
|
|
87
|
-
labels?: Readonly<Record<string, Words>>;
|
|
88
|
-
}) => FieldMeta;
|
|
89
|
-
/** Points at ONE record of another aggregate, by the scalar key this record holds. */
|
|
90
|
-
many2one: (relation: string, keys: {
|
|
91
|
-
identifiedBy: string;
|
|
92
|
-
definition?: Meta;
|
|
93
|
-
}, options?: FieldOptions) => FieldMeta;
|
|
94
|
-
/** The records of another aggregate that point back here, by THEIR key. */
|
|
95
|
-
one2many: (relation: string, keys: {
|
|
96
|
-
identifiedBy: string;
|
|
97
|
-
definition?: Meta;
|
|
98
|
-
}, options?: FieldOptions) => FieldMeta;
|
|
99
|
-
many2many: (relation: string, keys: {
|
|
100
|
-
identifiedBy: string;
|
|
101
|
-
definition?: Meta;
|
|
102
|
-
}, options?: FieldOptions) => FieldMeta;
|
|
103
|
-
/** A value object kept inside the record — an address, a specification. No identity. */
|
|
104
|
-
embedded: (definition: Meta, options?: FieldOptions & {
|
|
105
|
-
many?: boolean;
|
|
106
|
-
}) => FieldMeta;
|
|
107
|
-
};
|
|
108
|
-
export type MetaSpec = {
|
|
109
|
-
aggregate: string;
|
|
110
|
-
identity: string;
|
|
111
|
-
fields: Record<string, FieldMeta>;
|
|
112
|
-
name?: Words;
|
|
113
|
-
/** The field shown beside the identity: what a select and a many2one show. */
|
|
114
|
-
display?: string;
|
|
115
|
-
/** `"-created_at"`, the way a URL writes it. Without it, the identity ascending. */
|
|
116
|
-
defaultOrder?: string;
|
|
117
|
-
/** The text fields a typed text is looked for in — the entity's search template. */
|
|
118
|
-
search?: readonly string[];
|
|
119
|
-
};
|
|
120
|
-
/** A whole definition, from its fields. See the file header. */
|
|
121
|
-
export declare function defineMeta(spec: MetaSpec): Meta;
|
package/dist/core/meta.js
DELETED
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
function hints(options) {
|
|
2
|
-
return {
|
|
3
|
-
...(options.readonlyWhen ? { readonly_when: options.readonlyWhen } : {}),
|
|
4
|
-
...(options.requiredWhen ? { required_when: options.requiredWhen } : {}),
|
|
5
|
-
...(options.visibleWhen ? { visible_when: options.visibleWhen } : {}),
|
|
6
|
-
};
|
|
7
|
-
}
|
|
8
|
-
/** The comparisons each type takes — the same table sincpro_framework publishes. */
|
|
9
|
-
const OPS = {
|
|
10
|
-
text: ["=", "!=", "like", "starts with", "in", "not in"],
|
|
11
|
-
integer: ["=", "!=", ">", ">=", "<", "<=", "between", "in", "not in"],
|
|
12
|
-
number: ["=", "!=", ">", ">=", "<", "<=", "between", "in", "not in"],
|
|
13
|
-
date: ["=", "!=", ">", ">=", "<", "<=", "between"],
|
|
14
|
-
datetime: ["=", "!=", ">", ">=", "<", "<=", "between"],
|
|
15
|
-
boolean: ["="],
|
|
16
|
-
"text[]": ["contains", "not contains", "=", "!="],
|
|
17
|
-
};
|
|
18
|
-
function words(value) {
|
|
19
|
-
if (value === undefined)
|
|
20
|
-
return undefined;
|
|
21
|
-
return typeof value === "string" ? { default: value } : { ...value };
|
|
22
|
-
}
|
|
23
|
-
function scalar(type, options = {}) {
|
|
24
|
-
const nullable = options.nullable ?? false;
|
|
25
|
-
const ops = [...(OPS[type] ?? [])];
|
|
26
|
-
if (nullable && type !== "text[]" && type !== "boolean")
|
|
27
|
-
ops.push("is null");
|
|
28
|
-
const listed = type === "text[]";
|
|
29
|
-
return {
|
|
30
|
-
type,
|
|
31
|
-
kind: "scalar",
|
|
32
|
-
nullable,
|
|
33
|
-
sortable: !nullable && !listed,
|
|
34
|
-
groupable: !listed,
|
|
35
|
-
ops,
|
|
36
|
-
...(options.label === undefined ? {} : { label: words(options.label) }),
|
|
37
|
-
...(options.help === undefined ? {} : { help: words(options.help) }),
|
|
38
|
-
...(options.required === undefined ? {} : { required: options.required }),
|
|
39
|
-
...(options.readonly === undefined ? {} : { readonly: options.readonly }),
|
|
40
|
-
...(options.default === undefined ? {} : { default: options.default }),
|
|
41
|
-
...hints(options),
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
function relational(type, relation, keys, options = {}) {
|
|
45
|
-
return {
|
|
46
|
-
type,
|
|
47
|
-
kind: "relation",
|
|
48
|
-
nullable: options.nullable ?? type === "many2one",
|
|
49
|
-
sortable: false,
|
|
50
|
-
ops: [],
|
|
51
|
-
many: type !== "many2one",
|
|
52
|
-
relation,
|
|
53
|
-
identified_by: keys.identifiedBy,
|
|
54
|
-
...(keys.definition === undefined ? {} : { definition: keys.definition }),
|
|
55
|
-
...(options.label === undefined ? {} : { label: words(options.label) }),
|
|
56
|
-
...(options.help === undefined ? {} : { help: words(options.help) }),
|
|
57
|
-
...(options.readonly === undefined ? {} : { readonly: options.readonly }),
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
/** The field builders: one per type the framework publishes. */
|
|
61
|
-
export const field = {
|
|
62
|
-
text: (options) => scalar("text", options),
|
|
63
|
-
integer: (options) => scalar("integer", options),
|
|
64
|
-
/** A decimal travels as text so no digit is lost (`"10.50"`): pass `exact: true`. */
|
|
65
|
-
number: (options) => ({
|
|
66
|
-
...scalar("number", options),
|
|
67
|
-
...(options?.exact ? { exact: true } : {}),
|
|
68
|
-
}),
|
|
69
|
-
boolean: (options) => scalar("boolean", options),
|
|
70
|
-
date: (options) => scalar("date", options),
|
|
71
|
-
datetime: (options) => scalar("datetime", options),
|
|
72
|
-
tags: (options) => scalar("text[]", options),
|
|
73
|
-
/** An enum: the values in order, and the word each one is read as. */
|
|
74
|
-
choice: (values, options = {}) => ({
|
|
75
|
-
...scalar("text", options),
|
|
76
|
-
ops: ["=", "!=", "in", "not in", ...(options.nullable ? ["is null"] : [])],
|
|
77
|
-
choices: [...values],
|
|
78
|
-
...(options.labels === undefined
|
|
79
|
-
? {}
|
|
80
|
-
: {
|
|
81
|
-
choice_labels: Object.fromEntries(Object.entries(options.labels).map(([value, said]) => [value, words(said) ?? {}])),
|
|
82
|
-
}),
|
|
83
|
-
}),
|
|
84
|
-
/** Points at ONE record of another aggregate, by the scalar key this record holds. */
|
|
85
|
-
many2one: (relation, keys, options) => relational("many2one", relation, keys, options),
|
|
86
|
-
/** The records of another aggregate that point back here, by THEIR key. */
|
|
87
|
-
one2many: (relation, keys, options) => relational("one2many", relation, keys, options),
|
|
88
|
-
many2many: (relation, keys, options) => relational("many2many", relation, keys, options),
|
|
89
|
-
/** A value object kept inside the record — an address, a specification. No identity. */
|
|
90
|
-
embedded: (definition, options = {}) => ({
|
|
91
|
-
type: "embedded",
|
|
92
|
-
kind: "embedded",
|
|
93
|
-
nullable: options.nullable ?? false,
|
|
94
|
-
sortable: false,
|
|
95
|
-
ops: [],
|
|
96
|
-
definition,
|
|
97
|
-
...(options.many ? { many: true } : {}),
|
|
98
|
-
...(options.label === undefined ? {} : { label: words(options.label) }),
|
|
99
|
-
}),
|
|
100
|
-
};
|
|
101
|
-
/** The template a typed text fills: `like` over each field, any of them matching. */
|
|
102
|
-
function searchTemplate(fields) {
|
|
103
|
-
if (fields.length === 0)
|
|
104
|
-
return null;
|
|
105
|
-
const like = (name) => ({
|
|
106
|
-
field: name,
|
|
107
|
-
operator: "like",
|
|
108
|
-
value: { $: "param", name: "text" },
|
|
109
|
-
});
|
|
110
|
-
const [only] = fields;
|
|
111
|
-
return {
|
|
112
|
-
selection: fields.length === 1 && only !== undefined ? like(only) : { any: fields.map(like) },
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
/** A whole definition, from its fields. See the file header. */
|
|
116
|
-
export function defineMeta(spec) {
|
|
117
|
-
const missing = [spec.identity, spec.display, ...(spec.search ?? [])].filter((name) => name !== undefined && name !== "" && !(name in spec.fields));
|
|
118
|
-
if (missing.length > 0)
|
|
119
|
-
throw new Error(`${spec.aggregate}: ${missing.join(", ")} named but not declared`);
|
|
120
|
-
return {
|
|
121
|
-
aggregate: spec.aggregate,
|
|
122
|
-
identity: spec.identity,
|
|
123
|
-
default_order: spec.defaultOrder ?? spec.identity,
|
|
124
|
-
fields: spec.fields,
|
|
125
|
-
get_id: spec.identity,
|
|
126
|
-
...(spec.name === undefined ? {} : { name: words(spec.name) }),
|
|
127
|
-
...(spec.display === undefined ? {} : { display: spec.display }),
|
|
128
|
-
search: searchTemplate(spec.search ?? []),
|
|
129
|
-
};
|
|
130
|
-
}
|
package/dist/core/metas.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import type { Meta } from "@sincpro/criteria";
|
|
2
|
-
/**
|
|
3
|
-
* The model definitions already known, by entity key (`"billing:invoice"`).
|
|
4
|
-
*
|
|
5
|
-
* Context: sincpro_framework publishes each model once, in the discovery document the
|
|
6
|
-
* transport reads (`core/discovery`); an answer carries only its version. This keeps the
|
|
7
|
-
* definitions ACROSS readings, so the second list of the same entity, the record opened from it
|
|
8
|
-
* and the select that points at it draw their labels before the rows arrive.
|
|
9
|
-
*
|
|
10
|
-
* Seeded, it is the preload: definitions written at build time (a snapshot of the server's,
|
|
11
|
-
* or `defineMeta`) reach the first screen with no request at all.
|
|
12
|
-
*/
|
|
13
|
-
export type MetaStore = {
|
|
14
|
-
get(key: string): Meta | undefined;
|
|
15
|
-
set(key: string, meta: Meta): void;
|
|
16
|
-
};
|
|
17
|
-
export declare function metaStore(seed?: Readonly<Record<string, Meta>>): MetaStore;
|
package/dist/core/metas.js
DELETED
package/dist/core/rest.d.ts
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { type Criteria } from "@sincpro/criteria";
|
|
2
|
-
import { type Transport } from "./transport.js";
|
|
3
|
-
/**
|
|
4
|
-
* The REST wire of sincpro_framework (`FastApiGateway`).
|
|
5
|
-
*
|
|
6
|
-
* Context: a list is criteria's `restSource` (0.3.1 sends the criteria as plain JSON and joins
|
|
7
|
-
* a route that carries its own query, `/projects?tenant_id=acme` — the two things forge's
|
|
8
|
-
* console found wrong in 0.3.0, fixed there). What this adds is what criteria does not know:
|
|
9
|
-
* one record (`one()`), a write (`send()`, with its `Idempotency-Key`), and its refusals as a
|
|
10
|
-
* `TransportError`, the one error every screen explains.
|
|
11
|
-
*
|
|
12
|
-
* The definition of each model — what a list, a form and a select are built from — is read
|
|
13
|
-
* from the bounded context's discovery document (`document`, `/openapi.json` by default), once,
|
|
14
|
-
* and read again only when an answer says another version of its model (`model_version`); the
|
|
15
|
-
* document is revalidated with its `ETag`, so an unchanged one costs a `304`.
|
|
16
|
-
*
|
|
17
|
-
* const transport = restTransport({
|
|
18
|
-
* base: "/v1",
|
|
19
|
-
* headers: () => ({ Authorization: `Bearer ${session.secret}` }),
|
|
20
|
-
* });
|
|
21
|
-
*/
|
|
22
|
-
export type RestOptions = {
|
|
23
|
-
/** Prepended to every route: `"/v1"`, `"https://forge.sincpro.com.bo/v1"`. */
|
|
24
|
-
base?: string;
|
|
25
|
-
/** Sent with every request. A function is called fresh each time — a token that rotates. */
|
|
26
|
-
headers?: Record<string, string> | (() => Record<string, string>);
|
|
27
|
-
/** Defaults to the global `fetch`. Inject one for tests. */
|
|
28
|
-
fetch?: typeof fetch;
|
|
29
|
-
/** Where the bounded context's discovery document is: `"/openapi.json"` by default, at the
|
|
30
|
-
* origin of `base`. `null`: none is read, and the definitions come only from `metas`. */
|
|
31
|
-
document?: string | null;
|
|
32
|
-
/**
|
|
33
|
-
* The version of each model the application was written against — the snapshot it was
|
|
34
|
-
* generated from (`versionsOf(contract)`). With it, an answer of another version is told to
|
|
35
|
-
* `onVersionDrift`: the server moved, the generated code did not.
|
|
36
|
-
*/
|
|
37
|
-
snapshot?: Readonly<Record<string, string>>;
|
|
38
|
-
/** Called once per model and version the server answers that the snapshot does not hold —
|
|
39
|
-
* never an error: the screen keeps working on the live definition. */
|
|
40
|
-
onVersionDrift?: (drift: VersionDrift) => void;
|
|
41
|
-
};
|
|
42
|
-
/** A model the server answers at another version than the snapshot the code came from. */
|
|
43
|
-
export type VersionDrift = {
|
|
44
|
-
model: string;
|
|
45
|
-
snapshot: string;
|
|
46
|
-
live: string;
|
|
47
|
-
};
|
|
48
|
-
export declare function urlOf<T>(base: string, route: string, criteria?: Criteria<T>): string;
|
|
49
|
-
export declare function restTransport(options?: RestOptions): Transport;
|
package/dist/core/rest.js
DELETED
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
import { readProblem } from "@sincpro/criteria";
|
|
2
|
-
import { RestError, restSource } from "@sincpro/criteria/rest";
|
|
3
|
-
import { definitionOf, modelAt, } from "./discovery.js";
|
|
4
|
-
import { TransportError, } from "./transport.js";
|
|
5
|
-
/** The keys of an answer that are not the record: what is left is the record's own field. */
|
|
6
|
-
const ENVELOPE = new Set(["cursor", "count", "model_version", "dropped", "draft", "missing"]);
|
|
7
|
-
export function urlOf(base, route, criteria) {
|
|
8
|
-
const url = `${base}${route}`;
|
|
9
|
-
if (criteria === undefined)
|
|
10
|
-
return url;
|
|
11
|
-
const joiner = url.includes("?") ? "&" : "?";
|
|
12
|
-
return `${url}${joiner}criteria=${encodeURIComponent(JSON.stringify(criteria))}`;
|
|
13
|
-
}
|
|
14
|
-
/** The path a route is asked at, without its query: what a document path is matched with. */
|
|
15
|
-
function pathOf(url) {
|
|
16
|
-
const path = url.split("?")[0] ?? url;
|
|
17
|
-
return /^https?:\/\//.test(path) ? new URL(path).pathname : path;
|
|
18
|
-
}
|
|
19
|
-
/** Where the document is: `document` as given when absolute, else at the origin of `base`. */
|
|
20
|
-
function documentUrl(base, document) {
|
|
21
|
-
if (/^https?:\/\//.test(document) || !/^https?:\/\//.test(base))
|
|
22
|
-
return document;
|
|
23
|
-
return new URL(document, base).toString();
|
|
24
|
-
}
|
|
25
|
-
export function restTransport(options = {}) {
|
|
26
|
-
const base = options.base ?? "";
|
|
27
|
-
const doFetch = options.fetch ?? ((input, init) => fetch(input, init));
|
|
28
|
-
const own = () => typeof options.headers === "function" ? options.headers() : (options.headers ?? {});
|
|
29
|
-
// The discovery document, read once; asked again — with its ETag — when an answer says a
|
|
30
|
-
// version of its model the document did not.
|
|
31
|
-
const located = options.document === null ? null : (options.document ?? "/openapi.json");
|
|
32
|
-
let held;
|
|
33
|
-
let tag = null;
|
|
34
|
-
function documentRead(fresh) {
|
|
35
|
-
if (located === null)
|
|
36
|
-
return Promise.resolve(null);
|
|
37
|
-
if (held !== undefined && !fresh)
|
|
38
|
-
return held;
|
|
39
|
-
const before = held;
|
|
40
|
-
held = (async () => {
|
|
41
|
-
try {
|
|
42
|
-
const response = await doFetch(documentUrl(base, located), {
|
|
43
|
-
headers: { ...own(), ...(tag === null ? {} : { "If-None-Match": tag }) },
|
|
44
|
-
});
|
|
45
|
-
if (response.status === 304 && before !== undefined)
|
|
46
|
-
return await before;
|
|
47
|
-
if (!response.ok)
|
|
48
|
-
return null;
|
|
49
|
-
tag = response.headers.get("etag");
|
|
50
|
-
return (await response.json());
|
|
51
|
-
}
|
|
52
|
-
catch {
|
|
53
|
-
return null;
|
|
54
|
-
}
|
|
55
|
-
})();
|
|
56
|
-
return held;
|
|
57
|
-
}
|
|
58
|
-
/** The definition of the model `url` answers; read again when `version` is not the one held. */
|
|
59
|
-
// Each drift is told once: a list read every few seconds would say it on every page.
|
|
60
|
-
const told = new Set();
|
|
61
|
-
function noticeDrift(model, live) {
|
|
62
|
-
const snapshot = options.snapshot?.[model];
|
|
63
|
-
if (snapshot === undefined || !live || live === snapshot)
|
|
64
|
-
return;
|
|
65
|
-
const key = `${model}\u0000${live}`;
|
|
66
|
-
if (told.has(key))
|
|
67
|
-
return;
|
|
68
|
-
told.add(key);
|
|
69
|
-
options.onVersionDrift?.({ model, snapshot, live });
|
|
70
|
-
}
|
|
71
|
-
async function definitionAt(url, method, version) {
|
|
72
|
-
const published = await documentRead(false);
|
|
73
|
-
if (published === null)
|
|
74
|
-
return null;
|
|
75
|
-
const name = modelAt(published, pathOf(url), method);
|
|
76
|
-
if (name === undefined)
|
|
77
|
-
return null;
|
|
78
|
-
noticeDrift(name, version);
|
|
79
|
-
const known = definitionOf(published, name);
|
|
80
|
-
if (known === undefined || !version || known.version === version)
|
|
81
|
-
return known?.meta ?? null;
|
|
82
|
-
const again = await documentRead(true);
|
|
83
|
-
return (again === null ? undefined : definitionOf(again, name))?.meta ?? known.meta;
|
|
84
|
-
}
|
|
85
|
-
async function get(route, url, signal, write) {
|
|
86
|
-
const headers = write
|
|
87
|
-
? {
|
|
88
|
-
...own(),
|
|
89
|
-
"Content-Type": "application/json",
|
|
90
|
-
...(write.key === undefined ? {} : { "Idempotency-Key": write.key }),
|
|
91
|
-
}
|
|
92
|
-
: own();
|
|
93
|
-
const response = await doFetch(url, {
|
|
94
|
-
headers,
|
|
95
|
-
...(write ? { method: write.method, body: JSON.stringify(write.body) } : {}),
|
|
96
|
-
...(signal ? { signal } : {}),
|
|
97
|
-
});
|
|
98
|
-
const text = await response.text();
|
|
99
|
-
if (!response.ok)
|
|
100
|
-
throw new TransportError(route, response.status, readProblem(text), undefined);
|
|
101
|
-
return text ? JSON.parse(text) : null;
|
|
102
|
-
}
|
|
103
|
-
return {
|
|
104
|
-
async list(route, criteria, signal) {
|
|
105
|
-
const source = restSource(`${base}${route}`, { headers: own, fetch: doFetch });
|
|
106
|
-
try {
|
|
107
|
-
const page = await source.read(criteria, signal);
|
|
108
|
-
return {
|
|
109
|
-
...page,
|
|
110
|
-
meta: page.meta ?? (await definitionAt(`${base}${route}`, "get", page.version)),
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
catch (error) {
|
|
114
|
-
if (error instanceof RestError)
|
|
115
|
-
throw new TransportError(route, error.status, error.problem, undefined);
|
|
116
|
-
throw error;
|
|
117
|
-
}
|
|
118
|
-
},
|
|
119
|
-
async read(route, signal) {
|
|
120
|
-
return (await get(route, urlOf(base, route), signal));
|
|
121
|
-
},
|
|
122
|
-
async one(route, signal) {
|
|
123
|
-
const answer = (await get(route, urlOf(base, route), signal));
|
|
124
|
-
const own = Object.keys(answer).filter((key) => !ENVELOPE.has(key));
|
|
125
|
-
const [key] = own;
|
|
126
|
-
if (own.length !== 1 || key === undefined)
|
|
127
|
-
throw new TransportError(route, 200, null, `${route} answered ${own.length} fields of its own (${own.join(", ")}); a record answers one`);
|
|
128
|
-
return {
|
|
129
|
-
record: answer[key],
|
|
130
|
-
meta: await definitionAt(urlOf(base, route), "get", answer["model_version"]),
|
|
131
|
-
dropped: answer["dropped"] ?? [],
|
|
132
|
-
draft: answer["draft"] ?? null,
|
|
133
|
-
};
|
|
134
|
-
},
|
|
135
|
-
async send(method, route, body, signal, sending) {
|
|
136
|
-
return (await get(route, urlOf(base, route), signal, {
|
|
137
|
-
method,
|
|
138
|
-
body,
|
|
139
|
-
key: sending?.idempotencyKey,
|
|
140
|
-
}));
|
|
141
|
-
},
|
|
142
|
-
};
|
|
143
|
-
}
|
package/dist/core/retry.d.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The `Idempotency-Key` of a gesture: one per thing a person meant to do, the SAME one while
|
|
3
|
-
* that thing is retried.
|
|
4
|
-
*
|
|
5
|
-
* Context: a Command that got no answer — the network dropped, a proxy answered 502 — may have
|
|
6
|
-
* run. Sending it again with a new key could pay twice; with the same key the server answers
|
|
7
|
-
* what it already did (sincpro_framework's `@idempotency`). So a key lives until the server
|
|
8
|
-
* gives a definite answer: done, or refused for a reason a retry does not change. Changing the
|
|
9
|
-
* body is a different gesture, and gets a new key — the server refuses a key that comes back
|
|
10
|
-
* with another payload.
|
|
11
|
-
*
|
|
12
|
-
* const keys = retryKeys();
|
|
13
|
-
* const key = keys.keyFor(body); // the same while the previous one went unanswered
|
|
14
|
-
* try { await send(…, { idempotencyKey: key }); keys.settle(null); }
|
|
15
|
-
* catch (error) { keys.settle(error); } // kept when nobody knows what happened
|
|
16
|
-
*/
|
|
17
|
-
export type RetryKeys = {
|
|
18
|
-
keyFor: (body: unknown) => string;
|
|
19
|
-
/** What the last attempt ended in: `null` when it was answered. */
|
|
20
|
-
settle: (error: unknown) => void;
|
|
21
|
-
};
|
|
22
|
-
/** Whether a failure leaves it unknown if the Command ran — so a retry must reuse its key. */
|
|
23
|
-
export declare function unanswered(error: unknown): boolean;
|
|
24
|
-
export declare function retryKeys(make?: () => string): RetryKeys;
|
package/dist/core/retry.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { TransportError } from "./transport.js";
|
|
2
|
-
/** Whether a failure leaves it unknown if the Command ran — so a retry must reuse its key. */
|
|
3
|
-
export function unanswered(error) {
|
|
4
|
-
if (!(error instanceof TransportError))
|
|
5
|
-
return true;
|
|
6
|
-
if (error.status === null || error.status >= 500)
|
|
7
|
-
return true;
|
|
8
|
-
// The same key is still running elsewhere: retry once it completes, with that key.
|
|
9
|
-
return error.problem?.kind === "in_progress";
|
|
10
|
-
}
|
|
11
|
-
export function retryKeys(make = () => globalThis.crypto.randomUUID()) {
|
|
12
|
-
let pending = null;
|
|
13
|
-
return {
|
|
14
|
-
keyFor: (body) => {
|
|
15
|
-
const sent = JSON.stringify(body ?? null);
|
|
16
|
-
if (pending === null || pending.body !== sent)
|
|
17
|
-
pending = { body: sent, key: make() };
|
|
18
|
-
return pending.key;
|
|
19
|
-
},
|
|
20
|
-
settle: (error) => {
|
|
21
|
-
if (error === null || !unanswered(error))
|
|
22
|
-
pending = null;
|
|
23
|
-
},
|
|
24
|
-
};
|
|
25
|
-
}
|
package/dist/core/schema.d.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import type { Meta } from "@sincpro/criteria";
|
|
2
|
-
/**
|
|
3
|
-
* A model definition read from a JSON Schema — what an OpenAPI document holds.
|
|
4
|
-
*
|
|
5
|
-
* Context: a model a READ answers is published by sincpro_framework as its contract — JSON
|
|
6
|
-
* Schema with `x-sincpro` — and read by the transport (`metaFromContract`, `core/discovery`).
|
|
7
|
-
* What has no `x-sincpro` is a plain JSON Schema: a COMMAND's body, a value object it carries.
|
|
8
|
-
*
|
|
9
|
-
* So this reads such a component schema into a `Meta` — the form of a Command, a value object —
|
|
10
|
-
* and `completeMeta` fills, from it, what a definition left `unknown`.
|
|
11
|
-
*
|
|
12
|
-
* const spec = metaFromSchema("ProjectSpecification", doc.components.schemas);
|
|
13
|
-
* const fixed = completeMeta(project.fields.specification.definition, spec);
|
|
14
|
-
*/
|
|
15
|
-
export type JsonSchema = {
|
|
16
|
-
type?: string | string[];
|
|
17
|
-
format?: string;
|
|
18
|
-
enum?: readonly unknown[];
|
|
19
|
-
items?: JsonSchema;
|
|
20
|
-
$ref?: string;
|
|
21
|
-
anyOf?: readonly JsonSchema[];
|
|
22
|
-
oneOf?: readonly JsonSchema[];
|
|
23
|
-
properties?: Readonly<Record<string, JsonSchema>>;
|
|
24
|
-
required?: readonly string[];
|
|
25
|
-
title?: string;
|
|
26
|
-
description?: string;
|
|
27
|
-
default?: unknown;
|
|
28
|
-
};
|
|
29
|
-
export type SchemaComponents = Readonly<Record<string, JsonSchema>>;
|
|
30
|
-
/** The definition a component schema describes. `identity` empty: a value object has none. */
|
|
31
|
-
export declare function metaFromSchema(name: string, components: SchemaComponents, options?: {
|
|
32
|
-
identity?: string;
|
|
33
|
-
}): Meta;
|
|
34
|
-
/**
|
|
35
|
-
* The server's definition, with what it left `unknown` taken from the schema — recursively
|
|
36
|
-
* into value objects. What the server DID say always wins: its labels, its operators, its
|
|
37
|
-
* hints. A field the schema does not know is left as the server sent it.
|
|
38
|
-
*/
|
|
39
|
-
export declare function completeMeta(meta: Meta, schema: Meta): Meta;
|