@kukeon/api-client 0.0.0-stage → 0.10.0-edge.g3240207e

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.
@@ -0,0 +1 @@
1
+ export * from "./transport.js";
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ // @kukeon/api-client: the transport every kukeon console request and terminal socket goes through.
2
+ export * from "./transport.js";
@@ -0,0 +1,15 @@
1
+ export type Transport = {
2
+ apiBase: string;
3
+ fetch: (input: string, init?: RequestInit) => Promise<Response>;
4
+ wsUrl: (path: string) => string;
5
+ };
6
+ export declare const defaultApiBase = "/api";
7
+ export declare function createTransport(options?: Partial<Transport>): Transport;
8
+ export declare function configureTransport(options?: Partial<Transport>): Transport;
9
+ export declare function transport(): Transport;
10
+ export declare function apiUrl(path: string): string;
11
+ type UnauthorizedListener = (path: string) => void;
12
+ export declare function onUnauthorized(listener: UnauthorizedListener): () => void;
13
+ export declare function apiFetch(path: string, init?: RequestInit): Promise<Response>;
14
+ export declare function apiSocket(path: string): WebSocket;
15
+ export {};
@@ -0,0 +1,64 @@
1
+ // The transport seam (#1752): the one module that knows where the API lives. Every API request and
2
+ // every attach/exec WebSocket goes through it, so the console can be mounted under another base
3
+ // path or carried over another transport without touching the pages.
4
+ //
5
+ // Paths handed to apiFetch and apiSocket are relative to the API root: "/realms", not "/api/realms".
6
+ export const defaultApiBase = "/api";
7
+ // sameOriginSocket is the WebSocket URL of base + path on the page's own host: wss: on an https:
8
+ // page, ws: otherwise.
9
+ function sameOriginSocket(base, path) {
10
+ const url = new URL(base + path, location.href);
11
+ url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
12
+ return url.href;
13
+ }
14
+ // trimTrailingSlashes drops every trailing "/" of base. A loop rather than /\/+$/, whose backtracking
15
+ // is quadratic on a long run of slashes followed by another character (CodeQL js/polynomial-redos).
16
+ function trimTrailingSlashes(base) {
17
+ let end = base.length;
18
+ while (end > 0 && base[end - 1] === "/")
19
+ end--;
20
+ return base.slice(0, end);
21
+ }
22
+ // createTransport fills in what an override leaves out: the page's fetch (looked up per call, so a
23
+ // test or polyfill that replaces window.fetch later still applies) and a same-origin WebSocket URL
24
+ // under the transport's own apiBase.
25
+ export function createTransport(options = {}) {
26
+ const apiBase = trimTrailingSlashes(options.apiBase ?? defaultApiBase);
27
+ return {
28
+ apiBase,
29
+ fetch: options.fetch ?? ((input, init) => window.fetch(input, init)),
30
+ wsUrl: options.wsUrl ?? (path => sameOriginSocket(apiBase, path)),
31
+ };
32
+ }
33
+ let current = createTransport();
34
+ // configureTransport replaces the transport; mountConsole calls it before the first render.
35
+ export function configureTransport(options = {}) {
36
+ current = createTransport(options);
37
+ return current;
38
+ }
39
+ export function transport() {
40
+ return current;
41
+ }
42
+ // apiUrl is the full URL of an API path under the current transport.
43
+ export function apiUrl(path) {
44
+ return current.apiBase + path;
45
+ }
46
+ const unauthorizedListeners = new Set();
47
+ // onUnauthorized calls listener with the API path of every response that answers 401; the auth
48
+ // adapter decides whether that ends the session. It returns the unsubscribe.
49
+ export function onUnauthorized(listener) {
50
+ unauthorizedListeners.add(listener);
51
+ return () => { unauthorizedListeners.delete(listener); };
52
+ }
53
+ // apiFetch performs one API request.
54
+ export async function apiFetch(path, init) {
55
+ const response = await current.fetch(apiUrl(path), init);
56
+ if (response.status === 401)
57
+ for (const listener of [...unauthorizedListeners])
58
+ listener(path);
59
+ return response;
60
+ }
61
+ // apiSocket opens a WebSocket to an API path.
62
+ export function apiSocket(path) {
63
+ return new WebSocket(current.wsUrl(path));
64
+ }
package/package.json CHANGED
@@ -1,6 +1,29 @@
1
1
  {
2
2
  "name": "@kukeon/api-client",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.10.0-edge.g3240207e",
4
+ "description": "Transport of the kukeon web console: API requests and attach/exec WebSockets.",
5
+ "license": "Apache-2.0",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/eminwux/kukeon.git",
9
+ "directory": "web/packages/api-client"
10
+ },
11
+ "type": "module",
12
+ "exports": {
13
+ ".": {
14
+ "@kukeon/source": "./src/index.ts",
15
+ "types": "./dist/index.d.ts",
16
+ "default": "./dist/index.js"
17
+ }
18
+ },
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "publishConfig": {
23
+ "access": "public"
24
+ },
25
+ "scripts": {
26
+ "build": "tsc -p tsconfig.build.json",
27
+ "typecheck": "tsc --noEmit"
28
+ }
29
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.