diff --git a/src/mock/react.tsx b/src/mock/react.tsx index 275ccc7..bdb95c7 100644 --- a/src/mock/react.tsx +++ b/src/mock/react.tsx @@ -1,11 +1,11 @@ import { createOidcReactApi_dependencyInjection } from "../react/react"; import { createMockOidc, type ParamsOfCreateMockOidc } from "./oidc"; -import { PromiseOrNot } from "../tools/PromiseOrNot"; +import type { ValueOrAsyncGetter } from "../tools/ValueOrAsyncGetter"; /** @see: https://docs.oidc-spa.dev/documentation/mock */ export function createMockReactOidc< DecodedIdToken extends Record = Record, IsAuthGloballyRequired extends boolean = false ->(params: PromiseOrNot>) { +>(params: ValueOrAsyncGetter>) { return createOidcReactApi_dependencyInjection(params, createMockOidc); } diff --git a/src/react/react.tsx b/src/react/react.tsx index 4872c12..c8fc6f2 100644 --- a/src/react/react.tsx +++ b/src/react/react.tsx @@ -5,7 +5,7 @@ import { assert } from "../vendor/frontend/tsafe"; import { id } from "../vendor/frontend/tsafe"; import { useGuaranteedMemo } from "../tools/powerhooks/useGuaranteedMemo"; import type { PromiseOrNot } from "../tools/PromiseOrNot"; -import type { ValueOrPromiseOrAsyncGetter } from "../tools/ValueOrPromiseOrAsyncGetter"; +import type { ValueOrAsyncGetter } from "../tools/ValueOrAsyncGetter"; import { Deferred } from "../tools/Deferred"; export type OidcReact> = @@ -120,7 +120,7 @@ export function createOidcReactApi_dependencyInjection< | {} ) >( - params: ValueOrPromiseOrAsyncGetter, + paramsOrGetParams: ValueOrAsyncGetter, createOidc: (params: ParamsOfCreateOidc) => PromiseOrNot> ): OidcReactApi< DecodedIdToken, @@ -128,35 +128,42 @@ export function createOidcReactApi_dependencyInjection< > { const dReadyToCreate = new Deferred(); + let decodedIdTokenSchema: { parse: (data: unknown) => DecodedIdToken } | undefined = undefined; + // NOTE: It can be InitializationError only if isAuthGloballyRequired is true - const prOidcOrInitializationError = Promise.resolve(params) - .then(async paramsOrGetParams => { - const params = await (async () => { - if (typeof paramsOrGetParams === "function") { - const getParams = paramsOrGetParams; + const prOidcOrInitializationError = (async () => { + const params = await (async () => { + if (typeof paramsOrGetParams === "function") { + const getParams = paramsOrGetParams; - await dReadyToCreate.pr; + await dReadyToCreate.pr; - const params = getParams(); + const params = await getParams(); - return params; - } + return params; + } - return paramsOrGetParams; - })(); + const params = paramsOrGetParams; - return createOidc(params); - }) - .catch(error => { - if (!(error instanceof OidcInitializationError)) { - throw error; - } + return params; + })(); + + if ("decodedIdTokenSchema" in params) { + decodedIdTokenSchema = params.decodedIdTokenSchema; + } + + let oidc; + + try { + oidc = await createOidc(params); + } catch (error) { + assert(error instanceof OidcInitializationError); return error; - }); + } - const { decodedIdTokenSchema } = - "decodedIdTokenSchema" in params ? params : { "decodedIdTokenSchema": undefined }; + return oidc; + })(); function OidcProvider(props: { fallback?: ReactNode; @@ -346,6 +353,6 @@ export function createOidcReactApi_dependencyInjection< export function createReactOidc< DecodedIdToken extends Record = Record, IsAuthGloballyRequired extends boolean = false ->(params: ValueOrPromiseOrAsyncGetter>) { +>(params: ValueOrAsyncGetter>) { return createOidcReactApi_dependencyInjection(params, createOidc); } diff --git a/src/tools/ValueOrAsyncGetter.ts b/src/tools/ValueOrAsyncGetter.ts new file mode 100644 index 0000000..d73d0dc --- /dev/null +++ b/src/tools/ValueOrAsyncGetter.ts @@ -0,0 +1 @@ +export type ValueOrAsyncGetter = T | (() => Promise); diff --git a/src/tools/ValueOrPromiseOrAsyncGetter.ts b/src/tools/ValueOrPromiseOrAsyncGetter.ts deleted file mode 100644 index 243fc01..0000000 --- a/src/tools/ValueOrPromiseOrAsyncGetter.ts +++ /dev/null @@ -1 +0,0 @@ -export type ValueOrPromiseOrAsyncGetter = T | Promise | (() => Promise);