{"version":3,"file":"index.mjs","names":[],"sources":["../src/context.ts","../src/provider.tsx","../src/hook.ts","../src/script.tsx"],"sourcesContent":["import { createContext } from 'react'\nimport type { RuntimeConfig, ClientRuntimeConfig } from '@yanuaraditia/config'\n\n// On the server: RuntimeConfig (public + private).\n// On the client: ClientRuntimeConfig (public only), sourced from\n//   window.__RUNTIME_CONFIG__ or an SSR-injected provider.\nexport type AnyRuntimeConfig = RuntimeConfig | ClientRuntimeConfig\n\nexport const RuntimeConfigContext = createContext<AnyRuntimeConfig | null>(null)\nRuntimeConfigContext.displayName = 'RuntimeConfigContext'\n","'use client'\n\nimport React from 'react'\nimport { readClientConfig } from '@yanuaraditia/config'\nimport { RuntimeConfigContext } from './context'\nimport type { AnyRuntimeConfig } from './context'\n\nexport interface RuntimeConfigProviderProps {\n  /**\n   * The **public** config to expose to the React tree.\n   *\n   * Pass the value from `usePublicRuntimeConfig()` in your root loader so that\n   * both the server render and client hydration share the same value.\n   *\n   * ⚠️  Only the `public` key is ever exposed to components. Any private keys\n   * on this object are stripped automatically — but to prevent them from being\n   * sent over the network in the first place, always use `usePublicRuntimeConfig()`\n   * (not `useRuntimeConfig()`) when building your loader return value.\n   *\n   * Omit this prop in SPA mode — the provider reads `window.__RUNTIME_CONFIG__`\n   * that was injected by the Vite plugin or `<RuntimeConfigScript>`.\n   */\n  config?: AnyRuntimeConfig\n  children: React.ReactNode\n}\n\n/**\n * Provides the runtime config to the entire React tree.\n *\n * ### SPA usage\n * ```tsx\n * // main.tsx\n * createRoot(document.getElementById('root')!).render(\n *   <RuntimeConfigProvider>\n *     <App />\n *   </RuntimeConfigProvider>\n * )\n * ```\n *\n * ### React Router v7 SSR usage\n * ```tsx\n * // app/root.tsx\n * import { usePublicRuntimeConfig } from '@yanuaraditia/config-react/server'\n *\n * export async function loader() {\n *   // usePublicRuntimeConfig strips private keys before they reach the client\n *   return { runtimeConfig: usePublicRuntimeConfig() }\n * }\n *\n * export default function Root() {\n *   const { runtimeConfig } = useLoaderData<typeof loader>()\n *   return (\n *     <RuntimeConfigProvider config={runtimeConfig}>\n *       <Outlet />\n *     </RuntimeConfigProvider>\n *   )\n * }\n * ```\n */\nexport function RuntimeConfigProvider({ config, children }: RuntimeConfigProviderProps) {\n  // Resolve the raw value from the prop or window.__RUNTIME_CONFIG__\n  const raw: AnyRuntimeConfig = config ?? readClientConfig() ?? { public: {} }\n\n  // Strip private keys — only expose the `public` portion to components.\n  // This is a safety net; private keys should never reach here in the first\n  // place (use usePublicRuntimeConfig() in your loader).\n  const value: AnyRuntimeConfig = { public: (raw as Record<string, unknown>).public ?? {} }\n\n  return React.createElement(\n    RuntimeConfigContext.Provider,\n    { value },\n    children,\n  )\n}\n","'use client'\n\nimport { useContext } from 'react'\nimport { RuntimeConfigContext } from './context'\nimport type { ClientRuntimeConfig } from '@yanuaraditia/config'\n\n/**\n * Returns the **public** runtime config from the nearest `RuntimeConfigProvider`\n * or from `window.__RUNTIME_CONFIG__` (injected by the Vite plugin / `<RuntimeConfigScript>`).\n *\n * Private keys are never available here — they exist only on the server.\n * TypeScript enforces this: accessing anything outside `config.public` is a compile error.\n *\n * ```tsx\n * function ApiClient() {\n *   const { public: { apiBase } } = useRuntimeConfig()\n *   return <span>{apiBase}</span>\n * }\n * ```\n */\nexport function useRuntimeConfig(): ClientRuntimeConfig {\n  const ctx = useContext(RuntimeConfigContext)\n\n  if (ctx !== null) return ctx as ClientRuntimeConfig\n\n  // Outside a provider (SPA before React mounts): try window.__RUNTIME_CONFIG__\n  if (typeof window !== 'undefined') {\n    const win = window as unknown as { __RUNTIME_CONFIG__?: ClientRuntimeConfig }\n    if (win.__RUNTIME_CONFIG__) return win.__RUNTIME_CONFIG__\n  }\n\n  return { public: {} } as ClientRuntimeConfig\n}\n","import React from 'react'\nimport { serializePublicConfig } from '@yanuaraditia/config'\nimport type { RuntimeConfigInput } from '@yanuaraditia/config'\n\nexport interface RuntimeConfigScriptProps {\n  /**\n   * The runtime config whose **public** portion will be serialised and\n   * embedded in the page as `window.__RUNTIME_CONFIG__`.\n   *\n   * Pass the value from your root loader / `getRuntimeConfig()`.\n   */\n  config: RuntimeConfigInput\n  /**\n   * HTML `nonce` attribute for strict Content-Security-Policy environments.\n   */\n  nonce?: string\n}\n\n/**\n * Renders an inline `<script>` tag that seeds `window.__RUNTIME_CONFIG__`\n * with the **public** portion of your runtime config.\n *\n * Place this inside `<head>` in your root layout.  On the server it outputs\n * a real `<script>` tag; on the client it re-uses the already-rendered tag\n * (safe for hydration).\n *\n * @example\n * ```tsx\n * // app/root.tsx — React Router v7 SSR\n * import { getRuntimeConfig } from '@yanuaraditia/config-react/server'\n * import { RuntimeConfigScript } from '@yanuaraditia/config-react'\n *\n * export async function loader() {\n *   return { runtimeConfig: getRuntimeConfig() }\n * }\n *\n * export default function Root() {\n *   const { runtimeConfig } = useLoaderData<typeof loader>()\n *   return (\n *     <html>\n *       <head>\n *         <RuntimeConfigScript config={runtimeConfig} />\n *       </head>\n *       <body>…</body>\n *     </html>\n *   )\n * }\n * ```\n */\nexport function RuntimeConfigScript({ config, nonce }: RuntimeConfigScriptProps) {\n  const json = serializePublicConfig(config)\n  return React.createElement('script', {\n    nonce,\n    // dangerouslySetInnerHTML is required for inline scripts in React.\n    // The value is JSON-encoded and HTML-escaped in serializePublicConfig.\n    dangerouslySetInnerHTML: {\n      __html: `window.__RUNTIME_CONFIG__=${json}`,\n    },\n  })\n}\n"],"mappings":";;;AAQA,MAAa,uBAAuB,cAAuC,KAAK;AAChF,qBAAqB,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACkDnC,SAAgB,sBAAsB,EAAE,QAAQ,YAAwC;CAOtF,MAAM,QAA0B,EAAE,SALJ,UAAU,kBAAkB,IAAI,EAAE,QAAQ,EAAE,EAAE,EAKD,UAAU,EAAE,EAAE;AAEzF,QAAO,MAAM,cACX,qBAAqB,UACrB,EAAE,OAAO,EACT,SACD;;;;;;;;;;;;;;;;;;ACpDH,SAAgB,mBAAwC;CACtD,MAAM,MAAM,WAAW,qBAAqB;AAE5C,KAAI,QAAQ,KAAM,QAAO;AAGzB,KAAI,OAAO,WAAW,aAAa;EACjC,MAAM,MAAM;AACZ,MAAI,IAAI,mBAAoB,QAAO,IAAI;;AAGzC,QAAO,EAAE,QAAQ,EAAE,EAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACkBvB,SAAgB,oBAAoB,EAAE,QAAQ,SAAmC;CAC/E,MAAM,OAAO,sBAAsB,OAAO;AAC1C,QAAO,MAAM,cAAc,UAAU;EACnC;EAGA,yBAAyB,EACvB,QAAQ,6BAA6B,QACtC;EACF,CAAC"}