@dudousxd/nestjs-media-dashboard 0.10.0 → 0.11.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.
@@ -0,0 +1,69 @@
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
+ * No UI here on purpose: you own the button, the page and the copy. This module owns the two things
10
+ * a host would otherwise have to rediscover — where the session endpoint actually lives, and how to
11
+ * call it without the failure mode below.
12
+ */
13
+ /**
14
+ * Where `POST` mints the console session cookie.
15
+ *
16
+ * Note this derives from `apiBasePath`, NOT `basePath`: the auth controller is mounted with the
17
+ * console's JSON API, which `MediaDashboardModule` mounts separately from the SPA. Deriving it here
18
+ * rather than at the call site means a host cannot get that split wrong, and cannot be silently
19
+ * broken by the route moving under a version bump — the break would only show up as a runtime 404.
20
+ */
21
+ export declare function mediaConsoleSessionUrl(apiBasePath?: string): string;
22
+ /** Where the console SPA itself is served. */
23
+ export declare function mediaConsoleUrl(basePath?: string): string;
24
+ export interface OpenConsoleOptions {
25
+ /** Where the console SPA is mounted. MUST match `MediaDashboardModule.forRoot({ basePath })`. */
26
+ basePath?: string;
27
+ /**
28
+ * Where the console's JSON API — and with it the session endpoint — is mounted. MUST match
29
+ * `MediaDashboardModule.forRoot({ apiBasePath })`. Defaults to `${basePath}/api`, the same
30
+ * defaulting the module itself does, so a host that set neither can pass neither.
31
+ */
32
+ apiBasePath?: string;
33
+ /**
34
+ * Headers for the mint request — in practice your app's `Authorization` header. A function (sync
35
+ * or async) is accepted so a token can be read at call time rather than captured at wiring time,
36
+ * which is what a refreshing token needs.
37
+ */
38
+ headers?: HeadersInit | (() => HeadersInit | Promise<HeadersInit>);
39
+ /** Injected for tests and non-browser callers. Defaults to the global `fetch`. */
40
+ fetch?: typeof globalThis.fetch;
41
+ signal?: AbortSignal;
42
+ /**
43
+ * Performs the navigation after a successful mint. Defaults to `location.assign`. Override to
44
+ * route through your own router, or to open in a new tab.
45
+ */
46
+ navigate?: (url: string) => void;
47
+ }
48
+ /** Thrown when the session could not be minted. Never thrown after a successful mint. */
49
+ export declare class ConsoleSessionError extends Error {
50
+ readonly url: string;
51
+ /** The HTTP status, or `undefined` when the request never produced one (network error). */
52
+ readonly status?: number | undefined;
53
+ constructor(message: string, url: string,
54
+ /** The HTTP status, or `undefined` when the request never produced one (network error). */
55
+ status?: number | undefined);
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 declare function mintMediaConsoleSession(options?: OpenConsoleOptions): Promise<void>;
63
+ /**
64
+ * Mint the session, then navigate to the console. Throws without navigating when the mint is
65
+ * refused, so a denied user gets a real error instead of landing on the console's
66
+ * "no session" page — which reads as a bug rather than a permission decision.
67
+ */
68
+ export declare function openMediaConsole(options?: OpenConsoleOptions): Promise<void>;
69
+ //# 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;;;;;;;;;;;GAWG;AAaH;;;;;;;GAOG;AACH,wBAAgB,sBAAsB,CAAC,WAAW,GAAE,MAA8B,GAAG,MAAM,CAE1F;AAED,8CAA8C;AAC9C,wBAAgB,eAAe,CAAC,QAAQ,GAAE,MAA0B,GAAG,MAAM,CAE5E;AAED,MAAM,WAAW,kBAAkB;IACjC,iGAAiG;IACjG,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;;;OAIG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;;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;AAiBD;;;;GAIG;AACH,wBAAsB,uBAAuB,CAAC,OAAO,GAAE,kBAAuB,GAAG,OAAO,CAAC,IAAI,CAAC,CAkD7F;AAED;;;;GAIG;AACH,wBAAsB,gBAAgB,CAAC,OAAO,GAAE,kBAAuB,GAAG,OAAO,CAAC,IAAI,CAAC,CAKtF"}
@@ -0,0 +1,120 @@
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
+ * No UI here on purpose: you own the button, the page and the copy. This module owns the two things
10
+ * a host would otherwise have to rediscover — where the session endpoint actually lives, and how to
11
+ * call it without the failure mode below.
12
+ */
13
+ /** The mount default; matches `DurableDashboardModule.forRoot()`'s own `basePath` default. */
14
+ const DEFAULT_BASE_PATH = '/media';
15
+ /** Matches `MediaDashboardModule`'s own `apiBasePath` default of `${basePath}/api`. */
16
+ const DEFAULT_API_BASE_PATH = '/media/api';
17
+ function normalizeBasePath(basePath) {
18
+ const trimmed = basePath.trim().replace(/\/+$/, '');
19
+ if (trimmed === '')
20
+ return '';
21
+ return trimmed.startsWith('/') ? trimmed : `/${trimmed}`;
22
+ }
23
+ /**
24
+ * Where `POST` mints the console session cookie.
25
+ *
26
+ * Note this derives from `apiBasePath`, NOT `basePath`: the auth controller is mounted with the
27
+ * console's JSON API, which `MediaDashboardModule` mounts separately from the SPA. Deriving it here
28
+ * rather than at the call site means a host cannot get that split wrong, and cannot be silently
29
+ * broken by the route moving under a version bump — the break would only show up as a runtime 404.
30
+ */
31
+ export function mediaConsoleSessionUrl(apiBasePath = DEFAULT_API_BASE_PATH) {
32
+ return `${normalizeBasePath(apiBasePath)}/session`;
33
+ }
34
+ /** Where the console SPA itself is served. */
35
+ export function mediaConsoleUrl(basePath = DEFAULT_BASE_PATH) {
36
+ return normalizeBasePath(basePath) || '/';
37
+ }
38
+ /** Thrown when the session could not be minted. Never thrown after a successful mint. */
39
+ export class ConsoleSessionError extends Error {
40
+ url;
41
+ status;
42
+ constructor(message, url,
43
+ /** The HTTP status, or `undefined` when the request never produced one (network error). */
44
+ status) {
45
+ super(message);
46
+ this.url = url;
47
+ this.status = status;
48
+ this.name = 'ConsoleSessionError';
49
+ }
50
+ }
51
+ /**
52
+ * Mirror the module's own defaulting: an explicit `apiBasePath` wins, otherwise it hangs off
53
+ * `basePath`, so a host that only customised the SPA mount still hits the right session endpoint.
54
+ */
55
+ function resolveApiBasePath(options) {
56
+ if (options.apiBasePath !== undefined)
57
+ return options.apiBasePath;
58
+ if (options.basePath !== undefined)
59
+ return `${normalizeBasePath(options.basePath)}/api`;
60
+ return DEFAULT_API_BASE_PATH;
61
+ }
62
+ async function resolveHeaders(headers) {
63
+ if (headers === undefined)
64
+ return {};
65
+ return typeof headers === 'function' ? await headers() : headers;
66
+ }
67
+ /**
68
+ * Mint the console session cookie. Resolves on success; throws {@link ConsoleSessionError} on
69
+ * refusal. Use this directly when you want to mint without navigating (a pre-flight check, or a
70
+ * link the user opens later).
71
+ */
72
+ export async function mintMediaConsoleSession(options = {}) {
73
+ const url = mediaConsoleSessionUrl(resolveApiBasePath(options));
74
+ const doFetch = options.fetch ?? globalThis.fetch;
75
+ if (typeof doFetch !== 'function') {
76
+ throw new ConsoleSessionError('No `fetch` available; pass one via `options.fetch`.', url);
77
+ }
78
+ let response;
79
+ try {
80
+ response = await doFetch(url, {
81
+ method: 'POST',
82
+ // The whole point: the response's Set-Cookie must stick, and the cookie must ride the
83
+ // navigation that follows.
84
+ credentials: 'include',
85
+ headers: await resolveHeaders(options.headers),
86
+ // Load-bearing, and the reason this helper exists rather than three lines at the call site.
87
+ // `fetch` FOLLOWS redirects by default, so an app whose auth layer rewrites a 401 into a
88
+ // "go to /signin" redirect makes this request resolve 200 against the sign-in HTML —
89
+ // `response.ok` reads true, the caller navigates, and the user lands in a console with no
90
+ // session, which looks exactly like a permissions bug. Handling the redirect explicitly turns
91
+ // that into a clear error instead of a silent false success.
92
+ redirect: 'manual',
93
+ ...(options.signal ? { signal: options.signal } : {}),
94
+ });
95
+ }
96
+ catch (cause) {
97
+ throw new ConsoleSessionError(`Could not reach the console session endpoint at ${url}: ${String(cause)}`, url);
98
+ }
99
+ // `redirect: 'manual'` surfaces differently per runtime: browsers give an opaque response
100
+ // (`type: 'opaqueredirect'`, status 0), Node/undici gives the real 3xx. Both mean the same thing.
101
+ const redirected = response.type === 'opaqueredirect' || (response.status >= 300 && response.status < 400);
102
+ if (redirected) {
103
+ 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);
104
+ }
105
+ if (!response.ok) {
106
+ throw new ConsoleSessionError(`The console refused to open (HTTP ${response.status}).`, url, response.status);
107
+ }
108
+ }
109
+ /**
110
+ * Mint the session, then navigate to the console. Throws without navigating when the mint is
111
+ * refused, so a denied user gets a real error instead of landing on the console's
112
+ * "no session" page — which reads as a bug rather than a permission decision.
113
+ */
114
+ export async function openMediaConsole(options = {}) {
115
+ await mintMediaConsoleSession(options);
116
+ const target = mediaConsoleUrl(options.basePath);
117
+ const navigate = options.navigate ?? ((url) => globalThis.location?.assign(url));
118
+ navigate(target);
119
+ }
120
+ //# 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;;;;;;;;;;;GAWG;AAEH,8FAA8F;AAC9F,MAAM,iBAAiB,GAAG,QAAQ,CAAC;AACnC,uFAAuF;AACvF,MAAM,qBAAqB,GAAG,YAAY,CAAC;AAE3C,SAAS,iBAAiB,CAAC,QAAgB;IACzC,MAAM,OAAO,GAAG,QAAQ,CAAC,IAAI,EAAE,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;IACpD,IAAI,OAAO,KAAK,EAAE;QAAE,OAAO,EAAE,CAAC;IAC9B,OAAO,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,OAAO,EAAE,CAAC;AAC3D,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,sBAAsB,CAAC,cAAsB,qBAAqB;IAChF,OAAO,GAAG,iBAAiB,CAAC,WAAW,CAAC,UAAU,CAAC;AACrD,CAAC;AAED,8CAA8C;AAC9C,MAAM,UAAU,eAAe,CAAC,WAAmB,iBAAiB;IAClE,OAAO,iBAAiB,CAAC,QAAQ,CAAC,IAAI,GAAG,CAAC;AAC5C,CAAC;AA2BD,yFAAyF;AACzF,MAAM,OAAO,mBAAoB,SAAQ,KAAK;IAGjC;IAEA;IAJX,YACE,OAAe,EACN,GAAW;IACpB,2FAA2F;IAClF,MAAe;QAExB,KAAK,CAAC,OAAO,CAAC,CAAC;QAJN,QAAG,GAAH,GAAG,CAAQ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CAAC;AACnB,CAAC"}
@@ -1,3 +1,4 @@
1
+ export { ConsoleSessionError, mediaConsoleSessionUrl, mediaConsoleUrl, mintMediaConsoleSession, openMediaConsole, type OpenConsoleOptions, } from './console-session.js';
1
2
  export * from './types.js';
