@omneedia/ssr
Version:
Use the omneedia JavaScript library in popular server-side rendering (SSR) frameworks.
49 lines • 2.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createBrowserClient = createBrowserClient;
const client_js_1 = require("@omneedia/client-js");
const version_1 = require("./version");
const utils_1 = require("./utils");
const cookies_1 = require("./cookies");
let cachedBrowserClient;
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)) && (0, utils_1.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 } = (0, cookies_1.createStorageFromOptions)({
...options,
cookieEncoding: options?.cookieEncoding ?? 'base64url',
}, false);
const client = (0, client_js_1.createClient)(omneediaUrl, omneediaKey, {
...options,
global: {
...options?.global,
headers: {
...options?.global?.headers,
'X-Client-Info': `omneedia-ssr/${version_1.VERSION}`,
},
},
auth: {
...options?.auth,
...(options?.cookieOptions?.name
? { storageKey: options.cookieOptions.name }
: null),
flowType: 'pkce',
autoRefreshToken: (0, utils_1.isBrowser)(),
detectSessionInUrl: (0, utils_1.isBrowser)(),
persistSession: true,
storage,
},
});
if (shouldUseSingleton) {
cachedBrowserClient = client;
}
return client;
}
//# sourceMappingURL=createBrowserClient.js.map