UNPKG

svelte-clerk

Version:

Svelte Clerk is the easiest way to add authentication and user management to your Svelte and SvelteKit applications. Add sign up, sign in, and profile management to your application in minutes.

52 lines (51 loc) 1.55 kB
import { errorThrower } from '../errors/errorThrower'; import { incompatibleRoutingWithPathProvidedError, noPathProvidedError } from '../errors/messages'; import { untrack } from 'svelte'; export const watch = (deps, cb) => { let first = true; $effect(() => { deps(); if (first) { first = false; return; } return untrack(cb); }); }; const toValue = (value) => { if (typeof value === 'function') { return value(); } return value; }; export const useRoutingProps = (componentName, props, routingOptions) => { const result = $derived.by(() => { const propsValue = toValue(props) || {}; const routingOptionsValue = toValue(routingOptions); const path = propsValue.path || routingOptionsValue?.path; const routing = propsValue.routing || routingOptionsValue?.routing || 'path'; if (routing === 'path') { if (!path) { return errorThrower.throw(noPathProvidedError(componentName)); } return { ...routingOptionsValue, ...propsValue, routing: 'path' }; } if (propsValue.path) { return errorThrower.throw(incompatibleRoutingWithPathProvidedError(componentName)); } return { ...routingOptionsValue, ...propsValue, path: undefined }; }); return { get current() { return result; } }; };