2
3
  export { apiBase, mediaConsoleClient, type MediaConsoleClient } from './media-console-client.js';
3
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAE,OAAO,EAAE,kBAAkB,EAAE,KAAK,kBAAkB,EAAE,MAAM,2BAA2B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,mBAAmB,EACnB,sBAAsB,EACtB,eAAe,EACf,uBAAuB,EACvB,gBAAgB,EAChB,KAAK,kBAAkB,GACxB,MAAM,sBAAsB,CAAC;AAE9B,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAE,OAAO,EAAE,kBAAkB,EAAE,KAAK,kBAAkB,EAAE,MAAM,2BAA2B,CAAC"}
@@ -1,3 +1,6 @@
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, mediaConsoleSessionUrl, mediaConsoleUrl, mintMediaConsoleSession, openMediaConsole, } from './console-session.js';
1
4
  export * from './types.js';
2
5
  export { apiBase, mediaConsoleClient } from './media-console-client.js';
3
6
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAE,OAAO,EAAE,kBAAkB,EAA2B,MAAM,2BAA2B,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA,gGAAgG;AAChG,8EAA8E;AAC9E,OAAO,EACL,mBAAmB,EACnB,sBAAsB,EACtB,eAAe,EACf,uBAAuB,EACvB,gBAAgB,GAEjB,MAAM,sBAAsB,CAAC;AAE9B,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAE,OAAO,EAAE,kBAAkB,EAA2B,MAAM,2BAA2B,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dudousxd/nestjs-media-dashboard",
3
- "version": "0.10.0",
3
+ "version": "0.11.0",
4
4
  "description": "Standalone navigable /media console (disks, live uploads, media library) for nestjs-media — a bundled React SPA served by NestJS",
5
5
  "license": "MIT",
6
6
  "author": "Davide Carvalho",