@variamosple/variamos-components 0.2.2 → 0.2.3

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,29 @@
1
+ /**
2
+ * Short-lived token used to call the notification service, issued by the
3
+ * admin service to the signed-in user (ADR-0001 of the admin module).
4
+ */
5
+ export interface NotificationAccessToken {
6
+ token: string;
7
+ expiresAt: number;
8
+ }
9
+ /** Address of the token route from the admin API base address. */
10
+ export declare function accessTokenUrl(adminApiUrl: string): string;
11
+ export interface AccessTokenProviderOptions {
12
+ url: string;
13
+ /** Session token of the platform, sent to the admin service. */
14
+ getSessionToken?: () => string | null;
15
+ fetchImpl?: typeof fetch;
16
+ now?: () => number;
17
+ /** A token is renewed this long before it expires. */
18
+ refreshMarginMs?: number;
19
+ }
20
+ export interface AccessTokenProvider {
21
+ /** A valid token, or null when the admin service cannot issue one. */
22
+ get(): Promise<string | null>;
23
+ clear(): void;
24
+ }
25
+ /**
26
+ * Keeps one token in memory, renews it shortly before it expires, and
27
+ * shares a single request between concurrent callers.
28
+ */
29
+ export declare function createAccessTokenProvider({ url, getSessionToken, fetchImpl, now, refreshMarginMs, }: AccessTokenProviderOptions): AccessTokenProvider;
@@ -0,0 +1,40 @@
1
+ function g(t) {
2
+ return `${t.replace(/\/+$/, "").replace(/\/v1$/, "")}/v1/notifications/access-token`;
3
+ }
4
+ function y({
5
+ url: t,
6
+ getSessionToken: s = () => localStorage.getItem("authToken"),
7
+ fetchImpl: p = (...e) => fetch(...e),
8
+ now: f = Date.now,
9
+ refreshMarginMs: k = 6e4
10
+ }) {
11
+ let e = null, n = null;
12
+ const d = async () => {
13
+ var a, o;
14
+ try {
15
+ const c = s(), l = await p(t, {
16
+ credentials: "include",
17
+ headers: c ? { Authorization: `Bearer ${c}` } : {}
18
+ });
19
+ if (!l.ok) return null;
20
+ const i = await l.json(), r = (a = i.data) == null ? void 0 : a.token, u = Date.parse(String((o = i.data) == null ? void 0 : o.expiresAt));
21
+ return typeof r != "string" || Number.isNaN(u) ? null : (e = { token: r, expiresAt: u }, r);
22
+ } catch {
23
+ return null;
24
+ }
25
+ };
26
+ return {
27
+ async get() {
28
+ return e && f() < e.expiresAt - k ? e.token : (n ?? (n = d().finally(() => {
29
+ n = null;
30
+ })), n);
31
+ },
32
+ clear() {
33
+ e = null;
34
+ }
35
+ };
36
+ }
37
+ export {
38
+ g as accessTokenUrl,
39
+ y as createAccessTokenProvider
40
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@variamosple/variamos-components",
3
3
  "private": false,
4
- "version": "0.2.2",
4
+ "version": "0.2.3",
5
5
  "type": "module",
6
6
  "main": "dist/main.js",
7
7
  "types": "dist/main.d.ts",
@@ -16,6 +16,7 @@
16
16
  "build": "tsc --p ./tsconfig.lib.json && vite build",
17
17
  "lint": "eslint .",
18
18
  "preview": "vite preview",
19
+ "test": "vitest run",
19
20
  "prepack": "npm run build"
20
21
  },
21
22
  "peerDependencies": {
@@ -44,8 +45,9 @@
44
45
  "typescript": "~5.7.2",
45
46
  "typescript-eslint": "^8.22.0",
46
47
  "vite": "^6.1.0",
47
- "vite-plugin-dts": "^4.5.0",
48
- "vite-plugin-lib-inject-css": "^2.2.1"
48
+ "vite-plugin-dts": "^5.1.2",
49
+ "vite-plugin-lib-inject-css": "^2.2.1",
50
+ "vitest": "^4.1.11"
49
51
  },
50
52
  "allowScripts": {
51
53
  "esbuild@0.25.12": true