@smartmemory/sdk-js 1.4.100 → 1.4.120
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 +69 -9
- package/dist/connection.js +5 -0
- package/dist/{core-McKeQ81o.js → core-DUg_hHl8.js} +268 -263
- package/dist/core.js +12 -10
- package/dist/fetch.js +18 -37
- package/dist/index-BKI1h_Wn.js +107 -0
- package/dist/index.js +14 -11
- package/dist/progress.js +166 -142
- package/dist/react.js +20 -20
- package/dist/recovery-JyUr2KHh.js +34 -0
- package/package.json +10 -2
- package/types/connection.d.ts +30 -0
- package/types/fetch.d.ts +12 -0
- package/types/progress.d.ts +48 -0
package/dist/react.js
CHANGED
|
@@ -1,40 +1,40 @@
|
|
|
1
|
-
import h, { createContext as d, useRef as
|
|
2
|
-
import { l as y } from "./core-
|
|
3
|
-
const
|
|
1
|
+
import h, { createContext as d, useRef as I, useState as S, useEffect as C, useCallback as c, useContext as g } from "react";
|
|
2
|
+
import { l as y } from "./core-DUg_hHl8.js";
|
|
3
|
+
const f = d(null), m = d(null);
|
|
4
4
|
function b({ children: e, ...r }) {
|
|
5
|
-
const n =
|
|
5
|
+
const n = I(null);
|
|
6
6
|
n.current || (n.current = new y(r));
|
|
7
|
-
const t = n.current, [o, u] =
|
|
7
|
+
const t = n.current, [o, u] = S({
|
|
8
8
|
isAuthenticated: t.auth.isAuthenticated(),
|
|
9
9
|
user: t.auth.getCurrentUser(),
|
|
10
10
|
token: t.auth.getCurrentToken(),
|
|
11
11
|
tenantId: t.auth.getTenantId(),
|
|
12
|
-
workspaceId: t.auth.tokenManager.
|
|
12
|
+
workspaceId: t.auth.tokenManager.getWorkspaceId(),
|
|
13
13
|
loading: !1,
|
|
14
14
|
error: null
|
|
15
15
|
});
|
|
16
|
-
C(() => t.auth.addListener((
|
|
17
|
-
u((
|
|
18
|
-
...
|
|
19
|
-
isAuthenticated:
|
|
20
|
-
user:
|
|
21
|
-
token:
|
|
22
|
-
tenantId:
|
|
23
|
-
workspaceId:
|
|
16
|
+
C(() => t.auth.addListener((s) => {
|
|
17
|
+
u((p) => ({
|
|
18
|
+
...p,
|
|
19
|
+
isAuthenticated: s.isAuthenticated,
|
|
20
|
+
user: s.user,
|
|
21
|
+
token: s.token,
|
|
22
|
+
tenantId: s.tenantId,
|
|
23
|
+
workspaceId: s.workspaceId
|
|
24
24
|
}));
|
|
25
25
|
}), [t]);
|
|
26
26
|
const a = c(async () => {
|
|
27
27
|
await t.auth.logout();
|
|
28
|
-
}, [t]), w = c((
|
|
28
|
+
}, [t]), w = c((i) => t.auth.hasRole(i), [t]), A = c((i) => t.auth.getLoginUrl(i), [t]), k = {
|
|
29
29
|
...o,
|
|
30
30
|
logout: a,
|
|
31
31
|
hasRole: w,
|
|
32
32
|
getLoginUrl: A
|
|
33
33
|
};
|
|
34
|
-
return /* @__PURE__ */ h.createElement(
|
|
34
|
+
return /* @__PURE__ */ h.createElement(f.Provider, { value: k }, /* @__PURE__ */ h.createElement(m.Provider, { value: t }, e));
|
|
35
35
|
}
|
|
36
36
|
function l() {
|
|
37
|
-
const e = g(
|
|
37
|
+
const e = g(f);
|
|
38
38
|
if (!e)
|
|
39
39
|
throw new Error("useAuth must be used within a SmartMemoryProvider");
|
|
40
40
|
return e;
|
|
@@ -48,7 +48,7 @@ function R() {
|
|
|
48
48
|
return { login: e, logout: r, hasRole: n, getLoginUrl: t };
|
|
49
49
|
}
|
|
50
50
|
function U() {
|
|
51
|
-
const e = g(
|
|
51
|
+
const e = g(m);
|
|
52
52
|
if (!e)
|
|
53
53
|
throw new Error("useSmartMemory must be used within a SmartMemoryProvider");
|
|
54
54
|
return e;
|
|
@@ -68,9 +68,9 @@ function E({ children: e, fallback: r = null, redirectToLogin: n = !1 }) {
|
|
|
68
68
|
return e;
|
|
69
69
|
}
|
|
70
70
|
export {
|
|
71
|
-
|
|
71
|
+
f as AuthContext,
|
|
72
72
|
E as AuthWrapper,
|
|
73
|
-
|
|
73
|
+
m as ClientContext,
|
|
74
74
|
b as SmartMemoryProvider,
|
|
75
75
|
l as useAuth,
|
|
76
76
|
R as useAuthActions,
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
async function A(e, n, { source: o, signal: t, retry: i = !0 } = {}) {
|
|
2
|
+
var c, f, p, d, w, l;
|
|
3
|
+
const s = () => new Headers(e.getAuthHeaders()).get("X-Workspace-Id"), h = s(), u = new Headers(e.getAuthHeaders()).get("Authorization"), a = () => {
|
|
4
|
+
if (s() !== h) throw new DOMException("Workspace changed during request", "AbortError");
|
|
5
|
+
};
|
|
6
|
+
try {
|
|
7
|
+
let r = await n(!1);
|
|
8
|
+
if (r.status === 401 && i) {
|
|
9
|
+
if (console.warn("[session] Unauthorized; refreshing and retrying once"), (c = e.connection) == null || c.report(o, "Refreshing session"), a(), t != null && t.aborted) throw new DOMException("Request aborted", "AbortError");
|
|
10
|
+
if (new Headers(e.getAuthHeaders()).get("Authorization") === u && await e.refreshToken(), a(), t != null && t.aborted) throw new DOMException("Request aborted", "AbortError");
|
|
11
|
+
r = await n(!0);
|
|
12
|
+
}
|
|
13
|
+
return r.status === 401 ? (console.warn("[session] Authentication required after recovery"), (f = e.clearLocalAuth) == null || f.call(e)) : r.status === 429 || r.status >= 500 ? (console.warn(`[session] Service unavailable: HTTP ${r.status}`), (p = e.connection) == null || p.report(o, `HTTP ${r.status}`)) : (d = e.connection) == null || d.report(o, null), r;
|
|
14
|
+
} catch (r) {
|
|
15
|
+
throw r.name === "AbortError" && ((w = e.connection) == null || w.report(o, null)), r.name !== "AbortError" && r.recoverable !== !1 && (console.warn("[session] Connection interrupted", r), (l = e.connection) == null || l.report(o, r.message)), r;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
function b(e) {
|
|
19
|
+
return new URL(
|
|
20
|
+
e instanceof Request ? e.url : String(e),
|
|
21
|
+
typeof location < "u" ? location.href : "http://localhost"
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
function E(e, n, o = []) {
|
|
25
|
+
return [e.apiBaseUrl || "/", ...o].some((t) => {
|
|
26
|
+
const i = b(t), s = i.pathname.replace(/\/$/, "");
|
|
27
|
+
return n.origin === i.origin && (!s || n.pathname === s || n.pathname.startsWith(`${s}/`));
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
export {
|
|
31
|
+
A as f,
|
|
32
|
+
E as i,
|
|
33
|
+
b as r
|
|
34
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@smartmemory/sdk-js",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.120",
|
|
4
4
|
"description": "Unified JavaScript SDK for SmartMemory API — auth + API client for all frontends",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -23,16 +23,24 @@
|
|
|
23
23
|
"default": "./dist/react/analytics.js"
|
|
24
24
|
},
|
|
25
25
|
"./fetch": {
|
|
26
|
+
"types": "./types/fetch.d.ts",
|
|
26
27
|
"development": "./src/fetch/index.js",
|
|
27
28
|
"default": "./dist/fetch.js"
|
|
28
29
|
},
|
|
29
30
|
"./progress": {
|
|
31
|
+
"types": "./types/progress.d.ts",
|
|
30
32
|
"development": "./src/progress.ts",
|
|
31
33
|
"default": "./dist/progress.js"
|
|
34
|
+
},
|
|
35
|
+
"./connection": {
|
|
36
|
+
"types": "./types/connection.d.ts",
|
|
37
|
+
"development": "./src/connection/index.js",
|
|
38
|
+
"default": "./dist/connection.js"
|
|
32
39
|
}
|
|
33
40
|
},
|
|
34
41
|
"files": [
|
|
35
|
-
"dist"
|
|
42
|
+
"dist",
|
|
43
|
+
"types"
|
|
36
44
|
],
|
|
37
45
|
"scripts": {
|
|
38
46
|
"build": "vite build",
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export type ConnectionState = 'connected' | 'reconnecting' | 'signed_out';
|
|
2
|
+
export interface ConnectionSnapshot {
|
|
3
|
+
readonly status: ConnectionState;
|
|
4
|
+
readonly reason: string | null;
|
|
5
|
+
}
|
|
6
|
+
export declare class ConnectionStatus {
|
|
7
|
+
readonly snapshot: ConnectionSnapshot;
|
|
8
|
+
subscribe(listener: (snapshot: ConnectionSnapshot) => void): () => void;
|
|
9
|
+
report(source: string | symbol, reason: string | null): void;
|
|
10
|
+
signedOut(): void;
|
|
11
|
+
authenticated(): void;
|
|
12
|
+
}
|
|
13
|
+
/** Structural type for client.connection / client.auth.connection. */
|
|
14
|
+
export interface RecoveryClient {
|
|
15
|
+
connection: ConnectionStatus;
|
|
16
|
+
auth: RecoveryAuth;
|
|
17
|
+
}
|
|
18
|
+
export interface RecoveryAuth {
|
|
19
|
+
apiBaseUrl: string;
|
|
20
|
+
connection: ConnectionStatus;
|
|
21
|
+
getAuthHeaders(extra?: HeadersInit): Record<string, string>;
|
|
22
|
+
getRequestOptions(extra?: RequestInit): RequestInit;
|
|
23
|
+
refreshToken(): Promise<string | null>;
|
|
24
|
+
clearLocalAuth(): void;
|
|
25
|
+
}
|
|
26
|
+
export declare class SessionRefreshError extends Error {
|
|
27
|
+
constructor(message: string, status?: number, recoverable?: boolean);
|
|
28
|
+
status: number;
|
|
29
|
+
recoverable: boolean;
|
|
30
|
+
}
|
package/types/fetch.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { RecoveryAuth } from './connection.js';
|
|
2
|
+
export type { RecoveryAuth, RecoveryClient, ConnectionSnapshot, ConnectionState } from './connection.js';
|
|
3
|
+
export interface AuthFetchOptions {
|
|
4
|
+
fetchFn?: typeof fetch;
|
|
5
|
+
apiBases?: string[];
|
|
6
|
+
}
|
|
7
|
+
export interface InterceptorOptions extends AuthFetchOptions {
|
|
8
|
+
urlPatterns?: string[];
|
|
9
|
+
}
|
|
10
|
+
export declare function createAuthFetch(authCore: RecoveryAuth, options?: AuthFetchOptions): typeof fetch;
|
|
11
|
+
/** Returns an injectable fetch; does not mutate global fetch or return an uninstaller. */
|
|
12
|
+
export declare function installInterceptor(authCore: RecoveryAuth, options?: InterceptorOptions): typeof fetch;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
export interface ProgressEvent {
|
|
2
|
+
run_id: string;
|
|
3
|
+
scope: string;
|
|
4
|
+
seq: number;
|
|
5
|
+
ts: number;
|
|
6
|
+
kind: string;
|
|
7
|
+
status: 'started' | 'progress' | 'ok' | 'warn' | 'error' | 'skipped';
|
|
8
|
+
payload: Record<string, unknown>;
|
|
9
|
+
/** Optional — pipeline stage name when applicable */
|
|
10
|
+
stage?: string | null;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface ProgressAuth {
|
|
14
|
+
getAuthHeaders(): Record<string, string>;
|
|
15
|
+
getRequestOptions(options?: RequestInit): RequestInit;
|
|
16
|
+
refreshToken(): Promise<unknown>;
|
|
17
|
+
clearLocalAuth(): void;
|
|
18
|
+
connection?: ProgressConnection;
|
|
19
|
+
}
|
|
20
|
+
export interface ProgressConnection {
|
|
21
|
+
report(source: string | symbol, reason: string | null): void;
|
|
22
|
+
}
|
|
23
|
+
export interface SubscribeProgressOptions {
|
|
24
|
+
baseUrl?: string;
|
|
25
|
+
/** Static credentials remain supported. Prefer auth for session recovery. */
|
|
26
|
+
token?: string;
|
|
27
|
+
apiKey?: string;
|
|
28
|
+
workspaceId?: string;
|
|
29
|
+
auth?: ProgressAuth;
|
|
30
|
+
/** Synchronous live headers, read on every connection; overrides static headers. */
|
|
31
|
+
getHeaders?: () => Record<string, string>;
|
|
32
|
+
connection?: ProgressConnection;
|
|
33
|
+
fetchFn?: typeof fetch;
|
|
34
|
+
runId?: string;
|
|
35
|
+
fromSeq?: number;
|
|
36
|
+
since?: string;
|
|
37
|
+
useCookieAuth?: boolean;
|
|
38
|
+
/** Default true. Set false for deliberate finite replay; EOF invokes onComplete. */
|
|
39
|
+
reconnect?: boolean;
|
|
40
|
+
onComplete?: () => void;
|
|
41
|
+
onEvent: (event: ProgressEvent) => void;
|
|
42
|
+
/** Terminal errors only; transient interruption invokes onReconnect. */
|
|
43
|
+
onError: (err: unknown) => void;
|
|
44
|
+
onReconnect?: () => void;
|
|
45
|
+
}
|
|
46
|
+
export interface ProgressSubscription { close(): void; }
|
|
47
|
+
|
|
48
|
+
export declare function subscribeProgress(options: SubscribeProgressOptions): ProgressSubscription;
|