@dudousxd/nestjs-telescope-ui 1.17.0 → 1.18.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/CHANGELOG.md CHANGED
@@ -1,5 +1,40 @@
1
1
  # @dudousxd/nestjs-telescope-ui
2
2
 
3
+ ## 1.18.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#63](https://github.com/DavideCarvalho/nestjs-telescope/pull/63) [`2d0e76e`](https://github.com/DavideCarvalho/nestjs-telescope/commit/2d0e76ee52e3a556c24b567fdb4b9e6cef25c067) Thanks [@DavideCarvalho](https://github.com/DavideCarvalho)! - **Headless console launcher: `openTelescopeConsole` / `mintTelescopeConsoleSession` / `telescopeConsoleSessionUrl`, exported from `@dudousxd/nestjs-telescope-ui/client`.**
8
+
9
+ The console is entered from the HOST's app: a browser navigation to it carries no identity, so
10
+ something inside the host has to mint the Mode A session cookie first (an XHR that _does_ carry the
11
+ host's auth), then navigate. Every host was writing that by hand, which meant hardcoding two things
12
+ this package owns:
13
+
14
+ - **the session endpoint's path** — `<path>/api/auth/session`. Nothing tells a host when that moves; the break
15
+ only shows up as a runtime 404 after a version bump.
16
+ - **`redirect: 'manual'`** — and this one is a real trap. `fetch` follows redirects by default, so a
17
+ host whose auth layer rewrites a 401 into a sign-in redirect gets a resolved 200 against the
18
+ sign-in HTML. `response.ok` reads true, the caller navigates, and the user lands in a console with
19
+ no session — indistinguishable from a permissions bug. The helper detects the redirect (browser
20
+ opaque response _and_ Node/undici 3xx) and throws a message naming the likely cause.
21
+
22
+ ```ts
23
+ import { openTelescopeConsole } from "@dudousxd/nestjs-telescope-ui/client";
24
+
25
+ await openTelescopeConsole({
26
+ headers: () => ({ Authorization: `Bearer ${token()}` }),
27
+ });
28
+ ```
29
+
30
+ No UI: the host owns the button, the page and the copy. `headers` accepts a sync or async function
31
+ so a refreshing token is read at call time rather than captured at wiring time. `fetch` and
32
+ `navigate` are injectable (tests, routers, non-browser callers). A refused mint throws
33
+ `ConsoleSessionError` (carrying `status` and `url`) and **does not navigate** — a denied user gets a
34
+ real error instead of the console's "no session" page.
35
+
36
+ Additive only: nothing existing changes.
37
+
3
38
  ## 1.17.0
4
39
 
5
40
  ### Minor Changes
@@ -0,0 +1,66 @@
1
+ /**
2
+ * Headless client for opening the console from YOUR app.
3
+ *
4
+ * The console SPA is served at a literal path and knows nothing about your app's auth, so a plain
5
+ * browser navigation to it carries no identity. Mode A closes that gap: an XHR from inside your app
6
+ * — which DOES carry your auth — posts to the console's session endpoint, the `session` hook
7
+ * decides, and the library answers with its own signed cookie. The navigation that follows rides it.
8
+ *
9
+ * Telescope's mount is configured as `path` (no leading slash, e.g. `'telescope'`); this accepts either
10
+ * form and normalizes it.
11
+ *
12
+ * No UI here on purpose: you own the button, the page and the copy. This module owns the two things
13
+ * a host would otherwise have to rediscover — where the session endpoint actually lives, and how to
14
+ * call it without the failure mode below.
15
+ */
16
+ /**
17
+ * Where `POST` mints the console session cookie.
18
+ *
19
+ * Derived from `basePath` rather than hardcoded by the caller: this package owns the route (the auth
20
+ * controller lives under the configured mount), so a host that hardcodes it
21
+ * is guessing at something that can change under a version bump — with no compile error when it
22
+ * does, because the break only shows up as a 404 at runtime.
23
+ */
24
+ export declare function telescopeConsoleSessionUrl(basePath?: string): string;
25
+ /** Where the console SPA itself is served. */
26
+ export declare function telescopeConsoleUrl(basePath?: string): string;
27
+ export interface OpenConsoleOptions {
28
+ /** Where the console is mounted. MUST match `TelescopeUiModule.forRoot({ path }) / TelescopeModule.forRoot({ path })`. */
29
+ basePath?: string;
30
+ /**
31
+ * Headers for the mint request — in practice your app's `Authorization` header. A function (sync
32
+ * or async) is accepted so a token can be read at call time rather than captured at wiring time,
33
+ * which is what a refreshing token needs.
34
+ */
35
+ headers?: HeadersInit | (() => HeadersInit | Promise<HeadersInit>);
36
+ /** Injected for tests and non-browser callers. Defaults to the global `fetch`. */
37
+ fetch?: typeof globalThis.fetch;
38
+ signal?: AbortSignal;
39
+ /**
40
+ * Performs the navigation after a successful mint. Defaults to `location.assign`. Override to
41
+ * route through your own router, or to open in a new tab.
42
+ */
43
+ navigate?: (url: string) => void;
44
+ }
45
+ /** Thrown when the session could not be minted. Never thrown after a successful mint. */
46
+ export declare class ConsoleSessionError extends Error {
47
+ readonly url: string;
48
+ /** The HTTP status, or `undefined` when the request never produced one (network error). */
49
+ readonly status?: number | undefined;
50
+ constructor(message: string, url: string,
51
+ /** The HTTP status, or `undefined` when the request never produced one (network error). */
52
+ status?: number | undefined);
53
+ }
54
+ /**
55
+ * Mint the console session cookie. Resolves on success; throws {@link ConsoleSessionError} on
56
+ * refusal. Use this directly when you want to mint without navigating (a pre-flight check, or a
57
+ * link the user opens later).
58
+ */
59
+ export declare function mintTelescopeConsoleSession(options?: OpenConsoleOptions): Promise<void>;
60
+ /**
61
+ * Mint the session, then navigate to the console. Throws without navigating when the mint is
62
+ * refused, so a denied user gets a real error instead of landing on the console's
63
+ * "no session" page — which reads as a bug rather than a permission decision.
64
+ */
65
+ export declare function openTelescopeConsole(options?: OpenConsoleOptions): Promise<void>;
66
+ //# sourceMappingURL=console-session.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"console-session.d.ts","sourceRoot":"","sources":["../../src/client/console-session.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAWH;;;;;;;GAOG;AACH,wBAAgB,0BAA0B,CAAC,QAAQ,GAAE,MAA0B,GAAG,MAAM,CAEvF;AAED,8CAA8C;AAC9C,wBAAgB,mBAAmB,CAAC,QAAQ,GAAE,MAA0B,GAAG,MAAM,CAEhF;AAED,MAAM,WAAW,kBAAkB;IACjC,0HAA0H;IAC1H,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;;;OAIG;IACH,OAAO,CAAC,EAAE,WAAW,GAAG,CAAC,MAAM,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC;IACnE,kFAAkF;IAClF,KAAK,CAAC,EAAE,OAAO,UAAU,CAAC,KAAK,CAAC;IAChC,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB;;;OAGG;IACH,QAAQ,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;CAClC;AAED,yFAAyF;AACzF,qBAAa,mBAAoB,SAAQ,KAAK;IAG1C,QAAQ,CAAC,GAAG,EAAE,MAAM;IACpB,2FAA2F;IAC3F,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM;gBAHxB,OAAO,EAAE,MAAM,EACN,GAAG,EAAE,MAAM;IACpB,2FAA2F;IAClF,MAAM,CAAC,EAAE,MAAM,YAAA;CAK3B;AAOD;;;;GAIG;AACH,wBAAsB,2BAA2B,CAAC,OAAO,GAAE,kBAAuB,GAAG,OAAO,CAAC,IAAI,CAAC,CAkDjG;AAED;;;;GAIG;AACH,wBAAsB,oBAAoB,CAAC,OAAO,GAAE,kBAAuB,GAAG,OAAO,CAAC,IAAI,CAAC,CAK1F"}
@@ -0,0 +1,110 @@
1
+ /**
2
+ * Headless client for opening the console from YOUR app.
3
+ *
4
+ * The console SPA is served at a literal path and knows nothing about your app's auth, so a plain
5
+ * browser navigation to it carries no identity. Mode A closes that gap: an XHR from inside your app
6
+ * — which DOES carry your auth — posts to the console's session endpoint, the `session` hook
7
+ * decides, and the library answers with its own signed cookie. The navigation that follows rides it.
8
+ *
9
+ * Telescope's mount is configured as `path` (no leading slash, e.g. `'telescope'`); this accepts either
10
+ * form and normalizes it.
11
+ *
12
+ * No UI here on purpose: you own the button, the page and the copy. This module owns the two things
13
+ * a host would otherwise have to rediscover — where the session endpoint actually lives, and how to
14
+ * call it without the failure mode below.
15
+ */
16
+ /** The mount default; matches `TelescopeUiModule.forRoot()`'s own default. */
17
+ const DEFAULT_BASE_PATH = '/telescope';
18
+ function normalizeBasePath(basePath) {
19
+ const trimmed = basePath.trim().replace(/\/+$/, '');
20
+ if (trimmed === '')
21
+ return '';
22
+ return trimmed.startsWith('/') ? trimmed : `/${trimmed}`;
23
+ }
24
+ /**
25
+ * Where `POST` mints the console session cookie.
26
+ *
27
+ * Derived from `basePath` rather than hardcoded by the caller: this package owns the route (the auth
28
+ * controller lives under the configured mount), so a host that hardcodes it
29
+ * is guessing at something that can change under a version bump — with no compile error when it
30
+ * does, because the break only shows up as a 404 at runtime.
31
+ */
32
+ export function telescopeConsoleSessionUrl(basePath = DEFAULT_BASE_PATH) {
33
+ return `${normalizeBasePath(basePath)}/api/auth/session`;
34
+ }
35
+ /** Where the console SPA itself is served. */
36
+ export function telescopeConsoleUrl(basePath = DEFAULT_BASE_PATH) {
37
+ return normalizeBasePath(basePath) || '/';
38
+ }
39
+ /** Thrown when the session could not be minted. Never thrown after a successful mint. */
40
+ export class ConsoleSessionError extends Error {
41
+ url;
42
+ status;
43
+ constructor(message, url,
44
+ /** The HTTP status, or `undefined` when the request never produced one (network error). */
45
+ status) {
46
+ super(message);
47
+ this.url = url;
48
+ this.status = status;
49
+ this.name = 'ConsoleSessionError';
50
+ }
51
+ }
52
+ async function resolveHeaders(headers) {
53
+ if (headers === undefined)
54
+ return {};
55
+ return typeof headers === 'function' ? await headers() : headers;
56
+ }
57
+ /**
58
+ * Mint the console session cookie. Resolves on success; throws {@link ConsoleSessionError} on
59
+ * refusal. Use this directly when you want to mint without navigating (a pre-flight check, or a
60
+ * link the user opens later).
61
+ */
62
+ export async function mintTelescopeConsoleSession(options = {}) {
63
+ const url = telescopeConsoleSessionUrl(options.basePath);
64
+ const doFetch = options.fetch ?? globalThis.fetch;
65
+ if (typeof doFetch !== 'function') {
66
+ throw new ConsoleSessionError('No `fetch` available; pass one via `options.fetch`.', url);
67
+ }
68
+ let response;
69
+ try {
70
+ response = await doFetch(url, {
71
+ method: 'POST',
72
+ // The whole point: the response's Set-Cookie must stick, and the cookie must ride the
73
+ // navigation that follows.
74
+ credentials: 'include',
75
+ headers: await resolveHeaders(options.headers),
76
+ // Load-bearing, and the reason this helper exists rather than three lines at the call site.
77
+ // `fetch` FOLLOWS redirects by default, so an app whose auth layer rewrites a 401 into a
78
+ // "go to /signin" redirect makes this request resolve 200 against the sign-in HTML —
79
+ // `response.ok` reads true, the caller navigates, and the user lands in a console with no
80
+ // session, which looks exactly like a permissions bug. Handling the redirect explicitly turns
81
+ // that into a clear error instead of a silent false success.
82
+ redirect: 'manual',
83
+ ...(options.signal ? { signal: options.signal } : {}),
84
+ });
85
+ }
86
+ catch (cause) {
87
+ throw new ConsoleSessionError(`Could not reach the console session endpoint at ${url}: ${String(cause)}`, url);
88
+ }
89
+ // `redirect: 'manual'` surfaces differently per runtime: browsers give an opaque response
90
+ // (`type: 'opaqueredirect'`, status 0), Node/undici gives the real 3xx. Both mean the same thing.
91
+ const redirected = response.type === 'opaqueredirect' || (response.status >= 300 && response.status < 400);
92
+ if (redirected) {
93
+ throw new ConsoleSessionError(`The console session endpoint at ${url} answered with a redirect instead of minting a session. Something in front of it — usually an auth middleware or exception filter that rewrites 401s into a sign-in redirect — is intercepting the response. Exempt this path from that rewrite so the real status reaches this caller.`, url, response.status || undefined);
94
+ }
95
+ if (!response.ok) {
96
+ throw new ConsoleSessionError(`The console refused to open (HTTP ${response.status}).`, url, response.status);
97
+ }
98
+ }
99
+ /**
100
+ * Mint the session, then navigate to the console. Throws without navigating when the mint is
101
+ * refused, so a denied user gets a real error instead of landing on the console's
102
+ * "no session" page — which reads as a bug rather than a permission decision.
103
+ */
104
+ export async function openTelescopeConsole(options = {}) {
105
+ await mintTelescopeConsoleSession(options);
106
+ const target = telescopeConsoleUrl(options.basePath);
107
+ const navigate = options.navigate ?? ((url) => globalThis.location?.assign(url));
108
+ navigate(target);
109
+ }
110
+ //# sourceMappingURL=console-session.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"console-session.js","sourceRoot":"","sources":["../../src/client/console-session.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,8EAA8E;AAC9E,MAAM,iBAAiB,GAAG,YAAY,CAAC;AAEvC,SAAS,iBAAiB,CAAC,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AAqC,QAAQ,CAAC,MAAM,IAAI,EACxD,GAAG,EACH,QAAQ,CAAC,MAAM,CAChB,CAAC;IACJ,CAAC;AACH,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,oBAAoB,CAAC,UAA8B,EAAE;IACzE,MAAM,2BAA2B,CAAC,OAAO,CAAC,CAAC;IAC3C,MAAM,MAAM,GAAG,mBAAmB,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IACrD,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAQ,IAAI,CAAC,CAAC,GAAW,EAAE,EAAE,CAAC,UAAU,CAAC,QAAQ,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;IACzF,QAAQ,CAAC,MAAM,CAAC,CAAC;AACnB,CAAC"}
@@ -1,3 +1,4 @@
1
+ export { ConsoleSessionError, telescopeConsoleSessionUrl, telescopeConsoleUrl, mintTelescopeConsoleSession, openTelescopeConsole, type OpenConsoleOptions, } from './console-session.js';
1
2
  export { createTelescopeClient } from './telescope-client.js';
2
3
  export type { BulkActionName, JobActionName, TelescopeClient, TelescopeClientOptions, } from './telescope-client.js';
3
4
  export type * from './types.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAC;AAC9D,YAAY,EACV,cAAc,EACd,aAAa,EACb,eAAe,EACf,sBAAsB,GACvB,MAAM,uBAAuB,CAAC;AAC/B,mBAAmB,YAAY,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,mBAAmB,EACnB,0BAA0B,EAC1B,mBAAmB,EACnB,2BAA2B,EAC3B,oBAAoB,EACpB,KAAK,kBAAkB,GACxB,MAAM,sBAAsB,CAAC;AAE9B,OAAO,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAC;AAC9D,YAAY,EACV,cAAc,EACd,aAAa,EACb,eAAe,EACf,sBAAsB,GACvB,MAAM,uBAAuB,CAAC;AAC/B,mBAAmB,YAAY,CAAC"}
@@ -1,2 +1,5 @@
1
+ // Headless console-launcher primitives (path derivation + mint-then-navigate). Re-exported here
2
+ // because `./client` resolves to this file — see the package's `exports` map.
3
+ export { ConsoleSessionError, telescopeConsoleSessionUrl, telescopeConsoleUrl, mintTelescopeConsoleSession, openTelescopeConsole, } from './console-session.js';
1
4
  export { createTelescopeClient } from './telescope-client.js';
2
5
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA,gGAAgG;AAChG,8EAA8E;AAC9E,OAAO,EACL,mBAAmB,EACnB,0BAA0B,EAC1B,mBAAmB,EACnB,2BAA2B,EAC3B,oBAAoB,GAErB,MAAM,sBAAsB,CAAC;AAE9B,OAAO,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dudousxd/nestjs-telescope-ui",
3
- "version": "1.17.0",
3
+ "version": "1.18.0",
4
4
  "description": "Bundled dashboard + composable React components for @dudousxd/nestjs-telescope.",
5
5
  "license": "MIT",
6
6
  "repository": {