UNPKG

@omneedia/ssr

Version:

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

54 lines 2.36 kB
import { createClient } from '@omneedia/client-js'; import { VERSION } from './version'; import { createStorageFromOptions, applyServerStorage } from './cookies'; export function createServerClient(omneediaUrl, omneediaKey, options) { if (!omneediaUrl || !omneediaKey) { throw new Error(`Your project's URL and 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, getAll, setAll, setItems, removedItems } = createStorageFromOptions({ ...options, cookieEncoding: options?.cookieEncoding ?? 'base64url', }, true); const client = createClient(omneediaUrl, omneediaKey, { ...options, global: { ...options?.global, headers: { ...options?.global?.headers, 'X-Client-Info': `omneedia-ssr/${VERSION}`, }, }, auth: { ...(options?.cookieOptions?.name ? { storageKey: options.cookieOptions.name } : null), ...options?.auth, flowType: 'pkce', autoRefreshToken: false, detectSessionInUrl: false, persistSession: true, storage, }, }); client.auth.onAuthStateChange(async (event) => { // The SIGNED_IN event is fired very often, but we don't need to // apply the storage each time it fires, only if there are changes // that need to be set -- which is if setItems / removeItems have // data. const hasStorageChanges = Object.keys(setItems).length > 0 || Object.keys(removedItems).length > 0; if (hasStorageChanges && (event === 'SIGNED_IN' || event === 'TOKEN_REFRESHED' || event === 'USER_UPDATED' || event === 'PASSWORD_RECOVERY' || event === 'SIGNED_OUT' || event === 'MFA_CHALLENGE_VERIFIED')) { await applyServerStorage({ getAll, setAll, setItems, removedItems }, { cookieOptions: options?.cookieOptions ?? null, cookieEncoding: options?.cookieEncoding ?? 'base64url', }); } }); return client; } //# sourceMappingURL=createServerClient.js.map