|
| 1 | +import { inBrowser } from '@clerk/shared/browser'; |
| 2 | +import type { Errors, State } from '@clerk/types'; |
| 3 | + |
| 4 | +import { errorThrower } from './errors/errorThrower'; |
| 5 | +import type { IsomorphicClerk } from './isomorphicClerk'; |
| 6 | + |
| 7 | +const defaultErrors = (): Errors => ({ |
| 8 | + fields: { |
| 9 | + firstName: null, |
| 10 | + lastName: null, |
| 11 | + emailAddress: null, |
| 12 | + identifier: null, |
| 13 | + phoneNumber: null, |
| 14 | + password: null, |
| 15 | + username: null, |
| 16 | + code: null, |
| 17 | + captcha: null, |
| 18 | + legalAccepted: null, |
| 19 | + }, |
| 20 | + raw: [], |
| 21 | + global: [], |
| 22 | +}); |
| 23 | + |
| 24 | +export class StateProxy implements State { |
| 25 | + constructor(private isomorphicClerk: IsomorphicClerk) {} |
| 26 | + |
| 27 | + private readonly signInSignalProxy = this.buildSignInProxy(); |
| 28 | + private readonly signUpSignalProxy = this.buildSignUpProxy(); |
| 29 | + |
| 30 | + signInSignal() { |
| 31 | + return this.signInSignalProxy; |
| 32 | + } |
| 33 | + signUpSignal() { |
| 34 | + return this.signUpSignalProxy; |
| 35 | + } |
| 36 | + |
| 37 | + private buildSignInProxy() { |
| 38 | + const target = () => this.client.signIn.__internal_future; |
| 39 | + |
| 40 | + return { |
| 41 | + errors: defaultErrors(), |
| 42 | + fetchStatus: 'idle' as const, |
| 43 | + signIn: { |
| 44 | + status: 'needs_identifier' as const, |
| 45 | + availableStrategies: [], |
| 46 | + |
| 47 | + create: this.gateMethod(target, 'create'), |
| 48 | + password: this.gateMethod(target, 'password'), |
| 49 | + sso: this.gateMethod(target, 'sso'), |
| 50 | + finalize: this.gateMethod(target, 'finalize'), |
| 51 | + |
| 52 | + emailCode: this.wrapMethods(() => target().emailCode, ['sendCode', 'verifyCode'] as const), |
| 53 | + resetPasswordEmailCode: this.wrapMethods(() => target().resetPasswordEmailCode, [ |
| 54 | + 'sendCode', |
| 55 | + 'verifyCode', |
| 56 | + 'submitPassword', |
| 57 | + ] as const), |
| 58 | + }, |
| 59 | + }; |
| 60 | + } |
| 61 | + |
| 62 | + private buildSignUpProxy() { |
| 63 | + const target = () => this.client.signUp.__internal_future; |
| 64 | + |
| 65 | + return { |
| 66 | + errors: defaultErrors(), |
| 67 | + fetchStatus: 'idle' as const, |
| 68 | + signUp: { |
| 69 | + status: 'missing_requirements' as const, |
| 70 | + unverifiedFields: [], |
| 71 | + |
| 72 | + password: this.gateMethod(target, 'password'), |
| 73 | + finalize: this.gateMethod(target, 'finalize'), |
| 74 | + |
| 75 | + verifications: this.wrapMethods(() => target().verifications, ['sendEmailCode', 'verifyEmailCode'] as const), |
| 76 | + }, |
| 77 | + }; |
| 78 | + } |
| 79 | + |
| 80 | + __internal_effect(_: () => void): () => void { |
| 81 | + throw new Error('__internal_effect called before Clerk is loaded'); |
| 82 | + } |
| 83 | + __internal_computed<T>(_: (prev?: T) => T): () => T { |
| 84 | + throw new Error('__internal_computed called before Clerk is loaded'); |
| 85 | + } |
| 86 | + |
| 87 | + private get client() { |
| 88 | + const c = this.isomorphicClerk.client; |
| 89 | + if (!c) throw new Error('Clerk client not ready'); |
| 90 | + return c; |
| 91 | + } |
| 92 | + |
| 93 | + private gateMethod<T extends object, K extends keyof T & string>(getTarget: () => T, key: K) { |
| 94 | + type F = Extract<T[K], (...args: unknown[]) => unknown>; |
| 95 | + return (async (...args: Parameters<F>): Promise<ReturnType<F>> => { |
| 96 | + if (!inBrowser()) { |
| 97 | + return errorThrower.throw(`Attempted to call a method (${key}) that is not supported on the server.`); |
| 98 | + } |
| 99 | + if (!this.isomorphicClerk.loaded) { |
| 100 | + await new Promise<void>(resolve => this.isomorphicClerk.addOnLoaded(resolve)); |
| 101 | + } |
| 102 | + const t = getTarget(); |
| 103 | + return (t[key] as (...args: Parameters<F>) => ReturnType<F>).apply(t, args); |
| 104 | + }) as F; |
| 105 | + } |
| 106 | + |
| 107 | + private wrapMethods<T extends object, K extends readonly (keyof T & string)[]>( |
| 108 | + getTarget: () => T, |
| 109 | + keys: K, |
| 110 | + ): Pick<T, K[number]> { |
| 111 | + return Object.fromEntries(keys.map(k => [k, this.gateMethod(getTarget, k)])) as Pick<T, K[number]>; |
| 112 | + } |
| 113 | +} |
0 commit comments