Changeset 0.37.3 (#215)

Co-authored-by: gobha <jasafpro@gmail.com>
Co-committed-by: gobha <jasafpro@gmail.com>
This commit is contained in:
2026-03-21 00:34:36 +00:00
committed by xcaliber
parent 4f1abc6321
commit fc43618501
9 changed files with 1722 additions and 0 deletions

120
src/js/sw/sdk/index.js Normal file
View File

@@ -0,0 +1,120 @@
// ==========================================
// Chat Switchboard — SDK Entry Point
// ==========================================
// Assembles all SDK modules into the `sw` object
// and runs the boot sequence.
//
// Usage:
// import { boot } from './js/sw/sdk/index.js';
// const sw = await boot();
//
// Exports: boot() → window.sw
// ==========================================
import { createRestClient } from './rest-client.js';
import { createAuth } from './auth.js';
import { createCan } from './can.js';
import { createDomains } from './api-domains.js';
import { createEvents } from './events.js';
import { createPipe } from './pipe.js';
import { createTheme } from './theme.js';
/**
* Boot the SDK. Assembles modules, loads auth state, connects WebSocket.
* Idempotent — returns the same sw object on subsequent calls.
*
* @returns {Promise<object>} sw
*/
export async function boot() {
// Idempotent
if (window.sw?._sdk) return window.sw;
// 1. Auth — owns token state
const auth = createAuth();
// 2. REST client — uses auth for token injection + refresh
const restClient = createRestClient(
() => auth._getToken(),
() => auth._onRefresh(),
() => auth._on401Failure()
);
// 3. Wire rest client into auth (auth needs it for login/refresh/permissions calls)
auth._setRestClient(restClient);
// 4. Events — with WS auth callback
const events = createEvents(async () => {
// Prefer ticket exchange
try {
const data = await restClient.post('/api/v1/ws/ticket', {});
if (data?.ticket) return `ticket=${encodeURIComponent(data.ticket)}`;
} catch (_) { /* fall through to legacy */ }
// Legacy fallback: JWT in query param
const token = auth._getToken();
return token ? `token=${encodeURIComponent(token)}` : null;
});
// 5. Wire emit into auth
auth._setEmit(events.emit.bind(events));
// 6. Remaining modules
const { can, isAdmin, isTeamAdmin } = createCan(auth);
const api = createDomains(restClient);
const pipe = createPipe();
const theme = createTheme(events.emit.bind(events));
// 7. Assemble sw object
const sw = Object.create(null);
// Auth
sw.auth = auth;
// API domains
sw.api = api;
// RBAC
sw.can = can;
Object.defineProperty(sw, 'isAdmin', {
get() { return isAdmin(); },
enumerable: true,
});
sw.isTeamAdmin = isTeamAdmin;
// Events (expose as top-level convenience + events namespace for connect/disconnect)
sw.on = events.on.bind(events);
sw.once = events.once.bind(events);
sw.off = events.off.bind(events);
sw.emit = events.emit.bind(events);
sw.events = events;
// Pipe & Theme
sw.pipe = pipe;
sw.theme = theme;
// Marker for idempotency
sw._sdk = '0.37.3';
// 8. Expose globally
window.sw = sw;
// 9. Boot sequence
theme.init();
try {
await auth.boot();
} catch (e) {
console.warn('[sw] Auth boot failed (expected without backend):', e.message);
}
if (auth.isAuthenticated) {
const BASE = window.__BASE__ || '';
events.connect(BASE + '/ws');
}
// 10. Signal ready
events.emit('sdk.ready', {}, { localOnly: true });
document.dispatchEvent(new CustomEvent('sw:ready', { detail: { sw } }));
console.log('[sw] SDK v0.37.3 ready');
return sw;
}