import NativeMyidSdk from './NativeMyidSdk'; import type { MyIdAppearanceInput, MyIdConfigInput, MyIdOrganizationDetailsInput, } from './NativeMyidSdk'; import { MyIdError, MyIdErrorCode } from './myid-error'; import type { MyIdResult, StartMyIdOptions } from './myid-types'; /** * Drops undefined keys. Native only applies a setting when its key is present, * so an absent key preserves the SDK default. */ function stripUndefined(value: T): T { const entries = Object.entries(value).filter( ([, fieldValue]) => fieldValue !== undefined ); return Object.fromEntries(entries) as T; } /** Arrives as a string on one platform and a number on the other. */ function readErrorCode(error: unknown): string { if (typeof error === 'object' && error !== null && 'code' in error) { const { code } = error as { code: unknown }; if (typeof code === 'string' && code.length > 0) { return code; } if (typeof code === 'number') { return String(code); } } return MyIdErrorCode.FAILURE; } function readErrorMessage(error: unknown): string { if (error instanceof Error && error.message) { return error.message; } return 'MyID verification failed'; } /** * Launches the MyID verification flow. * * Rejects with a {@link MyIdError} on every failure path, including the user * dismissing the flow (code `101`). * * @example * ```ts * const result = await start({ * config: { sessionId, clientHash, clientHashId }, * }); * ``` */ export async function start(options: StartMyIdOptions): Promise { const { config, iosAppearance } = options; // An empty details object would replace the SDK default, which carries MyID's // call-centre number. The ternary also blocks an explicit null: the generated // iOS accessor checks for nil but not NSNull, and would crash on first access. const organizationDetails = config.organizationDetails ? stripUndefined(config.organizationDetails) : undefined; const payload = stripUndefined({ ...config, organizationDetails: organizationDetails && Object.keys(organizationDetails).length > 0 ? organizationDetails : undefined, }); const appearance: MyIdAppearanceInput = iosAppearance ? stripUndefined(iosAppearance) : {}; try { const result = await NativeMyidSdk.start(payload, appearance); return { code: result.code, base64: result.base64 }; } catch (error) { throw new MyIdError(readErrorCode(error), readErrorMessage(error)); } }