From 8ca0450363a194016fe7d1e5c4fc8359e2f4dba2 Mon Sep 17 00:00:00 2001 From: thaddmt <68032955+thaddmt@users.noreply.github.com> Date: Tue, 19 Sep 2023 15:53:34 -0700 Subject: [PATCH] chore(liveness): export error types (#4450) * chore(liveness): export error types * Create thin-experts-destroy.md * fix unti tests * update error types * chore: switch to union type only * Update packages/react-liveness/src/components/FaceLivenessDetector/service/utils/liveness.ts Co-authored-by: Caleb Pollman --------- Co-authored-by: Ioana Brooks <68251134+ioanabrooks@users.noreply.github.com> Co-authored-by: Caleb Pollman --- .changeset/thin-experts-destroy.md | 5 ++++ packages/react-liveness/jest.config.js | 2 +- .../components/FaceLivenessDetector/index.ts | 1 + .../service/machine/index.ts | 3 ++- .../service/types/error.ts | 24 ++++++++++--------- .../service/types/liveness.ts | 4 ++-- .../service/types/machine.ts | 4 ++-- .../service/utils/liveness.ts | 12 ++++------ .../shared/FaceLivenessErrorModal.tsx | 6 ++--- .../__tests__/FaceLivenessErrorModal.test.tsx | 4 ++-- .../shared/__tests__/Hint.test.tsx | 3 ++- .../__tests__/LandscapeErrorModal.test.tsx | 4 ++-- packages/react-liveness/src/index.ts | 1 + 13 files changed, 40 insertions(+), 33 deletions(-) create mode 100644 .changeset/thin-experts-destroy.md diff --git a/.changeset/thin-experts-destroy.md b/.changeset/thin-experts-destroy.md new file mode 100644 index 00000000000..83196bafaf3 --- /dev/null +++ b/.changeset/thin-experts-destroy.md @@ -0,0 +1,5 @@ +--- +"@aws-amplify/ui-react-liveness": patch +--- + +chore(liveness): export error types diff --git a/packages/react-liveness/jest.config.js b/packages/react-liveness/jest.config.js index 8650d5500eb..956feb60541 100644 --- a/packages/react-liveness/jest.config.js +++ b/packages/react-liveness/jest.config.js @@ -10,7 +10,7 @@ module.exports = { coverageThreshold: { global: { branches: 80, - functions: 83, + functions: 82, lines: 89, statements: 89, }, diff --git a/packages/react-liveness/src/components/FaceLivenessDetector/index.ts b/packages/react-liveness/src/components/FaceLivenessDetector/index.ts index dc80d2c5909..4960329df3c 100644 --- a/packages/react-liveness/src/components/FaceLivenessDetector/index.ts +++ b/packages/react-liveness/src/components/FaceLivenessDetector/index.ts @@ -10,4 +10,5 @@ export { AwsCredentialProvider, AwsTemporaryCredentials, AwsCredentials, + ErrorState, } from './service'; diff --git a/packages/react-liveness/src/components/FaceLivenessDetector/service/machine/index.ts b/packages/react-liveness/src/components/FaceLivenessDetector/service/machine/index.ts index 4f8a42cc86f..f2b8f26fbe3 100644 --- a/packages/react-liveness/src/components/FaceLivenessDetector/service/machine/index.ts +++ b/packages/react-liveness/src/components/FaceLivenessDetector/service/machine/index.ts @@ -19,6 +19,7 @@ import { IlluminationState, StreamActorCallback, LivenessError, + ErrorState, } from '../types'; import { BlazeFaceFaceDetection, @@ -717,7 +718,7 @@ export const livenessMachine = createMachine( // callbacks callUserPermissionDeniedCallback: assign({ errorState: (context, event) => { - let errorState: LivenessErrorState; + let errorState: ErrorState; if ((event.data!.message as string).includes('15 fps')) { errorState = LivenessErrorState.CAMERA_FRAMERATE_ERROR; diff --git a/packages/react-liveness/src/components/FaceLivenessDetector/service/types/error.ts b/packages/react-liveness/src/components/FaceLivenessDetector/service/types/error.ts index 91abd091706..db742249c30 100644 --- a/packages/react-liveness/src/components/FaceLivenessDetector/service/types/error.ts +++ b/packages/react-liveness/src/components/FaceLivenessDetector/service/types/error.ts @@ -2,14 +2,16 @@ * The liveness error states */ -export enum LivenessErrorState { - TIMEOUT = 'TIMEOUT', - RUNTIME_ERROR = 'RUNTIME_ERROR', - FRESHNESS_TIMEOUT = 'FRESHNESS_TIMEOUT', - SERVER_ERROR = 'SERVER_ERROR', - CAMERA_FRAMERATE_ERROR = 'CAMERA_FRAMERATE_ERROR', - CAMERA_ACCESS_ERROR = 'CAMERA_ACCESS_ERROR', - FACE_DISTANCE_ERROR = 'FACE_DISTANCE_ERROR', - MOBILE_LANDSCAPE_ERROR = 'MOBILE_LANDSCAPE_ERROR', - MULTIPLE_FACES_ERROR = 'MULTIPLE_FACES_ERROR', -} +export const LivenessErrorState = { + TIMEOUT: 'TIMEOUT', + RUNTIME_ERROR: 'RUNTIME_ERROR', + FRESHNESS_TIMEOUT: 'FRESHNESS_TIMEOUT', + SERVER_ERROR: 'SERVER_ERROR', + CAMERA_FRAMERATE_ERROR: 'CAMERA_FRAMERATE_ERROR', + CAMERA_ACCESS_ERROR: 'CAMERA_ACCESS_ERROR', + FACE_DISTANCE_ERROR: 'FACE_DISTANCE_ERROR', + MOBILE_LANDSCAPE_ERROR: 'MOBILE_LANDSCAPE_ERROR', + MULTIPLE_FACES_ERROR: 'MULTIPLE_FACES_ERROR', +} as const; + +export type ErrorState = keyof typeof LivenessErrorState; diff --git a/packages/react-liveness/src/components/FaceLivenessDetector/service/types/liveness.ts b/packages/react-liveness/src/components/FaceLivenessDetector/service/types/liveness.ts index e04e85f5362..5fe1a94df9a 100644 --- a/packages/react-liveness/src/components/FaceLivenessDetector/service/types/liveness.ts +++ b/packages/react-liveness/src/components/FaceLivenessDetector/service/types/liveness.ts @@ -1,5 +1,5 @@ import { AwsCredentialProvider } from './credentials'; -import { LivenessErrorState } from './error'; +import { ErrorState } from './error'; /** * The props for the FaceLivenessDetectorCore which allows for full configuration of auth @@ -124,6 +124,6 @@ export enum FaceMatchState { } export interface LivenessError { - state: LivenessErrorState; + state: ErrorState; error: Error; } diff --git a/packages/react-liveness/src/components/FaceLivenessDetector/service/types/machine.ts b/packages/react-liveness/src/components/FaceLivenessDetector/service/types/machine.ts index 23fe128ecb4..924e17e71bc 100644 --- a/packages/react-liveness/src/components/FaceLivenessDetector/service/types/machine.ts +++ b/packages/react-liveness/src/components/FaceLivenessDetector/service/types/machine.ts @@ -13,7 +13,7 @@ import { LivenessOvalDetails, IlluminationState, } from './liveness'; -import { LivenessErrorState } from './error'; +import { ErrorState } from './error'; import { VideoRecorder, LivenessStreamProvider, @@ -67,7 +67,7 @@ export interface HydratedLivenessContext { ovalAssociatedParams: OvalAssociatedParams; faceMatchAssociatedParams: FaceMatchAssociatedParams; freshnessColorAssociatedParams: FreshnessColorAssociatedParams; - errorState: LivenessErrorState; + errorState: ErrorState; livenessStreamProvider: LivenessStreamProvider; responseStreamActorRef: ActorRef; shouldDisconnect: boolean; diff --git a/packages/react-liveness/src/components/FaceLivenessDetector/service/utils/liveness.ts b/packages/react-liveness/src/components/FaceLivenessDetector/service/utils/liveness.ts index 5e3bc6db18b..bdb3262da58 100644 --- a/packages/react-liveness/src/components/FaceLivenessDetector/service/utils/liveness.ts +++ b/packages/react-liveness/src/components/FaceLivenessDetector/service/utils/liveness.ts @@ -6,6 +6,7 @@ import { FaceMatchState, BoundingBox, LivenessErrorState, + ErrorState, } from '../types'; import { FaceDetection } from '../types/faceDetection'; import { ClientFreshnessColorSequence } from '../types/service'; @@ -459,7 +460,7 @@ export function isCameraDeviceVirtual(device: MediaDeviceInfo): boolean { return device.label.toLowerCase().includes('virtual'); } -export const LivenessErrorStateStringMap: Record = { +export const LivenessErrorStateStringMap = { [LivenessErrorState.RUNTIME_ERROR]: 'RUNTIME_ERROR', [LivenessErrorState.SERVER_ERROR]: 'SERVER_ERROR', [LivenessErrorState.TIMEOUT]: 'TIMEOUT', @@ -741,18 +742,13 @@ export async function isFaceDistanceBelowThreshold({ isMobile?: boolean; }): Promise<{ isDistanceBelowThreshold: boolean; - error?: - | LivenessErrorState.FACE_DISTANCE_ERROR - | LivenessErrorState.MULTIPLE_FACES_ERROR; + error?: ErrorState; }> { const detectedFaces = await faceDetector.detectFaces(videoEl); let detectedFace: Face; let isDistanceBelowThreshold = false; - let error: - | LivenessErrorState.FACE_DISTANCE_ERROR - | LivenessErrorState.MULTIPLE_FACES_ERROR - | undefined; + let error: ErrorState | undefined; switch (detectedFaces.length) { case 0: { diff --git a/packages/react-liveness/src/components/FaceLivenessDetector/shared/FaceLivenessErrorModal.tsx b/packages/react-liveness/src/components/FaceLivenessDetector/shared/FaceLivenessErrorModal.tsx index 5380151a7d7..47fcf11e337 100644 --- a/packages/react-liveness/src/components/FaceLivenessDetector/shared/FaceLivenessErrorModal.tsx +++ b/packages/react-liveness/src/components/FaceLivenessDetector/shared/FaceLivenessErrorModal.tsx @@ -3,7 +3,7 @@ import React from 'react'; import { Flex, Button, Text } from '@aws-amplify/ui-react'; import { AlertIcon } from '@aws-amplify/ui-react/internal'; -import { LivenessErrorState } from '../service'; +import { LivenessErrorState, ErrorState } from '../service'; import { Toast } from './Toast'; import { Overlay } from './Overlay'; @@ -21,7 +21,7 @@ export interface FaceLivenessErrorModalProps { } const renderToastErrorModal = (props: { - error: LivenessErrorState; + error: ErrorState; displayText: Required; }) => { const { error: errorState, displayText } = props; @@ -80,7 +80,7 @@ export const renderErrorModal = ({ errorState, overrideErrorDisplayText, }: { - errorState: LivenessErrorState; + errorState: ErrorState; overrideErrorDisplayText?: ErrorDisplayText; }): JSX.Element | null => { const displayText: Required = { diff --git a/packages/react-liveness/src/components/FaceLivenessDetector/shared/__tests__/FaceLivenessErrorModal.test.tsx b/packages/react-liveness/src/components/FaceLivenessDetector/shared/__tests__/FaceLivenessErrorModal.test.tsx index 6a98f0b8427..ea14ae4d0a0 100644 --- a/packages/react-liveness/src/components/FaceLivenessDetector/shared/__tests__/FaceLivenessErrorModal.test.tsx +++ b/packages/react-liveness/src/components/FaceLivenessDetector/shared/__tests__/FaceLivenessErrorModal.test.tsx @@ -1,7 +1,7 @@ import * as React from 'react'; import { render, screen } from '@testing-library/react'; -import { LivenessErrorState } from '../../service'; +import { LivenessErrorState, ErrorState } from '../../service'; import { FaceLivenessErrorModal, renderErrorModal, @@ -60,7 +60,7 @@ describe('FaceLivenessErrorModal', () => { render( {}}> {renderErrorModal({ - errorState: errorState as unknown as LivenessErrorState, + errorState: errorState as unknown as ErrorState, })} ); diff --git a/packages/react-liveness/src/components/FaceLivenessDetector/shared/__tests__/Hint.test.tsx b/packages/react-liveness/src/components/FaceLivenessDetector/shared/__tests__/Hint.test.tsx index e15d1b3a45d..2de757918f9 100644 --- a/packages/react-liveness/src/components/FaceLivenessDetector/shared/__tests__/Hint.test.tsx +++ b/packages/react-liveness/src/components/FaceLivenessDetector/shared/__tests__/Hint.test.tsx @@ -6,6 +6,7 @@ import { IlluminationState, FaceMatchState, LivenessErrorState, + ErrorState, } from '../../service'; import { @@ -33,7 +34,7 @@ describe('Hint', () => { }; const mockActorSend = jest.fn(); - let errorState: LivenessErrorState | null = null; + let errorState: ErrorState | null = null; let faceMatchState: FaceMatchState | null = null; let illuminationState: IlluminationState | null = null; let faceMatchStateBeforeStart: FaceMatchState | null = null; diff --git a/packages/react-liveness/src/components/FaceLivenessDetector/shared/__tests__/LandscapeErrorModal.test.tsx b/packages/react-liveness/src/components/FaceLivenessDetector/shared/__tests__/LandscapeErrorModal.test.tsx index 6a98f0b8427..14b51d41ad4 100644 --- a/packages/react-liveness/src/components/FaceLivenessDetector/shared/__tests__/LandscapeErrorModal.test.tsx +++ b/packages/react-liveness/src/components/FaceLivenessDetector/shared/__tests__/LandscapeErrorModal.test.tsx @@ -1,7 +1,7 @@ import * as React from 'react'; import { render, screen } from '@testing-library/react'; -import { LivenessErrorState } from '../../service'; +import { ErrorState, LivenessErrorState } from '../../service'; import { FaceLivenessErrorModal, renderErrorModal, @@ -60,7 +60,7 @@ describe('FaceLivenessErrorModal', () => { render( {}}> {renderErrorModal({ - errorState: errorState as unknown as LivenessErrorState, + errorState: errorState as unknown as ErrorState, })} ); diff --git a/packages/react-liveness/src/index.ts b/packages/react-liveness/src/index.ts index 8b2997eb5fa..41aa4c4dace 100644 --- a/packages/react-liveness/src/index.ts +++ b/packages/react-liveness/src/index.ts @@ -6,4 +6,5 @@ export { AwsCredentialProvider, AwsCredentials, AwsTemporaryCredentials, + ErrorState, } from './components';