@bymax-one/nest-auth 1.4.21 → 1.5.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.
@@ -1,6 +1,6 @@
1
1
  import { ReactNode } from 'react';
2
- import { AuthClient, RegisterInput, ResetPasswordInput } from '@bymax-one/nest-auth/client';
3
- import { AuthUserClient, LoginResult, AuthResult } from '@bymax-one/nest-auth/shared';
2
+ import { AuthClient, RegisterInput, ResetPasswordInput, WorkspaceClientMethods } from '@bymax-one/nest-auth/client';
3
+ import { AuthUserClient, LoginResult, AuthResult, WorkspaceAccessClient } from '@bymax-one/nest-auth/shared';
4
4
 
5
5
  /**
6
6
  * React provider for @bymax-one/nest-auth.
@@ -372,5 +372,122 @@ interface UseAuthStatusResult {
372
372
  */
373
373
  declare function useAuthStatus(): UseAuthStatusResult;
374
374
 
375
- export { AuthProvider, useAuth, useAuthStatus, useSession };
376
- export type { AuthContextValue, AuthProviderProps, AuthStatus, UseAuthResult, UseAuthStatusResult, UseSessionResult };
375
+ /**
376
+ * Where the list of the caller's workspaces stands.
377
+ *
378
+ * `disabled` means the host rendered no `<WorkspaceProvider>`, so nothing is loaded and no request
379
+ * is made. `idle` is the state before sign-in and after sign-out.
380
+ */
381
+ type WorkspacesStatus = 'disabled' | 'idle' | 'loading' | 'ready' | 'error';
382
+ /** The codes that mean the active workspace refused the caller. */
383
+ type WorkspaceRefusalCode = 'auth.workspace_not_found' | 'auth.insufficient_workspace_role' | 'auth.workspace_pending_deletion';
384
+ /** What a host passes to `<WorkspaceProvider>`, beside its `client`. */
385
+ interface AuthWorkspacesOptions {
386
+ /**
387
+ * The workspace the current URL names, read by the host from its router. It outranks everything
388
+ * else, because the workspace a request acts in travels with the request, so two tabs on two
389
+ * workspaces each keep their own. When it is absent the active workspace is the one chosen with
390
+ * `switchWorkspace`, then the server's `lastActiveWorkspaceId`; it is never read from
391
+ * `localStorage`.
392
+ */
393
+ activeWorkspaceId?: string | undefined;
394
+ /**
395
+ * The server's `workspaces.roles.hierarchy`, so `hasWorkspaceRole` can answer for a role that
396
+ * includes another. It must match the server's configuration or the hints will be wrong; without
397
+ * it only an exact role matches. Either way the answer is a hint for what to render and carries
398
+ * no authority: the server decides every request.
399
+ */
400
+ hierarchy?: Readonly<Record<string, readonly string[]>> | undefined;
401
+ /**
402
+ * Called when the active workspace changes after the first load, with the one it left and the one
403
+ * it became (`null` when none). Clear the caches keyed by workspace here, so data of the previous
404
+ * workspace is never rendered under the new one. A callback that throws is logged and ignored.
405
+ */
406
+ onWorkspaceChanged?: ((previous: WorkspaceAccessClient | null, next: WorkspaceAccessClient | null) => void) | undefined;
407
+ /**
408
+ * Called when `handleWorkspaceError` is given a workspace refusal that the ACTIVE workspace
409
+ * answered, for example to redirect to a workspace picker. A refusal answered for any other
410
+ * workspace never calls it. A callback that throws is logged and ignored.
411
+ */
412
+ onWorkspaceAccessLost?: ((workspaceId: string, code: WorkspaceRefusalCode) => void) | undefined;
413
+ }
414
+ /** What `useWorkspaces()` returns. */
415
+ interface WorkspacesContextValue {
416
+ /** The caller's own workspaces and their role in each. Empty until `status` is `ready`. */
417
+ memberships: readonly WorkspaceAccessClient[];
418
+ /** The active workspace, or `null` when none is chosen or it is not among the memberships. */
419
+ activeWorkspace: WorkspaceAccessClient | null;
420
+ /** The workspace the caller last switched to, as the server remembers it. */
421
+ lastActiveWorkspaceId: string | null;
422
+ status: WorkspacesStatus;
423
+ /**
424
+ * Records the workspace as the caller's last active one and makes it the chosen one. The server's
425
+ * refusal rejects and changes nothing. With `activeWorkspaceId` set the URL still outranks it:
426
+ * navigate there too.
427
+ */
428
+ switchWorkspace: (workspaceId: string) => Promise<void>;
429
+ /**
430
+ * Whether the caller's role in a workspace is the given one, or includes it through `hierarchy`.
431
+ * Answers for the active workspace, or for `workspaceId` when given. A hint, never an authority.
432
+ */
433
+ hasWorkspaceRole: (role: string, workspaceId?: string) => boolean;
434
+ /**
435
+ * Hands an error caught from a call made for `workspaceId` to the provider. A workspace refusal
436
+ * for the ACTIVE workspace reloads the list and calls `onWorkspaceAccessLost`; anything else is
437
+ * left alone. Returns whether it was one, so a caller can stop handling it.
438
+ */
439
+ handleWorkspaceError: (workspaceId: string, error: unknown) => boolean;
440
+ /** Reloads the list. */
441
+ refresh: () => Promise<void>;
442
+ }
443
+
444
+ /**
445
+ * The state of `<WorkspaceProvider>`, as small hooks: the list of the caller's workspaces, the
446
+ * notice that the active one changed, the cross-tab channel, the role hint and the refusal handler.
447
+ * Each takes what it reads and nothing else, so `WorkspaceProvider` only assembles them.
448
+ */
449
+
450
+ /** What the provider needs of the workspace client: the list and the switch. */
451
+ type WorkspaceProviderClient = Pick<WorkspaceClientMethods, 'listWorkspaces' | 'switchWorkspace'>;
452
+
453
+ /**
454
+ * The workspace half of the provider: loads the caller's workspaces after sign-in, resolves the
455
+ * active one, switches, and tells the host when the active workspace changes or refuses the caller.
456
+ *
457
+ * Rendered by the host, inside `<AuthProvider>`, and by nothing else: a host that does not render it
458
+ * makes no workspace request, and a bundler drops all of it from that host's build.
459
+ */
460
+
461
+ /** Props of {@link WorkspaceProvider}: the workspace client, the host's options and the tree. */
462
+ interface WorkspaceProviderProps extends AuthWorkspacesOptions {
463
+ /** The result of `createWorkspaceClient`. */
464
+ client: WorkspaceProviderClient;
465
+ children: ReactNode;
466
+ }
467
+ /**
468
+ * Provides {@link WorkspacesContext} to the tree below it.
469
+ *
470
+ * @param props - The client, the host's options and the tree.
471
+ * @returns The tree inside the context.
472
+ */
473
+ declare function WorkspaceProvider({ client, children, ...options }: WorkspaceProviderProps): ReactNode;
474
+
475
+ /**
476
+ * `useWorkspaces()`: the caller's workspaces, the active one, and the calls that act on them.
477
+ */
478
+
479
+ /**
480
+ * Reads the workspace state of the nearest `<WorkspaceProvider>`.
481
+ *
482
+ * Under a `<WorkspaceProvider>` it answers from the caller's own workspaces; without one every call
483
+ * is inert and `status` is `disabled`, so a component can be written once and used with
484
+ * or without the feature. `hasWorkspaceRole` is a hint for what to render, never an authority: the
485
+ * server decides every request.
486
+ *
487
+ * @returns The workspace state and calls.
488
+ * @throws {Error} When called outside an `<AuthProvider>`.
489
+ */
490
+ declare function useWorkspaces(): WorkspacesContextValue;
491
+
492
+ export { AuthProvider, WorkspaceProvider, useAuth, useAuthStatus, useSession, useWorkspaces };
493
+ export type { AuthContextValue, AuthProviderProps, AuthStatus, AuthWorkspacesOptions, UseAuthResult, UseAuthStatusResult, UseSessionResult, WorkspaceProviderClient, WorkspaceProviderProps, WorkspaceRefusalCode, WorkspacesContextValue, WorkspacesStatus };
@@ -1,6 +1,6 @@
1
1
  import { ReactNode } from 'react';
