@kottera/tera-web 0.0.0-stage → 0.1.0-rc.2
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/LICENSE +201 -0
- package/NOTICE +28 -0
- package/README.md +37 -2
- package/dist/client.d.ts +138 -0
- package/dist/client.js +250 -0
- package/dist/feed/connection.d.ts +158 -0
- package/dist/feed/connection.js +427 -0
- package/dist/feed/sse.d.ts +51 -0
- package/dist/feed/sse.js +134 -0
- package/dist/generated/feed.d.ts +199 -0
- package/dist/generated/feed.js +16 -0
- package/dist/generated/kit.d.ts +246 -0
- package/dist/generated/kit.js +148 -0
- package/dist/generated/workorder-events.d.ts +47 -0
- package/dist/generated/workorder-events.js +10 -0
- package/dist/generated/workorder.d.ts +3522 -0
- package/dist/generated/workorder.js +74 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +12 -0
- package/dist/routes.d.ts +10 -0
- package/dist/routes.js +32 -0
- package/package.json +32 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Kottera
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
Kottera SDK
|
|
2
|
+
Copyright 2026 Kottera
|
|
3
|
+
|
|
4
|
+
This product includes software developed by Kottera.
|
|
5
|
+
|
|
6
|
+
The Kottera SDK (github.com/kottera/sdk) is the public, stable surface of the
|
|
7
|
+
Kottera platform: the capability contract and schema, the tool SDK, the connector
|
|
8
|
+
SDK contract and the standalone connector-authoring SDK, credential specifications
|
|
9
|
+
and keychain resolution, the KPP provider-protocol handshake and its env/conn
|
|
10
|
+
dialect frame schemas, and the sandbox/telemetry OTLP-JSON frame schemas
|
|
11
|
+
(ADR-059 Part 1, surfaces 1-4). It is licensed under the Apache License, Version
|
|
12
|
+
2.0; see the LICENSE file in this directory.
|
|
13
|
+
|
|
14
|
+
This SDK module MUST NOT import any github.com/kottera/kottera/... package. The
|
|
15
|
+
Kottera daemon (`tera`) is a separate, private-source work and is NOT licensed
|
|
16
|
+
under this Apache License; it is distributed as a free-to-use binary governed by
|
|
17
|
+
the Kottera End User License Agreement that accompanies each release.
|
|
18
|
+
|
|
19
|
+
----------------------------------------------------------------------------------
|
|
20
|
+
Third-party components
|
|
21
|
+
----------------------------------------------------------------------------------
|
|
22
|
+
|
|
23
|
+
This module depends on third-party Go packages resolved via go.mod / go.sum. Their
|
|
24
|
+
copyrights and licenses are held by their respective authors and are reproduced in
|
|
25
|
+
the third-party license inventory bundled with each release (the `licenses/`
|
|
26
|
+
directory produced by the release SBOM tooling). No third-party dependency of this
|
|
27
|
+
module is licensed under a copyleft license (GPL/LGPL/AGPL); all are permissive
|
|
28
|
+
(Apache-2.0, MIT, BSD-2-Clause, or BSD-3-Clause).
|
package/README.md
CHANGED
|
@@ -1,3 +1,38 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @kottera/tera-web
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The browser half of a UI over tera ([ADR-162](../../docs/adr/ADR-162-a-ui-over-tera.md)
|
|
4
|
+
D4): a framework-free TypeScript client for the `/tera/v1/` routes a plugin's
|
|
5
|
+
webui kit serves (D3). It carries no UI component — the framework is the
|
|
6
|
+
author's — and has no runtime dependencies. It is not yet published.
|
|
7
|
+
|
|
8
|
+
## Layout
|
|
9
|
+
|
|
10
|
+
| Path | What it holds |
|
|
11
|
+
|------|---------------|
|
|
12
|
+
| `src/index.ts` | The package's one entry point. Everything public is re-exported here. |
|
|
13
|
+
| `src/routes.ts` | The route prefix and `routePath`. |
|
|
14
|
+
| `src/client.ts` | `TeraClient`, the typed client over D3's routes (D4 items 1, 3 and 4): `me()`, `invoke(capability, params)`, `feed(options)` (a `FeedConnection` read with the client's fetch), `upload(file, options)` with progress, and `signOut()`. A kit refusal throws `KitRequestError`; a verb's refusal answers as its verdict. |
|
|
15
|
+
| `src/generated/` | The contract, generated from the committed schemas under `sdk/plugin/webui/schema/`, `sdk/workorder/schema/` and `sdk/feed/schema/` (D4 item 1): one type per schema, the kit's route table as `KIT_ROUTE_TABLE`, and the maps from a route or a feed event to the type it carries. Never hand-edited, and never hand-write a type it declares: `make web-types` regenerates it, and `TestWebTypes_MatchCommitted` (`cmd/internal/genwebtypes`) fails on a hand edit or a stale generation. |
|
|
16
|
+
| `src/feed/` | The stream connection (D4 item 2). |
|
|
17
|
+
| `src/**/*.test.ts` | The suite, beside what it tests. |
|
|
18
|
+
|
|
19
|
+
## Tests
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
make test-web-sdk
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
runs `node --test` inside this directory on the host's `node`, at or above
|
|
26
|
+
`package.json`'s `engines.node`. Nothing is installed: the tests use only
|
|
27
|
+
`node:test` and `node:assert`, and node strips the sources' types itself, so
|
|
28
|
+
write only erasable TypeScript (no `enum`, no `namespace`, no parameter
|
|
29
|
+
properties) and import siblings by their `.ts` name. The gate runs the same
|
|
30
|
+
suite for any diff under `sdk/web/`
|
|
31
|
+
([`docs/testing-infrastructure.md`](../../docs/testing-infrastructure.md),
|
|
32
|
+
"The browser package").
|
|
33
|
+
|
|
34
|
+
The gate does not type-check: `tsc` would have to be fetched. `tsconfig.json`
|
|
35
|
+
is here for an author with one, `tsc -p sdk/web`. What the gate does run over
|
|
36
|
+
the generated types is `src/contract.test.ts`: every committed schema has a
|
|
37
|
+
generated type, the route table is the committed one, and a fixture of each
|
|
38
|
+
D3 shape, annotated with its generated type, is valid against its schema.
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { FeedConnection, type FeedConnectionOptions } from "./feed/connection.ts";
|
|
2
|
+
import type { KitError, KitInvokeResponse, KitMeResponse, KitUploadResponse } from "./generated/kit.ts";
|
|
3
|
+
/** The body of a kit failure: the envelope's `error` member. */
|
|
4
|
+
export type KitErrorBody = KitError["error"];
|
|
5
|
+
/** A verb's refusal, as the kit passed it through: the reply envelope's `error` member. */
|
|
6
|
+
export type VerbRefusal = NonNullable<KitInvokeResponse["error"]>;
|
|
7
|
+
/**
|
|
8
|
+
* What {@link TeraClient.invoke} answers when the verb ran: its result, or its
|
|
9
|
+
* refusal. `Result` is the verb's result type — for a Work Order verb, the
|
|
10
|
+
* generated `WorkOrder<Verb>Result`.
|
|
11
|
+
*/
|
|
12
|
+
export type InvokeVerdict<Result = unknown> = {
|
|
13
|
+
readonly status: "success";
|
|
14
|
+
readonly result: Result;
|
|
15
|
+
} | {
|
|
16
|
+
readonly status: "error";
|
|
17
|
+
readonly error: VerbRefusal;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* The `code` of a {@link KitRequestError} whose answer was not the kit's
|
|
21
|
+
* envelope — a proxy's page, a dropped body, a success that is not the
|
|
22
|
+
* route's JSON. It is this package's, never one the kit sends.
|
|
23
|
+
*/
|
|
24
|
+
export declare const UNRECOGNIZED_RESPONSE = "unrecognized_response";
|
|
25
|
+
/**
|
|
26
|
+
* A kit route that did not answer as the route: the kit's own refusal, or
|
|
27
|
+
* something that was not the route's answer at all.
|
|
28
|
+
*/
|
|
29
|
+
export declare class KitRequestError extends Error {
|
|
30
|
+
/** The HTTP status the route answered with. */
|
|
31
|
+
readonly status: number;
|
|
32
|
+
/**
|
|
33
|
+
* The envelope's code, as it arrived — one of the kit's `KitErrorCode`s, or
|
|
34
|
+
* a code this package does not know, kept as it is — or
|
|
35
|
+
* {@link UNRECOGNIZED_RESPONSE} when there was no envelope.
|
|
36
|
+
*/
|
|
37
|
+
readonly code: string;
|
|
38
|
+
/** Whether the identical request may succeed if sent again later. Branch on this, never on a list of codes. */
|
|
39
|
+
readonly retryable: boolean;
|
|
40
|
+
/** How long to wait before retrying, when the envelope said. */
|
|
41
|
+
readonly retryAfterMs: number | undefined;
|
|
42
|
+
/** The envelope's structured detail, when it carried any. */
|
|
43
|
+
readonly data: unknown;
|
|
44
|
+
/** The envelope as it arrived, or null when the answer carried none. */
|
|
45
|
+
readonly envelope: KitErrorBody | null;
|
|
46
|
+
constructor(status: number, envelope: KitErrorBody | null, fallback: string);
|
|
47
|
+
}
|
|
48
|
+
/** The progress of one upload's body. */
|
|
49
|
+
export interface UploadProgress {
|
|
50
|
+
/** Bytes sent so far. */
|
|
51
|
+
readonly loaded: number;
|
|
52
|
+
/** Bytes to send: the file's size. */
|
|
53
|
+
readonly total: number;
|
|
54
|
+
}
|
|
55
|
+
export interface UploadOptions {
|
|
56
|
+
/** The file's name; the `File`'s own name when absent. Required by the route: an upload with neither is refused before it is sent. */
|
|
57
|
+
filename?: string;
|
|
58
|
+
/** The file's media type; the `Blob`'s own type, or `application/octet-stream`, when absent. */
|
|
59
|
+
contentType?: string;
|
|
60
|
+
/** A description of the file, carried to the artifact. */
|
|
61
|
+
description?: string;
|
|
62
|
+
/** Called as the body is sent. */
|
|
63
|
+
onProgress?: (progress: UploadProgress) => void;
|
|
64
|
+
/** Aborting it cancels the upload, which then rejects with an `AbortError`. */
|
|
65
|
+
signal?: AbortSignal;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* The part of `XMLHttpRequest` the upload helper uses. Upload progress is the
|
|
69
|
+
* reason it is not `fetch`: a fetch reports nothing while its body is sent.
|
|
70
|
+
*/
|
|
71
|
+
export interface UploadRequest {
|
|
72
|
+
open(method: string, url: string): void;
|
|
73
|
+
setRequestHeader(name: string, value: string): void;
|
|
74
|
+
send(body: Blob): void;
|
|
75
|
+
abort(): void;
|
|
76
|
+
readonly status: number;
|
|
77
|
+
readonly responseText: string;
|
|
78
|
+
getResponseHeader(name: string): string | null;
|
|
79
|
+
withCredentials: boolean;
|
|
80
|
+
onload: ((ev: unknown) => void) | null;
|
|
81
|
+
onerror: ((ev: unknown) => void) | null;
|
|
82
|
+
onabort: ((ev: unknown) => void) | null;
|
|
83
|
+
readonly upload: {
|
|
84
|
+
onprogress: ((ev: {
|
|
85
|
+
loaded: number;
|
|
86
|
+
total: number;
|
|
87
|
+
lengthComputable: boolean;
|
|
88
|
+
}) => void) | null;
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
export interface TeraClientOptions {
|
|
92
|
+
/** The fetch every route but the upload is read with; the page's own when absent. */
|
|
93
|
+
fetch?: typeof fetch;
|
|
94
|
+
/** Makes the upload's request; `new XMLHttpRequest()` when absent. */
|
|
95
|
+
uploadRequest?: () => UploadRequest;
|
|
96
|
+
/** Sends the page to a path; `location.assign` when absent. */
|
|
97
|
+
navigate?: (path: string) => void;
|
|
98
|
+
}
|
|
99
|
+
/** The options of {@link TeraClient.feed}: a {@link FeedConnection}'s, with the opener the client's own. */
|
|
100
|
+
export type FeedOptions = Omit<FeedConnectionOptions, "open">;
|
|
101
|
+
export declare class TeraClient {
|
|
102
|
+
private readonly fetchFn;
|
|
103
|
+
private readonly uploadRequest;
|
|
104
|
+
private readonly navigate;
|
|
105
|
+
constructor(options?: TeraClientOptions);
|
|
106
|
+
/** `GET /tera/v1/me`: who is signed in, and where to send them to sign out. */
|
|
107
|
+
me(): Promise<KitMeResponse>;
|
|
108
|
+
/**
|
|
109
|
+
* `POST /tera/v1/invoke/{capability}`: invokes one capability as the
|
|
110
|
+
* signed-in person, with `params` as its params, and answers the verb's
|
|
111
|
+
* verdict. A refusal by the verb answers `{ status: "error" }`; only the
|
|
112
|
+
* kit's own refusal, or an answer that is not the route's, throws.
|
|
113
|
+
*
|
|
114
|
+
* const got = await client.invoke<WorkOrderGetResult, WorkOrderGetParams>("work_order.get", { id });
|
|
115
|
+
* if (got.status === "success") show(got.result);
|
|
116
|
+
*/
|
|
117
|
+
invoke<Result = unknown, Params = unknown>(capability: string, params?: Params): Promise<InvokeVerdict<Result>>;
|
|
118
|
+
/**
|
|
119
|
+
* A {@link FeedConnection} over `GET /tera/v1/feeds/{feed}`, read with this
|
|
120
|
+
* client's fetch. It is not started: call `start()` on it, and `stop()` when
|
|
121
|
+
* the page no longer wants it.
|
|
122
|
+
*/
|
|
123
|
+
feed(options: FeedOptions): FeedConnection;
|
|
124
|
+
/**
|
|
125
|
+
* `POST /tera/v1/uploads`: sends one file as the signed-in person and
|
|
126
|
+
* answers the staged artifact, whose `handle` a Work Order's attachments
|
|
127
|
+
* name. Its name and description travel percent-encoded (UTF-8) in the
|
|
128
|
+
* route's headers, and `onProgress` reports the body as it is sent.
|
|
129
|
+
*/
|
|
130
|
+
upload(file: Blob, options?: UploadOptions): Promise<KitUploadResponse>;
|
|
131
|
+
/**
|
|
132
|
+
* Signs the person out: navigates to the `signout_path` `me()` answers,
|
|
133
|
+
* tera's own path, which ends their session for every plugin. A path that
|
|
134
|
+
* is not one on this origin is refused rather than followed.
|
|
135
|
+
*/
|
|
136
|
+
signOut(): Promise<void>;
|
|
137
|
+
private json;
|
|
138
|
+
}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The typed client over D3's routes (ADR-162 D4 items 1, 3 and 4): who is
|
|
3
|
+
* signed in, invoking a capability as them, opening a feed, uploading a file
|
|
4
|
+
* with progress, and signing out.
|
|
5
|
+
*
|
|
6
|
+
* Every type here for what the contract declares — a route's answer, the
|
|
7
|
+
* error envelope, the upload's answer — is the one generated into
|
|
8
|
+
* `./generated/` from the committed schemas; none is written by hand. A
|
|
9
|
+
* Work Order verb's params and result are generated there too
|
|
10
|
+
* (`WorkOrderGetParams`, `WorkOrderGetResult`, …), and a caller names them as
|
|
11
|
+
* {@link TeraClient.invoke}'s type arguments.
|
|
12
|
+
*
|
|
13
|
+
* # Two kinds of failure, kept apart
|
|
14
|
+
*
|
|
15
|
+
* - **The kit's refusal** — no verified caller, a cross-origin write, an
|
|
16
|
+
* unknown or unserved route — and anything else that is not the route's
|
|
17
|
+
* answer, is thrown as a {@link KitRequestError}, carrying the kit's
|
|
18
|
+
* envelope (`code`, `message`, `retryable`) as it arrived.
|
|
19
|
+
* - **A verb's refusal** is not a failure of the route. The verb ran and said
|
|
20
|
+
* no; the kit passes its verdict through with HTTP 200 (D3), and
|
|
21
|
+
* {@link TeraClient.invoke} answers it as that verdict, `{ status: "error",
|
|
22
|
+
* error }`, beside `{ status: "success", result }`.
|
|
23
|
+
*
|
|
24
|
+
* # Same origin
|
|
25
|
+
*
|
|
26
|
+
* Every request is to a path under the kit's prefix on the page's own origin,
|
|
27
|
+
* with `credentials: "same-origin"`; a fetch is also `mode: "same-origin"`, so
|
|
28
|
+
* a page that somehow aimed one elsewhere is refused by the browser rather
|
|
29
|
+
* than sent. The browser then marks it `Sec-Fetch-Site: same-origin`, which is
|
|
30
|
+
* what the kit's same-origin rule for writes admits (D3). No header here
|
|
31
|
+
* claims it: a page cannot set `Sec-Fetch-*` or `Origin`, and the rule rests
|
|
32
|
+
* on that.
|
|
33
|
+
*
|
|
34
|
+
* It is a plain class with an injected fetch, request factory and navigation,
|
|
35
|
+
* so its tests drive it against a fake and no network.
|
|
36
|
+
*/
|
|
37
|
+
import { routePath } from "./routes.js";
|
|
38
|
+
import { FeedConnection } from "./feed/connection.js";
|
|
39
|
+
import { fetchOpener } from "./feed/sse.js";
|
|
40
|
+
/**
|
|
41
|
+
* The `code` of a {@link KitRequestError} whose answer was not the kit's
|
|
42
|
+
* envelope — a proxy's page, a dropped body, a success that is not the
|
|
43
|
+
* route's JSON. It is this package's, never one the kit sends.
|
|
44
|
+
*/
|
|
45
|
+
export const UNRECOGNIZED_RESPONSE = "unrecognized_response";
|
|
46
|
+
/**
|
|
47
|
+
* A kit route that did not answer as the route: the kit's own refusal, or
|
|
48
|
+
* something that was not the route's answer at all.
|
|
49
|
+
*/
|
|
50
|
+
export class KitRequestError extends Error {
|
|
51
|
+
/** The HTTP status the route answered with. */
|
|
52
|
+
status;
|
|
53
|
+
/**
|
|
54
|
+
* The envelope's code, as it arrived — one of the kit's `KitErrorCode`s, or
|
|
55
|
+
* a code this package does not know, kept as it is — or
|
|
56
|
+
* {@link UNRECOGNIZED_RESPONSE} when there was no envelope.
|
|
57
|
+
*/
|
|
58
|
+
code;
|
|
59
|
+
/** Whether the identical request may succeed if sent again later. Branch on this, never on a list of codes. */
|
|
60
|
+
retryable;
|
|
61
|
+
/** How long to wait before retrying, when the envelope said. */
|
|
62
|
+
retryAfterMs;
|
|
63
|
+
/** The envelope's structured detail, when it carried any. */
|
|
64
|
+
data;
|
|
65
|
+
/** The envelope as it arrived, or null when the answer carried none. */
|
|
66
|
+
envelope;
|
|
67
|
+
constructor(status, envelope, fallback) {
|
|
68
|
+
super(envelope?.message ?? fallback);
|
|
69
|
+
this.name = "KitRequestError";
|
|
70
|
+
this.status = status;
|
|
71
|
+
this.envelope = envelope;
|
|
72
|
+
this.code = envelope?.code ?? UNRECOGNIZED_RESPONSE;
|
|
73
|
+
// Without an envelope only the status speaks: a 5xx, a 408 or a 429 says "later".
|
|
74
|
+
this.retryable = envelope?.retryable ?? (status >= 500 || status === 408 || status === 429);
|
|
75
|
+
this.retryAfterMs = envelope?.retry_after_ms;
|
|
76
|
+
this.data = envelope?.data;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
const JSON_TYPE = "application/json";
|
|
80
|
+
export class TeraClient {
|
|
81
|
+
fetchFn;
|
|
82
|
+
uploadRequest;
|
|
83
|
+
navigate;
|
|
84
|
+
constructor(options = {}) {
|
|
85
|
+
this.fetchFn = options.fetch ?? ((...args) => globalThis.fetch(...args));
|
|
86
|
+
this.uploadRequest = options.uploadRequest ?? (() => new XMLHttpRequest());
|
|
87
|
+
this.navigate = options.navigate ?? ((path) => globalThis.location.assign(path));
|
|
88
|
+
}
|
|
89
|
+
/** `GET /tera/v1/me`: who is signed in, and where to send them to sign out. */
|
|
90
|
+
async me() {
|
|
91
|
+
return this.json(routePath("me"), { method: "GET" });
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* `POST /tera/v1/invoke/{capability}`: invokes one capability as the
|
|
95
|
+
* signed-in person, with `params` as its params, and answers the verb's
|
|
96
|
+
* verdict. A refusal by the verb answers `{ status: "error" }`; only the
|
|
97
|
+
* kit's own refusal, or an answer that is not the route's, throws.
|
|
98
|
+
*
|
|
99
|
+
* const got = await client.invoke<WorkOrderGetResult, WorkOrderGetParams>("work_order.get", { id });
|
|
100
|
+
* if (got.status === "success") show(got.result);
|
|
101
|
+
*/
|
|
102
|
+
async invoke(capability, params) {
|
|
103
|
+
const url = routePath("invoke", capability);
|
|
104
|
+
const reply = await this.json(url, {
|
|
105
|
+
method: "POST",
|
|
106
|
+
headers: { "Content-Type": JSON_TYPE },
|
|
107
|
+
body: JSON.stringify(params ?? {}),
|
|
108
|
+
});
|
|
109
|
+
if (reply.status === "success" && reply.result !== undefined) {
|
|
110
|
+
return { status: "success", result: reply.result.structured };
|
|
111
|
+
}
|
|
112
|
+
if (reply.status === "error" && reply.error !== undefined) {
|
|
113
|
+
return { status: "error", error: reply.error };
|
|
114
|
+
}
|
|
115
|
+
throw new KitRequestError(200, null, `${url} answered a reply envelope with no ${reply.status === "error" ? "error" : "result"}`);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* A {@link FeedConnection} over `GET /tera/v1/feeds/{feed}`, read with this
|
|
119
|
+
* client's fetch. It is not started: call `start()` on it, and `stop()` when
|
|
120
|
+
* the page no longer wants it.
|
|
121
|
+
*/
|
|
122
|
+
feed(options) {
|
|
123
|
+
return new FeedConnection({ ...options, open: fetchOpener(this.fetchFn) });
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* `POST /tera/v1/uploads`: sends one file as the signed-in person and
|
|
127
|
+
* answers the staged artifact, whose `handle` a Work Order's attachments
|
|
128
|
+
* name. Its name and description travel percent-encoded (UTF-8) in the
|
|
129
|
+
* route's headers, and `onProgress` reports the body as it is sent.
|
|
130
|
+
*/
|
|
131
|
+
upload(file, options = {}) {
|
|
132
|
+
const filename = options.filename ?? file.name ?? "";
|
|
133
|
+
if (filename === "") {
|
|
134
|
+
return Promise.reject(new TypeError("upload: a filename is required: pass a File, or options.filename"));
|
|
135
|
+
}
|
|
136
|
+
const contentType = options.contentType || file.type || "application/octet-stream";
|
|
137
|
+
const url = routePath("uploads");
|
|
138
|
+
const { signal, onProgress } = options;
|
|
139
|
+
if (signal?.aborted)
|
|
140
|
+
return Promise.reject(abortError());
|
|
141
|
+
return new Promise((resolve, reject) => {
|
|
142
|
+
const xhr = this.uploadRequest();
|
|
143
|
+
const onAbort = () => xhr.abort();
|
|
144
|
+
const settle = () => signal?.removeEventListener("abort", onAbort);
|
|
145
|
+
xhr.open("POST", url);
|
|
146
|
+
// Same origin sends the page's cookies; it never needs withCredentials.
|
|
147
|
+
xhr.withCredentials = false;
|
|
148
|
+
xhr.setRequestHeader("Content-Type", contentType);
|
|
149
|
+
xhr.setRequestHeader("Tera-Upload-Filename", encodeURIComponent(filename));
|
|
150
|
+
if (options.description !== undefined && options.description !== "") {
|
|
151
|
+
xhr.setRequestHeader("Tera-Upload-Description", encodeURIComponent(options.description));
|
|
152
|
+
}
|
|
153
|
+
xhr.setRequestHeader("Accept", JSON_TYPE);
|
|
154
|
+
if (onProgress !== undefined) {
|
|
155
|
+
xhr.upload.onprogress = (ev) => onProgress({ loaded: ev.loaded, total: ev.lengthComputable ? ev.total : file.size });
|
|
156
|
+
}
|
|
157
|
+
xhr.onload = () => {
|
|
158
|
+
settle();
|
|
159
|
+
try {
|
|
160
|
+
resolve(answer(url, xhr.status, xhr.getResponseHeader("content-type") ?? "", xhr.responseText));
|
|
161
|
+
}
|
|
162
|
+
catch (err) {
|
|
163
|
+
reject(err);
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
xhr.onerror = () => {
|
|
167
|
+
settle();
|
|
168
|
+
reject(new TypeError(`${url}: the upload failed with no answer`));
|
|
169
|
+
};
|
|
170
|
+
xhr.onabort = () => {
|
|
171
|
+
settle();
|
|
172
|
+
reject(abortError());
|
|
173
|
+
};
|
|
174
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
175
|
+
try {
|
|
176
|
+
xhr.send(file);
|
|
177
|
+
}
|
|
178
|
+
catch (err) {
|
|
179
|
+
settle();
|
|
180
|
+
reject(err);
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Signs the person out: navigates to the `signout_path` `me()` answers,
|
|
186
|
+
* tera's own path, which ends their session for every plugin. A path that
|
|
187
|
+
* is not one on this origin is refused rather than followed.
|
|
188
|
+
*/
|
|
189
|
+
async signOut() {
|
|
190
|
+
const { signout_path: path } = await this.me();
|
|
191
|
+
if (typeof path !== "string" || !path.startsWith("/") || path.startsWith("//") || path.startsWith("/\\")) {
|
|
192
|
+
throw new KitRequestError(200, null, `${routePath("me")} answered a signout_path that is not a path on this origin: ${JSON.stringify(path)}`);
|
|
193
|
+
}
|
|
194
|
+
this.navigate(path);
|
|
195
|
+
}
|
|
196
|
+
async json(url, init) {
|
|
197
|
+
const response = await this.fetchFn(url, {
|
|
198
|
+
...init,
|
|
199
|
+
headers: { Accept: JSON_TYPE, ...init.headers },
|
|
200
|
+
credentials: "same-origin",
|
|
201
|
+
mode: "same-origin",
|
|
202
|
+
cache: "no-store",
|
|
203
|
+
});
|
|
204
|
+
let text = "";
|
|
205
|
+
try {
|
|
206
|
+
text = await response.text();
|
|
207
|
+
}
|
|
208
|
+
catch {
|
|
209
|
+
// An unread body is read as no envelope: the status decides.
|
|
210
|
+
}
|
|
211
|
+
return answer(url, response.status, response.headers.get("content-type") ?? "", text);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Reads a route's answer: on a 2xx, the JSON body the route declares; on
|
|
216
|
+
* anything else, the kit's envelope as a thrown {@link KitRequestError}.
|
|
217
|
+
*/
|
|
218
|
+
function answer(url, status, contentType, text) {
|
|
219
|
+
const isJSON = contentType.toLowerCase().split(";")[0].trim() === JSON_TYPE;
|
|
220
|
+
let body;
|
|
221
|
+
if (isJSON) {
|
|
222
|
+
try {
|
|
223
|
+
body = JSON.parse(text);
|
|
224
|
+
}
|
|
225
|
+
catch {
|
|
226
|
+
body = undefined;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
if (status >= 200 && status < 300) {
|
|
230
|
+
if (typeof body === "object" && body !== null)
|
|
231
|
+
return body;
|
|
232
|
+
throw new KitRequestError(status, null, `${url} answered HTTP ${status} with a body that is not its JSON (${contentType || "no content type"})`);
|
|
233
|
+
}
|
|
234
|
+
throw new KitRequestError(status, envelopeOf(body), `${url} answered HTTP ${status} (${contentType || "no content type"})`);
|
|
235
|
+
}
|
|
236
|
+
/** The envelope's `error` member, when `body` is a kit envelope; null otherwise. */
|
|
237
|
+
function envelopeOf(body) {
|
|
238
|
+
if (typeof body !== "object" || body === null)
|
|
239
|
+
return null;
|
|
240
|
+
const error = body.error;
|
|
241
|
+
if (typeof error !== "object" || error === null)
|
|
242
|
+
return null;
|
|
243
|
+
const e = error;
|
|
244
|
+
if (typeof e.code !== "string" || typeof e.message !== "string" || typeof e.retryable !== "boolean")
|
|
245
|
+
return null;
|
|
246
|
+
return e;
|
|
247
|
+
}
|
|
248
|
+
function abortError() {
|
|
249
|
+
return new DOMException("the upload was aborted", "AbortError");
|
|
250
|
+
}
|