From a6a5efd31aa2b8694cb3500787069bf6d192124e Mon Sep 17 00:00:00 2001 From: Miguel Piedrafita Date: Sat, 9 Dec 2023 21:49:40 +0000 Subject: [PATCH] feat: Improve DX for self-hosted use-cases (#171) * feat: Improve DX for self-hosted use-cases * switch to advanced.selfhosted * selfhosted -> self_hosted --- examples/with-next/pages/index.tsx | 4 ++-- packages/core/src/types/config.ts | 2 +- .../src/components/IDKitWidget/States/WorldIDState.tsx | 2 +- packages/react/src/store/idkit.ts | 9 ++++++--- packages/react/src/types/config.ts | 4 +++- 5 files changed, 13 insertions(+), 8 deletions(-) diff --git a/examples/with-next/pages/index.tsx b/examples/with-next/pages/index.tsx index 166f3f5d..a2bcee3d 100644 --- a/examples/with-next/pages/index.tsx +++ b/examples/with-next/pages/index.tsx @@ -2,9 +2,9 @@ import { IDKitWidget } from '@worldcoin/idkit' import { GetServerSideProps, InferGetServerSidePropsType } from 'next' export const getServerSideProps = (async context => { - return { props: { app_id: context.query.app_id?.toString() || 'app_staging_45068dca85829d2fd90e2dd6f0bff997' } } + return { props: { app_id: context.query.app_id?.toString() as `app_${string}` || 'app_staging_45068dca85829d2fd90e2dd6f0bff997' } } }) satisfies GetServerSideProps<{ - app_id: string + app_id: `app_${string}` }> const Home = ({ app_id }: InferGetServerSidePropsType) => ( diff --git a/packages/core/src/types/config.ts b/packages/core/src/types/config.ts index b4810420..4df9f019 100644 --- a/packages/core/src/types/config.ts +++ b/packages/core/src/types/config.ts @@ -10,7 +10,7 @@ export enum CredentialType { export type IDKitConfig = { /** Unique identifier for the app verifying the action. This should be the app ID obtained from the Developer Portal. */ - app_id: string + app_id: `app_${string}` /** The description of the specific action (shown to users in World App). Only recommended for actions created on-the-fly. */ action_description?: string /** Encodes data into a proof that must match when validating. Read more on the [On-chain section](https://docs.worldcoin.org/advanced/on-chain). */ diff --git a/packages/react/src/components/IDKitWidget/States/WorldIDState.tsx b/packages/react/src/components/IDKitWidget/States/WorldIDState.tsx index 01cf15f1..1bc05bcb 100644 --- a/packages/react/src/components/IDKitWidget/States/WorldIDState.tsx +++ b/packages/react/src/components/IDKitWidget/States/WorldIDState.tsx @@ -40,7 +40,7 @@ const WorldIDState = () => { } = useIDKitStore(getOptions, shallow) const { connectorURI, reset, errorCode, result, verificationState } = useWorldBridge( - app_id, + app_id as `app_${string}`, action, signal, bridge_url, diff --git a/packages/react/src/store/idkit.ts b/packages/react/src/store/idkit.ts index 0c100344..31a94a02 100644 --- a/packages/react/src/store/idkit.ts +++ b/packages/react/src/store/idkit.ts @@ -13,8 +13,10 @@ import { type ISuccessResult, } from '@worldcoin/idkit-core' +const SELF_HOSTED_APP_ID = 'self_hosted' + export type IDKitStore = { - app_id: IDKitConfig['app_id'] + app_id: IDKitConfig['app_id'] | typeof SELF_HOSTED_APP_ID | '' action: IDKitConfig['action'] signal: IDKitConfig['signal'] bridge_url?: IDKitConfig['bridge_url'] @@ -116,6 +118,7 @@ const useIDKitStore = createWithEqualityFn()( bridge_url, autoClose, theme, + advanced, }: Config, source: ConfigSource ) => { @@ -126,11 +129,11 @@ const useIDKitStore = createWithEqualityFn()( theme, signal, action, - app_id, autoClose, bridge_url, - credential_types: sanitizedCredentialTypes.length ? sanitizedCredentialTypes : DEFAULT_CREDENTIAL_TYPES, action_description, + credential_types: sanitizedCredentialTypes.length ? sanitizedCredentialTypes : DEFAULT_CREDENTIAL_TYPES, + app_id: advanced?.self_hosted ? SELF_HOSTED_APP_ID : app_id, }) get().addSuccessCallback(onSuccess, source) diff --git a/packages/react/src/types/config.ts b/packages/react/src/types/config.ts index ce927966..25299ebe 100644 --- a/packages/react/src/types/config.ts +++ b/packages/react/src/types/config.ts @@ -20,7 +20,9 @@ export type WidgetConfig = { onError?: CallbackFn } -export type Config = IDKitConfig & Required> & WidgetConfig +export type Config = Required> & + WidgetConfig & + ((Exclude & { advanced: { self_hosted: true } }) | (IDKitConfig & { advanced?: { self_hosted?: false } })) export type WidgetProps = Config & { children?: ({ open }: { open: () => void }) => JSX.Element