2
- import { AuthClient, RegisterInput, ResetPasswordInput } from '@bymax-one/nest-auth/client';
3
- import { AuthUserClient, LoginResult, AuthResult } from '@bymax-one/nest-auth/shared';
2
+ import { AuthClient, RegisterInput, ResetPasswordInput, WorkspaceClientMethods } from '@bymax-one/nest-auth/client';
3
+ import { AuthUserClient, LoginResult, AuthResult, WorkspaceAccessClient } from '@bymax-one/nest-auth/shared';
4
4
 
5
5
  /**
6
6
  * React provider for @bymax-one/nest-auth.
@@ -372,5 +372,122 @@ interface UseAuthStatusResult {
372
372
  */
373
373
  declare function useAuthStatus(): UseAuthStatusResult;
374
374
 
375
- export { AuthProvider, useAuth, useAuthStatus, useSession };
376
- export type { AuthContextValue, AuthProviderProps, AuthStatus, UseAuthResult, UseAuthStatusResult, UseSessionResult };
375
+ /**
376
+ * Where the list of the caller's workspaces stands.
377
+ *
378
+ * `disabled` means the host rendered no `<WorkspaceProvider>`, so nothing is loaded and no request
379
+ * is made. `idle` is the state before sign-in and after sign-out.
380
+ */
381
+ type WorkspacesStatus = 'disabled' | 'idle' | 'loading' | 'ready' | 'error';
382
+ /** The codes that mean the active workspace refused the caller. */
383
+ type WorkspaceRefusalCode = 'auth.workspace_not_found' | 'auth.insufficient_workspace_role' | 'auth.workspace_pending_deletion';
384
+ /** What a host passes to `<WorkspaceProvider>`, beside its `client`. */
385
+ interface AuthWorkspacesOptions {
386
+ /**
387
+ * The workspace the current URL names, read by the host from its router. It outranks everything
388
+ * else, because the workspace a request acts in travels with the request, so two tabs on two
389
+ * workspaces each keep their own. When it is absent the active workspace is the one chosen with
390
+ * `switchWorkspace`, then the server's `lastActiveWorkspaceId`; it is never read from
391
+ * `localStorage`.
392
+ */
393
+ activeWorkspaceId?: string | undefined;
394
+ /**
395
+ * The server's `workspaces.roles.hierarchy`, so `hasWorkspaceRole` can answer for a role that
396
+ * includes another. It must match the server's configuration or the hints will be wrong; without
397
+ * it only an exact role matches. Either way the answer is a hint for what to render and carries
398
+ * no authority: the server decides every request.
399
+ */
400
+ hierarchy?: Readonly<Record<string, readonly string[]>> | undefined;
401
+ /**
402
+ * Called when the active workspace changes after the first load, with the one it left and the one
403
+ * it became (`null` when none). Clear the caches keyed by workspace here, so data of the previous
404
+ * workspace is never rendered under the new one. A callback that throws is logged and ignored.
405
+ */
406
+ onWorkspaceChanged?: ((previous: WorkspaceAccessClient | null, next: WorkspaceAccessClient | null) => void) | undefined;
407
+ /**
408
+ * Called when `handleWorkspaceError` is given a workspace refusal that the ACTIVE workspace
409
+ * answered, for example to redirect to a workspace picker. A refusal answered for any other
410
+ * workspace never calls it. A callback that throws is logged and ignored.
411
+ */
412
+ onWorkspaceAccessLost?: ((workspaceId: string, code: WorkspaceRefusalCode) => void) | undefined;
413
+ }
414
+ /** What `useWorkspaces()` returns. */
415
+ interface WorkspacesContextValue {
416
+ /** The caller's own workspaces and their role in each. Empty until `status` is `ready`. */
417
+ memberships: readonly WorkspaceAccessClient[];
418
+ /** The active workspace, or `null` when none is chosen or it is not among the memberships. */
419
+ activeWorkspace: WorkspaceAccessClient | null;
420
+ /** The workspace the caller last switched to, as the server remembers it. */
421
+ lastActiveWorkspaceId: string | null;
422
+ status: WorkspacesStatus;
423
+ /**
424
+ * Records the workspace as the caller's last active one and makes it the chosen one. The server's
425
+ * refusal rejects and changes nothing. With `activeWorkspaceId` set the URL still outranks it:
426
+ * navigate there too.
427
+ */
428
+ switchWorkspace: (workspaceId: string) => Promise<void>;
429
+ /**
430
+ * Whether the caller's role in a workspace is the given one, or includes it through `hierarchy`.
431
+ * Answers for the active workspace, or for `workspaceId` when given. A hint, never an authority.
432
+ */
433
+ hasWorkspaceRole: (role: string, workspaceId?: string) => boolean;
434
+ /**
435
+ * Hands an error caught from a call made for `workspaceId` to the provider. A workspace refusal
436
+ * for the ACTIVE workspace reloads the list and calls `onWorkspaceAccessLost`; anything else is
437
+ * left alone. Returns whether it was one, so a caller can stop handling it.
438
+ */
439
+ handleWorkspaceError: (workspaceId: string, error: unknown) => boolean;
440
+ /** Reloads the list. */
441
+ refresh: () => Promise<void>;
442
+ }
443
+
444
+ /**
445
+ * The state of `<WorkspaceProvider>`, as small hooks: the list of the caller's workspaces, the
446
+ * notice that the active one changed, the cross-tab channel, the role hint and the refusal handler.
447
+ * Each takes what it reads and nothing else, so `WorkspaceProvider` only assembles them.
448
+ */
449
+
450
+ /** What the provider needs of the workspace client: the list and the switch. */
451
+ type WorkspaceProviderClient = Pick<WorkspaceClientMethods, 'listWorkspaces' | 'switchWorkspace'>;
452
+
453
+ /**
454
+ * The workspace half of the provider: loads the caller's workspaces after sign-in, resolves the
455
+ * active one, switches, and tells the host when the active workspace changes or refuses the caller.
456
+ *
457
+ * Rendered by the host, inside `<AuthProvider>`, and by nothing else: a host that does not render it
458
+ * makes no workspace request, and a bundler drops all of it from that host's build.
459
+ */
460
+
461
+ /** Props of {@link WorkspaceProvider}: the workspace client, the host's options and the tree. */
462
+ interface WorkspaceProviderProps extends AuthWorkspacesOptions {
463
+ /** The result of `createWorkspaceClient`. */
464
+ client: WorkspaceProviderClient;
465
+ children: ReactNode;
466
+ }
467
+ /**
468
+ * Provides {@link WorkspacesContext} to the tree below it.
469
+ *
470
+ * @param props - The client, the host's options and the tree.
471
+ * @returns The tree inside the context.
472
+ */
473
+ declare function WorkspaceProvider({ client, children, ...options }: WorkspaceProviderProps): ReactNode;
474
+
475
+ /**
476
+ * `useWorkspaces()`: the caller's workspaces, the active one, and the calls that act on them.
477
+ */
478
+
479
+ /**
480
+ * Reads the workspace state of the nearest `<WorkspaceProvider>`.
481
+ *
482
+ * Under a `<WorkspaceProvider>` it answers from the caller's own workspaces; without one every call
483
+ * is inert and `status` is `disabled`, so a component can be written once and used with
484
+ * or without the feature. `hasWorkspaceRole` is a hint for what to render, never an authority: the
485
+ * server decides every request.
486
+ *
487
+ * @returns The workspace state and calls.
488
+ * @throws {Error} When called outside an `<AuthProvider>`.
489
+ */
490
+ declare function useWorkspaces(): WorkspacesContextValue;
491
+
492
+ export { AuthProvider, WorkspaceProvider, useAuth, useAuthStatus, useSession, useWorkspaces };
493
+ export type { AuthContextValue, AuthProviderProps, AuthStatus, AuthWorkspacesOptions, UseAuthResult, UseAuthStatusResult, UseSessionResult, WorkspaceProviderClient, WorkspaceProviderProps, WorkspaceRefusalCode, WorkspacesContextValue, WorkspacesStatus };