UNPKG

@omneedia/ssr

Version:

Use the omneedia JavaScript library in popular server-side rendering (SSR) frameworks.

46 lines 1.86 kB
import { createClient } from '@omneedia/client-js'; import { VERSION } from './version'; import { isBrowser, } from './utils'; import { createStorageFromOptions } from './cookies'; let cachedBrowserClient; export function createBrowserClient(omneediaUrl, omneediaKey, options) { // singleton client is created only if isSingleton is set to true, or if isSingleton is not defined and we detect a browser const shouldUseSingleton = options?.isSingleton === true || ((!options || !('isSingleton' in options)) && isBrowser()); if (shouldUseSingleton && cachedBrowserClient) { return cachedBrowserClient; } if (!omneediaUrl || !omneediaKey) { throw new Error(`@omneedia/ssr: Your project's URL and API key are required to create a omneedia client!\n\nCheck your omneedia project's API settings to find these values\n\nhttps://omneedia.com/dashboard/project/_/settings/api`); } const { storage } = createStorageFromOptions({ ...options, cookieEncoding: options?.cookieEncoding ?? 'base64url', }, false); const client = createClient(omneediaUrl, omneediaKey, { ...options, global: { ...options?.global, headers: { ...options?.global?.headers, 'X-Client-Info': `omneedia-ssr/${VERSION}`, }, }, auth: { ...options?.auth, ...(options?.cookieOptions?.name ? { storageKey: options.cookieOptions.name } : null), flowType: 'pkce', autoRefreshToken: isBrowser(), detectSessionInUrl: isBrowser(), persistSession: true, storage, }, }); if (shouldUseSingleton) { cachedBrowserClient = client; } return client; } //# sourceMappingURL=createBrowserClient.js.map