@operad-io/tui 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +12 -2
- package/package.json +27 -4
- package/src/actions.ts +997 -0
- package/src/app.ts +1665 -0
- package/src/args.ts +198 -0
- package/src/autostart.ts +302 -0
- package/src/client.ts +478 -0
- package/src/discover.ts +90 -0
- package/src/events.ts +345 -0
- package/src/index.ts +11 -0
- package/src/layout.ts +260 -0
- package/src/live.ts +113 -0
- package/src/markdown.ts +85 -0
- package/src/recent.ts +40 -0
- package/src/sanitize.ts +18 -0
- package/src/state.ts +3200 -0
- package/src/theme.ts +525 -0
- package/src/themes.ts +60 -0
- package/src/view/index.ts +1204 -0
- package/src/wrap.ts +156 -0
- package/theme.schema.json +197 -0
- package/themes/LICENSE-catppuccin +21 -0
- package/themes/LICENSE-dracula +21 -0
- package/themes/LICENSE-github +21 -0
- package/themes/LICENSE-kanagawa +21 -0
- package/themes/LICENSE-nord +21 -0
- package/themes/LICENSE-one +20 -0
- package/themes/LICENSE-rose-pine +21 -0
- package/themes/LICENSE-solarized +20 -0
- package/themes/LICENSE-tokyo-night +22 -0
- package/themes/README.md +81 -0
- package/themes/catppuccin-frappe.json +19 -0
- package/themes/catppuccin-latte.json +19 -0
- package/themes/catppuccin-macchiato.json +19 -0
- package/themes/catppuccin-mocha.json +19 -0
- package/themes/dracula.json +19 -0
- package/themes/github-dark.json +19 -0
- package/themes/github-light.json +19 -0
- package/themes/kanagawa-dragon.json +19 -0
- package/themes/kanagawa-lotus.json +19 -0
- package/themes/kanagawa-wave.json +19 -0
- package/themes/nord.json +19 -0
- package/themes/one-dark.json +19 -0
- package/themes/one-light.json +19 -0
- package/themes/rose-pine-dawn.json +19 -0
- package/themes/rose-pine-main.json +19 -0
- package/themes/solarized-dark.json +19 -0
- package/themes/solarized-light.json +19 -0
- package/themes/tokyo-night-day.json +19 -0
- package/themes/tokyo-night-night.json +19 -0
- package/tui.schema.json +24 -0
package/src/client.ts
ADDED
|
@@ -0,0 +1,478 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `/v1` client every terminal screen calls. It carries one bearer token,
|
|
3
|
+
* turns each endpoint into one method, and keeps the server's own error message
|
|
4
|
+
* on a typed error. It never opens the store and knows no plugin name.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type {
|
|
8
|
+
PendingItem,
|
|
9
|
+
RunningItem,
|
|
10
|
+
ViewActionRun,
|
|
11
|
+
ViewListPage,
|
|
12
|
+
ViewObject,
|
|
13
|
+
ViewSectionContent,
|
|
14
|
+
} from "@operad-io/engine";
|
|
15
|
+
import { isRecord, messageOf } from "@operad-io/engine";
|
|
16
|
+
|
|
17
|
+
/** The fetch shape this package calls; tests pass a wrapper around the real one. */
|
|
18
|
+
export type FetchLike = (
|
|
19
|
+
input: string | URL | Request,
|
|
20
|
+
init?: RequestInit,
|
|
21
|
+
) => Promise<Response>;
|
|
22
|
+
|
|
23
|
+
/** The default fetch, so a client needs no global to be constructed. */
|
|
24
|
+
export const defaultFetch: FetchLike = (input, init) => fetch(input, init);
|
|
25
|
+
|
|
26
|
+
/** One level of the hierarchy a list asks for: the top level, or the objects under one. */
|
|
27
|
+
export interface TuiLevel {
|
|
28
|
+
readonly root?: boolean | undefined;
|
|
29
|
+
readonly parent?: string | undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** One object type a client can list. */
|
|
33
|
+
export interface TuiTypeInfo {
|
|
34
|
+
readonly type: string;
|
|
35
|
+
readonly title: string;
|
|
36
|
+
/** The plugin the type belongs to. */
|
|
37
|
+
readonly plugin?: string;
|
|
38
|
+
/** The type's filters in display order; absent when it declares none. */
|
|
39
|
+
readonly filters?: readonly { readonly name: string; readonly states?: readonly string[] }[] | undefined;
|
|
40
|
+
/** The filter a list opens on; present with `filters`. */
|
|
41
|
+
readonly defaultFilter?: string | undefined;
|
|
42
|
+
/** The object types an action that creates this type works on; absent when none does. */
|
|
43
|
+
readonly parents?: readonly string[] | undefined;
|
|
44
|
+
/**
|
|
45
|
+
* The plugin's objects belong under the settings view instead of a tab of
|
|
46
|
+
* their own; a client gives that plugin no top-bar tab.
|
|
47
|
+
*/
|
|
48
|
+
readonly settings?: boolean | undefined;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** One entry of `GET /v1/keys`. */
|
|
52
|
+
export interface TuiKeyBinding {
|
|
53
|
+
readonly key: string;
|
|
54
|
+
readonly action: string;
|
|
55
|
+
readonly source: "plugin" | "user";
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** One entry of `GET /v1/settings`. */
|
|
59
|
+
export interface TuiSetting {
|
|
60
|
+
/** `<plugin>.<path>`; a project file adds its name. */
|
|
61
|
+
readonly name: string;
|
|
62
|
+
/** `global`, or the name of the repository the setting belongs to. */
|
|
63
|
+
readonly scope: string;
|
|
64
|
+
readonly value?: unknown;
|
|
65
|
+
readonly default?: unknown;
|
|
66
|
+
readonly description?: string | undefined;
|
|
67
|
+
readonly type?: string | undefined;
|
|
68
|
+
readonly enum?: readonly (string | number | boolean)[] | undefined;
|
|
69
|
+
readonly editable: boolean;
|
|
70
|
+
readonly confirm: boolean;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** What `POST /v1/settings` carries. */
|
|
74
|
+
export interface TuiSetInput {
|
|
75
|
+
readonly name: string;
|
|
76
|
+
readonly value: unknown;
|
|
77
|
+
readonly scope?: string | undefined;
|
|
78
|
+
readonly confirmed?: boolean | undefined;
|
|
79
|
+
readonly signal?: AbortSignal | undefined;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The body of `GET /v1/types`. */
|
|
83
|
+
export interface TuiTypes {
|
|
84
|
+
readonly types: readonly TuiTypeInfo[];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** What `GET /v1/objects/<id>/sections/<section>` accepts. */
|
|
88
|
+
export interface TuiSectionOptions {
|
|
89
|
+
readonly cursor?: string | undefined;
|
|
90
|
+
/** Return only the finished content after a mark from an earlier read. */
|
|
91
|
+
readonly after?: string | undefined;
|
|
92
|
+
readonly full?: boolean | undefined;
|
|
93
|
+
/** Cancels the request; it then rejects. */
|
|
94
|
+
readonly signal?: AbortSignal | undefined;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** What `POST /v1/act` carries. Only `action` is always required. */
|
|
98
|
+
export interface TuiActInput {
|
|
99
|
+
readonly action: string;
|
|
100
|
+
readonly id?: string | undefined;
|
|
101
|
+
readonly args?: Readonly<Record<string, unknown>> | undefined;
|
|
102
|
+
readonly note?: string | undefined;
|
|
103
|
+
readonly confirmed?: boolean | undefined;
|
|
104
|
+
/** Cancels the request; it then rejects. */
|
|
105
|
+
readonly signal?: AbortSignal | undefined;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** What `POST /v1/answer` carries. */
|
|
109
|
+
export interface TuiAnswerInput {
|
|
110
|
+
readonly runId: string;
|
|
111
|
+
readonly waitId: string;
|
|
112
|
+
readonly choice: string;
|
|
113
|
+
readonly note?: string | undefined;
|
|
114
|
+
readonly confirmed?: boolean | undefined;
|
|
115
|
+
/** Cancels the request; it then rejects. */
|
|
116
|
+
readonly signal?: AbortSignal | undefined;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Options for a client: where the server is and the token it checks. */
|
|
120
|
+
export interface TuiClientOptions {
|
|
121
|
+
readonly url: string;
|
|
122
|
+
readonly token: string;
|
|
123
|
+
readonly fetch?: FetchLike | undefined;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Extra fields every TUI error may carry. */
|
|
127
|
+
export interface TuiErrorOptions {
|
|
128
|
+
readonly status?: number | undefined;
|
|
129
|
+
readonly kind?: string | undefined;
|
|
130
|
+
readonly cause?: unknown;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** The base of every error this client raises. */
|
|
134
|
+
export class TuiError extends Error {
|
|
135
|
+
readonly status: number | undefined;
|
|
136
|
+
readonly kind: string | undefined;
|
|
137
|
+
|
|
138
|
+
constructor(message: string, options: TuiErrorOptions = {}) {
|
|
139
|
+
super(
|
|
140
|
+
message,
|
|
141
|
+
options.cause === undefined ? undefined : { cause: options.cause },
|
|
142
|
+
);
|
|
143
|
+
this.name = "TuiError";
|
|
144
|
+
this.status = options.status;
|
|
145
|
+
this.kind = options.kind;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** The server refused the bearer token (`401`). */
|
|
150
|
+
export class TuiUnauthorizedError extends TuiError {
|
|
151
|
+
constructor(kind: string | undefined, message: string) {
|
|
152
|
+
super(message, { status: 401, kind });
|
|
153
|
+
this.name = "TuiUnauthorizedError";
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** The object, section, action or endpoint does not exist (`404`). */
|
|
158
|
+
export class TuiNotFoundError extends TuiError {
|
|
159
|
+
constructor(kind: string | undefined, message: string) {
|
|
160
|
+
super(message, { status: 404, kind });
|
|
161
|
+
this.name = "TuiNotFoundError";
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** The action is unavailable, needs confirmation, or its plugin is off (`409`). */
|
|
166
|
+
export class TuiConflictError extends TuiError {
|
|
167
|
+
constructor(kind: string | undefined, message: string) {
|
|
168
|
+
super(message, { status: 409, kind });
|
|
169
|
+
this.name = "TuiConflictError";
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** The request body is larger than one mebibyte (`413`). */
|
|
174
|
+
export class TuiPayloadTooLargeError extends TuiError {
|
|
175
|
+
constructor(kind: string | undefined, message: string) {
|
|
176
|
+
super(message, { status: 413, kind });
|
|
177
|
+
this.name = "TuiPayloadTooLargeError";
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** The action's plugin refused it (`422`); the message is the plugin's own. */
|
|
182
|
+
export class TuiRefusedError extends TuiError {
|
|
183
|
+
constructor(kind: string | undefined, message: string) {
|
|
184
|
+
super(message, { status: 422, kind });
|
|
185
|
+
this.name = "TuiRefusedError";
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** A section did not load in time (`504`). */
|
|
190
|
+
export class TuiTimeoutError extends TuiError {
|
|
191
|
+
constructor(kind: string | undefined, message: string) {
|
|
192
|
+
super(message, { status: 504, kind });
|
|
193
|
+
this.name = "TuiTimeoutError";
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** No server answered at the URL; there is no server message to keep. */
|
|
198
|
+
export class TuiUnreachableError extends TuiError {
|
|
199
|
+
constructor(message: string, cause?: unknown) {
|
|
200
|
+
super(message, { kind: "unreachable", cause });
|
|
201
|
+
this.name = "TuiUnreachableError";
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** The URL a client was given is not an http(s) base URL. */
|
|
206
|
+
export class TuiUrlError extends TuiError {
|
|
207
|
+
constructor(message: string) {
|
|
208
|
+
super(message, { kind: "url" });
|
|
209
|
+
this.name = "TuiUrlError";
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* The base URL without a trailing slash. It must be an http or https URL with
|
|
215
|
+
* no credentials, query or fragment, as the pairing URL does.
|
|
216
|
+
*/
|
|
217
|
+
export function normalizeBaseUrl(value: string): string {
|
|
218
|
+
let url: URL;
|
|
219
|
+
try {
|
|
220
|
+
url = new URL(value);
|
|
221
|
+
} catch {
|
|
222
|
+
throw new TuiUrlError(`"${value}" is not an http or https URL`);
|
|
223
|
+
}
|
|
224
|
+
if (
|
|
225
|
+
(url.protocol !== "http:" && url.protocol !== "https:") ||
|
|
226
|
+
url.username !== "" ||
|
|
227
|
+
url.password !== "" ||
|
|
228
|
+
url.search !== "" ||
|
|
229
|
+
url.hash !== ""
|
|
230
|
+
) {
|
|
231
|
+
throw new TuiUrlError(
|
|
232
|
+
"the URL must be http or https, without credentials, a query or a fragment",
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
return `${url.origin}${url.pathname.replace(/\/+$/, "")}`;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
interface ServerErrorBody {
|
|
239
|
+
readonly kind: string | undefined;
|
|
240
|
+
readonly message: string;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function serverErrorBody(text: string, status: number): ServerErrorBody {
|
|
244
|
+
let parsed: unknown;
|
|
245
|
+
try {
|
|
246
|
+
parsed = JSON.parse(text);
|
|
247
|
+
} catch {
|
|
248
|
+
return {
|
|
249
|
+
kind: undefined,
|
|
250
|
+
message: text.length > 0 ? text : `the server answered ${status}`,
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
if (!isRecord(parsed) || !isRecord(parsed.error)) {
|
|
254
|
+
return { kind: undefined, message: `the server answered ${status}` };
|
|
255
|
+
}
|
|
256
|
+
const kind = parsed.error.kind;
|
|
257
|
+
const message = parsed.error.message;
|
|
258
|
+
return {
|
|
259
|
+
kind: typeof kind === "string" ? kind : undefined,
|
|
260
|
+
message:
|
|
261
|
+
typeof message === "string"
|
|
262
|
+
? message
|
|
263
|
+
: `the server answered ${status}`,
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/** The typed error for a non-ok response; it reads the body. */
|
|
268
|
+
export async function httpError(response: Response): Promise<TuiError> {
|
|
269
|
+
const text = await response.text().catch(() => "");
|
|
270
|
+
const body = serverErrorBody(text, response.status);
|
|
271
|
+
switch (response.status) {
|
|
272
|
+
case 401:
|
|
273
|
+
return new TuiUnauthorizedError(body.kind, body.message);
|
|
274
|
+
case 404:
|
|
275
|
+
return new TuiNotFoundError(body.kind, body.message);
|
|
276
|
+
case 409:
|
|
277
|
+
return new TuiConflictError(body.kind, body.message);
|
|
278
|
+
case 413:
|
|
279
|
+
return new TuiPayloadTooLargeError(body.kind, body.message);
|
|
280
|
+
case 422:
|
|
281
|
+
return new TuiRefusedError(body.kind, body.message);
|
|
282
|
+
case 504:
|
|
283
|
+
return new TuiTimeoutError(body.kind, body.message);
|
|
284
|
+
default:
|
|
285
|
+
return new TuiError(body.message, {
|
|
286
|
+
status: response.status,
|
|
287
|
+
kind: body.kind,
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
interface RequestOptions {
|
|
293
|
+
readonly method?: "GET" | "POST" | undefined;
|
|
294
|
+
readonly query?: Readonly<Record<string, string | undefined>> | undefined;
|
|
295
|
+
readonly body?: unknown;
|
|
296
|
+
readonly signal?: AbortSignal | undefined;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** One client for one running `serve`. */
|
|
300
|
+
export class TuiClient {
|
|
301
|
+
private readonly baseUrl: string;
|
|
302
|
+
private readonly token: string;
|
|
303
|
+
private readonly fetchImpl: FetchLike;
|
|
304
|
+
|
|
305
|
+
constructor(options: TuiClientOptions) {
|
|
306
|
+
this.baseUrl = normalizeBaseUrl(options.url);
|
|
307
|
+
this.token = options.token;
|
|
308
|
+
this.fetchImpl = options.fetch ?? defaultFetch;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** The types the loaded plugins show, with their display names. */
|
|
312
|
+
async types(signal?: AbortSignal): Promise<TuiTypes> {
|
|
313
|
+
return (await this.request("/v1/types", { signal })) as TuiTypes;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/** The key bindings in force: plugin defaults with the person's overrides. */
|
|
317
|
+
async keys(signal?: AbortSignal): Promise<readonly TuiKeyBinding[]> {
|
|
318
|
+
const body = (await this.request("/v1/keys", { signal })) as {
|
|
319
|
+
readonly keys: readonly TuiKeyBinding[];
|
|
320
|
+
};
|
|
321
|
+
return body.keys;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/** One page of a type's list; pass the previous page's `next` as the cursor. */
|
|
325
|
+
async objects(
|
|
326
|
+
type: string,
|
|
327
|
+
cursor?: string,
|
|
328
|
+
signal?: AbortSignal,
|
|
329
|
+
filter?: string,
|
|
330
|
+
level?: TuiLevel,
|
|
331
|
+
): Promise<ViewListPage> {
|
|
332
|
+
return (await this.request("/v1/objects", {
|
|
333
|
+
query: {
|
|
334
|
+
type,
|
|
335
|
+
cursor,
|
|
336
|
+
filter,
|
|
337
|
+
...(level?.root === true ? { root: "1" } : {}),
|
|
338
|
+
...(level?.parent === undefined ? {} : { parent: level.parent }),
|
|
339
|
+
},
|
|
340
|
+
signal,
|
|
341
|
+
})) as ViewListPage;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/** Everything waiting for a person, with the actions each item offers. */
|
|
345
|
+
async pending(signal?: AbortSignal): Promise<readonly PendingItem[]> {
|
|
346
|
+
return (await this.request("/v1/pending", { signal })) as readonly PendingItem[];
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/** The runs that are working now. */
|
|
350
|
+
async running(signal?: AbortSignal): Promise<readonly RunningItem[]> {
|
|
351
|
+
return (await this.request("/v1/running", { signal })) as readonly RunningItem[];
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/** One object: its row, section directory and available actions. */
|
|
355
|
+
async object(id: string, signal?: AbortSignal): Promise<ViewObject> {
|
|
356
|
+
return (await this.request(`/v1/objects/${encodeURIComponent(id)}`, {
|
|
357
|
+
signal,
|
|
358
|
+
})) as ViewObject;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/** One section's Markdown; `full` skips the server's size cap. */
|
|
362
|
+
async section(
|
|
363
|
+
id: string,
|
|
364
|
+
section: string,
|
|
365
|
+
options: TuiSectionOptions = {},
|
|
366
|
+
): Promise<ViewSectionContent> {
|
|
367
|
+
return (await this.request(
|
|
368
|
+
`/v1/objects/${encodeURIComponent(id)}/sections/${encodeURIComponent(section)}`,
|
|
369
|
+
{
|
|
370
|
+
query: {
|
|
371
|
+
cursor: options.cursor,
|
|
372
|
+
after: options.after,
|
|
373
|
+
full: options.full === true ? "true" : undefined,
|
|
374
|
+
},
|
|
375
|
+
signal: options.signal,
|
|
376
|
+
},
|
|
377
|
+
)) as ViewSectionContent;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/** Run one `<plugin>:<action>`; a confirm action needs `confirmed`. */
|
|
381
|
+
async act(input: TuiActInput): Promise<ViewActionRun> {
|
|
382
|
+
return (await this.request("/v1/act", {
|
|
383
|
+
method: "POST",
|
|
384
|
+
signal: input.signal,
|
|
385
|
+
body: {
|
|
386
|
+
action: input.action,
|
|
387
|
+
...(input.id === undefined ? {} : { id: input.id }),
|
|
388
|
+
...(input.args === undefined ? {} : { args: input.args }),
|
|
389
|
+
...(input.note === undefined ? {} : { note: input.note }),
|
|
390
|
+
...(input.confirmed === undefined
|
|
391
|
+
? {}
|
|
392
|
+
: { confirmed: input.confirmed }),
|
|
393
|
+
},
|
|
394
|
+
})) as ViewActionRun;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/** Answers one open question of a pending item by the wait id it named. */
|
|
398
|
+
async answer(input: TuiAnswerInput): Promise<unknown> {
|
|
399
|
+
return this.request("/v1/answer", {
|
|
400
|
+
method: "POST",
|
|
401
|
+
signal: input.signal,
|
|
402
|
+
body: {
|
|
403
|
+
runId: input.runId,
|
|
404
|
+
waitId: input.waitId,
|
|
405
|
+
choice: input.choice,
|
|
406
|
+
...(input.note === undefined ? {} : { note: input.note }),
|
|
407
|
+
...(input.confirmed === true ? { confirmed: true } : {}),
|
|
408
|
+
},
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/** The settings of one scope (`global`, or a repository's name). */
|
|
413
|
+
async settings(scope?: string, signal?: AbortSignal): Promise<readonly TuiSetting[]> {
|
|
414
|
+
const body = (await this.request("/v1/settings", {
|
|
415
|
+
query: { scope },
|
|
416
|
+
signal,
|
|
417
|
+
})) as { readonly settings: readonly TuiSetting[] };
|
|
418
|
+
return body.settings;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/** Change one setting; the server validates the value and refuses with its reason. */
|
|
422
|
+
async setSetting(input: TuiSetInput): Promise<TuiSetting> {
|
|
423
|
+
return (await this.request("/v1/settings", {
|
|
424
|
+
method: "POST",
|
|
425
|
+
signal: input.signal,
|
|
426
|
+
body: {
|
|
427
|
+
name: input.name,
|
|
428
|
+
value: input.value,
|
|
429
|
+
...(input.scope === undefined ? {} : { scope: input.scope }),
|
|
430
|
+
...(input.confirmed === true ? { confirmed: true } : {}),
|
|
431
|
+
},
|
|
432
|
+
})) as TuiSetting;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
private async request(path: string, options: RequestOptions = {}): Promise<unknown> {
|
|
436
|
+
const url = new URL(`${this.baseUrl}${path}`);
|
|
437
|
+
for (const [name, value] of Object.entries(options.query ?? {})) {
|
|
438
|
+
if (value !== undefined) url.searchParams.set(name, value);
|
|
439
|
+
}
|
|
440
|
+
const headers: Record<string, string> = {
|
|
441
|
+
authorization: `Bearer ${this.token}`,
|
|
442
|
+
"x-operad-client": "tui",
|
|
443
|
+
accept: "application/json",
|
|
444
|
+
};
|
|
445
|
+
if (options.body !== undefined) {
|
|
446
|
+
headers["content-type"] = "application/json";
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
let response: Response;
|
|
450
|
+
try {
|
|
451
|
+
response = await this.fetchImpl(url.toString(), {
|
|
452
|
+
method: options.method ?? "GET",
|
|
453
|
+
headers,
|
|
454
|
+
...(options.body === undefined
|
|
455
|
+
? {}
|
|
456
|
+
: { body: JSON.stringify(options.body) }),
|
|
457
|
+
...(options.signal === undefined ? {} : { signal: options.signal }),
|
|
458
|
+
});
|
|
459
|
+
} catch (error) {
|
|
460
|
+
// A cancelled request is the caller's own doing, not an unreachable server.
|
|
461
|
+
if (options.signal?.aborted === true) throw error;
|
|
462
|
+
throw new TuiUnreachableError(
|
|
463
|
+
`no server answered at ${this.baseUrl}: ${messageOf(error)}`,
|
|
464
|
+
error,
|
|
465
|
+
);
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
if (!response.ok) throw await httpError(response);
|
|
469
|
+
try {
|
|
470
|
+
return (await response.json()) as unknown;
|
|
471
|
+
} catch (error) {
|
|
472
|
+
throw new TuiError(
|
|
473
|
+
`the server answered ${response.status} without JSON`,
|
|
474
|
+
{ status: response.status, cause: error },
|
|
475
|
+
);
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
}
|
package/src/discover.ts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where `operad tui` finds its server: the flags first, then the home's
|
|
3
|
+
* `config.json` (or its defaults) for the address and the token file `operad
|
|
4
|
+
* pair` reads. It never starts a server and never opens the store.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
defaultOperadHome,
|
|
9
|
+
ensureHttpToken,
|
|
10
|
+
readConfig,
|
|
11
|
+
validateListenAddresses,
|
|
12
|
+
} from "@operad-io/engine";
|
|
13
|
+
import { normalizeBaseUrl, TuiError } from "./client.ts";
|
|
14
|
+
|
|
15
|
+
/** No usable server location could be found. */
|
|
16
|
+
export class TuiDiscoveryError extends TuiError {
|
|
17
|
+
constructor(message: string) {
|
|
18
|
+
super(message, { kind: "config" });
|
|
19
|
+
this.name = "TuiDiscoveryError";
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** The flags and home a caller may name. */
|
|
24
|
+
export interface TuiDiscoverOptions {
|
|
25
|
+
readonly home?: string | undefined;
|
|
26
|
+
readonly url?: string | undefined;
|
|
27
|
+
readonly token?: string | undefined;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The address and bearer token a client needs. */
|
|
31
|
+
export interface TuiServerLocation {
|
|
32
|
+
readonly url: string;
|
|
33
|
+
readonly token: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The base URL `http.listen` and `http.port` name. */
|
|
37
|
+
export function urlFromConfig(
|
|
38
|
+
listen: readonly string[],
|
|
39
|
+
port: number,
|
|
40
|
+
): string {
|
|
41
|
+
const address = listen[0];
|
|
42
|
+
if (address === undefined) {
|
|
43
|
+
throw new TuiDiscoveryError(
|
|
44
|
+
"http.listen needs at least one address; set http.listen in config.json or pass --url",
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
validateListenAddresses(listen);
|
|
48
|
+
if (port === 0) {
|
|
49
|
+
throw new TuiDiscoveryError(
|
|
50
|
+
'http.port is 0, so no address can be reached; set http.port in config.json or pass --url',
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
const host = address.includes(":") ? `[${address}]` : address;
|
|
54
|
+
return `http://${host}:${port}`;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Resolves the server location. A given `--url` wins over `http.listen`; a
|
|
59
|
+
* given `--token` wins over the home's token file.
|
|
60
|
+
*/
|
|
61
|
+
export async function discoverServer(
|
|
62
|
+
options: TuiDiscoverOptions = {},
|
|
63
|
+
): Promise<TuiServerLocation> {
|
|
64
|
+
const home = options.home ?? defaultOperadHome();
|
|
65
|
+
|
|
66
|
+
let url: string;
|
|
67
|
+
if (options.url !== undefined) {
|
|
68
|
+
if (options.url.length === 0) {
|
|
69
|
+
throw new TuiDiscoveryError("--url needs a URL");
|
|
70
|
+
}
|
|
71
|
+
url = normalizeBaseUrl(options.url);
|
|
72
|
+
} else {
|
|
73
|
+
const config = await readConfig(home);
|
|
74
|
+
url = urlFromConfig(config.http.listen, config.http.port);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
let token: string;
|
|
78
|
+
if (options.token !== undefined) {
|
|
79
|
+
if (options.token.length === 0) {
|
|
80
|
+
throw new TuiDiscoveryError("--token needs a token");
|
|
81
|
+
}
|
|
82
|
+
token = options.token;
|
|
83
|
+
} else {
|
|
84
|
+
// `operad pair` reads the token through ensureHttpToken: the first enabled
|
|
85
|
+
// serve created it, and a home that never served creates it now.
|
|
86
|
+
token = (await ensureHttpToken(home)).token;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return { url, token };
|
|
90
|
+
}
|