@sanity/sdk-react 0.0.0-alpha.12 → 0.0.0-alpha.13

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.
@@ -94,7 +94,10 @@ function LoginError({
94
94
  /* @__PURE__ */ jsx("button", { className: "sc-login-error__button", onClick: handleRetry, children: "Retry" })
95
95
  ] }) });
96
96
  }
97
- isInIframe() && import("@sanity/os/bridge");
97
+ if (isInIframe()) {
98
+ const parsedUrl = new URL(window.location.href), mode = new URLSearchParams(parsedUrl.hash.slice(1)).get("mode"), script = document.createElement("script");
99
+ script.src = mode === "core-ui--staging" ? "https://core.sanity-cdn.work/bridge.js" : "https://core.sanity-cdn.com/bridge.js", script.type = "module", script.async = !0, document.head.appendChild(script);
100
+ }
98
101
  function AuthBoundary({
99
102
  LoginErrorComponent = LoginError,
100
103
  ...props
@@ -1 +1 @@
1
- {"version":3,"file":"components.js","sources":["../src/components/utils.ts","../src/components/auth/AuthError.ts","../src/components/auth/LoginFooter.tsx","../src/components/auth/LoginLayout.tsx","../src/components/auth/Login.tsx","../src/components/auth/LoginCallback.tsx","../src/components/auth/LoginError.tsx","../src/components/auth/AuthBoundary.tsx","../src/components/SanityApp.tsx"],"sourcesContent":["export function isInIframe(): boolean {\n return typeof window !== 'undefined' && window.self !== window.top\n}\n","/**\n * Error class for authentication-related errors. Wraps errors thrown during the\n * authentication flow.\n *\n * @remarks\n * This class provides a consistent error type for authentication failures while\n * preserving the original error as the cause. If the original error has a\n * message property, it will be used as the error message.\n *\n * @alpha\n */\nexport class AuthError extends Error {\n constructor(error: unknown) {\n if (\n typeof error === 'object' &&\n !!error &&\n 'message' in error &&\n typeof error.message === 'string'\n ) {\n super(error.message)\n } else {\n super()\n }\n\n this.cause = error\n }\n}\n","import {SanityLogo} from '@sanity/logos'\nimport {Fragment} from 'react'\n\nconst LINKS = [\n {\n url: 'https://slack.sanity.io/',\n i18nKey: 'workspaces.community-title',\n title: 'Community',\n },\n {\n url: 'https://www.sanity.io/docs',\n i18nKey: 'workspaces.docs-title',\n title: 'Docs',\n },\n {\n url: 'https://www.sanity.io/legal/privacy',\n i18nKey: 'workspaces.privacy-title',\n title: 'Privacy',\n },\n {\n url: 'https://www.sanity.io',\n i18nKey: 'workspaces.sanity-io-title',\n title: 'sanity.io',\n },\n]\n\n/**\n * Default footer component for login screens showing Sanity branding and legal\n * links.\n *\n * @alpha\n */\nexport function LoginFooter(): React.ReactNode {\n return (\n <div className=\"sc-login-footer\">\n <SanityLogo className=\"sc-login-footer__logo\" />\n\n <ul className=\"sc-login-footer__links\">\n {LINKS.map((link) => (\n <Fragment key={link.title}>\n <li className=\"sc-login-footer__link\">\n <a href={link.url} target=\"_blank\" rel=\"noopener noreferrer\">\n {link.title}\n </a>\n </li>\n </Fragment>\n ))}\n </ul>\n </div>\n )\n}\n","import {LoginFooter} from './LoginFooter'\n\n/**\n * @alpha\n */\nexport interface LoginLayoutProps {\n /** Optional header content rendered at top of card */\n header?: React.ReactNode\n\n /** Optional footer content rendered below card. Defaults to an internal login footer */\n footer?: React.ReactNode\n\n /** Main content rendered in card body */\n children?: React.ReactNode\n}\n\n/**\n * Layout component for login-related screens providing consistent styling and structure.\n * Renders content in a centered card with optional header and footer sections.\n *\n * Can be used to build custom login screens for the AuthBoundary component, including:\n * - Login provider selection (LoginComponent)\n * - OAuth callback handling (CallbackComponent)\n * - Error states (LoginErrorComponent)\n *\n * @example\n * ```tsx\n * // Custom login screen using the layout\n * function CustomLogin({header, footer}: LoginLayoutProps) {\n * return (\n * <LoginLayout\n * header={header}\n * footer={footer}\n * >\n * <CustomLoginContent />\n * </LoginLayout>\n * )\n * }\n *\n * // Use with AuthBoundary\n * <AuthBoundary\n * LoginComponent={CustomLogin}\n * header={<Logo />}\n * >\n * <ProtectedContent />\n * </AuthBoundary>\n * ```\n *\n * @alpha\n */\nexport function LoginLayout({\n children,\n footer = <LoginFooter />,\n header,\n}: LoginLayoutProps): React.ReactNode {\n return (\n <div className=\"sc-login-layout\">\n <div className=\"sc-login-layout__container\">\n <div className=\"sc-login-layout__card\">\n {header && <div className=\"sc-login-layout__card-header\">{header}</div>}\n\n {children && <div className=\"sc-login-layout__card-body\">{children}</div>}\n </div>\n\n {footer}\n </div>\n </div>\n )\n}\n","import {type JSX, Suspense} from 'react'\n\nimport {useLoginUrls} from '../../hooks/auth/useLoginUrls'\nimport {LoginLayout, type LoginLayoutProps} from './LoginLayout'\n\n/**\n * Login component that displays available authentication providers.\n * Renders a list of login options with a loading fallback while providers load.\n *\n * @alpha\n * @internal\n */\nexport function Login({header, footer}: LoginLayoutProps): JSX.Element {\n return (\n <LoginLayout header={header} footer={footer}>\n <div className=\"sc-login\">\n <h1 className=\"sc-login__title\">Choose login provider</h1>\n\n <Suspense fallback={<div className=\"sc-login__loading\">Loading…</div>}>\n <Providers />\n </Suspense>\n </div>\n </LoginLayout>\n )\n}\n\nfunction Providers() {\n const loginUrls = useLoginUrls()\n\n return (\n <div className=\"sc-login-providers\">\n {loginUrls.map(({title, url}) => (\n <a key={url} href={url}>\n {title}\n </a>\n ))}\n </div>\n )\n}\n","import {useEffect} from 'react'\n\nimport {useHandleCallback} from '../../hooks/auth/useHandleCallback'\nimport {LoginLayout, type LoginLayoutProps} from './LoginLayout'\n\n/**\n/**\n * Component shown during auth callback processing that handles login completion.\n * Automatically processes the auth callback when mounted and updates the URL\n * to remove callback parameters without triggering a page reload.\n *\n * @alpha\n */\nexport function LoginCallback({header, footer}: LoginLayoutProps): React.ReactNode {\n const handleCallback = useHandleCallback()\n\n useEffect(() => {\n const url = new URL(location.href)\n handleCallback(url.toString()).then((replacementLocation) => {\n if (replacementLocation) {\n // history API with `replaceState` is used to prevent a reload but still\n // remove the short-lived token from the URL\n history.replaceState(null, '', replacementLocation)\n }\n })\n }, [handleCallback])\n\n return (\n <LoginLayout header={header} footer={footer}>\n <div className=\"sc-login-callback\">\n <h1 className=\"sc-login-callback__title\">Logging you in…</h1>\n <div className=\"sc-login-callback__loading\">Loading…</div>\n </div>\n </LoginLayout>\n )\n}\n","import {useCallback} from 'react'\nimport {type FallbackProps} from 'react-error-boundary'\n\nimport {useLogOut} from '../../hooks/auth/useLogOut'\nimport {AuthError} from './AuthError'\nimport {LoginLayout, type LoginLayoutProps} from './LoginLayout'\n\n/**\n * @alpha\n */\nexport type LoginErrorProps = FallbackProps & LoginLayoutProps\n\n/**\n * Displays authentication error details and provides retry functionality.\n * Only handles {@link AuthError} instances - rethrows other error types.\n *\n * @alpha\n */\nexport function LoginError({\n error,\n resetErrorBoundary,\n header,\n footer,\n}: LoginErrorProps): React.ReactNode {\n if (!(error instanceof AuthError)) throw error\n const logout = useLogOut()\n\n const handleRetry = useCallback(async () => {\n await logout()\n resetErrorBoundary()\n }, [logout, resetErrorBoundary])\n\n return (\n <LoginLayout header={header} footer={footer}>\n <div className=\"sc-login-error\">\n <div className=\"sc-login-error__content\">\n <h2 className=\"sc-login-error__title\">Authentication Error</h2>\n <p className=\"sc-login-error__description\">\n Please try again or contact support if the problem persists.\n </p>\n </div>\n\n <button className=\"sc-login-error__button\" onClick={handleRetry}>\n Retry\n </button>\n </div>\n </LoginLayout>\n )\n}\n","import {AuthStateType} from '@sanity/sdk'\nimport {useMemo} from 'react'\nimport {ErrorBoundary, type FallbackProps} from 'react-error-boundary'\n\nimport {useAuthState} from '../../hooks/auth/useAuthState'\nimport {isInIframe} from '../utils'\nimport {AuthError} from './AuthError'\nimport {Login} from './Login'\nimport {LoginCallback} from './LoginCallback'\nimport {LoginError, type LoginErrorProps} from './LoginError'\nimport {type LoginLayoutProps} from './LoginLayout'\n\n// Only import bridge if we're in an iframe. This assumes that the app is\n// running within SanityOS if it is in an iframe.\nif (isInIframe()) {\n import('@sanity/os/bridge')\n}\n\n/**\n * @internal\n */\ninterface AuthBoundaryProps extends LoginLayoutProps {\n /**\n * Custom component to render the login screen.\n * Receives all login layout props. Defaults to {@link Login}.\n */\n LoginComponent?: React.ComponentType<LoginLayoutProps>\n\n /**\n * Custom component to render during OAuth callback processing.\n * Receives all login layout props. Defaults to {@link LoginCallback}.\n */\n CallbackComponent?: React.ComponentType<LoginLayoutProps>\n\n /**\n * Custom component to render when authentication errors occur.\n * Receives login layout props and error boundary props. Defaults to\n * {@link LoginError}\n */\n LoginErrorComponent?: React.ComponentType<LoginErrorProps>\n}\n\n/**\n * A component that handles authentication flow and error boundaries for a\n * protected section of the application.\n *\n * @remarks\n * This component manages different authentication states and renders the\n * appropriate components based on that state.\n *\n * @example\n * ```tsx\n * function App() {\n * return (\n * <AuthBoundary header={<MyLogo />}>\n * <ProtectedContent />\n * </AuthBoundary>\n * )\n * }\n * ```\n *\n * @internal\n */\nexport function AuthBoundary({\n LoginErrorComponent = LoginError,\n ...props\n}: AuthBoundaryProps): React.ReactNode {\n const {header, footer} = props\n const FallbackComponent = useMemo(() => {\n return function LoginComponentWithLayoutProps(fallbackProps: FallbackProps) {\n return <LoginErrorComponent {...fallbackProps} header={header} footer={footer} />\n }\n }, [header, footer, LoginErrorComponent])\n\n return (\n <ErrorBoundary FallbackComponent={FallbackComponent}>\n <AuthSwitch {...props} />\n </ErrorBoundary>\n )\n}\n\ninterface AuthSwitchProps extends LoginLayoutProps {\n LoginComponent?: React.ComponentType<LoginLayoutProps>\n CallbackComponent?: React.ComponentType<LoginLayoutProps>\n}\n\nfunction AuthSwitch({\n LoginComponent = Login,\n CallbackComponent = LoginCallback,\n children,\n ...props\n}: AuthSwitchProps) {\n const authState = useAuthState()\n\n switch (authState.type) {\n case AuthStateType.ERROR: {\n throw new AuthError(authState.error)\n }\n case AuthStateType.LOGGING_IN: {\n return <CallbackComponent {...props} />\n }\n case AuthStateType.LOGGED_IN: {\n return children\n }\n default: {\n return <LoginComponent {...props} />\n }\n }\n}\n","import {createSanityInstance, type SanityConfig} from '@sanity/sdk'\nimport {type ReactElement} from 'react'\n\nimport {SanityProvider} from '../context/SanityProvider'\nimport {AuthBoundary} from './auth/AuthBoundary'\nimport {isInIframe} from './utils'\n\n/**\n * @public\n */\nexport interface SanityAppProps {\n sanityConfig: SanityConfig\n children: React.ReactNode\n}\n\n/**\n * @public\n *\n * The SanityApp component provides your Sanity application with access to your Sanity configuration,\n * as well as application context and state which is used by the Sanity React hooks. Your application\n * must be wrapped with the SanityApp component to function properly.\n *\n * @param props - Your Sanity configuration and the React children to render\n * @returns Your Sanity application, integrated with your Sanity configuration and application context\n *\n * @example\n * ```\n * import { SanityApp } from '@sanity/sdk-react\n *\n * import MyAppRoot from './Root'\n *\n * const mySanityConfig = {\n * procectId: 'my-project-id',\n * dataset: 'production',\n * }\n *\n * export default function MyApp() {\n * return (\n * <SanityApp sanityConfig={mySanityConfig}>\n * <MyAppRoot />\n * </SanityApp>\n * )\n * }\n * ```\n */\nexport function SanityApp({sanityConfig, children}: SanityAppProps): ReactElement {\n if (isInIframe()) {\n // When running in an iframe Content OS, we don't want to store tokens\n sanityConfig.auth = {\n ...sanityConfig.auth,\n storageArea: undefined,\n }\n }\n const sanityInstance = createSanityInstance(sanityConfig)\n\n return (\n <SanityProvider sanityInstance={sanityInstance}>\n <AuthBoundary>{children}</AuthBoundary>\n </SanityProvider>\n )\n}\n"],"names":[],"mappings":";;;;;;;AAAO,SAAS,aAAsB;AACpC,SAAO,OAAO,SAAW,OAAe,OAAO,SAAS,OAAO;AACjE;ACSO,MAAM,kBAAkB,MAAM;AAAA,EACnC,YAAY,OAAgB;AAExB,WAAO,SAAU,YACf,SACF,aAAa,SACb,OAAO,MAAM,WAAY,WAEzB,MAAM,MAAM,OAAO,IAEnB,MAAM,GAGR,KAAK,QAAQ;AAAA,EAAA;AAEjB;ACvBA,MAAM,QAAQ;AAAA,EACZ;AAAA,IACE,KAAK;AAAA,IACL,SAAS;AAAA,IACT,OAAO;AAAA,EACT;AAAA,EACA;AAAA,IACE,KAAK;AAAA,IACL,SAAS;AAAA,IACT,OAAO;AAAA,EACT;AAAA,EACA;AAAA,IACE,KAAK;AAAA,IACL,SAAS;AAAA,IACT,OAAO;AAAA,EACT;AAAA,E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ts","../src/components/auth/LoginFooter.tsx","../src/components/auth/LoginLayout.tsx","../src/components/auth/Login.tsx","../src/components/auth/LoginCallback.tsx","../src/components/auth/LoginError.tsx","../src/components/auth/AuthBoundary.tsx","../src/components/SanityApp.tsx"],"sourcesContent":["export function isInIframe(): boolean {\n return typeof window !== 'undefined' && window.self !== window.top\n}\n","/**\n * Error class for authentication-related errors. Wraps errors thrown during the\n * authentication flow.\n *\n * @remarks\n * This class provides a consistent error type for authentication failures while\n * preserving the original error as the cause. If the original error has a\n * message property, it will be used as the error message.\n *\n * @alpha\n */\nexport class AuthError extends Error {\n constructor(error: unknown) {\n if (\n typeof error === 'object' &&\n !!error &&\n 'message' in error &&\n typeof error.message === 'string'\n ) {\n super(error.message)\n } else {\n super()\n }\n\n this.cause = error\n }\n}\n","import {SanityLogo} from '@sanity/logos'\nimport {Fragment} from 'react'\n\nconst LINKS = [\n {\n url: 'https://slack.sanity.io/',\n i18nKey: 'workspaces.community-title',\n title: 'Community',\n },\n {\n url: 'https://www.sanity.io/docs',\n i18nKey: 'workspaces.docs-title',\n title: 'Docs',\n },\n {\n url: 'https://www.sanity.io/legal/privacy',\n i18nKey: 'workspaces.privacy-title',\n title: 'Privacy',\n },\n {\n url: 'https://www.sanity.io',\n i18nKey: 'workspaces.sanity-io-title',\n title: 'sanity.io',\n },\n]\n\n/**\n * Default footer component for login screens showing Sanity branding and legal\n * links.\n *\n * @alpha\n */\nexport function LoginFooter(): React.ReactNode {\n return (\n <div className=\"sc-login-footer\">\n <SanityLogo className=\"sc-login-footer__logo\" />\n\n <ul className=\"sc-login-footer__links\">\n {LINKS.map((link) => (\n <Fragment key={link.title}>\n <li className=\"sc-login-footer__link\">\n <a href={link.url} target=\"_blank\" rel=\"noopener noreferrer\">\n {link.title}\n </a>\n </li>\n </Fragment>\n ))}\n </ul>\n </div>\n )\n}\n","import {LoginFooter} from './LoginFooter'\n\n/**\n * @alpha\n */\nexport interface LoginLayoutProps {\n /** Optional header content rendered at top of card */\n header?: React.ReactNode\n\n /** Optional footer content rendered below card. Defaults to an internal login footer */\n footer?: React.ReactNode\n\n /** Main content rendered in card body */\n children?: React.ReactNode\n}\n\n/**\n * Layout component for login-related screens providing consistent styling and structure.\n * Renders content in a centered card with optional header and footer sections.\n *\n * Can be used to build custom login screens for the AuthBoundary component, including:\n * - Login provider selection (LoginComponent)\n * - OAuth callback handling (CallbackComponent)\n * - Error states (LoginErrorComponent)\n *\n * @example\n * ```tsx\n * // Custom login screen using the layout\n * function CustomLogin({header, footer}: LoginLayoutProps) {\n * return (\n * <LoginLayout\n * header={header}\n * footer={footer}\n * >\n * <CustomLoginContent />\n * </LoginLayout>\n * )\n * }\n *\n * // Use with AuthBoundary\n * <AuthBoundary\n * LoginComponent={CustomLogin}\n * header={<Logo />}\n * >\n * <ProtectedContent />\n * </AuthBoundary>\n * ```\n *\n * @alpha\n */\nexport function LoginLayout({\n children,\n footer = <LoginFooter />,\n header,\n}: LoginLayoutProps): React.ReactNode {\n return (\n <div className=\"sc-login-layout\">\n <div className=\"sc-login-layout__container\">\n <div className=\"sc-login-layout__card\">\n {header && <div className=\"sc-login-layout__card-header\">{header}</div>}\n\n {children && <div className=\"sc-login-layout__card-body\">{children}</div>}\n </div>\n\n {footer}\n </div>\n </div>\n )\n}\n","import {type JSX, Suspense} from 'react'\n\nimport {useLoginUrls} from '../../hooks/auth/useLoginUrls'\nimport {LoginLayout, type LoginLayoutProps} from './LoginLayout'\n\n/**\n * Login component that displays available authentication providers.\n * Renders a list of login options with a loading fallback while providers load.\n *\n * @alpha\n * @internal\n */\nexport function Login({header, footer}: LoginLayoutProps): JSX.Element {\n return (\n <LoginLayout header={header} footer={footer}>\n <div className=\"sc-login\">\n <h1 className=\"sc-login__title\">Choose login provider</h1>\n\n <Suspense fallback={<div className=\"sc-login__loading\">Loading…</div>}>\n <Providers />\n </Suspense>\n </div>\n </LoginLayout>\n )\n}\n\nfunction Providers() {\n const loginUrls = useLoginUrls()\n\n return (\n <div className=\"sc-login-providers\">\n {loginUrls.map(({title, url}) => (\n <a key={url} href={url}>\n {title}\n </a>\n ))}\n </div>\n )\n}\n","import {useEffect} from 'react'\n\nimport {useHandleCallback} from '../../hooks/auth/useHandleCallback'\nimport {LoginLayout, type LoginLayoutProps} from './LoginLayout'\n\n/**\n/**\n * Component shown during auth callback processing that handles login completion.\n * Automatically processes the auth callback when mounted and updates the URL\n * to remove callback parameters without triggering a page reload.\n *\n * @alpha\n */\nexport function LoginCallback({header, footer}: LoginLayoutProps): React.ReactNode {\n const handleCallback = useHandleCallback()\n\n useEffect(() => {\n const url = new URL(location.href)\n handleCallback(url.toString()).then((replacementLocation) => {\n if (replacementLocation) {\n // history API with `replaceState` is used to prevent a reload but still\n // remove the short-lived token from the URL\n history.replaceState(null, '', replacementLocation)\n }\n })\n }, [handleCallback])\n\n return (\n <LoginLayout header={header} footer={footer}>\n <div className=\"sc-login-callback\">\n <h1 className=\"sc-login-callback__title\">Logging you in…</h1>\n <div className=\"sc-login-callback__loading\">Loading…</div>\n </div>\n </LoginLayout>\n )\n}\n","import {useCallback} from 'react'\nimport {type FallbackProps} from 'react-error-boundary'\n\nimport {useLogOut} from '../../hooks/auth/useLogOut'\nimport {AuthError} from './AuthError'\nimport {LoginLayout, type LoginLayoutProps} from './LoginLayout'\n\n/**\n * @alpha\n */\nexport type LoginErrorProps = FallbackProps & LoginLayoutProps\n\n/**\n * Displays authentication error details and provides retry functionality.\n * Only handles {@link AuthError} instances - rethrows other error types.\n *\n * @alpha\n */\nexport function LoginError({\n error,\n resetErrorBoundary,\n header,\n footer,\n}: LoginErrorProps): React.ReactNode {\n if (!(error instanceof AuthError)) throw error\n const logout = useLogOut()\n\n const handleRetry = useCallback(async () => {\n await logout()\n resetErrorBoundary()\n }, [logout, resetErrorBoundary])\n\n return (\n <LoginLayout header={header} footer={footer}>\n <div className=\"sc-login-error\">\n <div className=\"sc-login-error__content\">\n <h2 className=\"sc-login-error__title\">Authentication Error</h2>\n <p className=\"sc-login-error__description\">\n Please try again or contact support if the problem persists.\n </p>\n </div>\n\n <button className=\"sc-login-error__button\" onClick={handleRetry}>\n Retry\n </button>\n </div>\n </LoginLayout>\n )\n}\n","import {AuthStateType} from '@sanity/sdk'\nimport {useMemo} from 'react'\nimport {ErrorBoundary, type FallbackProps} from 'react-error-boundary'\n\nimport {useAuthState} from '../../hooks/auth/useAuthState'\nimport {isInIframe} from '../utils'\nimport {AuthError} from './AuthError'\nimport {Login} from './Login'\nimport {LoginCallback} from './LoginCallback'\nimport {LoginError, type LoginErrorProps} from './LoginError'\nimport {type LoginLayoutProps} from './LoginLayout'\n\n// Only import bridge if we're in an iframe. This assumes that the app is\n// running within SanityOS if it is in an iframe.\nif (isInIframe()) {\n const parsedUrl = new URL(window.location.href)\n const mode = new URLSearchParams(parsedUrl.hash.slice(1)).get('mode')\n const script = document.createElement('script')\n script.src =\n mode === 'core-ui--staging'\n ? 'https://core.sanity-cdn.work/bridge.js'\n : 'https://core.sanity-cdn.com/bridge.js'\n script.type = 'module'\n script.async = true\n document.head.appendChild(script)\n}\n\n/**\n * @internal\n */\ninterface AuthBoundaryProps extends LoginLayoutProps {\n /**\n * Custom component to render the login screen.\n * Receives all login layout props. Defaults to {@link Login}.\n */\n LoginComponent?: React.ComponentType<LoginLayoutProps>\n\n /**\n * Custom component to render during OAuth callback processing.\n * Receives all login layout props. Defaults to {@link LoginCallback}.\n */\n CallbackComponent?: React.ComponentType<LoginLayoutProps>\n\n /**\n * Custom component to render when authentication errors occur.\n * Receives login layout props and error boundary props. Defaults to\n * {@link LoginError}\n */\n LoginErrorComponent?: React.ComponentType<LoginErrorProps>\n}\n\n/**\n * A component that handles authentication flow and error boundaries for a\n * protected section of the application.\n *\n * @remarks\n * This component manages different authentication states and renders the\n * appropriate components based on that state.\n *\n * @example\n * ```tsx\n * function App() {\n * return (\n * <AuthBoundary header={<MyLogo />}>\n * <ProtectedContent />\n * </AuthBoundary>\n * )\n * }\n * ```\n *\n * @internal\n */\nexport function AuthBoundary({\n LoginErrorComponent = LoginError,\n ...props\n}: AuthBoundaryProps): React.ReactNode {\n const {header, footer} = props\n const FallbackComponent = useMemo(() => {\n return function LoginComponentWithLayoutProps(fallbackProps: FallbackProps) {\n return <LoginErrorComponent {...fallbackProps} header={header} footer={footer} />\n }\n }, [header, footer, LoginErrorComponent])\n\n return (\n <ErrorBoundary FallbackComponent={FallbackComponent}>\n <AuthSwitch {...props} />\n </ErrorBoundary>\n )\n}\n\ninterface AuthSwitchProps extends LoginLayoutProps {\n LoginComponent?: React.ComponentType<LoginLayoutProps>\n CallbackComponent?: React.ComponentType<LoginLayoutProps>\n}\n\nfunction AuthSwitch({\n LoginComponent = Login,\n CallbackComponent = LoginCallback,\n children,\n ...props\n}: AuthSwitchProps) {\n const authState = useAuthState()\n\n switch (authState.type) {\n case AuthStateType.ERROR: {\n throw new AuthError(authState.error)\n }\n case AuthStateType.LOGGING_IN: {\n return <CallbackComponent {...props} />\n }\n case AuthStateType.LOGGED_IN: {\n return children\n }\n default: {\n return <LoginComponent {...props} />\n }\n }\n}\n","import {createSanityInstance, type SanityConfig} from '@sanity/sdk'\nimport {type ReactElement} from 'react'\n\nimport {SanityProvider} from '../context/SanityProvider'\nimport {AuthBoundary} from './auth/AuthBoundary'\nimport {isInIframe} from './utils'\n\n/**\n * @public\n */\nexport interface SanityAppProps {\n sanityConfig: SanityConfig\n children: React.ReactNode\n}\n\n/**\n * @public\n *\n * The SanityApp component provides your Sanity application with access to your Sanity configuration,\n * as well as application context and state which is used by the Sanity React hooks. Your application\n * must be wrapped with the SanityApp component to function properly.\n *\n * @param props - Your Sanity configuration and the React children to render\n * @returns Your Sanity application, integrated with your Sanity configuration and application context\n *\n * @example\n * ```\n * import { SanityApp } from '@sanity/sdk-react\n *\n * import MyAppRoot from './Root'\n *\n * const mySanityConfig = {\n * procectId: 'my-project-id',\n * dataset: 'production',\n * }\n *\n * export default function MyApp() {\n * return (\n * <SanityApp sanityConfig={mySanityConfig}>\n * <MyAppRoot />\n * </SanityApp>\n * )\n * }\n * ```\n */\nexport function SanityApp({sanityConfig, children}: SanityAppProps): ReactElement {\n if (isInIframe()) {\n // When running in an iframe Content OS, we don't want to store tokens\n sanityConfig.auth = {\n ...sanityConfig.auth,\n storageArea: undefined,\n }\n }\n const sanityInstance = createSanityInstance(sanityConfig)\n\n return (\n <SanityProvider sanityInstance={sanityInstance}>\n <AuthBoundary>{children}</AuthBoundary>\n </SanityProvider>\n )\n}\n"],"names":[],"mappings":";;;;;;;AAAO,SAAS,aAAsB;AACpC,SAAO,OAAO,SAAW,OAAe,OAAO,SAAS,OAAO;AACjE;ACSO,MAAM,kBAAkB,MAAM;AAAA,EACnC,YAAY,OAAgB;AAExB,WAAO,SAAU,YACf,SACF,aAAa,SACb,OAAO,MAAM,WAAY,WAEzB,MAAM,MAAM,OAAO,IAEnB,MAAM,GAGR,KAAK,QAAQ;AAAA,EAAA;AAEjB;ACvBA,MAAM,QAAQ;AAAA,EACZ;AAAA,IACE,KAAK;AAAA,IACL,SAAS;AAAA,IACT,OAAO;AAAA,EACT;AAAA,EACA;AAAA,IACE,KAAK;AAAA,IACL,SAAS;AAAA,IACT,OAAO;AAAA,EACT;AAAA,EACA;AAAA,IACE,KAAK;AAAA,IACL,SAAS;AAAA,IACT,OAAO;AAAA,EACT;AAAA,EACA;AAAA,IACE,KAAK;AAAA,IACL,SAAS;AAAA,IACT,OAAO;AAAA,EAAA;AAEX;AAQO,SAAS,cAA+B;AAE3C,SAAA,qBAAC,OAAI,EAAA,WAAU,mBACb,UAAA;AAAA,IAAC,oBAAA,YAAA,EAAW,WAAU,wBAAwB,CAAA;AAAA,IAE7C,oBAAA,MAAA,EAAG,WAAU,0BACX,UAAM,MAAA,IAAI,CAAC,SACT,oBAAA,UAAA,EACC,UAAC,oBAAA,MAAA,EAAG,WAAU,yBACZ,UAAA,oBAAC,KAAE,EAAA,MAAM,KAAK,KAAK,QAAO,UAAS,KAAI,uBACpC,UAAK,KAAA,OACR,EACF,CAAA,E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,MAAM;AAClC;AA+CO,SAAS,aAAa;AAAA,EAC3B,sBAAsB;AAAA,EACtB,GAAG;AACL,GAAuC;AAC/B,QAAA,EAAC,QAAQ,WAAU,OACnB,oBAAoB,QAAQ,MACzB,SAAuC,eAA8B;AAC1E,WAAQ,oBAAA,qBAAA,EAAqB,GAAG,eAAe,QAAgB,QAAgB;AAAA,EAEhF,GAAA,CAAC,QAAQ,QAAQ,mBAAmB,CAAC;AAExC,6BACG,eAAc,EAAA,mBACb,8BAAC,YAAY,EAAA,GAAG,MAAO,CAAA,GACzB;AAEJ;AAOA,SAAS,WAAW;AAAA,EAClB,iBAAiB;AAAA,EACjB,oBAAoB;AAAA,EACpB;AAAA,EACA,GAAG;AACL,GAAoB;AAClB,QAAM,YAAY,aAAa;AAE/B,UAAQ,UAAU,MAAM;AAAA,IACtB,KAAK,cAAc;AACX,YAAA,IAAI,UAAU,UAAU,KAAK;AAAA,IAErC,KAAK,cAAc;AACV,aAAA,oBAAC,mBAAmB,EAAA,GAAG,MAAO,CAAA;AAAA,IAEvC,KAAK,cAAc;AACV,aAAA;AAAA,IAET;AACS,aAAA,oBAAC,gBAAgB,EAAA,GAAG,MAAO,CAAA;AAAA,EAAA;AAGxC;ACxEO,SAAS,UAAU,EAAC,cAAc,YAAyC;AAC5E,aAEF,MAAA,aAAa,OAAO;AAAA,IAClB,GAAG,aAAa;AAAA,IAChB,aAAa;AAAA,EAAA;AAGX,QAAA,iBAAiB,qBAAqB,YAAY;AAExD,6BACG,gBAAe,EAAA,gBACd,UAAC,oBAAA,cAAA,EAAc,SAAS,CAAA,GAC1B;AAEJ;"}
package/dist/hooks.d.ts CHANGED
@@ -516,7 +516,7 @@ declare type CreateAction = {
516
516
  ifExists: 'fail' | 'ignore'
517
517
  }
518
518
 
519
- /** @internal */
519
+ /** @public */
520
520
  declare interface CurrentSanityUser {
521
521
  id: string
522
522
  name: string
@@ -527,10 +527,10 @@ declare interface CurrentSanityUser {
527
527
 
528
528
  export {CurrentUser}
529
529
 
530
- /** @internal */
530
+ /** @public */
531
531
  declare type DatasetAclMode = 'public' | 'private' | 'custom'
532
532
 
533
- /** @internal */
533
+ /** @public */
534
534
  declare type DatasetResponse = {
535
535
  datasetName: string
536
536
  aclMode: DatasetAclMode
@@ -578,7 +578,7 @@ declare class DatasetsClient {
578
578
  list(): Promise<DatasetsResponse>
579
579
  }
580
580
 
581
- /** @internal */
581
+ /** @public */
582
582
  declare type DatasetsResponse = {
583
583
  name: string
584
584
  aclMode: DatasetAclMode
@@ -1016,6 +1016,16 @@ declare type Logger =
1016
1016
  Pick<typeof console, 'debug' | 'error' | 'groupCollapsed' | 'groupEnd' | 'log' | 'table'>
1017
1017
  >
1018
1018
 
1019
+ declare type Membership = {
1020
+ addedAt?: string
1021
+ resourceType: string
1022
+ resourceId: string
1023
+ roleNames: Array<string>
1024
+ lastSeenAt?: string | null
1025
+ }
1026
+
1027
+ declare type Memberships = Array<Membership>
1028
+
1019
1029
  /**
1020
1030
  * @internal
1021
1031
  */
@@ -2186,6 +2196,13 @@ declare interface RequestOptions {
2186
2196
  /** @alpha */
2187
2197
  declare type ResolveStudioUrl = (sourceDocument: ContentSourceMapDocuments[number]) => StudioUrl
2188
2198
 
2199
+ /**
2200
+ * Resources are entities that can be managed and accessed through the
2201
+ * Access API.
2202
+ *
2203
+ */
2204
+ declare type ResourceType = 'organization' | 'project'
2205
+
2189
2206
  /** @public */
2190
2207
  declare interface ResponseEvent<T = unknown> {
2191
2208
  type: 'response'
@@ -2820,7 +2837,7 @@ declare interface SanityImagePalette {
2820
2837
  title: string
2821
2838
  }
2822
2839
 
2823
- /** @internal */
2840
+ /** @public */
2824
2841
  declare interface SanityProject {
2825
2842
  id: string
2826
2843
  displayName: string
@@ -2848,7 +2865,7 @@ declare interface SanityProject {
2848
2865
  }
2849
2866
  }
2850
2867
 
2851
- /** @internal */
2868
+ /** @public */
2852
2869
  declare interface SanityProjectMember {
2853
2870
  id: string
2854
2871
  role: string
@@ -2859,7 +2876,7 @@ declare interface SanityProjectMember {
2859
2876
  /** @public */
2860
2877
  declare interface SanityQueries {}
2861
2878
 
2862
- /** @internal */
2879
+ /** @public */
2863
2880
  declare interface SanityUser {
2864
2881
  id: string
2865
2882
  projectId: string
@@ -3765,6 +3782,28 @@ export declare interface UsePreviewResults {
3765
3782
  isPending: boolean
3766
3783
  }
3767
3784
 
3785
+ declare type User = {
3786
+ sanityUserId: string
3787
+ profile: UserProfile
3788
+ memberships: Memberships
3789
+ }
3790
+
3791
+ declare type UserProfile = {
3792
+ id: string
3793
+ displayName: string
3794
+ email: string
3795
+ familyName?: string
3796
+ givenName?: string
3797
+ middleName?: string | null
3798
+ imageUrl?: string
3799
+ provider: string
3800
+ tosAcceptedAt?: string
3801
+ createdAt: string
3802
+ updatedAt?: string
3803
+ isCurrentUser?: boolean
3804
+ providerId?: string
3805
+ }
3806
+
3768
3807
  /** @public */
3769
3808
  declare class UsersClient {
3770
3809
  #private
@@ -3789,6 +3828,43 @@ declare class UsersClient {
3789
3828
  */
3790
3829
  export declare const useSanityInstance: () => SanityInstance
3791
3830
 
3831
+ /** @public */
3832
+ export declare function useUsers(params: UseUsersParams): UseUsersResult
3833
+
3834
+ /**
3835
+ * @public
3836
+ */
3837
+ declare interface UseUsersParams {
3838
+ /**
3839
+ * The type of resource to fetch users for.
3840
+ */
3841
+ resourceType: ResourceType
3842
+ /**
3843
+ * The ID of the resource to fetch users for.
3844
+ */
3845
+ resourceId: string
3846
+ /**
3847
+ * The limit of users to fetch.
3848
+ */
3849
+ limit?: number
3850
+ }
3851
+
3852
+ /** @public */
3853
+ export declare interface UseUsersResult {
3854
+ /**
3855
+ * The users fetched.
3856
+ */
3857
+ users: User[]
3858
+ /**
3859
+ * Whether there are more users to fetch.
3860
+ */
3861
+ hasMore: boolean
3862
+ /**
3863
+ * Load more users.
3864
+ */
3865
+ loadMore: () => void
3866
+ }
3867
+
3792
3868
  /**
3793
3869
  * @internal
3794
3870
  */
package/dist/hooks.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { createStateSourceHook, useSanityInstance, createCallbackHook } from "./_chunks-es/useLogOut.js";
2
2
  import { useAuthState, useHandleCallback, useLogOut, useLoginUrls } from "./_chunks-es/useLogOut.js";
3
- import { getTokenState, getCurrentUserState, getSubscribableClient, getClient, getOrCreateController, getOrCreateChannel, releaseChannel, getOrCreateNode, releaseNode, applyActions, getDocumentState, resolveDocument, subscribeDocumentEvents, getDocumentSyncStatus, editDocument, getPermissionsState, createDocumentListStore, getPreviewState, resolvePreview } from "@sanity/sdk";
3
+ import { getTokenState, getCurrentUserState, getSubscribableClient, getClient, getOrCreateController, getOrCreateChannel, releaseChannel, getOrCreateNode, releaseNode, applyActions, getDocumentState, resolveDocument, subscribeDocumentEvents, getDocumentSyncStatus, editDocument, getPermissionsState, createDocumentListStore, getPreviewState, resolvePreview, createUsersStore } from "@sanity/sdk";
4
4
  import { useCallback, useSyncExternalStore, useState, useMemo, useEffect, useRef, useInsertionEffect } from "react";
5
5
  import { firstValueFrom, filter, Observable, startWith, distinctUntilChanged, switchMap, EMPTY } from "rxjs";
6
6
  const useAuthToken = createStateSourceHook(getTokenState), useCurrentUser = createStateSourceHook(getCurrentUserState);
@@ -224,6 +224,235 @@ function usePreview({ document: { _id, _type }, ref }) {
224
224
  }, [_id, _type, instance, stateSource]);
225
225
  return useSyncExternalStore(subscribe, getSnapshot);
226
226
  }
227
+ var A = async (t, r) => {
228
+ let e = typeof r == "function" ? await r(t) : r;
229
+ if (e) return t.scheme === "bearer" ? `Bearer ${e}` : t.scheme === "basic" ? `Basic ${btoa(e)}` : e;
230
+ }, R = { bodySerializer: (t) => JSON.stringify(t, (r, e) => typeof e == "bigint" ? e.toString() : e) }, k = (t) => {
231
+ switch (t) {
232
+ case "label":
233
+ return ".";
234
+ case "matrix":
235
+ return ";";
236
+ case "simple":
237
+ return ",";
238
+ default:
239
+ return "&";
240
+ }
241
+ }, _ = (t) => {
242
+ switch (t) {
243
+ case "form":
244
+ return ",";
245
+ case "pipeDelimited":
246
+ return "|";
247
+ case "spaceDelimited":
248
+ return "%20";
249
+ default:
250
+ return ",";
251
+ }
252
+ }, D = (t) => {
253
+ switch (t) {
254
+ case "label":
255
+ return ".";
256
+ case "matrix":
257
+ return ";";
258
+ case "simple":
259
+ return ",";
260
+ default:
261
+ return "&";
262
+ }
263
+ }, q = ({ allowReserved: t, explode: r, name: e, style: i, value: a }) => {
264
+ if (!r) {
265
+ let s = (t ? a : a.map((l) => encodeURIComponent(l))).join(_(i));
266
+ switch (i) {
267
+ case "label":
268
+ return `.${s}`;
269
+ case "matrix":
270
+ return `;${e}=${s}`;
271
+ case "simple":
272
+ return s;
273
+ default:
274
+ return `${e}=${s}`;
275
+ }
276
+ }
277
+ let o = k(i), n = a.map((s) => i === "label" || i === "simple" ? t ? s : encodeURIComponent(s) : y({ allowReserved: t, name: e, value: s })).join(o);
278
+ return i === "label" || i === "matrix" ? o + n : n;
279
+ }, y = ({ allowReserved: t, name: r, value: e }) => {
280
+ if (e == null) return "";
281
+ if (typeof e == "object") throw new Error("Deeply-nested arrays/objects aren\u2019t supported. Provide your own `querySerializer()` to handle these.");
282
+ return `${r}=${t ? e : encodeURIComponent(e)}`;
283
+ }, S = ({ allowReserved: t, explode: r, name: e, style: i, value: a }) => {
284
+ if (a instanceof Date) return `${e}=${a.toISOString()}`;
285
+ if (i !== "deepObject" && !r) {
286
+ let s = [];
287
+ Object.entries(a).forEach(([f, u]) => {
288
+ s = [...s, f, t ? u : encodeURIComponent(u)];
289
+ });
290
+ let l = s.join(",");
291
+ switch (i) {
292
+ case "form":
293
+ return `${e}=${l}`;
294
+ case "label":
295
+ return `.${l}`;
296
+ case "matrix":
297
+ return `;${e}=${l}`;
298
+ default:
299
+ return l;
300
+ }
301
+ }
302
+ let o = D(i), n = Object.entries(a).map(([s, l]) => y({ allowReserved: t, name: i === "deepObject" ? `${e}[${s}]` : s, value: l })).join(o);
303
+ return i === "label" || i === "matrix" ? o + n : n;
304
+ }, H = /\{[^{}]+\}/g, B = ({ path: t, url: r }) => {
305
+ let e = r, i = r.match(H);
306
+ if (i) for (let a of i) {
307
+ let o = !1, n = a.substring(1, a.length - 1), s = "simple";
308
+ n.endsWith("*") && (o = !0, n = n.substring(0, n.length - 1)), n.startsWith(".") ? (n = n.substring(1), s = "label") : n.startsWith(";") && (n = n.substring(1), s = "matrix");
309
+ let l = t[n];
310
+ if (l == null) continue;
311
+ if (Array.isArray(l)) {
312
+ e = e.replace(a, q({ explode: o, name: n, style: s, value: l }));
313
+ continue;
314
+ }
315
+ if (typeof l == "object") {
316
+ e = e.replace(a, S({ explode: o, name: n, style: s, value: l }));
317
+ continue;
318
+ }
319
+ if (s === "matrix") {
320
+ e = e.replace(a, `;${y({ name: n, value: l })}`);
321
+ continue;
322
+ }
323
+ let f = encodeURIComponent(s === "label" ? `.${l}` : l);
324
+ e = e.replace(a, f);
325
+ }
326
+ return e;
327
+ }, E = ({ allowReserved: t, array: r, object: e } = {}) => (a) => {
328
+ let o = [];
329
+ if (a && typeof a == "object") for (let n in a) {
330
+ let s = a[n];
331
+ if (s != null) {
332
+ if (Array.isArray(s)) {
333
+ o = [...o, q({ allowReserved: t, explode: !0, name: n, style: "form", value: s, ...r })];
334
+ continue;
335
+ }
336
+ if (typeof s == "object") {
337
+ o = [...o, S({ allowReserved: t, explode: !0, name: n, style: "deepObject", value: s, ...e })];
338
+ continue;
339
+ }
340
+ o = [...o, y({ allowReserved: t, name: n, value: s })];
341
+ }
342
+ }
343
+ return o.join("&");
344
+ }, P = (t) => {
345
+ if (!t) return "stream";
346
+ let r = t.split(";")[0]?.trim();
347
+ if (r) {
348
+ if (r.startsWith("application/json") || r.endsWith("+json")) return "json";
349
+ if (r === "multipart/form-data") return "formData";
350
+ if (["application/", "audio/", "image/", "video/"].some((e) => r.startsWith(e))) return "blob";
351
+ if (r.startsWith("text/")) return "text";
352
+ }
353
+ }, I = async ({ security: t, ...r }) => {
354
+ for (let e of t) {
355
+ let i = await A(e, r.auth);
356
+ if (!i) continue;
357
+ let a = e.name ?? "Authorization";
358
+ switch (e.in) {
359
+ case "query":
360
+ r.query || (r.query = {}), r.query[a] = i;
361
+ break;
362
+ case "header":
363
+ default:
364
+ r.headers.set(a, i);
365
+ break;
366
+ }
367
+ return;
368
+ }
369
+ }, O = (t) => W({ baseUrl: t.baseUrl ?? "", path: t.path, query: t.query, querySerializer: typeof t.querySerializer == "function" ? t.querySerializer : E(t.querySerializer), url: t.url }), W = ({ baseUrl: t, path: r, query: e, querySerializer: i, url: a }) => {
370
+ let o = a.startsWith("/") ? a : `/${a}`, n = t + o;
371
+ r && (n = B({ path: r, url: n }));
372
+ let s = e ? i(e) : "";
373
+ return s.startsWith("?") && (s = s.substring(1)), s && (n += `?${s}`), n;
374
+ }, C = (t, r) => {
375
+ let e = { ...t, ...r };
376
+ return e.baseUrl?.endsWith("/") && (e.baseUrl = e.baseUrl.substring(0, e.baseUrl.length - 1)), e.headers = x(t.headers, r.headers), e;
377
+ }, x = (...t) => {
378
+ let r = new Headers();
379
+ for (let e of t) {
380
+ if (!e || typeof e != "object") continue;
381
+ let i = e instanceof Headers ? e.entries() : Object.entries(e);
382
+ for (let [a, o] of i) if (o === null) r.delete(a);
383
+ else if (Array.isArray(o)) for (let n of o) r.append(a, n);
384
+ else o !== void 0 && r.set(a, typeof o == "object" ? JSON.stringify(o) : o);
385
+ }
386
+ return r;
387
+ }, h = class {
388
+ _fns;
389
+ constructor() {
390
+ this._fns = [];
391
+ }
392
+ clear() {
393
+ this._fns = [];
394
+ }
395
+ exists(r) {
396
+ return this._fns.indexOf(r) !== -1;
397
+ }
398
+ eject(r) {
399
+ let e = this._fns.indexOf(r);
400
+ e !== -1 && (this._fns = [...this._fns.slice(0, e), ...this._fns.slice(e + 1)]);
401
+ }
402
+ use(r) {
403
+ this._fns = [...this._fns, r];
404
+ }
405
+ }, v = () => ({ error: new h(), request: new h(), response: new h() }), N = E({ allowReserved: !1, array: { explode: !0, style: "form" }, object: { explode: !0, style: "deepObject" } }), Q = { "Content-Type": "application/json" }, w = (t = {}) => ({ ...R, baseUrl: "", headers: Q, parseAs: "auto", querySerializer: N, ...t }), J = (t = {}) => {
406
+ let r = C(w(), t), e = () => ({ ...r }), i = (n) => (r = C(r, n), e()), a = v(), o = async (n) => {
407
+ let s = { ...r, ...n, fetch: n.fetch ?? r.fetch ?? globalThis.fetch, headers: x(r.headers, n.headers) };
408
+ s.security && await I({ ...s, security: s.security }), s.body && s.bodySerializer && (s.body = s.bodySerializer(s.body)), s.body || s.headers.delete("Content-Type");
409
+ let l = O(s), f = { redirect: "follow", ...s }, u = new Request(l, f);
410
+ for (let p of a.request._fns) u = await p(u, s);
411
+ let $ = s.fetch, c = await $(u);
412
+ for (let p of a.response._fns) c = await p(c, u, s);
413
+ let m = { request: u, response: c };
414
+ if (c.ok) {
415
+ if (c.status === 204 || c.headers.get("Content-Length") === "0") return { data: {}, ...m };
416
+ let p = (s.parseAs === "auto" ? P(c.headers.get("Content-Type")) : s.parseAs) ?? "json";
417
+ if (p === "stream") return { data: c.body, ...m };
418
+ let b = await c[p]();
419
+ return p === "json" && (s.responseValidator && await s.responseValidator(b), s.responseTransformer && (b = await s.responseTransformer(b))), { data: b, ...m };
420
+ }
421
+ let g = await c.text();
422
+ try {
423
+ g = JSON.parse(g);
424
+ } catch {
425
+ }
426
+ let d = g;
427
+ for (let p of a.error._fns) d = await p(g, c, u, s);
428
+ if (d = d || {}, s.throwOnError) throw d;
429
+ return { error: d, ...m };
430
+ };
431
+ return { buildUrl: O, connect: (n) => o({ ...n, method: "CONNECT" }), delete: (n) => o({ ...n, method: "DELETE" }), get: (n) => o({ ...n, method: "GET" }), getConfig: e, head: (n) => o({ ...n, method: "HEAD" }), interceptors: a, options: (n) => o({ ...n, method: "OPTIONS" }), patch: (n) => o({ ...n, method: "PATCH" }), post: (n) => o({ ...n, method: "POST" }), put: (n) => o({ ...n, method: "PUT" }), request: o, setConfig: i, trace: (n) => o({ ...n, method: "TRACE" }) };
432
+ };
433
+ J(w({
434
+ throwOnError: !0
435
+ }));
436
+ function useUsers(params) {
437
+ const instance = useSanityInstance(), [store] = useState(() => createUsersStore(instance));
438
+ useEffect(() => {
439
+ store.setOptions({
440
+ resourceType: params.resourceType,
441
+ resourceId: params.resourceId
442
+ });
443
+ }, [params.resourceType, params.resourceId, store]);
444
+ const subscribe = useCallback(
445
+ (onStoreChanged) => {
446
+ store.getState().getCurrent().initialFetchCompleted === !1 && store.resolveUsers();
447
+ const unsubscribe = store.getState().subscribe(onStoreChanged);
448
+ return () => {
449
+ unsubscribe(), store.dispose();
450
+ };
451
+ },
452
+ [store]
453
+ ), getSnapshot = useCallback(() => store.getState().getCurrent(), [store]), { users, hasMore } = useSyncExternalStore(subscribe, getSnapshot) || {};
454
+ return { users, hasMore, loadMore: store.loadMore };
455
+ }
227
456
  export {
228
457
  useApplyActions,
229
458
  useAuthState,
@@ -242,6 +471,7 @@ export {
242
471
  usePermissions,
243
472
  usePreview,
244
473
  useSanityInstance,
474
+ useUsers,
245
475
  useWindowConnection
246
476
  };
247
477
  //# sourceMappingURL=hooks.js.map
package/dist/hooks.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"hooks.js","sources":["../src/hooks/auth/useAuthToken.tsx","../src/hooks/auth/useCurrentUser.tsx","../src/hooks/client/useClient.ts","../src/hooks/comlink/useFrameConnection.ts","../src/hooks/comlink/useWindowConnection.ts","../src/hooks/document/useApplyActions.ts","../src/hooks/document/useDocument.ts","../src/hooks/document/useDocumentEvent.ts","../src/hooks/document/useDocumentSyncStatus.ts","../src/hooks/document/useEditDocument.ts","../src/hooks/document/usePermissions.ts","../src/hooks/documentCollection/useDocuments.ts","../src/hooks/preview/usePreview.tsx"],"sourcesContent":["import {getTokenState} from '@sanity/sdk'\n\nimport {createStateSourceHook} from '../helpers/createStateSourceHook'\n\n/**\n * Hook to get the currently logged in user\n * @internal\n * @returns The current user or null if not authenticated\n */\nexport const useAuthToken = createStateSourceHook(getTokenState)\n","import {type CurrentUser, getCurrentUserState} from '@sanity/sdk'\n\nimport {createStateSourceHook} from '../helpers/createStateSourceHook'\n\n/**\n * @TODO This should suspend! And possibly not return `null`?\n *\n * @public\n *\n * Provides the currently authenticated user’s profile information (their name, email, roles, etc).\n * If no users are currently logged in, the hook returns null.\n *\n * @category Authentication\n * @returns The current user data, or `null` if not authenticated\n *\n * @example Rendering a basic user profile\n * ```\n * const user = useCurrentUser()\n *\n * return (\n * <figure>\n * <img src={user?.profileImage} alt=`Profile image for ${user?.name}` />\n * <h2>{user?.name}</h2>\n * </figure>\n * )\n * ```\n */\nexport const useCurrentUser: () => CurrentUser | null = createStateSourceHook(getCurrentUserState)\n","import {type SanityClient} from '@sanity/client'\nimport {type ClientOptions, getClient, getSubscribableClient} from '@sanity/sdk'\nimport {useCallback, useSyncExternalStore} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/**\n * A React hook that provides a client that subscribes to changes in your application,\n * such as user authentication changes.\n *\n * @remarks\n * The hook uses `useSyncExternalStore` to safely subscribe to changes\n * and ensure consistency between server and client rendering.\n *\n * @category Platform\n * @returns A Sanity client\n *\n * @example\n * ```tsx\n * function MyComponent() {\n * const client = useClient()\n * const [document, setDocument] = useState(null)\n * useEffect(async () => {\n * const doc = client.fetch('*[_id == \"myDocumentId\"]')\n * setDocument(doc)\n * }, [])\n * return <div>{JSON.stringify(document) ?? 'Loading...'}</div>\n * }\n * ```\n *\n * @public\n */\nexport function useClient(options: ClientOptions): SanityClient {\n const instance = useSanityInstance()\n\n const subscribe = useCallback(\n (onStoreChange: () => void) => {\n const client$ = getSubscribableClient(instance, options)\n const subscription = client$.subscribe({\n next: onStoreChange,\n error: (error) => {\n // @TODO: We should tackle error handling / error boundaries soon\n // eslint-disable-next-line no-console\n console.error('Error in useClient subscription:', error)\n },\n })\n return () => subscription.unsubscribe()\n },\n [instance, options],\n )\n\n const getSnapshot = useCallback(() => {\n return getClient(instance, options)\n }, [instance, options])\n\n return useSyncExternalStore(subscribe, getSnapshot)\n}\n","import {type Status} from '@sanity/comlink'\nimport {\n type FrameMessage,\n getOrCreateChannel,\n getOrCreateController,\n releaseChannel,\n type WindowMessage,\n} from '@sanity/sdk'\nimport {useCallback, useEffect, useMemo, useState} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/**\n * @internal\n */\nexport type FrameMessageHandler<TWindowMessage extends WindowMessage> = (\n event: TWindowMessage['data'],\n) => TWindowMessage['response'] | Promise<TWindowMessage['response']>\n\n/**\n * @internal\n */\nexport interface UseFrameConnectionOptions<TWindowMessage extends WindowMessage> {\n name: string\n connectTo: string\n targetOrigin: string\n onMessage?: {\n [K in TWindowMessage['type']]: (data: Extract<TWindowMessage, {type: K}>['data']) => void\n }\n heartbeat?: boolean\n}\n\n/**\n * @internal\n */\nexport interface FrameConnection<TFrameMessage extends FrameMessage> {\n connect: (frameWindow: Window) => () => void // Return cleanup function\n sendMessage: <T extends TFrameMessage['type']>(\n ...params: Extract<TFrameMessage, {type: T}>['data'] extends undefined\n ? [type: T]\n : [type: T, data: Extract<TFrameMessage, {type: T}>['data']]\n ) => void\n status: Status\n}\n\n/**\n * @internal\n */\nexport function useFrameConnection<\n TFrameMessage extends FrameMessage,\n TWindowMessage extends WindowMessage,\n>(options: UseFrameConnectionOptions<TWindowMessage>): FrameConnection<TFrameMessage> {\n const {onMessage, targetOrigin, name, connectTo, heartbeat} = options\n const instance = useSanityInstance()\n const [status, setStatus] = useState<Status>('idle')\n\n const controller = useMemo(\n () => getOrCreateController(instance, targetOrigin),\n [instance, targetOrigin],\n )\n\n const channel = useMemo(\n () =>\n getOrCreateChannel(instance, {\n name,\n connectTo,\n heartbeat,\n }),\n [instance, name, connectTo, heartbeat],\n )\n\n useEffect(() => {\n if (!channel) return\n\n const unsubscribe = channel.onStatus((event) => {\n setStatus(event.status)\n })\n\n return unsubscribe\n }, [channel])\n\n useEffect(() => {\n if (!channel || !onMessage) return\n\n const unsubscribers: Array<() => void> = []\n\n Object.entries(onMessage).forEach(([type, handler]) => {\n // type assertion, but we've already constrained onMessage to have the correct handler type\n const unsubscribe = channel.on(type, handler as FrameMessageHandler<TWindowMessage>)\n unsubscribers.push(unsubscribe)\n })\n\n return () => {\n unsubscribers.forEach((unsub) => unsub())\n }\n }, [channel, onMessage])\n\n const connect = useCallback(\n (frameWindow: Window) => {\n const removeTarget = controller?.addTarget(frameWindow)\n return () => {\n removeTarget?.()\n }\n },\n [controller],\n )\n\n const sendMessage = useCallback(\n <T extends TFrameMessage['type']>(\n type: T,\n data?: Extract<TFrameMessage, {type: T}>['data'],\n ) => {\n channel?.post(type, data)\n },\n [channel],\n )\n\n // cleanup channel on unmount\n useEffect(() => {\n return () => {\n releaseChannel(instance, name)\n }\n }, [name, instance])\n\n return {\n connect,\n sendMessage,\n status,\n }\n}\n","import {type Status} from '@sanity/comlink'\nimport {type FrameMessage, getOrCreateNode, releaseNode, type WindowMessage} from '@sanity/sdk'\nimport {useCallback, useEffect, useMemo, useState} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/**\n * @internal\n */\nexport type WindowMessageHandler<TFrameMessage extends FrameMessage> = (\n event: TFrameMessage['data'],\n) => TFrameMessage['response']\n\n/**\n * @internal\n */\nexport interface UseWindowConnectionOptions<TMessage extends FrameMessage> {\n name: string\n connectTo: string\n onMessage?: Record<TMessage['type'], WindowMessageHandler<TMessage>>\n}\n\n/**\n * @internal\n */\nexport interface WindowConnection<TMessage extends WindowMessage> {\n sendMessage: <TType extends TMessage['type']>(\n type: TType,\n data?: Extract<TMessage, {type: TType}>['data'],\n ) => void\n status: Status\n}\n\n/**\n * @internal\n */\nexport function useWindowConnection<\n TWindowMessage extends WindowMessage,\n TFrameMessage extends FrameMessage,\n>(options: UseWindowConnectionOptions<TFrameMessage>): WindowConnection<TWindowMessage> {\n const {name, onMessage, connectTo} = options\n const instance = useSanityInstance()\n const [status, setStatus] = useState<Status>('idle')\n\n const node = useMemo(\n () => getOrCreateNode(instance, {name, connectTo}),\n [instance, name, connectTo],\n )\n\n useEffect(() => {\n const unsubscribe = node.onStatus((newStatus) => {\n setStatus(newStatus)\n })\n\n return unsubscribe\n }, [node, instance, name])\n\n useEffect(() => {\n if (!onMessage) return\n\n const unsubscribers: Array<() => void> = []\n\n Object.entries(onMessage).forEach(([type, handler]) => {\n const unsubscribe = node.on(type, handler as WindowMessageHandler<TFrameMessage>)\n unsubscribers.push(unsubscribe)\n })\n\n return () => {\n unsubscribers.forEach((unsub) => unsub())\n }\n }, [node, onMessage])\n\n const sendMessage = useCallback(\n <TType extends WindowMessage['type']>(\n type: TType,\n data?: Extract<WindowMessage, {type: TType}>['data'],\n ) => {\n node?.post(type, data)\n },\n [node],\n )\n\n // cleanup node on unmount\n useEffect(() => {\n return () => {\n releaseNode(instance, name)\n }\n }, [instance, name])\n\n return {\n sendMessage,\n status,\n }\n}\n","import {\n type ActionsResult,\n applyActions,\n type ApplyActionsOptions,\n type DocumentAction,\n} from '@sanity/sdk'\nimport {type SanityDocument} from '@sanity/types'\n\nimport {createCallbackHook} from '../helpers/createCallbackHook'\n\n/**\n *\n * @beta\n *\n * Provides a callback for applying one or more actions to a document.\n *\n * @category Documents\n * @returns A function that takes one more more {@link DocumentAction}s and returns a promise that resolves to an {@link ActionsResult}.\n * @example Publish or unpublish a document\n * ```\n * import { publishDocument, unpublishDocument } from '@sanity/sdk'\n * import { useApplyActions } from '@sanity/sdk-react'\n *\n * const apply = useApplyActions()\n * const myDocument = { _id: 'my-document-id', _type: 'my-document-type' }\n *\n * return (\n * <button onClick={() => apply(publishDocument(myDocument))}>Publish</button>\n * <button onClick={() => apply(unpublishDocument(myDocument))}>Unpublish</button>\n * )\n * ```\n *\n * @example Create and publish a new document\n * ```\n * import { createDocument, publishDocument } from '@sanity/sdk'\n * import { useApplyActions } from '@sanity/sdk-react'\n *\n * const apply = useApplyActions()\n *\n * const handleCreateAndPublish = () => {\n * const handle = { _id: window.crypto.randomUUID(), _type: 'my-document-type' }\n * apply([\n * createDocument(handle),\n * publishDocument(handle),\n * ])\n * }\n *\n * return (\n * <button onClick={handleCreateAndPublish}>\n * I’m feeling lucky\n * </button>\n * )\n * ```\n */\nexport function useApplyActions(): <TDocument extends SanityDocument>(\n action: DocumentAction<TDocument> | DocumentAction<TDocument>[],\n options?: ApplyActionsOptions,\n) => Promise<ActionsResult<TDocument>>\n\n/** @beta */\nexport function useApplyActions(): (\n action: DocumentAction | DocumentAction[],\n options?: ApplyActionsOptions,\n) => Promise<ActionsResult> {\n return _useApplyActions()\n}\n\nconst _useApplyActions = createCallbackHook(applyActions)\n","import {\n type DocumentHandle,\n getDocumentState,\n type JsonMatch,\n type JsonMatchPath,\n resolveDocument,\n} from '@sanity/sdk'\nimport {type SanityDocument} from '@sanity/types'\nimport {useCallback, useMemo, useSyncExternalStore} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/**\n * @beta\n *\n * ## useDocument(doc, path)\n * Read and subscribe to nested values in a document\n * @category Documents\n * @param doc - The document to read state from\n * @param path - The path to the nested value to read from\n * @returns The value at the specified path\n * @example\n * ```tsx\n * import {type DocumentHandle, useDocument} from '@sanity/sdk-react'\n *\n * function OrderLink({documentHandle}: {documentHandle: DocumentHandle}) {\n * const title = useDocument(documentHandle, 'title')\n * const id = useDocument(documentHandle, '_id')\n *\n * return (\n * <a href=`/order/${id}`>Order {title} today!</a>\n * )\n * }\n * ```\n *\n */\nexport function useDocument<\n TDocument extends SanityDocument,\n TPath extends JsonMatchPath<TDocument>,\n>(doc: string | DocumentHandle<TDocument>, path: TPath): JsonMatch<TDocument, TPath> | undefined\n\n/**\n * @beta\n * ## useDocument(doc)\n * Read and subscribe to an entire document\n * @param doc - The document to read state from\n * @returns The document state as an object\n * @example\n * ```tsx\n * import {type SanityDocument, type DocumentHandle, useDocument} from '@sanity/sdk-react'\n *\n * interface Book extends SanityDocument {\n * title: string\n * author: string\n * summary: string\n * }\n *\n * function DocumentView({documentHandle}: {documentHandle: DocumentHandle}) {\n * const book = useDocument<Book>(documentHandle)\n *\n * return (\n * <article>\n * <h1>{book?.title}</h1>\n * <address>By {book?.author}</address>\n *\n * <h2>Summary</h2>\n * {book?.summary}\n *\n * <h2>Order</h2>\n * <a href=`/order/${book._id}`>Order {book?.title} today!</a>\n * </article>\n * )\n * }\n * ```\n *\n */\nexport function useDocument<TDocument extends SanityDocument>(\n doc: string | DocumentHandle<TDocument>,\n): TDocument | null\n\n/**\n * @beta\n * Reads and subscribes to a document’s realtime state, incorporating both local and remote changes.\n * When called with a `path` argument, the hook will return the nested value’s state.\n * When called without a `path` argument, the entire document’s state will be returned.\n *\n * @remarks\n * `useDocument` is designed to be used within a realtime context in which local updates to documents\n * need to be displayed before they are persisted to the remote copy. This can be useful within a collaborative\n * or realtime editing interface where local changes need to be reflected immediately.\n *\n * However, this hook can be too resource intensive for applications where static document values simply\n * need to be displayed (or when changes to documents don’t need to be reflected immediately);\n * consider using `usePreview` or `useQuery` for these use cases instead. These hooks leverage the Sanity\n * Live Content API to provide a more efficient way to read and subscribe to document state.\n */\nexport function useDocument(doc: string | DocumentHandle, path?: string): unknown {\n const documentId = typeof doc === 'string' ? doc : doc._id\n const instance = useSanityInstance()\n const isDocumentReady = useCallback(\n () => getDocumentState(instance, documentId).getCurrent() !== undefined,\n [instance, documentId],\n )\n if (!isDocumentReady()) throw resolveDocument(instance, documentId)\n\n const {subscribe, getCurrent} = useMemo(\n () => getDocumentState(instance, documentId, path),\n [documentId, instance, path],\n )\n\n return useSyncExternalStore(subscribe, getCurrent)\n}\n","import {type DocumentEvent, subscribeDocumentEvents} from '@sanity/sdk'\nimport {useCallback, useEffect, useInsertionEffect, useRef} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/**\n *\n * @beta\n *\n * Subscribes an event handler to events in your application’s document store, such as document\n * creation, deletion, and updates.\n *\n * @category Documents\n * @param handler - The event handler to register.\n * @example\n * ```\n * import {useDocumentEvent} from '@sanity/sdk-react'\n * import {type DocumentEvent} from '@sanity/sdk'\n *\n * useDocumentEvent((event) => {\n * if (event.type === DocumentEvent.DocumentDeletedEvent) {\n * alert(`Document with ID ${event.documentId} deleted!`)\n * } else {\n * console.log(event)\n * }\n * })\n * ```\n */\nexport function useDocumentEvent(handler: (documentEvent: DocumentEvent) => void): void {\n const ref = useRef(handler)\n\n useInsertionEffect(() => {\n ref.current = handler\n })\n\n const stableHandler = useCallback((documentEvent: DocumentEvent) => {\n return ref.current(documentEvent)\n }, [])\n\n const instance = useSanityInstance()\n useEffect(() => {\n return subscribeDocumentEvents(instance, stableHandler)\n }, [instance, stableHandler])\n}\n","import {type DocumentHandle, getDocumentSyncStatus} from '@sanity/sdk'\n\nimport {createStateSourceHook} from '../helpers/createStateSourceHook'\n\ntype UseDocumentSyncStatus = {\n /**\n * Exposes the document’s sync status between local and remote document states.\n *\n * @category Documents\n * @param doc - The document handle to get sync status for\n * @returns `true` if local changes are synced with remote, `false` if the changes are not synced, and `undefined` if the document is not found\n * @example Disable a Save button when there are no changes to sync\n * ```\n * const myDocumentHandle = { _id: 'documentId', _type: 'documentType' }\n * const documentSynced = useDocumentSyncStatus(myDocumentHandle)\n *\n * return (\n * <button disabled={documentSynced}>\n * Save Changes\n * </button>\n * )\n * ```\n */\n (doc: DocumentHandle): boolean | undefined\n}\n\n/** @beta */\nexport const useDocumentSyncStatus: UseDocumentSyncStatus =\n createStateSourceHook(getDocumentSyncStatus)\n","import {\n type ActionsResult,\n type DocumentHandle,\n editDocument,\n getDocumentState,\n type JsonMatch,\n type JsonMatchPath,\n resolveDocument,\n} from '@sanity/sdk'\nimport {type SanityDocument} from '@sanity/types'\nimport {useCallback} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\nimport {useApplyActions} from './useApplyActions'\n\nconst ignoredKeys = ['_id', '_type', '_createdAt', '_updatedAt', '_rev']\n\ntype Updater<TValue> = TValue | ((nextValue: TValue) => TValue)\n\n/**\n *\n * @beta\n *\n * ## useEditDocument(doc, path)\n * Edit a nested value within a document\n *\n * @category Documents\n * @param doc - The document to be edited; either as a document handle or the document’s ID a string\n * @param path - The path to the nested value to be edited\n * @returns A function to update the nested value. Accepts either a new value, or an updater function that exposes the previous value and returns a new value.\n * @example Update a document’s name by providing the new value directly\n * ```\n * const handle = { _id: 'documentId', _type: 'documentType' }\n * const name = useDocument(handle, 'name')\n * const editName = useEditDocument(handle, 'name')\n *\n * function handleNameChange(event: React.ChangeEvent<HTMLInputElement>) {\n * editName(event.target.value)\n * }\n *\n * return (\n * <input type='text' value={name} onChange={handleNameChange} />\n * )\n * ```\n *\n * @example Update a count on a document by providing an updater function\n * ```\n * const handle = { _id: 'documentId', _type: 'documentType' }\n * const count = useDocument(handle, 'count')\n * const editCount = useEditDocument(handle, 'count')\n *\n * function incrementCount() {\n * editCount(previousCount => previousCount + 1)\n * }\n *\n * return (\n * <>\n * <button onClick={incrementCount}>\n * Increment\n * </button>\n * Current count: {count}\n * </>\n * )\n * ```\n */\nexport function useEditDocument<\n TDocument extends SanityDocument,\n TPath extends JsonMatchPath<TDocument>,\n>(\n doc: string | DocumentHandle<TDocument>,\n path: TPath,\n): (nextValue: Updater<JsonMatch<TDocument, TPath>>) => Promise<ActionsResult<TDocument>>\n\n/**\n *\n * @beta\n *\n * ## useEditDocument(doc)\n * Edit an entire document\n * @param doc - The document to be edited; either as a document handle or the document’s ID a string\n * @returns A function to update the document state. Accepts either a new document state, or an updater function that exposes the previous document state and returns the new document state.\n * @example\n * ```\n * const myDocumentHandle = { _id: 'documentId', _type: 'documentType' }\n *\n * const myDocument = useDocument(myDocumentHandle)\n * const { title, price } = myDocument\n *\n * const editMyDocument = useEditDocument(myDocumentHandle)\n *\n * function handleFieldChange(e: React.ChangeEvent<HTMLInputElement>) {\n * const {name, value} = e.currentTarget\n * // Use an updater function to update the document state based on the previous state\n * editMyDocument(previousDocument => ({\n * ...previousDocument,\n * [name]: value\n * }))\n * }\n *\n * function handleSaleChange(e: React.ChangeEvent<HTMLInputElement>) {\n * const { checked } = e.currentTarget\n * if (checked) {\n * // Use an updater function to add a new salePrice field;\n * // set it at a 20% discount off the normal price\n * editMyDocument(previousDocument => ({\n * ...previousDocument,\n * salePrice: previousDocument.price * 0.8,\n * }))\n * } else {\n * // Get the document state without the salePrice field\n * const { salePrice, ...rest } = myDocument\n * // Update the document state to remove the salePrice field\n * editMyDocument(rest)\n * }\n * }\n *\n * return (\n * <>\n * <form onSubmit={e => e.preventDefault()}>\n * <input name='title' type='text' value={title} onChange={handleFieldChange} />\n * <input name='price' type='number' value={price} onChange={handleFieldChange} />\n * <input\n * name='salePrice'\n * type='checkbox'\n * checked={Object(myDocument).hasOwnProperty('salePrice')}\n * onChange={handleSaleChange}\n * />\n * </form>\n * <pre><code>\n * {JSON.stringify(myDocument, null, 2)}\n * </code></pre>\n * </>\n * )\n * ```\n */\nexport function useEditDocument<TDocument extends SanityDocument>(\n doc: string | DocumentHandle<TDocument>,\n): (nextValue: Updater<TDocument>) => Promise<ActionsResult<TDocument>>\n\n/**\n *\n * @beta\n *\n * Enables editing of a document’s state.\n * When called with a `path` argument, the hook will return a function for updating a nested value.\n * When called without a `path` argument, the hook will return a function for updating the entire document.\n */\nexport function useEditDocument(\n doc: string | DocumentHandle,\n path?: string,\n): (updater: Updater<unknown>) => Promise<ActionsResult> {\n const documentId = typeof doc === 'string' ? doc : doc._id\n const instance = useSanityInstance()\n const apply = useApplyActions()\n const isDocumentReady = useCallback(\n () => getDocumentState(instance, documentId).getCurrent() !== undefined,\n [instance, documentId],\n )\n if (!isDocumentReady()) throw resolveDocument(instance, documentId)\n\n return useCallback(\n (updater: Updater<unknown>) => {\n if (path) {\n const nextValue =\n typeof updater === 'function'\n ? updater(getDocumentState(instance, documentId, path).getCurrent())\n : updater\n\n return apply(editDocument(documentId, {set: {[path]: nextValue}}))\n }\n\n const current = getDocumentState(instance, documentId).getCurrent()\n const nextValue = typeof updater === 'function' ? updater(current) : updater\n\n if (typeof nextValue !== 'object' || !nextValue) {\n throw new Error(\n `No path was provided to \\`useEditDocument\\` and the value provided was not a document object.`,\n )\n }\n\n const allKeys = Object.keys({...current, ...nextValue})\n const editActions = allKeys\n .filter((key) => !ignoredKeys.includes(key))\n .filter((key) => current?.[key] !== nextValue[key])\n .map((key) =>\n key in nextValue\n ? editDocument(documentId, {set: {[key]: nextValue[key]}})\n : editDocument(documentId, {unset: [key]}),\n )\n\n return apply(editActions)\n },\n [apply, documentId, instance, path],\n )\n}\n","import {type DocumentAction, getPermissionsState, type PermissionsResult} from '@sanity/sdk'\nimport {useCallback, useMemo, useSyncExternalStore} from 'react'\nimport {filter, firstValueFrom} from 'rxjs'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/** @beta */\nexport function usePermissions(actions: DocumentAction | DocumentAction[]): PermissionsResult {\n const instance = useSanityInstance()\n const isDocumentReady = useCallback(\n () => getPermissionsState(instance, actions).getCurrent() !== undefined,\n [actions, instance],\n )\n if (!isDocumentReady()) {\n throw firstValueFrom(\n getPermissionsState(instance, actions).observable.pipe(\n filter((result) => result !== undefined),\n ),\n )\n }\n\n const {subscribe, getCurrent} = useMemo(\n () => getPermissionsState(instance, actions),\n [actions, instance],\n )\n\n return useSyncExternalStore(subscribe, getCurrent) as PermissionsResult\n}\n","import {createDocumentListStore, type DocumentHandle, type DocumentListOptions} from '@sanity/sdk'\nimport {useCallback, useEffect, useState, useSyncExternalStore} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/**\n * @public\n * A live collection of {@link DocumentHandle}s, along with metadata about the collection and a function for loading more of them.\n * @category Types\n */\nexport interface DocumentHandleCollection {\n /** Retrieve more documents matching the provided options */\n loadMore: () => void\n /** The retrieved document handles of the documents matching the provided options */\n results: DocumentHandle[]\n /** Whether a retrieval of documents is in flight */\n isPending: boolean\n /** Whether more documents exist that match the provided options than have been retrieved */\n hasMore: boolean\n /** The total number of documents in the collection */\n count: number\n}\n\ntype DocumentListStore = ReturnType<typeof createDocumentListStore>\ntype DocumentListState = ReturnType<DocumentListStore['getState']>['getCurrent']\nconst STABLE_EMPTY = {\n results: [],\n isPending: false,\n hasMore: false,\n count: 0,\n}\n\n/**\n * @public\n *\n * Retrieves and provides access to a live collection of {@link DocumentHandle}s, with an optional filter and sort applied.\n * The returned document handles are canonical — that is, they refer to the document in its current state, whether draft, published, or within a release or perspective.\n * Because the returned document handle collection is live, the results will update in real time until the component invoking the hook is unmounted.\n *\n * @remarks\n * {@link DocumentHandle}s are used by many other hooks (such as {@link usePreview}, {@link useDocument}, and {@link useEditDocument})\n * to work with documents in various ways without the entire document needing to be fetched upfront.\n *\n * @category Documents\n * @param options - Options for narrowing and sorting the document collection\n * @returns The collection of document handles matching the provided options (if any), as well as properties describing the collection and a function to load more.\n *\n * @example Retrieving document handles for all documents of type 'movie'\n * ```\n * const { results, isPending } = useDocuments({ filter: '_type == \"movie\"' })\n *\n * return (\n * <div>\n * <h1>Movies</h1>\n * {results && (\n * <ul>\n * {results.map(movie => (<li key={movie._id}>…</li>))}\n * </ul>\n * )}\n * {isPending && <div>Loading movies…</div>}\n * </div>\n * )\n * ```\n *\n * @example Retrieving document handles for all movies released since 1980, sorted by director’s last name\n * ```\n * const { results } = useDocuments({\n * filter: '_type == \"movie\" && releaseDate >= \"1980-01-01\"',\n * sort: [\n * {\n * // Expand the `director` reference field with the dereferencing operator `->`\n * field: 'director->lastName',\n * sort: 'asc',\n * },\n * ],\n * })\n *\n * return (\n * <div>\n * <h1>Movies released since 1980</h1>\n * {results && (\n * <ol>\n * {results.map(movie => (<li key={movie._id}>…</li>))}\n * </ol>\n * )}\n * </div>\n * )\n * ```\n */\nexport function useDocuments(options: DocumentListOptions = {}): DocumentHandleCollection {\n const instance = useSanityInstance()\n\n // NOTE: useState is used because it guaranteed to return a stable reference\n // across renders\n const [ref] = useState<{\n storeInstance: DocumentListStore | null\n getCurrent: DocumentListState\n initialOptions: DocumentListOptions\n }>(() => ({\n storeInstance: null,\n getCurrent: () => STABLE_EMPTY,\n initialOptions: options,\n }))\n\n // serialize options to ensure it only calls `setOptions` when the values\n // themselves changes (in cases where devs put config inline)\n const serializedOptions = JSON.stringify(options)\n useEffect(() => {\n ref.storeInstance?.setOptions(JSON.parse(serializedOptions))\n }, [ref, serializedOptions])\n\n const subscribe = useCallback(\n (onStoreChanged: () => void) => {\n // to match the lifecycle of `useSyncExternalState`, we create the store\n // instance after subscribe and mutate the ref to connect everything\n ref.storeInstance = createDocumentListStore(instance)\n ref.storeInstance.setOptions(ref.initialOptions)\n const state = ref.storeInstance.getState()\n ref.getCurrent = state.getCurrent\n const unsubscribe = state.subscribe(onStoreChanged)\n\n return () => {\n // unsubscribe to clean up the state subscriptions\n unsubscribe()\n // dispose of the instance\n ref.storeInstance?.dispose()\n }\n },\n [instance, ref],\n )\n\n const getSnapshot = useCallback(() => {\n return ref.getCurrent()\n }, [ref])\n\n const state = useSyncExternalStore(subscribe, getSnapshot)\n\n const loadMore = useCallback(() => {\n ref.storeInstance?.loadMore()\n }, [ref])\n\n return {loadMore, ...state}\n}\n","import {type DocumentHandle, getPreviewState, type PreviewValue, resolvePreview} from '@sanity/sdk'\nimport {useCallback, useMemo, useSyncExternalStore} from 'react'\nimport {distinctUntilChanged, EMPTY, Observable, startWith, switchMap} from 'rxjs'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/**\n * @beta\n * @category Types\n */\nexport interface UsePreviewOptions {\n document: DocumentHandle\n ref?: React.RefObject<unknown>\n}\n\n/**\n * @beta\n * @category Types\n */\nexport interface UsePreviewResults {\n /** The results of resolving the document’s preview values */\n results: PreviewValue\n /** True when preview values are being refreshed */\n isPending: boolean\n}\n\n/**\n * @beta\n *\n * Returns the preview values of a document (specified via a `DocumentHandle`),\n * including the document’s `title`, `subtitle`, `media`, and `status`. These values are live and will update in realtime.\n * To reduce unnecessary network requests for resolving the preview values, an optional `ref` can be passed to the hook so that preview\n * resolution will only occur if the `ref` is intersecting the current viewport.\n *\n * @category Documents\n * @param options - The document handle for the document you want to resolve preview values for, and an optional ref\n * @returns The preview values for the given document and a boolean to indicate whether the resolution is pending\n *\n * @example Combining with useDocuments to render a collection of document previews\n * ```\n * // PreviewComponent.jsx\n * export default function PreviewComponent({ document }) {\n * const { results: { title, subtitle, media }, isPending } = usePreview({ document })\n * return (\n * <article style={{ opacity: isPending ? 0.5 : 1}}>\n * {media?.type === 'image-asset' ? <img src={media.url} alt='' /> : ''}\n * <h2>{title}</h2>\n * <p>{subtitle}</p>\n * </article>\n * )\n * }\n *\n * // DocumentList.jsx\n * const { results, isPending } = useDocuments({ filter: '_type == \"movie\"' })\n * return (\n * <div>\n * <h1>Movies</h1>\n * <ul>\n * {isPending ? 'Loading…' : results.map(movie => (\n * <li key={movie._id}>\n * <Suspense fallback='Loading…'>\n * <PreviewComponent document={movie} />\n * </Suspense>\n * </li>\n * ))}\n * </ul>\n * </div>\n * )\n * ```\n */\nexport function usePreview({document: {_id, _type}, ref}: UsePreviewOptions): UsePreviewResults {\n const instance = useSanityInstance()\n\n const stateSource = useMemo(\n () => getPreviewState(instance, {document: {_id, _type}}),\n [instance, _id, _type],\n )\n\n // Create subscribe function for useSyncExternalStore\n const subscribe = useCallback(\n (onStoreChanged: () => void) => {\n const subscription = new Observable<boolean>((observer) => {\n // for environments that don't have an intersection observer\n if (typeof IntersectionObserver === 'undefined' || typeof HTMLElement === 'undefined') {\n return\n }\n\n const intersectionObserver = new IntersectionObserver(\n ([entry]) => observer.next(entry.isIntersecting),\n {rootMargin: '0px', threshold: 0},\n )\n if (ref?.current && ref.current instanceof HTMLElement) {\n intersectionObserver.observe(ref.current)\n }\n return () => intersectionObserver.disconnect()\n })\n .pipe(\n startWith(false),\n distinctUntilChanged(),\n switchMap((isVisible) =>\n isVisible\n ? new Observable<void>((obs) => {\n return stateSource.subscribe(() => obs.next())\n })\n : EMPTY,\n ),\n )\n .subscribe({next: onStoreChanged})\n\n return () => subscription.unsubscribe()\n },\n [stateSource, ref],\n )\n\n // Create getSnapshot function to return current state\n const getSnapshot = useCallback(() => {\n const currentState = stateSource.getCurrent()\n if (currentState.results === null) throw resolvePreview(instance, {document: {_id, _type}})\n return currentState as UsePreviewResults\n }, [_id, _type, instance, stateSource])\n\n return useSyncExternalStore(subscribe, getSnapshot)\n}\n"],"names":["nextValue","state"],"mappings":";;;;;AASa,MAAA,eAAe,sBAAsB,aAAa,GCkBlD,iBAA2C,sBAAsB,mBAAmB;ACK1F,SAAS,UAAU,SAAsC;AACxD,QAAA,WAAW,qBAEX,YAAY;AAAA,IAChB,CAAC,kBAA8B;AAE7B,YAAM,eADU,sBAAsB,UAAU,OAAO,EAC1B,UAAU;AAAA,QACrC,MAAM;AAAA,QACN,OAAO,CAAC,UAAU;AAGR,kBAAA,MAAM,oCAAoC,KAAK;AAAA,QAAA;AAAA,MACzD,CACD;AACM,aAAA,MAAM,aAAa,YAAY;AAAA,IACxC;AAAA,IACA,CAAC,UAAU,OAAO;AAAA,EAAA,GAGd,cAAc,YAAY,MACvB,UAAU,UAAU,OAAO,GACjC,CAAC,UAAU,OAAO,CAAC;AAEf,SAAA,qBAAqB,WAAW,WAAW;AACpD;ACRO,SAAS,mBAGd,SAAoF;AACpF,QAAM,EAAC,WAAW,cAAc,MAAM,WAAW,cAAa,SACxD,WAAW,kBAAkB,GAC7B,CAAC,QAAQ,SAAS,IAAI,SAAiB,MAAM,GAE7C,aAAa;AAAA,IACjB,MAAM,sBAAsB,UAAU,YAAY;AAAA,IAClD,CAAC,UAAU,YAAY;AAAA,KAGnB,UAAU;AAAA,IACd,MACE,mBAAmB,UAAU;AAAA,MAC3B;AAAA,MACA;AAAA,MACA;AAAA,IAAA,CACD;AAAA,IACH,CAAC,UAAU,MAAM,WAAW,SAAS;AAAA,EACvC;AAEA,YAAU,MACH,UAEe,QAAQ,SAAS,CAAC,UAAU;AAC9C,cAAU,MAAM,MAAM;AAAA,EACvB,CAAA,IAJa,QAOb,CAAC,OAAO,CAAC,GAEZ,UAAU,MAAM;AACV,QAAA,CAAC,WAAW,CAAC,UAAW;AAE5B,UAAM,gBAAmC,CA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Ae,UAAU,EAAC,UAAU,EAAC,KAAK,MAAK,EAAA,CAAE;AACnF,WAAA;AAAA,KACN,CAAC,KAAK,OAAO,UAAU,WAAW,CAAC;AAE/B,SAAA,qBAAqB,WAAW,WAAW;AACpD;"}
1
+ {"version":3,"file":"hooks.js","sources":["../src/hooks/auth/useAuthToken.tsx","../src/hooks/auth/useCurrentUser.tsx","../src/hooks/client/useClient.ts","../src/hooks/comlink/useFrameConnection.ts","../src/hooks/comlink/useWindowConnection.ts","../src/hooks/document/useApplyActions.ts","../src/hooks/document/useDocument.ts","../src/hooks/document/useDocumentEvent.ts","../src/hooks/document/useDocumentSyncStatus.ts","../src/hooks/document/useEditDocument.ts","../src/hooks/document/usePermissions.ts","../src/hooks/documentCollection/useDocuments.ts","../src/hooks/preview/usePreview.tsx","../../../node_modules/.pnpm/@sanity+access-api@2.2.6/node_modules/@sanity/access-api/lib/index.js","../src/hooks/users/useUsers.ts"],"sourcesContent":["import {getTokenState} from '@sanity/sdk'\n\nimport {createStateSourceHook} from '../helpers/createStateSourceHook'\n\n/**\n * Hook to get the currently logged in user\n * @internal\n * @returns The current user or null if not authenticated\n */\nexport const useAuthToken = createStateSourceHook(getTokenState)\n","import {type CurrentUser, getCurrentUserState} from '@sanity/sdk'\n\nimport {createStateSourceHook} from '../helpers/createStateSourceHook'\n\n/**\n * @TODO This should suspend! And possibly not return `null`?\n *\n * @public\n *\n * Provides the currently authenticated user’s profile information (their name, email, roles, etc).\n * If no users are currently logged in, the hook returns null.\n *\n * @category Authentication\n * @returns The current user data, or `null` if not authenticated\n *\n * @example Rendering a basic user profile\n * ```\n * const user = useCurrentUser()\n *\n * return (\n * <figure>\n * <img src={user?.profileImage} alt=`Profile image for ${user?.name}` />\n * <h2>{user?.name}</h2>\n * </figure>\n * )\n * ```\n */\nexport const useCurrentUser: () => CurrentUser | null = createStateSourceHook(getCurrentUserState)\n","import {type SanityClient} from '@sanity/client'\nimport {type ClientOptions, getClient, getSubscribableClient} from '@sanity/sdk'\nimport {useCallback, useSyncExternalStore} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/**\n * A React hook that provides a client that subscribes to changes in your application,\n * such as user authentication changes.\n *\n * @remarks\n * The hook uses `useSyncExternalStore` to safely subscribe to changes\n * and ensure consistency between server and client rendering.\n *\n * @category Platform\n * @returns A Sanity client\n *\n * @example\n * ```tsx\n * function MyComponent() {\n * const client = useClient()\n * const [document, setDocument] = useState(null)\n * useEffect(async () => {\n * const doc = client.fetch('*[_id == \"myDocumentId\"]')\n * setDocument(doc)\n * }, [])\n * return <div>{JSON.stringify(document) ?? 'Loading...'}</div>\n * }\n * ```\n *\n * @public\n */\nexport function useClient(options: ClientOptions): SanityClient {\n const instance = useSanityInstance()\n\n const subscribe = useCallback(\n (onStoreChange: () => void) => {\n const client$ = getSubscribableClient(instance, options)\n const subscription = client$.subscribe({\n next: onStoreChange,\n error: (error) => {\n // @TODO: We should tackle error handling / error boundaries soon\n // eslint-disable-next-line no-console\n console.error('Error in useClient subscription:', error)\n },\n })\n return () => subscription.unsubscribe()\n },\n [instance, options],\n )\n\n const getSnapshot = useCallback(() => {\n return getClient(instance, options)\n }, [instance, options])\n\n return useSyncExternalStore(subscribe, getSnapshot)\n}\n","import {type Status} from '@sanity/comlink'\nimport {\n type FrameMessage,\n getOrCreateChannel,\n getOrCreateController,\n releaseChannel,\n type WindowMessage,\n} from '@sanity/sdk'\nimport {useCallback, useEffect, useMemo, useState} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/**\n * @internal\n */\nexport type FrameMessageHandler<TWindowMessage extends WindowMessage> = (\n event: TWindowMessage['data'],\n) => TWindowMessage['response'] | Promise<TWindowMessage['response']>\n\n/**\n * @internal\n */\nexport interface UseFrameConnectionOptions<TWindowMessage extends WindowMessage> {\n name: string\n connectTo: string\n targetOrigin: string\n onMessage?: {\n [K in TWindowMessage['type']]: (data: Extract<TWindowMessage, {type: K}>['data']) => void\n }\n heartbeat?: boolean\n}\n\n/**\n * @internal\n */\nexport interface FrameConnection<TFrameMessage extends FrameMessage> {\n connect: (frameWindow: Window) => () => void // Return cleanup function\n sendMessage: <T extends TFrameMessage['type']>(\n ...params: Extract<TFrameMessage, {type: T}>['data'] extends undefined\n ? [type: T]\n : [type: T, data: Extract<TFrameMessage, {type: T}>['data']]\n ) => void\n status: Status\n}\n\n/**\n * @internal\n */\nexport function useFrameConnection<\n TFrameMessage extends FrameMessage,\n TWindowMessage extends WindowMessage,\n>(options: UseFrameConnectionOptions<TWindowMessage>): FrameConnection<TFrameMessage> {\n const {onMessage, targetOrigin, name, connectTo, heartbeat} = options\n const instance = useSanityInstance()\n const [status, setStatus] = useState<Status>('idle')\n\n const controller = useMemo(\n () => getOrCreateController(instance, targetOrigin),\n [instance, targetOrigin],\n )\n\n const channel = useMemo(\n () =>\n getOrCreateChannel(instance, {\n name,\n connectTo,\n heartbeat,\n }),\n [instance, name, connectTo, heartbeat],\n )\n\n useEffect(() => {\n if (!channel) return\n\n const unsubscribe = channel.onStatus((event) => {\n setStatus(event.status)\n })\n\n return unsubscribe\n }, [channel])\n\n useEffect(() => {\n if (!channel || !onMessage) return\n\n const unsubscribers: Array<() => void> = []\n\n Object.entries(onMessage).forEach(([type, handler]) => {\n // type assertion, but we've already constrained onMessage to have the correct handler type\n const unsubscribe = channel.on(type, handler as FrameMessageHandler<TWindowMessage>)\n unsubscribers.push(unsubscribe)\n })\n\n return () => {\n unsubscribers.forEach((unsub) => unsub())\n }\n }, [channel, onMessage])\n\n const connect = useCallback(\n (frameWindow: Window) => {\n const removeTarget = controller?.addTarget(frameWindow)\n return () => {\n removeTarget?.()\n }\n },\n [controller],\n )\n\n const sendMessage = useCallback(\n <T extends TFrameMessage['type']>(\n type: T,\n data?: Extract<TFrameMessage, {type: T}>['data'],\n ) => {\n channel?.post(type, data)\n },\n [channel],\n )\n\n // cleanup channel on unmount\n useEffect(() => {\n return () => {\n releaseChannel(instance, name)\n }\n }, [name, instance])\n\n return {\n connect,\n sendMessage,\n status,\n }\n}\n","import {type Status} from '@sanity/comlink'\nimport {type FrameMessage, getOrCreateNode, releaseNode, type WindowMessage} from '@sanity/sdk'\nimport {useCallback, useEffect, useMemo, useState} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/**\n * @internal\n */\nexport type WindowMessageHandler<TFrameMessage extends FrameMessage> = (\n event: TFrameMessage['data'],\n) => TFrameMessage['response']\n\n/**\n * @internal\n */\nexport interface UseWindowConnectionOptions<TMessage extends FrameMessage> {\n name: string\n connectTo: string\n onMessage?: Record<TMessage['type'], WindowMessageHandler<TMessage>>\n}\n\n/**\n * @internal\n */\nexport interface WindowConnection<TMessage extends WindowMessage> {\n sendMessage: <TType extends TMessage['type']>(\n type: TType,\n data?: Extract<TMessage, {type: TType}>['data'],\n ) => void\n status: Status\n}\n\n/**\n * @internal\n */\nexport function useWindowConnection<\n TWindowMessage extends WindowMessage,\n TFrameMessage extends FrameMessage,\n>(options: UseWindowConnectionOptions<TFrameMessage>): WindowConnection<TWindowMessage> {\n const {name, onMessage, connectTo} = options\n const instance = useSanityInstance()\n const [status, setStatus] = useState<Status>('idle')\n\n const node = useMemo(\n () => getOrCreateNode(instance, {name, connectTo}),\n [instance, name, connectTo],\n )\n\n useEffect(() => {\n const unsubscribe = node.onStatus((newStatus) => {\n setStatus(newStatus)\n })\n\n return unsubscribe\n }, [node, instance, name])\n\n useEffect(() => {\n if (!onMessage) return\n\n const unsubscribers: Array<() => void> = []\n\n Object.entries(onMessage).forEach(([type, handler]) => {\n const unsubscribe = node.on(type, handler as WindowMessageHandler<TFrameMessage>)\n unsubscribers.push(unsubscribe)\n })\n\n return () => {\n unsubscribers.forEach((unsub) => unsub())\n }\n }, [node, onMessage])\n\n const sendMessage = useCallback(\n <TType extends WindowMessage['type']>(\n type: TType,\n data?: Extract<WindowMessage, {type: TType}>['data'],\n ) => {\n node?.post(type, data)\n },\n [node],\n )\n\n // cleanup node on unmount\n useEffect(() => {\n return () => {\n releaseNode(instance, name)\n }\n }, [instance, name])\n\n return {\n sendMessage,\n status,\n }\n}\n","import {\n type ActionsResult,\n applyActions,\n type ApplyActionsOptions,\n type DocumentAction,\n} from '@sanity/sdk'\nimport {type SanityDocument} from '@sanity/types'\n\nimport {createCallbackHook} from '../helpers/createCallbackHook'\n\n/**\n *\n * @beta\n *\n * Provides a callback for applying one or more actions to a document.\n *\n * @category Documents\n * @returns A function that takes one more more {@link DocumentAction}s and returns a promise that resolves to an {@link ActionsResult}.\n * @example Publish or unpublish a document\n * ```\n * import { publishDocument, unpublishDocument } from '@sanity/sdk'\n * import { useApplyActions } from '@sanity/sdk-react'\n *\n * const apply = useApplyActions()\n * const myDocument = { _id: 'my-document-id', _type: 'my-document-type' }\n *\n * return (\n * <button onClick={() => apply(publishDocument(myDocument))}>Publish</button>\n * <button onClick={() => apply(unpublishDocument(myDocument))}>Unpublish</button>\n * )\n * ```\n *\n * @example Create and publish a new document\n * ```\n * import { createDocument, publishDocument } from '@sanity/sdk'\n * import { useApplyActions } from '@sanity/sdk-react'\n *\n * const apply = useApplyActions()\n *\n * const handleCreateAndPublish = () => {\n * const handle = { _id: window.crypto.randomUUID(), _type: 'my-document-type' }\n * apply([\n * createDocument(handle),\n * publishDocument(handle),\n * ])\n * }\n *\n * return (\n * <button onClick={handleCreateAndPublish}>\n * I’m feeling lucky\n * </button>\n * )\n * ```\n */\nexport function useApplyActions(): <TDocument extends SanityDocument>(\n action: DocumentAction<TDocument> | DocumentAction<TDocument>[],\n options?: ApplyActionsOptions,\n) => Promise<ActionsResult<TDocument>>\n\n/** @beta */\nexport function useApplyActions(): (\n action: DocumentAction | DocumentAction[],\n options?: ApplyActionsOptions,\n) => Promise<ActionsResult> {\n return _useApplyActions()\n}\n\nconst _useApplyActions = createCallbackHook(applyActions)\n","import {\n type DocumentHandle,\n getDocumentState,\n type JsonMatch,\n type JsonMatchPath,\n resolveDocument,\n} from '@sanity/sdk'\nimport {type SanityDocument} from '@sanity/types'\nimport {useCallback, useMemo, useSyncExternalStore} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/**\n * @beta\n *\n * ## useDocument(doc, path)\n * Read and subscribe to nested values in a document\n * @category Documents\n * @param doc - The document to read state from\n * @param path - The path to the nested value to read from\n * @returns The value at the specified path\n * @example\n * ```tsx\n * import {type DocumentHandle, useDocument} from '@sanity/sdk-react'\n *\n * function OrderLink({documentHandle}: {documentHandle: DocumentHandle}) {\n * const title = useDocument(documentHandle, 'title')\n * const id = useDocument(documentHandle, '_id')\n *\n * return (\n * <a href=`/order/${id}`>Order {title} today!</a>\n * )\n * }\n * ```\n *\n */\nexport function useDocument<\n TDocument extends SanityDocument,\n TPath extends JsonMatchPath<TDocument>,\n>(doc: string | DocumentHandle<TDocument>, path: TPath): JsonMatch<TDocument, TPath> | undefined\n\n/**\n * @beta\n * ## useDocument(doc)\n * Read and subscribe to an entire document\n * @param doc - The document to read state from\n * @returns The document state as an object\n * @example\n * ```tsx\n * import {type SanityDocument, type DocumentHandle, useDocument} from '@sanity/sdk-react'\n *\n * interface Book extends SanityDocument {\n * title: string\n * author: string\n * summary: string\n * }\n *\n * function DocumentView({documentHandle}: {documentHandle: DocumentHandle}) {\n * const book = useDocument<Book>(documentHandle)\n *\n * return (\n * <article>\n * <h1>{book?.title}</h1>\n * <address>By {book?.author}</address>\n *\n * <h2>Summary</h2>\n * {book?.summary}\n *\n * <h2>Order</h2>\n * <a href=`/order/${book._id}`>Order {book?.title} today!</a>\n * </article>\n * )\n * }\n * ```\n *\n */\nexport function useDocument<TDocument extends SanityDocument>(\n doc: string | DocumentHandle<TDocument>,\n): TDocument | null\n\n/**\n * @beta\n * Reads and subscribes to a document’s realtime state, incorporating both local and remote changes.\n * When called with a `path` argument, the hook will return the nested value’s state.\n * When called without a `path` argument, the entire document’s state will be returned.\n *\n * @remarks\n * `useDocument` is designed to be used within a realtime context in which local updates to documents\n * need to be displayed before they are persisted to the remote copy. This can be useful within a collaborative\n * or realtime editing interface where local changes need to be reflected immediately.\n *\n * However, this hook can be too resource intensive for applications where static document values simply\n * need to be displayed (or when changes to documents don’t need to be reflected immediately);\n * consider using `usePreview` or `useQuery` for these use cases instead. These hooks leverage the Sanity\n * Live Content API to provide a more efficient way to read and subscribe to document state.\n */\nexport function useDocument(doc: string | DocumentHandle, path?: string): unknown {\n const documentId = typeof doc === 'string' ? doc : doc._id\n const instance = useSanityInstance()\n const isDocumentReady = useCallback(\n () => getDocumentState(instance, documentId).getCurrent() !== undefined,\n [instance, documentId],\n )\n if (!isDocumentReady()) throw resolveDocument(instance, documentId)\n\n const {subscribe, getCurrent} = useMemo(\n () => getDocumentState(instance, documentId, path),\n [documentId, instance, path],\n )\n\n return useSyncExternalStore(subscribe, getCurrent)\n}\n","import {type DocumentEvent, subscribeDocumentEvents} from '@sanity/sdk'\nimport {useCallback, useEffect, useInsertionEffect, useRef} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/**\n *\n * @beta\n *\n * Subscribes an event handler to events in your application’s document store, such as document\n * creation, deletion, and updates.\n *\n * @category Documents\n * @param handler - The event handler to register.\n * @example\n * ```\n * import {useDocumentEvent} from '@sanity/sdk-react'\n * import {type DocumentEvent} from '@sanity/sdk'\n *\n * useDocumentEvent((event) => {\n * if (event.type === DocumentEvent.DocumentDeletedEvent) {\n * alert(`Document with ID ${event.documentId} deleted!`)\n * } else {\n * console.log(event)\n * }\n * })\n * ```\n */\nexport function useDocumentEvent(handler: (documentEvent: DocumentEvent) => void): void {\n const ref = useRef(handler)\n\n useInsertionEffect(() => {\n ref.current = handler\n })\n\n const stableHandler = useCallback((documentEvent: DocumentEvent) => {\n return ref.current(documentEvent)\n }, [])\n\n const instance = useSanityInstance()\n useEffect(() => {\n return subscribeDocumentEvents(instance, stableHandler)\n }, [instance, stableHandler])\n}\n","import {type DocumentHandle, getDocumentSyncStatus} from '@sanity/sdk'\n\nimport {createStateSourceHook} from '../helpers/createStateSourceHook'\n\ntype UseDocumentSyncStatus = {\n /**\n * Exposes the document’s sync status between local and remote document states.\n *\n * @category Documents\n * @param doc - The document handle to get sync status for\n * @returns `true` if local changes are synced with remote, `false` if the changes are not synced, and `undefined` if the document is not found\n * @example Disable a Save button when there are no changes to sync\n * ```\n * const myDocumentHandle = { _id: 'documentId', _type: 'documentType' }\n * const documentSynced = useDocumentSyncStatus(myDocumentHandle)\n *\n * return (\n * <button disabled={documentSynced}>\n * Save Changes\n * </button>\n * )\n * ```\n */\n (doc: DocumentHandle): boolean | undefined\n}\n\n/** @beta */\nexport const useDocumentSyncStatus: UseDocumentSyncStatus =\n createStateSourceHook(getDocumentSyncStatus)\n","import {\n type ActionsResult,\n type DocumentHandle,\n editDocument,\n getDocumentState,\n type JsonMatch,\n type JsonMatchPath,\n resolveDocument,\n} from '@sanity/sdk'\nimport {type SanityDocument} from '@sanity/types'\nimport {useCallback} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\nimport {useApplyActions} from './useApplyActions'\n\nconst ignoredKeys = ['_id', '_type', '_createdAt', '_updatedAt', '_rev']\n\ntype Updater<TValue> = TValue | ((nextValue: TValue) => TValue)\n\n/**\n *\n * @beta\n *\n * ## useEditDocument(doc, path)\n * Edit a nested value within a document\n *\n * @category Documents\n * @param doc - The document to be edited; either as a document handle or the document’s ID a string\n * @param path - The path to the nested value to be edited\n * @returns A function to update the nested value. Accepts either a new value, or an updater function that exposes the previous value and returns a new value.\n * @example Update a document’s name by providing the new value directly\n * ```\n * const handle = { _id: 'documentId', _type: 'documentType' }\n * const name = useDocument(handle, 'name')\n * const editName = useEditDocument(handle, 'name')\n *\n * function handleNameChange(event: React.ChangeEvent<HTMLInputElement>) {\n * editName(event.target.value)\n * }\n *\n * return (\n * <input type='text' value={name} onChange={handleNameChange} />\n * )\n * ```\n *\n * @example Update a count on a document by providing an updater function\n * ```\n * const handle = { _id: 'documentId', _type: 'documentType' }\n * const count = useDocument(handle, 'count')\n * const editCount = useEditDocument(handle, 'count')\n *\n * function incrementCount() {\n * editCount(previousCount => previousCount + 1)\n * }\n *\n * return (\n * <>\n * <button onClick={incrementCount}>\n * Increment\n * </button>\n * Current count: {count}\n * </>\n * )\n * ```\n */\nexport function useEditDocument<\n TDocument extends SanityDocument,\n TPath extends JsonMatchPath<TDocument>,\n>(\n doc: string | DocumentHandle<TDocument>,\n path: TPath,\n): (nextValue: Updater<JsonMatch<TDocument, TPath>>) => Promise<ActionsResult<TDocument>>\n\n/**\n *\n * @beta\n *\n * ## useEditDocument(doc)\n * Edit an entire document\n * @param doc - The document to be edited; either as a document handle or the document’s ID a string\n * @returns A function to update the document state. Accepts either a new document state, or an updater function that exposes the previous document state and returns the new document state.\n * @example\n * ```\n * const myDocumentHandle = { _id: 'documentId', _type: 'documentType' }\n *\n * const myDocument = useDocument(myDocumentHandle)\n * const { title, price } = myDocument\n *\n * const editMyDocument = useEditDocument(myDocumentHandle)\n *\n * function handleFieldChange(e: React.ChangeEvent<HTMLInputElement>) {\n * const {name, value} = e.currentTarget\n * // Use an updater function to update the document state based on the previous state\n * editMyDocument(previousDocument => ({\n * ...previousDocument,\n * [name]: value\n * }))\n * }\n *\n * function handleSaleChange(e: React.ChangeEvent<HTMLInputElement>) {\n * const { checked } = e.currentTarget\n * if (checked) {\n * // Use an updater function to add a new salePrice field;\n * // set it at a 20% discount off the normal price\n * editMyDocument(previousDocument => ({\n * ...previousDocument,\n * salePrice: previousDocument.price * 0.8,\n * }))\n * } else {\n * // Get the document state without the salePrice field\n * const { salePrice, ...rest } = myDocument\n * // Update the document state to remove the salePrice field\n * editMyDocument(rest)\n * }\n * }\n *\n * return (\n * <>\n * <form onSubmit={e => e.preventDefault()}>\n * <input name='title' type='text' value={title} onChange={handleFieldChange} />\n * <input name='price' type='number' value={price} onChange={handleFieldChange} />\n * <input\n * name='salePrice'\n * type='checkbox'\n * checked={Object(myDocument).hasOwnProperty('salePrice')}\n * onChange={handleSaleChange}\n * />\n * </form>\n * <pre><code>\n * {JSON.stringify(myDocument, null, 2)}\n * </code></pre>\n * </>\n * )\n * ```\n */\nexport function useEditDocument<TDocument extends SanityDocument>(\n doc: string | DocumentHandle<TDocument>,\n): (nextValue: Updater<TDocument>) => Promise<ActionsResult<TDocument>>\n\n/**\n *\n * @beta\n *\n * Enables editing of a document’s state.\n * When called with a `path` argument, the hook will return a function for updating a nested value.\n * When called without a `path` argument, the hook will return a function for updating the entire document.\n */\nexport function useEditDocument(\n doc: string | DocumentHandle,\n path?: string,\n): (updater: Updater<unknown>) => Promise<ActionsResult> {\n const documentId = typeof doc === 'string' ? doc : doc._id\n const instance = useSanityInstance()\n const apply = useApplyActions()\n const isDocumentReady = useCallback(\n () => getDocumentState(instance, documentId).getCurrent() !== undefined,\n [instance, documentId],\n )\n if (!isDocumentReady()) throw resolveDocument(instance, documentId)\n\n return useCallback(\n (updater: Updater<unknown>) => {\n if (path) {\n const nextValue =\n typeof updater === 'function'\n ? updater(getDocumentState(instance, documentId, path).getCurrent())\n : updater\n\n return apply(editDocument(documentId, {set: {[path]: nextValue}}))\n }\n\n const current = getDocumentState(instance, documentId).getCurrent()\n const nextValue = typeof updater === 'function' ? updater(current) : updater\n\n if (typeof nextValue !== 'object' || !nextValue) {\n throw new Error(\n `No path was provided to \\`useEditDocument\\` and the value provided was not a document object.`,\n )\n }\n\n const allKeys = Object.keys({...current, ...nextValue})\n const editActions = allKeys\n .filter((key) => !ignoredKeys.includes(key))\n .filter((key) => current?.[key] !== nextValue[key])\n .map((key) =>\n key in nextValue\n ? editDocument(documentId, {set: {[key]: nextValue[key]}})\n : editDocument(documentId, {unset: [key]}),\n )\n\n return apply(editActions)\n },\n [apply, documentId, instance, path],\n )\n}\n","import {type DocumentAction, getPermissionsState, type PermissionsResult} from '@sanity/sdk'\nimport {useCallback, useMemo, useSyncExternalStore} from 'react'\nimport {filter, firstValueFrom} from 'rxjs'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/** @beta */\nexport function usePermissions(actions: DocumentAction | DocumentAction[]): PermissionsResult {\n const instance = useSanityInstance()\n const isDocumentReady = useCallback(\n () => getPermissionsState(instance, actions).getCurrent() !== undefined,\n [actions, instance],\n )\n if (!isDocumentReady()) {\n throw firstValueFrom(\n getPermissionsState(instance, actions).observable.pipe(\n filter((result) => result !== undefined),\n ),\n )\n }\n\n const {subscribe, getCurrent} = useMemo(\n () => getPermissionsState(instance, actions),\n [actions, instance],\n )\n\n return useSyncExternalStore(subscribe, getCurrent) as PermissionsResult\n}\n","import {createDocumentListStore, type DocumentHandle, type DocumentListOptions} from '@sanity/sdk'\nimport {useCallback, useEffect, useState, useSyncExternalStore} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/**\n * @public\n * A live collection of {@link DocumentHandle}s, along with metadata about the collection and a function for loading more of them.\n * @category Types\n */\nexport interface DocumentHandleCollection {\n /** Retrieve more documents matching the provided options */\n loadMore: () => void\n /** The retrieved document handles of the documents matching the provided options */\n results: DocumentHandle[]\n /** Whether a retrieval of documents is in flight */\n isPending: boolean\n /** Whether more documents exist that match the provided options than have been retrieved */\n hasMore: boolean\n /** The total number of documents in the collection */\n count: number\n}\n\ntype DocumentListStore = ReturnType<typeof createDocumentListStore>\ntype DocumentListState = ReturnType<DocumentListStore['getState']>['getCurrent']\nconst STABLE_EMPTY = {\n results: [],\n isPending: false,\n hasMore: false,\n count: 0,\n}\n\n/**\n * @public\n *\n * Retrieves and provides access to a live collection of {@link DocumentHandle}s, with an optional filter and sort applied.\n * The returned document handles are canonical — that is, they refer to the document in its current state, whether draft, published, or within a release or perspective.\n * Because the returned document handle collection is live, the results will update in real time until the component invoking the hook is unmounted.\n *\n * @remarks\n * {@link DocumentHandle}s are used by many other hooks (such as {@link usePreview}, {@link useDocument}, and {@link useEditDocument})\n * to work with documents in various ways without the entire document needing to be fetched upfront.\n *\n * @category Documents\n * @param options - Options for narrowing and sorting the document collection\n * @returns The collection of document handles matching the provided options (if any), as well as properties describing the collection and a function to load more.\n *\n * @example Retrieving document handles for all documents of type 'movie'\n * ```\n * const { results, isPending } = useDocuments({ filter: '_type == \"movie\"' })\n *\n * return (\n * <div>\n * <h1>Movies</h1>\n * {results && (\n * <ul>\n * {results.map(movie => (<li key={movie._id}>…</li>))}\n * </ul>\n * )}\n * {isPending && <div>Loading movies…</div>}\n * </div>\n * )\n * ```\n *\n * @example Retrieving document handles for all movies released since 1980, sorted by director’s last name\n * ```\n * const { results } = useDocuments({\n * filter: '_type == \"movie\" && releaseDate >= \"1980-01-01\"',\n * sort: [\n * {\n * // Expand the `director` reference field with the dereferencing operator `->`\n * field: 'director->lastName',\n * sort: 'asc',\n * },\n * ],\n * })\n *\n * return (\n * <div>\n * <h1>Movies released since 1980</h1>\n * {results && (\n * <ol>\n * {results.map(movie => (<li key={movie._id}>…</li>))}\n * </ol>\n * )}\n * </div>\n * )\n * ```\n */\nexport function useDocuments(options: DocumentListOptions = {}): DocumentHandleCollection {\n const instance = useSanityInstance()\n\n // NOTE: useState is used because it guaranteed to return a stable reference\n // across renders\n const [ref] = useState<{\n storeInstance: DocumentListStore | null\n getCurrent: DocumentListState\n initialOptions: DocumentListOptions\n }>(() => ({\n storeInstance: null,\n getCurrent: () => STABLE_EMPTY,\n initialOptions: options,\n }))\n\n // serialize options to ensure it only calls `setOptions` when the values\n // themselves changes (in cases where devs put config inline)\n const serializedOptions = JSON.stringify(options)\n useEffect(() => {\n ref.storeInstance?.setOptions(JSON.parse(serializedOptions))\n }, [ref, serializedOptions])\n\n const subscribe = useCallback(\n (onStoreChanged: () => void) => {\n // to match the lifecycle of `useSyncExternalState`, we create the store\n // instance after subscribe and mutate the ref to connect everything\n ref.storeInstance = createDocumentListStore(instance)\n ref.storeInstance.setOptions(ref.initialOptions)\n const state = ref.storeInstance.getState()\n ref.getCurrent = state.getCurrent\n const unsubscribe = state.subscribe(onStoreChanged)\n\n return () => {\n // unsubscribe to clean up the state subscriptions\n unsubscribe()\n // dispose of the instance\n ref.storeInstance?.dispose()\n }\n },\n [instance, ref],\n )\n\n const getSnapshot = useCallback(() => {\n return ref.getCurrent()\n }, [ref])\n\n const state = useSyncExternalStore(subscribe, getSnapshot)\n\n const loadMore = useCallback(() => {\n ref.storeInstance?.loadMore()\n }, [ref])\n\n return {loadMore, ...state}\n}\n","import {type DocumentHandle, getPreviewState, type PreviewValue, resolvePreview} from '@sanity/sdk'\nimport {useCallback, useMemo, useSyncExternalStore} from 'react'\nimport {distinctUntilChanged, EMPTY, Observable, startWith, switchMap} from 'rxjs'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/**\n * @beta\n * @category Types\n */\nexport interface UsePreviewOptions {\n document: DocumentHandle\n ref?: React.RefObject<unknown>\n}\n\n/**\n * @beta\n * @category Types\n */\nexport interface UsePreviewResults {\n /** The results of resolving the document’s preview values */\n results: PreviewValue\n /** True when preview values are being refreshed */\n isPending: boolean\n}\n\n/**\n * @beta\n *\n * Returns the preview values of a document (specified via a `DocumentHandle`),\n * including the document’s `title`, `subtitle`, `media`, and `status`. These values are live and will update in realtime.\n * To reduce unnecessary network requests for resolving the preview values, an optional `ref` can be passed to the hook so that preview\n * resolution will only occur if the `ref` is intersecting the current viewport.\n *\n * @category Documents\n * @param options - The document handle for the document you want to resolve preview values for, and an optional ref\n * @returns The preview values for the given document and a boolean to indicate whether the resolution is pending\n *\n * @example Combining with useDocuments to render a collection of document previews\n * ```\n * // PreviewComponent.jsx\n * export default function PreviewComponent({ document }) {\n * const { results: { title, subtitle, media }, isPending } = usePreview({ document })\n * return (\n * <article style={{ opacity: isPending ? 0.5 : 1}}>\n * {media?.type === 'image-asset' ? <img src={media.url} alt='' /> : ''}\n * <h2>{title}</h2>\n * <p>{subtitle}</p>\n * </article>\n * )\n * }\n *\n * // DocumentList.jsx\n * const { results, isPending } = useDocuments({ filter: '_type == \"movie\"' })\n * return (\n * <div>\n * <h1>Movies</h1>\n * <ul>\n * {isPending ? 'Loading…' : results.map(movie => (\n * <li key={movie._id}>\n * <Suspense fallback='Loading…'>\n * <PreviewComponent document={movie} />\n * </Suspense>\n * </li>\n * ))}\n * </ul>\n * </div>\n * )\n * ```\n */\nexport function usePreview({document: {_id, _type}, ref}: UsePreviewOptions): UsePreviewResults {\n const instance = useSanityInstance()\n\n const stateSource = useMemo(\n () => getPreviewState(instance, {document: {_id, _type}}),\n [instance, _id, _type],\n )\n\n // Create subscribe function for useSyncExternalStore\n const subscribe = useCallback(\n (onStoreChanged: () => void) => {\n const subscription = new Observable<boolean>((observer) => {\n // for environments that don't have an intersection observer\n if (typeof IntersectionObserver === 'undefined' || typeof HTMLElement === 'undefined') {\n return\n }\n\n const intersectionObserver = new IntersectionObserver(\n ([entry]) => observer.next(entry.isIntersecting),\n {rootMargin: '0px', threshold: 0},\n )\n if (ref?.current && ref.current instanceof HTMLElement) {\n intersectionObserver.observe(ref.current)\n }\n return () => intersectionObserver.disconnect()\n })\n .pipe(\n startWith(false),\n distinctUntilChanged(),\n switchMap((isVisible) =>\n isVisible\n ? new Observable<void>((obs) => {\n return stateSource.subscribe(() => obs.next())\n })\n : EMPTY,\n ),\n )\n .subscribe({next: onStoreChanged})\n\n return () => subscription.unsubscribe()\n },\n [stateSource, ref],\n )\n\n // Create getSnapshot function to return current state\n const getSnapshot = useCallback(() => {\n const currentState = stateSource.getCurrent()\n if (currentState.results === null) throw resolvePreview(instance, {document: {_id, _type}})\n return currentState as UsePreviewResults\n }, [_id, _type, instance, stateSource])\n\n return useSyncExternalStore(subscribe, getSnapshot)\n}\n","var A=async(t,r)=>{let e=typeof r==\"function\"?await r(t):r;if(e)return t.scheme===\"bearer\"?`Bearer ${e}`:t.scheme===\"basic\"?`Basic ${btoa(e)}`:e},R={bodySerializer:t=>JSON.stringify(t,(r,e)=>typeof e==\"bigint\"?e.toString():e)},k=t=>{switch(t){case \"label\":return \".\";case \"matrix\":return \";\";case \"simple\":return \",\";default:return \"&\"}},_=t=>{switch(t){case \"form\":return \",\";case \"pipeDelimited\":return \"|\";case \"spaceDelimited\":return \"%20\";default:return \",\"}},D=t=>{switch(t){case \"label\":return \".\";case \"matrix\":return \";\";case \"simple\":return \",\";default:return \"&\"}},q=({allowReserved:t,explode:r,name:e,style:i,value:a})=>{if(!r){let s=(t?a:a.map(l=>encodeURIComponent(l))).join(_(i));switch(i){case \"label\":return `.${s}`;case \"matrix\":return `;${e}=${s}`;case \"simple\":return s;default:return `${e}=${s}`}}let o=k(i),n=a.map(s=>i===\"label\"||i===\"simple\"?t?s:encodeURIComponent(s):y({allowReserved:t,name:e,value:s})).join(o);return i===\"label\"||i===\"matrix\"?o+n:n},y=({allowReserved:t,name:r,value:e})=>{if(e==null)return \"\";if(typeof e==\"object\")throw new Error(\"Deeply-nested arrays/objects aren\\u2019t supported. Provide your own `querySerializer()` to handle these.\");return `${r}=${t?e:encodeURIComponent(e)}`},S=({allowReserved:t,explode:r,name:e,style:i,value:a})=>{if(a instanceof Date)return `${e}=${a.toISOString()}`;if(i!==\"deepObject\"&&!r){let s=[];Object.entries(a).forEach(([f,u])=>{s=[...s,f,t?u:encodeURIComponent(u)];});let l=s.join(\",\");switch(i){case \"form\":return `${e}=${l}`;case \"label\":return `.${l}`;case \"matrix\":return `;${e}=${l}`;default:return l}}let o=D(i),n=Object.entries(a).map(([s,l])=>y({allowReserved:t,name:i===\"deepObject\"?`${e}[${s}]`:s,value:l})).join(o);return i===\"label\"||i===\"matrix\"?o+n:n};var H=/\\{[^{}]+\\}/g,B=({path:t,url:r})=>{let e=r,i=r.match(H);if(i)for(let a of i){let o=false,n=a.substring(1,a.length-1),s=\"simple\";n.endsWith(\"*\")&&(o=true,n=n.substring(0,n.length-1)),n.startsWith(\".\")?(n=n.substring(1),s=\"label\"):n.startsWith(\";\")&&(n=n.substring(1),s=\"matrix\");let l=t[n];if(l==null)continue;if(Array.isArray(l)){e=e.replace(a,q({explode:o,name:n,style:s,value:l}));continue}if(typeof l==\"object\"){e=e.replace(a,S({explode:o,name:n,style:s,value:l}));continue}if(s===\"matrix\"){e=e.replace(a,`;${y({name:n,value:l})}`);continue}let f=encodeURIComponent(s===\"label\"?`.${l}`:l);e=e.replace(a,f);}return e},E=({allowReserved:t,array:r,object:e}={})=>a=>{let o=[];if(a&&typeof a==\"object\")for(let n in a){let s=a[n];if(s!=null){if(Array.isArray(s)){o=[...o,q({allowReserved:t,explode:true,name:n,style:\"form\",value:s,...r})];continue}if(typeof s==\"object\"){o=[...o,S({allowReserved:t,explode:true,name:n,style:\"deepObject\",value:s,...e})];continue}o=[...o,y({allowReserved:t,name:n,value:s})];}}return o.join(\"&\")},P=t=>{if(!t)return \"stream\";let r=t.split(\";\")[0]?.trim();if(r){if(r.startsWith(\"application/json\")||r.endsWith(\"+json\"))return \"json\";if(r===\"multipart/form-data\")return \"formData\";if([\"application/\",\"audio/\",\"image/\",\"video/\"].some(e=>r.startsWith(e)))return \"blob\";if(r.startsWith(\"text/\"))return \"text\"}},I=async({security:t,...r})=>{for(let e of t){let i=await A(e,r.auth);if(!i)continue;let a=e.name??\"Authorization\";switch(e.in){case \"query\":r.query||(r.query={}),r.query[a]=i;break;case \"header\":default:r.headers.set(a,i);break}return}},O=t=>W({baseUrl:t.baseUrl??\"\",path:t.path,query:t.query,querySerializer:typeof t.querySerializer==\"function\"?t.querySerializer:E(t.querySerializer),url:t.url}),W=({baseUrl:t,path:r,query:e,querySerializer:i,url:a})=>{let o=a.startsWith(\"/\")?a:`/${a}`,n=t+o;r&&(n=B({path:r,url:n}));let s=e?i(e):\"\";return s.startsWith(\"?\")&&(s=s.substring(1)),s&&(n+=`?${s}`),n},C=(t,r)=>{let e={...t,...r};return e.baseUrl?.endsWith(\"/\")&&(e.baseUrl=e.baseUrl.substring(0,e.baseUrl.length-1)),e.headers=x(t.headers,r.headers),e},x=(...t)=>{let r=new Headers;for(let e of t){if(!e||typeof e!=\"object\")continue;let i=e instanceof Headers?e.entries():Object.entries(e);for(let[a,o]of i)if(o===null)r.delete(a);else if(Array.isArray(o))for(let n of o)r.append(a,n);else o!==undefined&&r.set(a,typeof o==\"object\"?JSON.stringify(o):o);}return r},h=class{_fns;constructor(){this._fns=[];}clear(){this._fns=[];}exists(r){return this._fns.indexOf(r)!==-1}eject(r){let e=this._fns.indexOf(r);e!==-1&&(this._fns=[...this._fns.slice(0,e),...this._fns.slice(e+1)]);}use(r){this._fns=[...this._fns,r];}},v=()=>({error:new h,request:new h,response:new h}),N=E({allowReserved:false,array:{explode:true,style:\"form\"},object:{explode:true,style:\"deepObject\"}}),Q={\"Content-Type\":\"application/json\"},w=(t={})=>({...R,baseUrl:\"\",headers:Q,parseAs:\"auto\",querySerializer:N,...t});var J=(t={})=>{let r=C(w(),t),e=()=>({...r}),i=n=>(r=C(r,n),e()),a=v(),o=async n=>{let s={...r,...n,fetch:n.fetch??r.fetch??globalThis.fetch,headers:x(r.headers,n.headers)};s.security&&await I({...s,security:s.security}),s.body&&s.bodySerializer&&(s.body=s.bodySerializer(s.body)),s.body||s.headers.delete(\"Content-Type\");let l=O(s),f={redirect:\"follow\",...s},u=new Request(l,f);for(let p of a.request._fns)u=await p(u,s);let $=s.fetch,c=await $(u);for(let p of a.response._fns)c=await p(c,u,s);let m={request:u,response:c};if(c.ok){if(c.status===204||c.headers.get(\"Content-Length\")===\"0\")return {data:{},...m};let p=(s.parseAs===\"auto\"?P(c.headers.get(\"Content-Type\")):s.parseAs)??\"json\";if(p===\"stream\")return {data:c.body,...m};let b=await c[p]();return p===\"json\"&&(s.responseValidator&&await s.responseValidator(b),s.responseTransformer&&(b=await s.responseTransformer(b))),{data:b,...m}}let g=await c.text();try{g=JSON.parse(g);}catch{}let d=g;for(let p of a.error._fns)d=await p(g,c,u,s);if(d=d||{},s.throwOnError)throw d;return {error:d,...m}};return {buildUrl:O,connect:n=>o({...n,method:\"CONNECT\"}),delete:n=>o({...n,method:\"DELETE\"}),get:n=>o({...n,method:\"GET\"}),getConfig:e,head:n=>o({...n,method:\"HEAD\"}),interceptors:a,options:n=>o({...n,method:\"OPTIONS\"}),patch:n=>o({...n,method:\"PATCH\"}),post:n=>o({...n,method:\"POST\"}),put:n=>o({...n,method:\"PUT\"}),request:o,setConfig:i,trace:n=>o({...n,method:\"TRACE\"})}};\n\n// This file is auto-generated by @hey-api/openapi-ts\nconst client = J(w({\n throwOnError: true\n}));\n\n// This file is auto-generated by @hey-api/openapi-ts\n/**\n * Get Permissions.\n * Gets the available permissions within the scope of a resource. These permissions can be applied to roles and are used to determine user/robot access to resources. <br /> <br /> Requires permission: `sanity.{resourceType}.roles.read`\n *\n */\nconst getPermissions = (options) => {\n var _a;\n return ((_a = options.client) !== null && _a !== void 0 ? _a : client).get(Object.assign({ security: [\n {\n scheme: 'bearer',\n type: 'http'\n }\n ], url: '/vX/access/{resourceType}/{resourceId}/permissions' }, options));\n};\n/**\n * Create a permission.\n * Creates a custom permission on a resource. <br /> <br /> Requires permission: `sanity.{resourceType}.roles.create` <br /> Requires feature: `advancedRolesManagement` <br /> <br /> See Example. It creates a permission to allow a user to read legal documents in project `c7ja4siy` and dataset `production`.\n *\n */\nconst createPermission = (options) => {\n var _a;\n return ((_a = options.client) !== null && _a !== void 0 ? _a : client).post(Object.assign(Object.assign({ security: [\n {\n scheme: 'bearer',\n type: 'http'\n }\n ], url: '/vX/access/{resourceType}/{resourceId}/permissions' }, options), { headers: Object.assign({ 'Content-Type': 'application/json' }, options === null || options === void 0 ? void 0 : options.headers) }));\n};\n/**\n * Delete a permission\n * Deletes a specific permission for a resource. Can only be used with custom permissions. Requires permission\n * - `sanity.{resourceType}.roles.delete`\n * Requires feature:\n * - `advancedRolesManagement`\n *\n */\nconst deletePermission = (options) => {\n var _a;\n return ((_a = options.client) !== null && _a !== void 0 ? _a : client).delete(Object.assign({ security: [\n {\n scheme: 'bearer',\n type: 'http'\n }\n ], url: '/vX/access/{resourceType}/{resourceId}/permissions/{permissionName}' }, options));\n};\n/**\n * Get a permission\n * Gets a permission for a resource by name. Requires permission\n * - `sanity.{resourceType}.roles.read`\n *\n */\nconst getPermission = (options) => {\n var _a;\n return ((_a = options.client) !== null && _a !== void 0 ? _a : client).get(Object.assign({ security: [\n {\n scheme: 'bearer',\n type: 'http'\n }\n ], url: '/vX/access/{resourceType}/{resourceId}/permissions/{permissionName}' }, options));\n};\n/**\n * Update a permission\n * Updates a permission for a resource. Requires permission\n * - `sanity.{resourceType}.roles.update`\n * Requires feature:\n * - `advancedRolesManagement`\n *\n */\nconst updatePermission = (options) => {\n var _a;\n return ((_a = options.client) !== null && _a !== void 0 ? _a : client).put(Object.assign(Object.assign({ security: [\n {\n scheme: 'bearer',\n type: 'http'\n }\n ], url: '/vX/access/{resourceType}/{resourceId}/permissions/{permissionName}' }, options), { headers: Object.assign({ 'Content-Type': 'application/json' }, options === null || options === void 0 ? void 0 : options.headers) }));\n};\n/**\n * Get current user permissions.\n * Gets the available permissions within the scope of a resource. These permissions can be applied to roles and are used to determine user/robot access to resources.\n *\n */\nconst getMyPermissions = (options) => {\n var _a;\n return ((_a = options === null || options === void 0 ? void 0 : options.client) !== null && _a !== void 0 ? _a : client).get(Object.assign({ security: [\n {\n scheme: 'bearer',\n type: 'http'\n }\n ], url: '/vX/access/permissions/me' }, options));\n};\n/**\n * Get current user permissions.\n * Gets current user permissions within scope of a resource. These permissions can be applied to roles and are used to determine user/robot access to resources.\n *\n */\nconst getUserPermissions = (options) => {\n var _a;\n return ((_a = options.client) !== null && _a !== void 0 ? _a : client).get(Object.assign({ security: [\n {\n scheme: 'bearer',\n type: 'http'\n }\n ], url: '/vX/access/{resourceType}/{resourceId}/user-permissions/me' }, options));\n};\n/**\n * List all users of a resource and their assigned roles.\n * Retrieve a list of all users of a resource along with their assigned roles. When the resourceType is `organization`, this endpoint will return users of projects owned by the organization. Requires permission\n * - `sanity.{resourceType}.members.read`\n *\n */\nconst getUsers = (options) => {\n var _a;\n return ((_a = options.client) !== null && _a !== void 0 ? _a : client).get(Object.assign({ security: [\n {\n scheme: 'bearer',\n type: 'http'\n }\n ], url: '/vX/access/{resourceType}/{resourceId}/users' }, options));\n};\n/**\n * Apply organization default role to all users.\n * Add default role to all resource users. Limited to Organization. Requires permission\n * - `sanity.organization.members.update`\n *\n */\nconst addDefaultRoleToUsers = (options) => {\n var _a;\n return ((_a = options.client) !== null && _a !== void 0 ? _a : client).put(Object.assign({ security: [\n {\n scheme: 'bearer',\n type: 'http'\n }\n ], url: '/vX/access/organization/{resourceId}/users/roles/default' }, options));\n};\n/**\n * Remove a user from a resource.\n * This removes all roles. If the resourceType is `organization`, this will also remove the user from all projects owned by the organization. Requires permission\n * - `sanity.{resourceType}.members.delete`\n *\n */\nconst removeUser = (options) => {\n var _a;\n return ((_a = options.client) !== null && _a !== void 0 ? _a : client).delete(Object.assign({ security: [\n {\n scheme: 'bearer',\n type: 'http'\n }\n ], url: '/vX/access/{resourceType}/{resourceId}/users/{sanityUserId}' }, options));\n};\n/**\n * Get user and roles.\n * Get the users for a single user of a resource. Requires permission - `sanity.{resourceType}.members.read`\n */\nconst getUser = (options) => {\n var _a;\n return ((_a = options.client) !== null && _a !== void 0 ? _a : client).get(Object.assign({ security: [\n {\n scheme: 'bearer',\n type: 'http'\n }\n ], url: '/vX/access/{resourceType}/{resourceId}/users/{sanityUserId}' }, options));\n};\n/**\n * Remove a role from a user in a resource.\n * You cannot remove the last role from a user. You must have at least one role assigned to a user. Requires permission\n * - `sanity.{resourceType}.members.update`\n *\n */\nconst removeRoleFromUser = (options) => {\n var _a;\n return ((_a = options.client) !== null && _a !== void 0 ? _a : client).delete(Object.assign({ security: [\n {\n scheme: 'bearer',\n type: 'http'\n }\n ], url: '/vX/access/{resourceType}/{resourceId}/users/{sanityUserId}/roles/{roleName}' }, options));\n};\n/**\n * Add a role to a user.\n * Add a role to a user. Requires permission\n * - `sanity.{resourceType}.members.update`\n *\n */\nconst addRoleToUser = (options) => {\n var _a;\n return ((_a = options.client) !== null && _a !== void 0 ? _a : client).put(Object.assign({ security: [\n {\n scheme: 'bearer',\n type: 'http'\n }\n ], url: '/vX/access/{resourceType}/{resourceId}/users/{sanityUserId}/roles/{roleName}' }, options));\n};\n/**\n * List permissions for a user\n * Normalized list of permission resources for a user across all assigned roles. Requires permissions:\n * - `sanity.{resourceType}.members.read` for resource scoped roles (e.g. project admin)\n *\n */\nconst getuserPermissions = (options) => {\n var _a;\n return ((_a = options.client) !== null && _a !== void 0 ? _a : client).get(Object.assign({ security: [\n {\n scheme: 'bearer',\n type: 'http'\n }\n ], url: '/vX/access/{resourceType}/{resourceId}/users/{sanityUserId}/permissions' }, options));\n};\n/**\n * List roles assignable to a user on this resource.\n * Requires permission\n * - `sanity.{resourceType}.roles.read`\n *\n */\nconst getRoles = (options) => {\n var _a;\n return ((_a = options.client) !== null && _a !== void 0 ? _a : client).get(Object.assign({ security: [\n {\n scheme: 'bearer',\n type: 'http'\n }\n ], url: '/vX/access/{resourceType}/{resourceId}/roles' }, options));\n};\n/**\n * Create a role\n * Requires permission:\n * - `sanity.{resourceType}.roles.create`\n * Requires feature:\n * - `advancedRolesManagement`\n *\n */\nconst createRole = (options) => {\n var _a;\n return ((_a = options.client) !== null && _a !== void 0 ? _a : client).post(Object.assign(Object.assign({ security: [\n {\n scheme: 'bearer',\n type: 'http'\n }\n ], url: '/vX/access/{resourceType}/{resourceId}/roles' }, options), { headers: Object.assign({ 'Content-Type': 'application/json' }, options === null || options === void 0 ? void 0 : options.headers) }));\n};\n/**\n * Delete a role\n * Requires permission:\n * - `sanity.{resourceType}.roles.delete` for resource scoped roles (e.g. project admin)\n *\n * Requires feature:\n * - `advancedRolesManagement`\n *\n * Cannot delete a role that is assigned to a user. The role needs to be removed from users first.\n *\n */\nconst deleteRole = (options) => {\n var _a;\n return ((_a = options.client) !== null && _a !== void 0 ? _a : client).delete(Object.assign({ security: [\n {\n scheme: 'bearer',\n type: 'http'\n }\n ], url: '/vX/access/{resourceType}/{resourceId}/roles/{roleName}' }, options));\n};\n/**\n * Get a role\n * Requires permission\n * - `sanity.{resourceType}.roles.read`\n *\n */\nconst getRole = (options) => {\n var _a;\n return ((_a = options.client) !== null && _a !== void 0 ? _a : client).get(Object.assign({ security: [\n {\n scheme: 'bearer',\n type: 'http'\n }\n ], url: '/vX/access/{resourceType}/{resourceId}/roles/{roleName}' }, options));\n};\n/**\n * Update a role\n * Requires permission:\n * - `sanity.{resourceType}.roles.update`\n *\n * Requires feature:\n * - `advancedRolesManagement`\n *\n * Replaces existing object values including permissions.\n *\n */\nconst updateRole = (options) => {\n var _a;\n return ((_a = options.client) !== null && _a !== void 0 ? _a : client).put(Object.assign(Object.assign({ security: [\n {\n scheme: 'bearer',\n type: 'http'\n }\n ], url: '/vX/access/{resourceType}/{resourceId}/roles/{roleName}' }, options), { headers: Object.assign({ 'Content-Type': 'application/json' }, options === null || options === void 0 ? void 0 : options.headers) }));\n};\n/**\n * List all requests for given project/organization.\n * Permissions `sanity.{resourceType}.members.read`\n */\nconst getRequests = (options) => {\n var _a;\n return ((_a = options.client) !== null && _a !== void 0 ? _a : client).get(Object.assign({ security: [\n {\n scheme: 'bearer',\n type: 'http'\n }\n ], url: '/v2024-07-01/access/{resourceType}/{resourceId}/requests' }, options));\n};\n/**\n * Create a new Request\n * Creates a new request for the given project/organization. Requires an authenticated user.\n */\nconst createRequest = (options) => {\n var _a;\n return ((_a = options.client) !== null && _a !== void 0 ? _a : client).post(Object.assign(Object.assign({ security: [\n {\n scheme: 'bearer',\n type: 'http'\n }\n ], url: '/v2024-07-01/access/{resourceType}/{resourceId}/requests' }, options), { headers: Object.assign({ 'Content-Type': 'application/json' }, options === null || options === void 0 ? void 0 : options.headers) }));\n};\n/**\n * Accept request\n * Permissions `sanity.{resourceType}.members.invite`\n */\nconst acceptRequest = (options) => {\n var _a;\n return ((_a = options.client) !== null && _a !== void 0 ? _a : client).put(Object.assign(Object.assign({ security: [\n {\n scheme: 'bearer',\n type: 'http'\n }\n ], url: '/v2024-07-01/access/{resourceType}/{resourceId}/requests/{requestId}/accept' }, options), { headers: Object.assign({ 'Content-Type': 'application/json' }, options === null || options === void 0 ? void 0 : options.headers) }));\n};\n/**\n * Decline request\n * Permissions `sanity.{resourceType}.members.invite`\n */\nconst declineRequest = (options) => {\n var _a;\n return ((_a = options.client) !== null && _a !== void 0 ? _a : client).put(Object.assign({ security: [\n {\n scheme: 'bearer',\n type: 'http'\n }\n ], url: '/v2024-07-01/access/{resourceType}/{resourceId}/requests/{requestId}/decline' }, options));\n};\n/**\n * List all request for current user\n * Requires an authenticated user.\n */\nconst getMyRequests = (options) => {\n var _a;\n return ((_a = options === null || options === void 0 ? void 0 : options.client) !== null && _a !== void 0 ? _a : client).get(Object.assign({ security: [\n {\n scheme: 'bearer',\n type: 'http'\n }\n ], url: '/v2024-07-01/access/requests/me' }, options));\n};\n/**\n * Get invites\n * Retrieves a list of invites for the specified resource.\n *\n * Only pending invites are retrieved by default. Use the optional `status`\n * parameter to change the filter behavior. You can select multiple\n * statuses by repeating the parameter.\n *\n * ### Children invites\n *\n * By default, only invites for the specified resource are returned. Use the\n * optional `includeChildren` parameter to include invites for children\n * resources as well. This only applies to `organization` resources.\n *\n * ### Authorization\n *\n * This endpoint requires an authenticated user session with the following\n * permissions:\n * - `sanity.{resourceType}.members.read`\n *\n */\nconst getInvites = (options) => {\n var _a;\n return ((_a = options.client) !== null && _a !== void 0 ? _a : client).get(Object.assign({ security: [\n {\n scheme: 'bearer',\n type: 'http'\n }\n ], url: '/vX/access/{resourceType}/{resourceId}/invites' }, options));\n};\n/**\n * Create invite\n * Creates an invite for the specified resource.\n *\n * The invitee will receive an email with a link to accept the invite.\n *\n * Each invitee can only receive one invite per resource and role.\n * Attempting to create an invite using a non-existent role, or a role that\n * cannot be granted to users will result in a Bad Request error.\n *\n * ### Unavailable resources\n *\n * If the underlying resource is unavailable then a Bad Request error will\n * be returned. A common example of an unavailable resource is a project\n * that is blocked or archived.\n *\n * ### Authorization\n *\n * This endpoint requires an authenticated user session with the following\n * permissions:\n * - `sanity.{resourceType}.members.invite`\n *\n * Additionally, only administrators can invite other administrators.\n *\n */\nconst createInvite = (options) => {\n var _a;\n return ((_a = options.client) !== null && _a !== void 0 ? _a : client).post(Object.assign(Object.assign({ security: [\n {\n scheme: 'bearer',\n type: 'http'\n }\n ], url: '/vX/access/{resourceType}/{resourceId}/invites' }, options), { headers: Object.assign({ 'Content-Type': 'application/json' }, options === null || options === void 0 ? void 0 : options.headers) }));\n};\n/**\n * Revoke invite\n * Revokes an invite.\n *\n * Attempting to revoke an invite that has already been accepted or revoked\n * will result in a Bad Request error.\n *\n * ### Authorization\n *\n * This endpoint requires an authenticated user session with the following\n * permissions:\n * - `sanity.{resourceType}.members.invite`\n *\n */\nconst revokeInvite = (options) => {\n var _a;\n return ((_a = options.client) !== null && _a !== void 0 ? _a : client).delete(Object.assign({ security: [\n {\n scheme: 'bearer',\n type: 'http'\n }\n ], url: '/vX/access/{resourceType}/{resourceId}/invites/{inviteId}' }, options));\n};\n/**\n * Get invite\n * Retrieves an invite using its token.\n *\n * ### Authorization\n *\n * This endpoint does not require authentication.\n *\n */\nconst getInviteByToken = (options) => {\n var _a;\n return ((_a = options.client) !== null && _a !== void 0 ? _a : client).get(Object.assign({ security: [\n {\n scheme: 'bearer',\n type: 'http'\n }\n ], url: '/vX/access/{resourceType}/{resourceId}/invites/token/{inviteToken}' }, options));\n};\n/**\n * Accept invite\n * Accepts an invite using its token.\n *\n * Attempting to accept an invite that is already accepted or revoked will\n * result in a Bad Request error.\n *\n * Once the invite has been accepted, the user will have access to the\n * resource with the role assigned as part of the invitation.\n *\n * > **Access is propagated internally and may take a up to a few minutes to\n * > be fully available across all systems.**\n *\n * ### Unavailable resources\n *\n * If the underlying resource is unavailable then a Bad Request error will\n * be returned. A common example of an unavailable resource is a project\n * that is blocked or archived.\n *\n * ### Member quota\n *\n * Some resources have a limit on the number of members. If accepting an\n * invite would go over this limit, then a Payment Required error is\n * returned.\n *\n * ### Authorization\n *\n * This endpoint requires an authenticated user session.\n *\n */\nconst acceptInvite = (options) => {\n var _a;\n return ((_a = options.client) !== null && _a !== void 0 ? _a : client).post(Object.assign({ security: [\n {\n scheme: 'bearer',\n type: 'http'\n }\n ], url: '/vX/access/{resourceType}/{resourceId}/invites/token/{inviteToken}/accept' }, options));\n};\n/**\n * Get robots with access to this resource\n * Retrieves a list of robots that have access to the specified resource.\n *\n * ### Access to children resources\n *\n * By default, this endpoint returns robots that have at least one role on\n * the specified resource. Use the optional `includeChildren` parameter to\n * include robots that have a role on children resources as well. This\n * only applies to `organization` resources.\n *\n * ### Authorization\n *\n * This endpoint requires an authenticated user session with the following\n * permissions:\n * - `sanity.{resourceType}.tokens.read`\n *\n */\nconst getRobots = (options) => {\n var _a;\n return ((_a = options.client) !== null && _a !== void 0 ? _a : client).get(Object.assign({ security: [\n {\n scheme: 'bearer',\n type: 'http'\n }\n ], url: '/vX/access/{resourceType}/{resourceId}/robots' }, options));\n};\n/**\n * Create robot and associated token\n * Creates a robot with the specified role grants and returns its secret\n * token.\n *\n * The API currently only supports organization-scoped robots. This means\n * that `organization` must be provided to the `resourceType` parameter.\n *\n * Only the specified resource and its children resources can be specified\n * in the role grants. The robot will not have access to other resources.\n *\n * ### Secret token\n *\n * The secret token is only returned once and can not be retrieved again.\n * If the token is lost, a new robot must be created. The previous robot\n * should be deleted.\n *\n * ### Authorization\n *\n * This endpoint requires an authenticated user session with the following\n * permissions:\n * - `sanity.{resourceType}.tokens.create`\n *\n */\nconst createRobot = (options) => {\n var _a;\n return ((_a = options.client) !== null && _a !== void 0 ? _a : client).post(Object.assign(Object.assign({ security: [\n {\n scheme: 'bearer',\n type: 'http'\n }\n ], url: '/vX/access/{resourceType}/{resourceId}/robots' }, options), { headers: Object.assign({ 'Content-Type': 'application/json' }, options === null || options === void 0 ? void 0 : options.headers) }));\n};\n/**\n * Delete robot and associated token\n * Deletes a robot and revokes its token.\n *\n * ### Authorization\n *\n * This endpoint requires an authenticated user session with the following\n * permissions:\n * - `sanity.{resourceType}.tokens.delete`\n *\n */\nconst deleteRobot = (options) => {\n var _a;\n return ((_a = options.client) !== null && _a !== void 0 ? _a : client).delete(Object.assign({ security: [\n {\n scheme: 'bearer',\n type: 'http'\n }\n ], url: '/vX/access/{resourceType}/{resourceId}/robots/{robotId}' }, options));\n};\n/**\n * Get robot metadata\n * Retrieves a robot using its unique identifier.\n *\n * It's not possible to retrieve a robot token.\n *\n * ### Authorization\n *\n * This endpoint requires an authenticated user session with the following\n * permissions:\n * - `sanity.{resourceType}.tokens.read`\n *\n */\nconst getRobot = (options) => {\n var _a;\n return ((_a = options.client) !== null && _a !== void 0 ? _a : client).get(Object.assign({ security: [\n {\n scheme: 'bearer',\n type: 'http'\n }\n ], url: '/vX/access/{resourceType}/{resourceId}/robots/{robotId}' }, options));\n};\n/**\n * Check if current user has specified permissions\n * Checks if the current user has the specified permissions for the given resource.\n * Returns an object mapping each requested permission to a boolean indicating whether the user has that permission.\n *\n */\nconst checkUserPermissions = (options) => {\n var _a;\n return ((_a = options.client) !== null && _a !== void 0 ? _a : client).get(Object.assign({ security: [\n {\n scheme: 'bearer',\n type: 'http'\n }\n ], url: '/vX/access/{resourceType}/{resourceId}/user-permissions/me/check' }, options));\n};\n\nexport { acceptInvite, acceptRequest, addDefaultRoleToUsers, addRoleToUser, checkUserPermissions, client, createInvite, createPermission, createRequest, createRobot, createRole, declineRequest, deletePermission, deleteRobot, deleteRole, getInviteByToken, getInvites, getMyPermissions, getMyRequests, getPermission, getPermissions, getRequests, getRobot, getRobots, getRole, getRoles, getUser, getUserPermissions, getUsers, getuserPermissions, removeRoleFromUser, removeUser, revokeInvite, updatePermission, updateRole };\n","import {type ResourceType, type User} from '@sanity/access-api'\nimport {createUsersStore} from '@sanity/sdk'\nimport {useCallback, useEffect, useState, useSyncExternalStore} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/**\n * @public\n */\ninterface UseUsersParams {\n /**\n * The type of resource to fetch users for.\n */\n resourceType: ResourceType\n /**\n * The ID of the resource to fetch users for.\n */\n resourceId: string\n /**\n * The limit of users to fetch.\n */\n limit?: number\n}\n\n/** @public */\nexport interface UseUsersResult {\n /**\n * The users fetched.\n */\n users: User[]\n /**\n * Whether there are more users to fetch.\n */\n hasMore: boolean\n /**\n * Load more users.\n */\n loadMore: () => void\n}\n\n/** @public */\nexport function useUsers(params: UseUsersParams): UseUsersResult {\n const instance = useSanityInstance()\n const [store] = useState(() => createUsersStore(instance))\n\n useEffect(() => {\n store.setOptions({\n resourceType: params.resourceType,\n resourceId: params.resourceId,\n })\n }, [params.resourceType, params.resourceId, store])\n\n const subscribe = useCallback(\n (onStoreChanged: () => void) => {\n if (store.getState().getCurrent().initialFetchCompleted === false) {\n store.resolveUsers()\n }\n const unsubscribe = store.getState().subscribe(onStoreChanged)\n\n return () => {\n unsubscribe()\n store.dispose()\n }\n },\n [store],\n )\n\n const getSnapshot = useCallback(() => store.getState().getCurrent(), [store])\n\n const {users, hasMore} = useSyncExternalStore(subscribe, getSnapshot) || {}\n\n return {users, hasMore, loadMore: store.loadMore}\n}\n"],"names":["nextValue","state"],"mappings":";;;;;AASa,MAAA,eAAe,sBAAsB,aAAa,GCkBlD,iBAA2C,sBAAsB,mBAAmB;ACK1F,SAAS,UAAU,SAAsC;AACxD,QAAA,WAAW,qBAEX,YAAY;AAAA,IAChB,CAAC,kBAA8B;AAE7B,YAAM,eADU,sBAAsB,UAAU,OAAO,EAC1B,UAAU;AAAA,QACrC,MAAM;AAAA,QACN,OAAO,CAAC,UAAU;AAGR,kBAAA,MAAM,oCAAoC,KAAK;AAAA,QAAA;AAAA,MACzD,CACD;AACM,aAAA,MAAM,aAAa,YAAY;AAAA,IACxC;AAAA,IACA,CAAC,UAAU,OAAO;AAAA,EAAA,GAGd,cAAc,YAAY,MACvB,UAAU,UAAU,OAAO,GACjC,CAAC,UAAU,OAAO,CAAC;AAEf,SAAA,qBAAqB,WAAW,WAAW;AACpD;ACRO,SAAS,mBAGd,SAAoF;AACpF,QAAM,EAAC,WAAW,cAAc,MAAM,WAAW,cAAa,SACxD,WAAW,kBAAkB,GAC7B,CAAC,QAAQ,SAAS,IAAI,SAAiB,MAAM,GAE7C,aAAa;AAAA,IACjB,MAAM,sBAAsB,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1
  {
2
2
  "name": "@sanity/sdk-react",
3
- "version": "0.0.0-alpha.12",
3
+ "version": "0.0.0-alpha.13",
4
4
  "private": false,
5
5
  "description": "Sanity SDK React toolkit for Content OS",
6
6
  "keywords": [
@@ -68,31 +68,31 @@
68
68
  "prettier": "@sanity/prettier-config",
69
69
  "dependencies": {
70
70
  "@sanity/logos": "^2.1.13",
71
- "@sanity/os": "^0.2.0",
72
71
  "@sanity/types": "^3.67.1",
73
72
  "react-error-boundary": "^4.1.2",
74
73
  "rxjs": "^7.8.1",
75
- "@sanity/sdk": "0.0.0-alpha.11"
74
+ "@sanity/sdk": "0.0.0-alpha.12"
76
75
  },
77
76
  "devDependencies": {
78
- "@sanity/client": "^6.27.2",
77
+ "@sanity/access-api": "^2.2.6",
78
+ "@sanity/client": "^6.28.2",
79
79
  "@sanity/comlink": "^2.0.5",
80
- "@sanity/pkg-utils": "^6.12.2",
80
+ "@sanity/pkg-utils": "^6.13.4",
81
81
  "@sanity/prettier-config": "^1.0.3",
82
82
  "@testing-library/jest-dom": "^6.6.3",
83
83
  "@testing-library/react": "^16.2.0",
84
- "@types/react": "^19.0.8",
85
- "@types/react-dom": "^19.0.3",
84
+ "@types/react": "^19.0.10",
85
+ "@types/react-dom": "^19.0.4",
86
86
  "@vitejs/plugin-react": "^4.3.4",
87
- "@vitest/coverage-v8": "3.0.5",
88
- "eslint": "^9.17.0",
87
+ "@vitest/coverage-v8": "3.0.7",
88
+ "eslint": "^9.21.0",
89
89
  "jsdom": "^25.0.1",
90
- "prettier": "^3.4.2",
90
+ "prettier": "^3.5.2",
91
91
  "react": "^19.0.0",
92
92
  "react-dom": "^19.0.0",
93
- "typescript": "^5.7.2",
94
- "vite": "^6.1.0",
95
- "vitest": "^3.0.5",
93
+ "typescript": "^5.7.3",
94
+ "vite": "^6.2.0",
95
+ "vitest": "^3.0.7",
96
96
  "@repo/config-eslint": "0.0.0",
97
97
  "@repo/config-test": "0.0.1",
98
98
  "@repo/package.config": "0.0.1",
@@ -30,5 +30,6 @@ export {
30
30
  type UsePreviewOptions,
31
31
  type UsePreviewResults,
32
32
  } from '../hooks/preview/usePreview'
33
+ export {useUsers, type UseUsersResult} from '../hooks/users/useUsers'
33
34
  export {type CurrentUser, type DocumentHandle} from '@sanity/sdk'
34
35
  export {type SanityDocument} from '@sanity/types'
@@ -13,7 +13,16 @@ import {type LoginLayoutProps} from './LoginLayout'
13
13
  // Only import bridge if we're in an iframe. This assumes that the app is
14
14
  // running within SanityOS if it is in an iframe.
15
15
  if (isInIframe()) {
16
- import('@sanity/os/bridge')
16
+ const parsedUrl = new URL(window.location.href)
17
+ const mode = new URLSearchParams(parsedUrl.hash.slice(1)).get('mode')
18
+ const script = document.createElement('script')
19
+ script.src =
20
+ mode === 'core-ui--staging'
21
+ ? 'https://core.sanity-cdn.work/bridge.js'
22
+ : 'https://core.sanity-cdn.com/bridge.js'
23
+ script.type = 'module'
24
+ script.async = true
25
+ document.head.appendChild(script)
17
26
  }
18
27
 
19
28
  /**
@@ -0,0 +1,164 @@
1
+ import {type ResourceType, type User} from '@sanity/access-api'
2
+ import {createUsersStore} from '@sanity/sdk'
3
+ import {act, renderHook} from '@testing-library/react'
4
+ import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest'
5
+
6
+ import {useSanityInstance} from '../context/useSanityInstance'
7
+ import {useUsers} from './useUsers'
8
+
9
+ vi.mock('@sanity/sdk')
10
+ vi.mock('../context/useSanityInstance')
11
+
12
+ describe('useUsers', () => {
13
+ const mockInstance = {}
14
+ const mockUser: User = {
15
+ profile: {
16
+ id: 'user1',
17
+ displayName: 'Test User',
18
+ email: 'test@test.com',
19
+ provider: 'test',
20
+ createdAt: '2021-01-01',
21
+ },
22
+ sanityUserId: 'user1',
23
+ memberships: [],
24
+ }
25
+
26
+ const getCurrent = vi.fn().mockReturnValue({
27
+ users: [],
28
+ totalCount: 0,
29
+ nextCursor: null,
30
+ hasMore: false,
31
+ initialFetchCompleted: false,
32
+ options: {
33
+ resourceType: '' as ResourceType,
34
+ resourceId: '',
35
+ limit: 100,
36
+ },
37
+ })
38
+ const unsubscribe = vi.fn()
39
+ const subscribe = vi.fn().mockReturnValue(unsubscribe)
40
+ const dispose = vi.fn()
41
+
42
+ const mockUsersStore: ReturnType<typeof createUsersStore> = {
43
+ setOptions: vi.fn(),
44
+ loadMore: vi.fn(),
45
+ resolveUsers: vi.fn(),
46
+ getState: vi.fn().mockReturnValue({getCurrent, subscribe}),
47
+ dispose,
48
+ }
49
+
50
+ beforeEach(() => {
51
+ vi.mocked(useSanityInstance).mockReturnValue(
52
+ mockInstance as unknown as ReturnType<typeof useSanityInstance>,
53
+ )
54
+ vi.mocked(createUsersStore).mockReturnValue(
55
+ mockUsersStore as unknown as ReturnType<typeof createUsersStore>,
56
+ )
57
+ })
58
+
59
+ afterEach(() => {
60
+ vi.clearAllMocks()
61
+ })
62
+
63
+ it('should initialize with given params', () => {
64
+ renderHook(() =>
65
+ useUsers({
66
+ resourceType: 'project',
67
+ resourceId: 'proj1',
68
+ }),
69
+ )
70
+
71
+ expect(createUsersStore).toHaveBeenCalledWith(mockInstance)
72
+ expect(mockUsersStore.setOptions).toHaveBeenCalledWith({
73
+ resourceType: 'project',
74
+ resourceId: 'proj1',
75
+ })
76
+ })
77
+
78
+ it('should subscribe to users store changes', () => {
79
+ renderHook(() =>
80
+ useUsers({
81
+ resourceType: 'organization',
82
+ resourceId: 'org1',
83
+ }),
84
+ )
85
+ expect(subscribe).toHaveBeenCalledTimes(1)
86
+ })
87
+
88
+ it('should return current users state', () => {
89
+ const mockState = {users: [mockUser], hasMore: true}
90
+ getCurrent.mockReturnValue(mockState)
91
+
92
+ const {result} = renderHook(() =>
93
+ useUsers({
94
+ resourceType: 'project',
95
+ resourceId: 'proj1',
96
+ }),
97
+ )
98
+ expect(result.current).toMatchObject(mockState)
99
+ })
100
+
101
+ it('should call loadMore when loadMore is invoked', () => {
102
+ const {result} = renderHook(() =>
103
+ useUsers({
104
+ resourceType: 'project',
105
+ resourceId: 'proj1',
106
+ }),
107
+ )
108
+
109
+ act(() => {
110
+ result.current.loadMore()
111
+ })
112
+
113
+ expect(mockUsersStore.loadMore).toHaveBeenCalled()
114
+ })
115
+
116
+ it('should update options when params change', () => {
117
+ const initialParams = {resourceType: 'project' as ResourceType, resourceId: 'proj1'}
118
+ const {rerender} = renderHook(({params}) => useUsers(params), {
119
+ initialProps: {params: initialParams},
120
+ })
121
+
122
+ const newParams = {resourceType: 'organization' as ResourceType, resourceId: 'org1'}
123
+ rerender({params: newParams})
124
+
125
+ expect(mockUsersStore.setOptions).toHaveBeenCalledWith(newParams)
126
+ })
127
+
128
+ it('should resolve users if initial fetch not completed', () => {
129
+ getCurrent.mockReturnValue({initialFetchCompleted: false})
130
+
131
+ renderHook(() =>
132
+ useUsers({
133
+ resourceType: 'project',
134
+ resourceId: 'proj1',
135
+ }),
136
+ )
137
+ expect(mockUsersStore.resolveUsers).toHaveBeenCalled()
138
+ })
139
+
140
+ it('should not resolve users if initial fetch already completed', () => {
141
+ getCurrent.mockReturnValue({initialFetchCompleted: true})
142
+
143
+ renderHook(() =>
144
+ useUsers({
145
+ resourceType: 'project',
146
+ resourceId: 'proj1',
147
+ }),
148
+ )
149
+ expect(mockUsersStore.resolveUsers).not.toHaveBeenCalled()
150
+ })
151
+
152
+ it('should clean up store on unmount', () => {
153
+ const {unmount} = renderHook(() =>
154
+ useUsers({
155
+ resourceType: 'project',
156
+ resourceId: 'proj1',
157
+ }),
158
+ )
159
+
160
+ unmount()
161
+ expect(mockUsersStore.dispose).toHaveBeenCalled()
162
+ expect(unsubscribe).toHaveBeenCalled()
163
+ })
164
+ })
@@ -0,0 +1,73 @@
1
+ import {type ResourceType, type User} from '@sanity/access-api'
2
+ import {createUsersStore} from '@sanity/sdk'
3
+ import {useCallback, useEffect, useState, useSyncExternalStore} from 'react'
4
+
5
+ import {useSanityInstance} from '../context/useSanityInstance'
6
+
7
+ /**
8
+ * @public
9
+ */
10
+ interface UseUsersParams {
11
+ /**
12
+ * The type of resource to fetch users for.
13
+ */
14
+ resourceType: ResourceType
15
+ /**
16
+ * The ID of the resource to fetch users for.
17
+ */
18
+ resourceId: string
19
+ /**
20
+ * The limit of users to fetch.
21
+ */
22
+ limit?: number
23
+ }
24
+
25
+ /** @public */
26
+ export interface UseUsersResult {
27
+ /**
28
+ * The users fetched.
29
+ */
30
+ users: User[]
31
+ /**
32
+ * Whether there are more users to fetch.
33
+ */
34
+ hasMore: boolean
35
+ /**
36
+ * Load more users.
37
+ */
38
+ loadMore: () => void
39
+ }
40
+
41
+ /** @public */
42
+ export function useUsers(params: UseUsersParams): UseUsersResult {
43
+ const instance = useSanityInstance()
44
+ const [store] = useState(() => createUsersStore(instance))
45
+
46
+ useEffect(() => {
47
+ store.setOptions({
48
+ resourceType: params.resourceType,
49
+ resourceId: params.resourceId,
50
+ })
51
+ }, [params.resourceType, params.resourceId, store])
52
+
53
+ const subscribe = useCallback(
54
+ (onStoreChanged: () => void) => {
55
+ if (store.getState().getCurrent().initialFetchCompleted === false) {
56
+ store.resolveUsers()
57
+ }
58
+ const unsubscribe = store.getState().subscribe(onStoreChanged)
59
+
60
+ return () => {
61
+ unsubscribe()
62
+ store.dispose()
63
+ }
64
+ },
65
+ [store],
66
+ )
67
+
68
+ const getSnapshot = useCallback(() => store.getState().getCurrent(), [store])
69
+
70
+ const {users, hasMore} = useSyncExternalStore(subscribe, getSnapshot) || {}
71
+
72
+ return {users, hasMore, loadMore: store.loadMore}
73
+ }