Skip to content

Commit

Permalink
Stop making params providable as a promise, bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
garronej committed Aug 5, 2024
1 parent 4c0748d commit cad5988
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/mock/react.tsx
Original file line number Diff line number Diff line change
@@ -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<string, unknown> = Record<string, unknown>,
IsAuthGloballyRequired extends boolean = false
>(params: PromiseOrNot<ParamsOfCreateMockOidc<DecodedIdToken, IsAuthGloballyRequired>>) {
>(params: ValueOrAsyncGetter<ParamsOfCreateMockOidc<DecodedIdToken, IsAuthGloballyRequired>>) {
return createOidcReactApi_dependencyInjection(params, createMockOidc);
}
53 changes: 30 additions & 23 deletions src/react/react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<DecodedIdToken extends Record<string, unknown>> =
Expand Down Expand Up @@ -120,43 +120,50 @@ export function createOidcReactApi_dependencyInjection<
| {}
)
>(
params: ValueOrPromiseOrAsyncGetter<ParamsOfCreateOidc>,
paramsOrGetParams: ValueOrAsyncGetter<ParamsOfCreateOidc>,
createOidc: (params: ParamsOfCreateOidc) => PromiseOrNot<Oidc<DecodedIdToken>>
): OidcReactApi<
DecodedIdToken,
ParamsOfCreateOidc extends { isAuthGloballyRequired?: true | undefined } ? true : false
> {
const dReadyToCreate = new Deferred<void>();

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;
Expand Down Expand Up @@ -346,6 +353,6 @@ export function createOidcReactApi_dependencyInjection<
export function createReactOidc<
DecodedIdToken extends Record<string, unknown> = Record<string, unknown>,
IsAuthGloballyRequired extends boolean = false
>(params: ValueOrPromiseOrAsyncGetter<ParamsOfCreateOidc<DecodedIdToken, IsAuthGloballyRequired>>) {
>(params: ValueOrAsyncGetter<ParamsOfCreateOidc<DecodedIdToken, IsAuthGloballyRequired>>) {
return createOidcReactApi_dependencyInjection(params, createOidc);
}
1 change: 1 addition & 0 deletions src/tools/ValueOrAsyncGetter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type ValueOrAsyncGetter<T> = T | (() => Promise<T>);
1 change: 0 additions & 1 deletion src/tools/ValueOrPromiseOrAsyncGetter.ts

This file was deleted.

0 comments on commit cad5988

Please sign in to comment.