@better-auth-ui/core
Version:
Authentication components and data utilities for [Better Auth](https://better-auth.com), available for React and Solid.
47 lines (46 loc) • 3.35 kB
TypeScript
import { DataTag, QueryClient, QueryOptions } from '@tanstack/query-core';
import { APIError } from 'better-auth';
import { OrganizationAuthServer } from '../../../plugins/organization/server/organization-auth-server';
import { FullOrganizationParams } from './full-organization-query';
import { ListOrganization } from './list-organizations-query';
/**
* Cache shape for the active organization. Intentionally narrowed to
* {@link ListOrganization} (basic fields only) so the cache stays
* compatible with `setActive`'s optimistic update, which can only produce
* list-shaped data. Use {@link FullOrganizationData} (via
* `fullOrganizationOptions`) when you need members / invitations.
*/
export type ActiveOrganizationData<TAuth extends OrganizationAuthServer = OrganizationAuthServer> = ListOrganization<TAuth> | null;
/**
* Same shape as {@link FullOrganizationParams} so server-side prefetches
* can target a specific organization (e.g. by `query.organizationSlug` for
* slug-driven routes) and produce a cache key that matches the client's
* `useActiveOrganization({ query: { organizationSlug } })` on hydration.
*/
export type ActiveOrganizationParams<TAuth extends OrganizationAuthServer = OrganizationAuthServer> = FullOrganizationParams<TAuth>;
/**
* Query options factory for the active organization. Shares its cache key
* with the client-side `activeOrganizationOptions` (including any
* `params.query` partition such as `{ organizationSlug }`) so SSR-prefetched
* data hydrates without a refetch — even on slug-prefixed routes.
*
* @param auth - The Better Auth server instance.
* @param userId - The signed-in user's ID. Used for cache partitioning so
* the key matches the client-side `activeOrganizationOptions` for SSR hydration.
* @param params - Parameters forwarded to `auth.api.getFullOrganization`. Pass
* `{ query: { organizationSlug } }` for slug-driven prefetches.
*/
export declare function activeOrganizationOptions<TAuth extends OrganizationAuthServer>(auth: TAuth, userId: string, params: ActiveOrganizationParams<TAuth>): QueryOptions<ActiveOrganizationData<TAuth>, APIError, ActiveOrganizationData<TAuth>, readonly ["auth", "user", string | undefined, "organization", "active", {
organizationId?: string | undefined;
organizationSlug?: string | undefined;
membersLimit?: string | number | undefined;
} | null], never> & {
queryKey: DataTag<readonly ["auth", "user", string | undefined, "organization", "active", {
organizationId?: string | undefined;
organizationSlug?: string | undefined;
membersLimit?: string | number | undefined;
} | null], ActiveOrganizationData<TAuth>, APIError>;
};
export declare const ensureActiveOrganization: <TAuth extends OrganizationAuthServer>(queryClient: QueryClient, auth: TAuth, userId: string, params: ActiveOrganizationParams<TAuth>) => Promise<ListOrganization<TAuth> | null>;
export declare const prefetchActiveOrganization: <TAuth extends OrganizationAuthServer>(queryClient: QueryClient, auth: TAuth, userId: string, params: ActiveOrganizationParams<TAuth>) => Promise<void>;
export declare const fetchActiveOrganization: <TAuth extends OrganizationAuthServer>(queryClient: QueryClient, auth: TAuth, userId: string, params: ActiveOrganizationParams<TAuth>) => Promise<ListOrganization<TAuth> | null>;