@supersoniks/concorde 4.2.0 → 4.3.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 +0 -0
- package/build-infos.json +1 -1
- package/concorde-core.bundle.js +175 -171
- package/concorde-core.es.js +2493 -2247
- package/dist/concorde-core.bundle.js +175 -171
- package/dist/concorde-core.es.js +2493 -2247
- package/docs/assets/{index-BbnRiebQ.js → index-B0IJ9I_B.js} +268 -242
- package/docs/assets/{index-BBv9CZqo.css → index-B3QHEJTV.css} +1 -1
- package/docs/index.html +2 -2
- package/docs/src/docs/_misc/bind.md +74 -0
- package/docs/src/docs/_misc/key.md +135 -0
- package/docs/src/docs/search/docs-search.json +310 -0
- package/docs/src/tsconfig-model.json +1 -1
- package/docs/src/tsconfig.json +322 -306
- package/package.json +22 -4
- package/php/get-challenge.php +0 -0
- package/php/some-service.php +0 -0
- package/scripts/pre-build.mjs +4 -0
- package/src/core/_types/endpoint.ts +4 -0
- package/src/core/_types/key.ts +1 -0
- package/src/core/components/functional/example/example.ts +38 -6
- package/src/core/components/ui/captcha/captcha.ts +12 -6
- package/src/core/decorators/Subscriber.ts +2 -0
- package/src/core/decorators/api.spec.ts +150 -0
- package/src/core/decorators/api.ts +244 -0
- package/src/core/decorators/subscriber/bind.ts +57 -145
- package/src/core/decorators/subscriber/dynamicPath.ts +77 -0
- package/src/core/decorators/subscriber/dynamicPropertyWatch.ts +105 -0
- package/src/core/decorators/subscriber/onAssign.ts +11 -147
- package/src/core/decorators/subscriber/publish.spec.ts +21 -0
- package/src/core/decorators/subscriber/publish.ts +148 -0
- package/src/core/decorators/subscriber/publisherPath.ts +13 -0
- package/src/core/decorators/subscriber/subscribe.spec.ts +21 -0
- package/src/core/decorators/subscriber/subscribe.ts +32 -0
- package/src/core/decorators/subscriber/subscribe.type-test.ts +32 -0
- package/src/core/utils/api.ts +83 -15
- package/src/core/utils/dataProviderKey.spec.ts +34 -0
- package/src/core/utils/dataProviderKey.ts +86 -0
- package/src/core/utils/endpoint.spec.ts +41 -0
- package/src/core/utils/endpoint.ts +87 -0
- package/src/decorators.ts +14 -0
- package/src/docs/{_misc → _decorators}/ancestor-attribute.md +15 -31
- package/src/docs/_decorators/bind.md +164 -0
- package/src/docs/_decorators/get.md +65 -0
- package/src/docs/_decorators/publish.md +54 -0
- package/src/docs/_decorators/subscribe.md +36 -0
- package/src/docs/_misc/dataProviderKey.md +135 -0
- package/src/docs/_misc/endpoint.md +42 -0
- package/src/docs/example/decorators-demo-bind-demos.ts +210 -0
- package/src/docs/example/decorators-demo-geo.ts +45 -0
- package/src/docs/example/decorators-demo-init.ts +228 -0
- package/src/docs/example/decorators-demo-subscribe-publish-get-demos.ts +324 -0
- package/src/docs/example/decorators-demo.ts +12 -459
- package/src/docs/navigation/navigation.ts +27 -10
- package/src/docs/search/docs-search.json +1059 -609
- package/src/tsconfig-model.json +1 -1
- package/src/tsconfig.json +65 -10
- package/src/tsconfig.tsbuildinfo +1 -1
- package/src/utils.ts +8 -1
- package/vite.config.mts +11 -0
- package/src/core/components/ui/modal/modal.stories.ts +0 -140
- package/src/docs/_misc/bind.md +0 -362
- /package/src/docs/{_misc → _decorators}/auto-subscribe.md +0 -0
- /package/src/docs/{_misc → _decorators}/on-assign.md +0 -0
- /package/src/docs/{_misc → _decorators}/wait-for-ancestors.md +0 -0
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { ApiGetResult } from "./api";
|
|
2
|
+
import { DataProviderKey } from "./dataProviderKey";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Représente un chemin d’endpoint HTTP (ou path accepté par `API.get`),
|
|
6
|
+
* typé par la forme de la réponse attendue `T`.
|
|
7
|
+
*
|
|
8
|
+
* **`U` (optionnel, défaut `any`)** : propriétés minimales sur le composant pour résoudre les
|
|
9
|
+
* segments dynamiques du path (`${…}` / `{$…}`), comme pour `DataProviderKey<T, U>`. Utilisé par `@get`.
|
|
10
|
+
*
|
|
11
|
+
* Contrairement à `DataProviderKey`, il n’y a **pas** de navigation par propriétés
|
|
12
|
+
* (pas de dot-syntax) : le path est une seule chaîne.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* new Endpoint<User, { userId: string }>("users/${userId}");
|
|
16
|
+
*/
|
|
17
|
+
export class Endpoint<T, U = any> {
|
|
18
|
+
declare readonly _phantom?: T;
|
|
19
|
+
declare readonly _phantomHost?: U;
|
|
20
|
+
|
|
21
|
+
readonly path: string;
|
|
22
|
+
|
|
23
|
+
constructor(path: string) {
|
|
24
|
+
this.path = Endpoint.normalizePath(path);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Même path qu’`Endpoint` ; le 2ᵉ générique `U` est propagé sur la clé publisher. */
|
|
28
|
+
getDataProviderKey(): DataProviderKey<ApiGetResult<T>, U> {
|
|
29
|
+
return new DataProviderKey<ApiGetResult<T>, U>(this.path);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Trim, refuse le path vide, enlève les `/` en tête (path relatif au `serviceURL`),
|
|
34
|
+
* et fusionne les `/` successifs en un seul.
|
|
35
|
+
* Pour une URL absolue (`http://` / `https://`), normalise surtout le pathname (pas de `//` redondants).
|
|
36
|
+
*/
|
|
37
|
+
static normalizePath(path: string): string {
|
|
38
|
+
const t = String(path).trim();
|
|
39
|
+
if (!t) {
|
|
40
|
+
throw new RangeError("Endpoint: path cannot be empty");
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (/^https?:\/\//i.test(t)) {
|
|
44
|
+
let u: URL;
|
|
45
|
+
try {
|
|
46
|
+
u = new URL(t);
|
|
47
|
+
} catch {
|
|
48
|
+
throw new RangeError("Endpoint: invalid absolute URL");
|
|
49
|
+
}
|
|
50
|
+
u.pathname = u.pathname.replace(/\/+/g, "/");
|
|
51
|
+
return u.href;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const q = t.indexOf("?");
|
|
55
|
+
const hash = t.indexOf("#");
|
|
56
|
+
let end = t.length;
|
|
57
|
+
if (q >= 0) end = Math.min(end, q);
|
|
58
|
+
if (hash >= 0) end = Math.min(end, hash);
|
|
59
|
+
let base = t.slice(0, end);
|
|
60
|
+
const tail = t.slice(end);
|
|
61
|
+
|
|
62
|
+
base = base.replace(/^\/+/, "");
|
|
63
|
+
base = base.replace(/\/+/g, "/");
|
|
64
|
+
|
|
65
|
+
if (!base && tail) {
|
|
66
|
+
throw new RangeError("Endpoint: path cannot be empty");
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return base + tail;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Utile avant construction dynamique. */
|
|
73
|
+
static isNonEmpty(path: string): boolean {
|
|
74
|
+
return String(path).trim().length > 0;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Indique un path local du type `dataProvider(id)…` reconnu par `API.get`.
|
|
79
|
+
*/
|
|
80
|
+
static looksLikeDataProviderPath(path: string): boolean {
|
|
81
|
+
return /^\s*dataProvider\s*\(/i.test(String(path));
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
toString(): string {
|
|
85
|
+
return this.path;
|
|
86
|
+
}
|
|
87
|
+
}
|
package/src/decorators.ts
CHANGED
|
@@ -1,11 +1,22 @@
|
|
|
1
1
|
import * as mySubscriber from "@supersoniks/concorde/core/decorators/Subscriber";
|
|
2
|
+
import * as api from "@supersoniks/concorde/core/decorators/api";
|
|
3
|
+
import { Endpoint } from "./core/utils/endpoint";
|
|
2
4
|
import * as lifecycle from "@supersoniks/concorde/core/decorators/lifecycle";
|
|
3
5
|
|
|
4
6
|
export const bind = mySubscriber.bind;
|
|
7
|
+
export const publish = mySubscriber.publish;
|
|
8
|
+
export const subscribe = mySubscriber.subscribe;
|
|
5
9
|
export const onAssign = mySubscriber.onAssign;
|
|
6
10
|
export const ancestorAttribute = mySubscriber.ancestorAttribute;
|
|
7
11
|
export const autoSubscribe = mySubscriber.autoSubscribe;
|
|
8
12
|
export const autoFill = mySubscriber.autoFill;
|
|
13
|
+
export const get = api.get;
|
|
14
|
+
export {
|
|
15
|
+
DataProviderKey,
|
|
16
|
+
type DataProviderKeyHost,
|
|
17
|
+
} from "./core/utils/dataProviderKey";
|
|
18
|
+
export { Endpoint };
|
|
19
|
+
export type { ApiGetResult } from "./core/utils/api";
|
|
9
20
|
export const awaitConnectedAncestors = lifecycle.awaitConnectedAncestors;
|
|
10
21
|
export const dispatchConnectedEvent = lifecycle.dispatchConnectedEvent;
|
|
11
22
|
export const CONNECTED = lifecycle.CONNECTED;
|
|
@@ -17,8 +28,11 @@ window["concorde-decorator-subscriber"] =
|
|
|
17
28
|
window["concorde-decorator-subscriber"] || {};
|
|
18
29
|
window["concorde-decorator-subscriber"] = {
|
|
19
30
|
bind: mySubscriber.bind,
|
|
31
|
+
publish: mySubscriber.publish,
|
|
32
|
+
subscribe: mySubscriber.subscribe,
|
|
20
33
|
onAssing: mySubscriber.onAssign,
|
|
21
34
|
ancestorAttribute: mySubscriber.ancestorAttribute,
|
|
22
35
|
autoSubscribe: mySubscriber.autoSubscribe,
|
|
23
36
|
autoFill: mySubscriber.autoFill,
|
|
37
|
+
get: api.get,
|
|
24
38
|
};
|
|
@@ -17,45 +17,29 @@ import { ancestorAttribute } from "@supersoniks/concorde/decorators";
|
|
|
17
17
|
</sonic-code>
|
|
18
18
|
|
|
19
19
|
### Basic example
|
|
20
|
-
|
|
20
|
+
|
|
21
|
+
The component reads `dataProvider` and `testAttribute` from its ancestor wrapper.
|
|
21
22
|
|
|
22
23
|
<sonic-code language="typescript">
|
|
23
24
|
<template>
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
static styles = [tailwind];
|
|
25
|
+
import { html, LitElement } from "lit";
|
|
26
|
+
import { customElement } from "lit/decorators.js";
|
|
27
|
+
import { ancestorAttribute } from "@supersoniks/concorde/decorators";
|
|
28
28
|
//
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
29
|
+
@customElement("demo-ancestor-attribute")
|
|
30
|
+
export class DemoAncestorAttribute extends LitElement {
|
|
31
|
+
@ancestorAttribute("dataProvider")
|
|
32
|
+
dataProvider: string | null = null;
|
|
32
33
|
//
|
|
33
|
-
@
|
|
34
|
-
|
|
35
|
-
withoutReflect: number = 0;
|
|
36
|
-
// initialize the publisher data
|
|
37
|
-
connectedCallback() {
|
|
38
|
-
super.connectedCallback();
|
|
39
|
-
this.resetData();
|
|
40
|
-
}
|
|
34
|
+
@ancestorAttribute("testAttribute")
|
|
35
|
+
testAttribute: string | null = null;
|
|
41
36
|
//
|
|
42
|
-
resetData() {
|
|
43
|
-
PublisherManager.get("bindReflectDemo").set({ count: 0 });
|
|
44
|
-
}
|
|
45
37
|
render() {
|
|
46
38
|
return html`
|
|
47
|
-
<
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
</div>
|
|
52
|
-
<sonic-button @click=${() => this.withReflect++}
|
|
53
|
-
>Increment with reflect</sonic-button
|
|
54
|
-
>
|
|
55
|
-
<sonic-button @click=${() => this.withoutReflect++}
|
|
56
|
-
>Increment without reflect</sonic-button
|
|
57
|
-
>
|
|
58
|
-
<sonic-button @click=${this.resetData}>Reset publisher data</sonic-button>
|
|
39
|
+
<section>
|
|
40
|
+
<p>dataProvider: <strong>${this.dataProvider || "null"}</strong></p>
|
|
41
|
+
<p>testAttribute: <strong>${this.testAttribute || "null"}</strong></p>
|
|
42
|
+
</section>
|
|
59
43
|
`;
|
|
60
44
|
}
|
|
61
45
|
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
# @bind
|
|
2
|
+
|
|
3
|
+
Binds a class property to a path in a publisher. The property updates when publisher data changes.
|
|
4
|
+
|
|
5
|
+
For Lit re-renders, also add `@state()` on the same property.
|
|
6
|
+
|
|
7
|
+
**See also:** [@subscribe](#docs/_decorators/subscribe.md/subscribe), [@publish](#docs/_decorators/publish.md/publish), [@get](#docs/_decorators/get.md/get).
|
|
8
|
+
|
|
9
|
+
## Principle
|
|
10
|
+
|
|
11
|
+
The decorator subscribes via `PublisherManager` using dot notation. Publisher updates flow into the decorated property.
|
|
12
|
+
|
|
13
|
+
## Import
|
|
14
|
+
|
|
15
|
+
<sonic-code language="typescript">
|
|
16
|
+
<template>
|
|
17
|
+
import { bind } from "@supersoniks/concorde/decorators";
|
|
18
|
+
</template>
|
|
19
|
+
</sonic-code>
|
|
20
|
+
|
|
21
|
+
## Example
|
|
22
|
+
|
|
23
|
+
<sonic-code language="typescript">
|
|
24
|
+
<template>
|
|
25
|
+
@customElement("demo-bind")
|
|
26
|
+
export class DemoBind extends LitElement {
|
|
27
|
+
static styles = [tailwind];
|
|
28
|
+
//
|
|
29
|
+
@bind("demoData.firstName")
|
|
30
|
+
@state()
|
|
31
|
+
firstName = "";
|
|
32
|
+
//
|
|
33
|
+
@bind("demoData.lastName")
|
|
34
|
+
@state()
|
|
35
|
+
lastName: string = "";
|
|
36
|
+
//
|
|
37
|
+
@bind("demoData.count")
|
|
38
|
+
@state()
|
|
39
|
+
count: number = 0;
|
|
40
|
+
//
|
|
41
|
+
render() {
|
|
42
|
+
return //......
|
|
43
|
+
}
|
|
44
|
+
//
|
|
45
|
+
updateData() {
|
|
46
|
+
const demoData = PublisherManager.get("demoData");
|
|
47
|
+
const demoUsers = PublisherManager.get("demoUsers");
|
|
48
|
+
const randomIndex = Math.floor(Math.random() * demoUsers.get().length);
|
|
49
|
+
const randomUser = demoUsers.get()[randomIndex];
|
|
50
|
+
demoData.set({
|
|
51
|
+
firstName: randomUser.firstName,
|
|
52
|
+
lastName: randomUser.lastName,
|
|
53
|
+
count: (demoData.count.get() || 0) + 1,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
//
|
|
58
|
+
</template>
|
|
59
|
+
</sonic-code>
|
|
60
|
+
|
|
61
|
+
<sonic-code>
|
|
62
|
+
<template>
|
|
63
|
+
<demo-bind></demo-bind>
|
|
64
|
+
</template>
|
|
65
|
+
</sonic-code>
|
|
66
|
+
|
|
67
|
+
## `DataProviderKey` (strict typing)
|
|
68
|
+
|
|
69
|
+
`@bind` accepts either a string path (legacy) or a `DataProviderKey<T>`. The property type must match `T`. Use `reflect: true` to push local writes back to the publisher (see below). See [DataProviderKey](#docs/_misc/dataProviderKey.md/dataProviderKey).
|
|
70
|
+
|
|
71
|
+
<sonic-code language="typescript">
|
|
72
|
+
<template>
|
|
73
|
+
import { bind } from "@supersoniks/concorde/decorators";
|
|
74
|
+
import { DataProviderKey } from "@supersoniks/concorde/dataProviderKey";
|
|
75
|
+
//
|
|
76
|
+
type Data = { count: number };
|
|
77
|
+
const dataKey = new DataProviderKey<Data>("data");
|
|
78
|
+
//
|
|
79
|
+
@bind(dataKey.count, { reflect: true })
|
|
80
|
+
@state()
|
|
81
|
+
count: number = 0;
|
|
82
|
+
</template>
|
|
83
|
+
</sonic-code>
|
|
84
|
+
|
|
85
|
+
## Reflect (`reflect: true`)
|
|
86
|
+
|
|
87
|
+
Two-way sync: reads from the publisher and local assignments call `publisher.set(...)`. An internal guard avoids infinite loops.
|
|
88
|
+
|
|
89
|
+
<sonic-code language="typescript">
|
|
90
|
+
<template>
|
|
91
|
+
@bind("userData.profile.avatarUrl", { reflect: true })
|
|
92
|
+
@state()
|
|
93
|
+
avatar: string;
|
|
94
|
+
</template>
|
|
95
|
+
</sonic-code>
|
|
96
|
+
|
|
97
|
+
<sonic-code language="typescript">
|
|
98
|
+
<template>
|
|
99
|
+
@customElement("demo-bind-reflect")
|
|
100
|
+
export class DemoBindReflect extends LitElement {
|
|
101
|
+
static styles = [tailwind];
|
|
102
|
+
//
|
|
103
|
+
@bind("bindReflectDemo.count", { reflect: true })
|
|
104
|
+
@state()
|
|
105
|
+
withReflect: number = 0;
|
|
106
|
+
//
|
|
107
|
+
@bind("bindReflectDemo.count")
|
|
108
|
+
@state()
|
|
109
|
+
withoutReflect: number = 0;
|
|
110
|
+
//
|
|
111
|
+
render() {
|
|
112
|
+
return html`
|
|
113
|
+
<div class="mb-3">
|
|
114
|
+
from publisher : ${sub("bindReflectDemo.count") || 0} <br />
|
|
115
|
+
from component with reflect : ${this.withReflect || 0} <br />
|
|
116
|
+
from component without reflect : ${this.withoutReflect || 0}
|
|
117
|
+
</div>
|
|
118
|
+
<sonic-button @click=${() => this.withReflect++}
|
|
119
|
+
>Increment with reflect</sonic-button
|
|
120
|
+
>
|
|
121
|
+
<sonic-button @click=${() => this.withoutReflect++}
|
|
122
|
+
>Increment without reflect</sonic-button
|
|
123
|
+
>
|
|
124
|
+
`;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
//
|
|
128
|
+
</template>
|
|
129
|
+
</sonic-code>
|
|
130
|
+
|
|
131
|
+
<sonic-code toggleCode>
|
|
132
|
+
<template>
|
|
133
|
+
<demo-bind-reflect></demo-bind-reflect>
|
|
134
|
+
</template>
|
|
135
|
+
</sonic-code>
|
|
136
|
+
|
|
137
|
+
## Path syntax
|
|
138
|
+
|
|
139
|
+
- First segment: data provider id.
|
|
140
|
+
- Nested properties: dot notation.
|
|
141
|
+
- Arrays: numeric index (`items.0`).
|
|
142
|
+
|
|
143
|
+
### Dynamic paths
|
|
144
|
+
|
|
145
|
+
Use `${prop}` or `${this.prop}` inside a **normal string literal** (not a JS template literal with backticks). `@bind` re-subscribes when a reactive dependency changes.
|
|
146
|
+
|
|
147
|
+
> Properties referenced in the pattern must be reactive (`@property`, etc.) or you must call `requestUpdate` manually.
|
|
148
|
+
|
|
149
|
+
<sonic-code>
|
|
150
|
+
<template>
|
|
151
|
+
<demo-bind-dynamic></demo-bind-dynamic>
|
|
152
|
+
</template>
|
|
153
|
+
</sonic-code>
|
|
154
|
+
|
|
155
|
+
## Behavior
|
|
156
|
+
|
|
157
|
+
- Subscribes at `connectedCallback`, unsubscribes at `disconnectedCallback`.
|
|
158
|
+
- If the path does not exist yet, a publisher may be created with `null`.
|
|
159
|
+
|
|
160
|
+
## Notes
|
|
161
|
+
|
|
162
|
+
Works with any component that has the usual DOM lifecycle (`LitElement`, `Subscriber` mixin, etc.).
|
|
163
|
+
|
|
164
|
+
Shared data: [Sharing data](#docs/_getting-started/pubsub.md/pubsub).
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# @get
|
|
2
|
+
|
|
3
|
+
Loads data through [`API.getDetailed`](../../core/utils/api.ts). The decorated property is **`ApiGetResult<T> | null`**: `request`, `response` (or `null` for `dataProvider(...)` resolution without HTTP), and typed `result`.
|
|
4
|
+
|
|
5
|
+
Pass an [`Endpoint<T>`](#docs/_misc/endpoint.md/endpoint) as the first argument. Import `get` and `ApiGetResult` from `@supersoniks/concorde/decorators`, and `Endpoint` from `@supersoniks/concorde/utils/endpoint`.
|
|
6
|
+
|
|
7
|
+
## Configuration
|
|
8
|
+
|
|
9
|
+
- **Default:** `HTML.getApiConfiguration(host)` (ancestor `serviceURL`, etc.).
|
|
10
|
+
- **Second argument:** `DataProviderKey<APIConfiguration>` — config is read from the publisher at the resolved path; internal mutations trigger another GET.
|
|
11
|
+
|
|
12
|
+
## When the GET runs again
|
|
13
|
+
|
|
14
|
+
- A referenced Lit property changes (endpoint path and/or config key contains `${...}`).
|
|
15
|
+
- `set` on the active configuration publisher (`onInternalMutation`).
|
|
16
|
+
|
|
17
|
+
## Import
|
|
18
|
+
|
|
19
|
+
<sonic-code language="typescript">
|
|
20
|
+
<template>
|
|
21
|
+
import { get, type ApiGetResult } from "@supersoniks/concorde/decorators";
|
|
22
|
+
import { Endpoint } from "@supersoniks/concorde/utils/endpoint";
|
|
23
|
+
import { DataProviderKey } from "@supersoniks/concorde/dataProviderKey";
|
|
24
|
+
</template>
|
|
25
|
+
</sonic-code>
|
|
26
|
+
|
|
27
|
+
## Minimal example
|
|
28
|
+
|
|
29
|
+
Same demo service as [`sonic-queue`](../../core/components/functional/queue/queue.demo.ts) (`geo.api.gouv.fr`). Publisher setup lives in `decorators-demo-geo.ts` and `decorators-demo-subscribe-publish-get-demos.ts`.
|
|
30
|
+
|
|
31
|
+
<sonic-code language="typescript">
|
|
32
|
+
<template>
|
|
33
|
+
@get(new Endpoint<User>("users/${userId}"))
|
|
34
|
+
@state()
|
|
35
|
+
payload: ApiGetResult<User> | null = null;
|
|
36
|
+
</template>
|
|
37
|
+
</sonic-code>
|
|
38
|
+
|
|
39
|
+
## Live demos
|
|
40
|
+
|
|
41
|
+
<sonic-code>
|
|
42
|
+
<template>
|
|
43
|
+
<demo-api-get></demo-api-get>
|
|
44
|
+
</template>
|
|
45
|
+
</sonic-code>
|
|
46
|
+
|
|
47
|
+
Dynamic config and endpoint path (`demo-api-get-configuration-key` in doc sources):
|
|
48
|
+
|
|
49
|
+
<sonic-code>
|
|
50
|
+
<template>
|
|
51
|
+
<demo-api-get-configuration-key></demo-api-get-configuration-key>
|
|
52
|
+
</template>
|
|
53
|
+
</sonic-code>
|
|
54
|
+
|
|
55
|
+
Scoped `@get` with `@publish` / `@subscribe` on the payload (see [@publish](#docs/_decorators/publish.md/publish) and [@subscribe](#docs/_decorators/subscribe.md/subscribe)) — wrap under an ancestor with `serviceURL="https://geo.api.gouv.fr/"`:
|
|
56
|
+
|
|
57
|
+
<sonic-code>
|
|
58
|
+
<template>
|
|
59
|
+
<div serviceURL="https://geo.api.gouv.fr/">
|
|
60
|
+
<demo-api-get-publish-subscribe></demo-api-get-publish-subscribe>
|
|
61
|
+
</div>
|
|
62
|
+
</template>
|
|
63
|
+
</sonic-code>
|
|
64
|
+
|
|
65
|
+
Stale responses are ignored if the path or generation changed before the request finished.
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# @publish
|
|
2
|
+
|
|
3
|
+
Write-only binding: assigning to the property publishes to the `DataProviderKey` path. No read subscription (inverse of [@subscribe](#docs/_decorators/subscribe.md/subscribe)).
|
|
4
|
+
|
|
5
|
+
Similar to the “reflect” half of [@bind](#docs/_decorators/bind.md/bind) without listening to the publisher.
|
|
6
|
+
|
|
7
|
+
## Import
|
|
8
|
+
|
|
9
|
+
<sonic-code language="typescript">
|
|
10
|
+
<template>
|
|
11
|
+
import { publish } from "@supersoniks/concorde/decorators";
|
|
12
|
+
import { sub } from "@supersoniks/concorde/directives";
|
|
13
|
+
import { DataProviderKey } from "@supersoniks/concorde/dataProviderKey";
|
|
14
|
+
</template>
|
|
15
|
+
</sonic-code>
|
|
16
|
+
|
|
17
|
+
## Example
|
|
18
|
+
|
|
19
|
+
<sonic-code language="typescript">
|
|
20
|
+
<template>
|
|
21
|
+
type PublishDemoData = { email: string; message: string };
|
|
22
|
+
const publishDemoKey = new DataProviderKey<PublishDemoData>("publishDemo");
|
|
23
|
+
//
|
|
24
|
+
@customElement("demo-publish")
|
|
25
|
+
export class DemoPublish extends LitElement {
|
|
26
|
+
@publish(publishDemoKey.email)
|
|
27
|
+
@state()
|
|
28
|
+
email = "";
|
|
29
|
+
//
|
|
30
|
+
@publish(publishDemoKey.message)
|
|
31
|
+
@state()
|
|
32
|
+
message = "";
|
|
33
|
+
//
|
|
34
|
+
render() {
|
|
35
|
+
return html`
|
|
36
|
+
<sonic-input
|
|
37
|
+
.value=${this.email}
|
|
38
|
+
@input=${(e) => (this.email = (e.target as HTMLInputElement).value)}
|
|
39
|
+
label="Email"
|
|
40
|
+
></sonic-input>
|
|
41
|
+
<p>${sub("publishDemo.email")}</p>
|
|
42
|
+
`;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
</template>
|
|
46
|
+
</sonic-code>
|
|
47
|
+
|
|
48
|
+
<sonic-code>
|
|
49
|
+
<template>
|
|
50
|
+
<demo-publish></demo-publish>
|
|
51
|
+
</template>
|
|
52
|
+
</sonic-code>
|
|
53
|
+
|
|
54
|
+
Dynamic paths use the same placeholder rules as `@bind` / `@subscribe`.
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# @subscribe
|
|
2
|
+
|
|
3
|
+
Read-only binding: **only** `DataProviderKey<T>` (no legacy string path). No `reflect` option — the publisher updates the property, not the other way around.
|
|
4
|
+
|
|
5
|
+
For bidirectional binding or string paths, use [@bind](#docs/_decorators/bind.md/bind).
|
|
6
|
+
|
|
7
|
+
## Import
|
|
8
|
+
|
|
9
|
+
<sonic-code language="typescript">
|
|
10
|
+
<template>
|
|
11
|
+
import { subscribe } from "@supersoniks/concorde/decorators";
|
|
12
|
+
import { DataProviderKey } from "@supersoniks/concorde/dataProviderKey";
|
|
13
|
+
//
|
|
14
|
+
type Data = { count: number };
|
|
15
|
+
const dataKey = new DataProviderKey<Data>("data");
|
|
16
|
+
//
|
|
17
|
+
@subscribe(dataKey.count)
|
|
18
|
+
@state()
|
|
19
|
+
count = 0;
|
|
20
|
+
</template>
|
|
21
|
+
</sonic-code>
|
|
22
|
+
|
|
23
|
+
## Highlights
|
|
24
|
+
|
|
25
|
+
- Strict typing: the property type must match `T`.
|
|
26
|
+
- Dynamic paths: placeholders in `DataProviderKey`, resolved from the host component’s properties.
|
|
27
|
+
|
|
28
|
+
## Demo
|
|
29
|
+
|
|
30
|
+
<sonic-code>
|
|
31
|
+
<template>
|
|
32
|
+
<demo-subscribe-dynamic></demo-subscribe-dynamic>
|
|
33
|
+
</template>
|
|
34
|
+
</sonic-code>
|
|
35
|
+
|
|
36
|
+
See also [DataProviderKey](#docs/_misc/dataProviderKey.md/dataProviderKey).
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
# DataProviderKey
|
|
2
|
+
|
|
3
|
+
The `DataProviderKey<T>` utility provides type-safe navigation through composite data structures. Each property or index access extends the path, and the final key can be retrieved via `toString()` or the `path` property.
|
|
4
|
+
|
|
5
|
+
For a **single HTTP path string** (no dot-syntax), see [Endpoint](#docs/_misc/endpoint.md/endpoint).
|
|
6
|
+
|
|
7
|
+
## Principle
|
|
8
|
+
|
|
9
|
+
`DataProviderKey` uses a Proxy to intercept property access and build a cumulative path string. TypeScript infers the nested type at each level, so `myKey.items[0]` is correctly typed as `DataProviderKey<Item>` when `items` is `Item[]`.
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Import
|
|
14
|
+
|
|
15
|
+
<sonic-code language="typescript">
|
|
16
|
+
<template>
|
|
17
|
+
import { DataProviderKey } from "@supersoniks/concorde/dataProviderKey";
|
|
18
|
+
</template>
|
|
19
|
+
</sonic-code>
|
|
20
|
+
|
|
21
|
+
### Basic example
|
|
22
|
+
|
|
23
|
+
<sonic-code language="typescript">
|
|
24
|
+
<template>
|
|
25
|
+
type Item = { id: string; name: string };
|
|
26
|
+
//
|
|
27
|
+
type Data = {
|
|
28
|
+
items: Item[];
|
|
29
|
+
count: number;
|
|
30
|
+
};
|
|
31
|
+
//
|
|
32
|
+
const myKey = new DataProviderKey<Data>("data").items[0];
|
|
33
|
+
// Equivalent to: new DataProviderKey<Item>("data.items.0")
|
|
34
|
+
myKey.toString(); // "data.items.0"
|
|
35
|
+
myKey.path; // same value
|
|
36
|
+
// myKey is typed as DataProviderKey<Item>
|
|
37
|
+
</template>
|
|
38
|
+
</sonic-code>
|
|
39
|
+
|
|
40
|
+
### Object property access
|
|
41
|
+
|
|
42
|
+
<sonic-code language="typescript">
|
|
43
|
+
<template>
|
|
44
|
+
const key = new DataProviderKey<Data>("data");
|
|
45
|
+
const countKey = key.count;
|
|
46
|
+
countKey.path; // "data.count"
|
|
47
|
+
countKey.toString(); // "data.count"
|
|
48
|
+
</template>
|
|
49
|
+
</sonic-code>
|
|
50
|
+
|
|
51
|
+
### Array index access
|
|
52
|
+
|
|
53
|
+
<sonic-code language="typescript">
|
|
54
|
+
<template>
|
|
55
|
+
const itemsKey = new DataProviderKey<Data>("data").items;
|
|
56
|
+
itemsKey.path; // "data.items"
|
|
57
|
+
// itemsKey is DataProviderKey<Item[]>
|
|
58
|
+
//
|
|
59
|
+
const firstItem = itemsKey[0];
|
|
60
|
+
firstItem.path; // "data.items.0"
|
|
61
|
+
// firstItem is DataProviderKey<Item>
|
|
62
|
+
</template>
|
|
63
|
+
</sonic-code>
|
|
64
|
+
|
|
65
|
+
### Dynamic paths
|
|
66
|
+
|
|
67
|
+
Use placeholders `${prop}` or `{$prop}` in the path string. The path is resolved at runtime from the component's properties. The type remains declarative:
|
|
68
|
+
|
|
69
|
+
<sonic-code language="typescript">
|
|
70
|
+
<template>
|
|
71
|
+
type User = { name: string; email: string };
|
|
72
|
+
//
|
|
73
|
+
// Path resolved from component.userIndex at runtime
|
|
74
|
+
@subscribe(new DataProviderKey<User>("users.${userIndex}"))
|
|
75
|
+
@state()
|
|
76
|
+
user: User | null = null;
|
|
77
|
+
</template>
|
|
78
|
+
</sonic-code>
|
|
79
|
+
|
|
80
|
+
## Path retrieval
|
|
81
|
+
|
|
82
|
+
The final path is built by concatenating each accessed property with a dot:
|
|
83
|
+
|
|
84
|
+
- `new DataProviderKey<T>("base")` → `"base"`
|
|
85
|
+
- `key.prop` → `"base.prop"`
|
|
86
|
+
- `key.items[0]` → `"base.items.0"`
|
|
87
|
+
|
|
88
|
+
Use `toString()` or `path` to get the full path string:
|
|
89
|
+
|
|
90
|
+
<sonic-code language="typescript">
|
|
91
|
+
<template>
|
|
92
|
+
const key = new DataProviderKey<Data>("data").count;
|
|
93
|
+
const pathString = key.toString(); // "data.count"
|
|
94
|
+
const pathProp = key.path; // "data.count"
|
|
95
|
+
</template>
|
|
96
|
+
</sonic-code>
|
|
97
|
+
|
|
98
|
+
## Use cases
|
|
99
|
+
|
|
100
|
+
- **Type-safe bindings**: paths for `@bind`, `@subscribe`, `@publish`
|
|
101
|
+
- **Dynamic paths**: reusable keys with `${...}` placeholders
|
|
102
|
+
- **Form fields**: form data paths with compile-time checking
|
|
103
|
+
|
|
104
|
+
## Integration with @subscribe and @publish
|
|
105
|
+
|
|
106
|
+
Use `DataProviderKey` with `@subscribe` (read-only) or `@publish` (write-only). The decorated property **must** match the key’s value type:
|
|
107
|
+
|
|
108
|
+
<sonic-code language="typescript">
|
|
109
|
+
<template>
|
|
110
|
+
import { subscribe } from "@supersoniks/concorde/decorators";
|
|
111
|
+
import { DataProviderKey } from "@supersoniks/concorde/dataProviderKey";
|
|
112
|
+
//
|
|
113
|
+
type FormData = { email: string };
|
|
114
|
+
const formKey = new DataProviderKey<FormData>("formData");
|
|
115
|
+
//
|
|
116
|
+
@customElement("user-form")
|
|
117
|
+
export class UserForm extends LitElement {
|
|
118
|
+
@subscribe(formKey.email)
|
|
119
|
+
@state()
|
|
120
|
+
email = "";
|
|
121
|
+
//
|
|
122
|
+
render() {
|
|
123
|
+
return html`<input .value=${this.email} @input=${(e) => this.email = (e.target as HTMLInputElement).value}>`;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
</template>
|
|
127
|
+
</sonic-code>
|
|
128
|
+
|
|
129
|
+
Both decorators support dynamic paths: `"base.${prop}"` in the constructor. A wrong property type (e.g. `number` for `DataProviderKey<string>`) is a TypeScript error.
|
|
130
|
+
|
|
131
|
+
## Notes
|
|
132
|
+
|
|
133
|
+
- Function properties are excluded from navigation (no `key.method()` chaining)
|
|
134
|
+
- Primitives have no navigable properties
|
|
135
|
+
- The `path` property and `toString()` are equivalent for retrieving the key
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# Endpoint
|
|
2
|
+
|
|
3
|
+
`Endpoint<T, U>` describes a single HTTP path (or a path accepted by `API.get`) and carries the expected response type `T`. Unlike [DataProviderKey](#docs/_misc/dataProviderKey.md/dataProviderKey), there is no dot-navigation: the path is one string.
|
|
4
|
+
|
|
5
|
+
The optional second generic `U` (default `any`) describes host properties used to resolve dynamic segments in the path (`${…}` / `{$…}`), for example with the [@get](#docs/_decorators/get.md/get) decorator.
|
|
6
|
+
|
|
7
|
+
## Import
|
|
8
|
+
|
|
9
|
+
<sonic-code language="typescript">
|
|
10
|
+
<template>
|
|
11
|
+
import { Endpoint } from "@supersoniks/concorde/utils/endpoint";
|
|
12
|
+
</template>
|
|
13
|
+
</sonic-code>
|
|
14
|
+
|
|
15
|
+
## Construction
|
|
16
|
+
|
|
17
|
+
<sonic-code language="typescript">
|
|
18
|
+
<template>
|
|
19
|
+
const users = new Endpoint<User[]>("users?limit=10");
|
|
20
|
+
users.path; // "users?limit=10"
|
|
21
|
+
//
|
|
22
|
+
const one = new Endpoint<User, { userId: string }>("users/${userId}");
|
|
23
|
+
// `userId` on the host class is observed when used with @get
|
|
24
|
+
</template>
|
|
25
|
+
</sonic-code>
|
|
26
|
+
|
|
27
|
+
## Normalization
|
|
28
|
+
|
|
29
|
+
`Endpoint.normalizePath` trims the string, rejects an empty path, strips leading slashes for paths relative to `serviceURL`, collapses duplicate slashes, and validates absolute `http(s)://` URLs.
|
|
30
|
+
|
|
31
|
+
## Publisher key for payloads
|
|
32
|
+
|
|
33
|
+
`getDataProviderKey()` returns a typed publisher key whose `path` matches the endpoint path (payload typing follows `ApiGetResult` for this endpoint). Useful when pairing `@get` with `@publish` / `@subscribe` (see [@get](#docs/_decorators/get.md/get)).
|
|
34
|
+
|
|
35
|
+
## Data-provider paths
|
|
36
|
+
|
|
37
|
+
`Endpoint.looksLikeDataProviderPath(path)` returns true for strings shaped like `dataProvider(id)…`, which `API.get` can resolve without HTTP.
|
|
38
|
+
|
|
39
|
+
## See also
|
|
40
|
+
|
|
41
|
+
- [@get](#docs/_decorators/get.md/get) — decorator that uses `Endpoint<T>`
|
|
42
|
+
- [DataProviderKey](#docs/_misc/dataProviderKey.md/dataProviderKey) — typed publisher paths (dot notation)
|