From 7b0666036863f47417959aeaa054a84a35cdbea9 Mon Sep 17 00:00:00 2001 From: Caleb Pollman Date: Tue, 9 Jan 2024 10:49:09 -0800 Subject: [PATCH] chore(liveness): type Liveness DisplayText with DisplayTextTemplate (#4894) --- .changeset/neat-shrimps-dream.md | 6 +++ .../FaceLivenessDetector/displayText.ts | 37 ++++++++++---- packages/ui/src/types/displayText.ts | 51 ++++++++++++++----- 3 files changed, 71 insertions(+), 23 deletions(-) create mode 100644 .changeset/neat-shrimps-dream.md diff --git a/.changeset/neat-shrimps-dream.md b/.changeset/neat-shrimps-dream.md new file mode 100644 index 00000000000..4641b24b6a7 --- /dev/null +++ b/.changeset/neat-shrimps-dream.md @@ -0,0 +1,6 @@ +--- +"@aws-amplify/ui-react-liveness": patch +"@aws-amplify/ui": patch +--- + +chore(liveness): type Liveness DisplayText with DisplayTextTemplate diff --git a/packages/react-liveness/src/components/FaceLivenessDetector/displayText.ts b/packages/react-liveness/src/components/FaceLivenessDetector/displayText.ts index 3e1622a228a..a04f29388cc 100644 --- a/packages/react-liveness/src/components/FaceLivenessDetector/displayText.ts +++ b/packages/react-liveness/src/components/FaceLivenessDetector/displayText.ts @@ -1,3 +1,5 @@ +import { DisplayTextTemplate } from '@aws-amplify/ui'; + export type HintDisplayText = { hintMoveFaceFrontOfCameraText?: string; hintTooManyFacesText?: string; @@ -31,14 +33,27 @@ export type InstructionDisplayText = { photosensitivityWarningHeadingText?: string; photosensitivityWarningInfoText?: string; photosensitivityWarningLabelText?: string; + startScreenBeginCheckText?: string; + tooFarCaptionText?: string; + tooFarAltText?: string; + // TODO remove this typo in next MV bump + /** + * @deprecated `photosensitivyWarningBodyText` has been replaced with `photosensitivityWarningBodyText` amd will be removed in a future major version of `@aws-amplify/ui-react-liveness` + */ photosensitivyWarningBodyText?: string; + /** + * @deprecated `photosensitivyWarningHeadingText` has been replaced with `photosensitivityWarningHeadingText` amd will be removed in a future major version of `@aws-amplify/ui-react-liveness` + */ photosensitivyWarningHeadingText?: string; + /** + * @deprecated `photosensitivyWarningInfoText` has been replaced with `photosensitivityWarningInfoText` amd will be removed in a future major version of `@aws-amplify/ui-react-liveness` + */ photosensitivyWarningInfoText?: string; + /** + * @deprecated `photosensitivyWarningLabelText` has been replaced with `photosensitivityWarningLabelText` amd will be removed in a future major version of `@aws-amplify/ui-react-liveness` + */ photosensitivyWarningLabelText?: string; - startScreenBeginCheckText?: string; - tooFarCaptionText?: string; - tooFarAltText?: string; }; export type StreamDisplayText = { @@ -68,8 +83,7 @@ export const defaultErrorDisplayText = { tryAgainText: 'Try again', }; -export type ErrorDisplayTextFoo = typeof defaultErrorDisplayText; -export type ErrorDisplayText = Partial; +export type ErrorDisplayText = Partial; export const defaultLivenessDisplayText: Required = { cameraMinSpecificationsHeadingText: @@ -119,9 +133,10 @@ export const defaultLivenessDisplayText: Required = { ...defaultErrorDisplayText, }; -export interface LivenessDisplayText - extends HintDisplayText, - CameraDisplayText, - InstructionDisplayText, - ErrorDisplayText, - StreamDisplayText {} +export type LivenessDisplayText = DisplayTextTemplate< + HintDisplayText & + CameraDisplayText & + InstructionDisplayText & + ErrorDisplayText & + StreamDisplayText +>; diff --git a/packages/ui/src/types/displayText.ts b/packages/ui/src/types/displayText.ts index b76b8f824f3..ae34494258b 100644 --- a/packages/ui/src/types/displayText.ts +++ b/packages/ui/src/types/displayText.ts @@ -59,15 +59,38 @@ type DisplayTextBody = string; // aggregate display text suffixes type DisplayTextSuffix = FieldSuffix | TextSuffix; -type GetDisplayTextKey = `${GetPrefix}${DisplayTextBody}${DisplayTextSuffix}`; -type DisplayTextKey = `${DisplayTextBody}${DisplayTextSuffix}`; +// example: `getSomeText` +type DisplayTextFunctionKey = + `${GetPrefix}${DisplayTextBody}${DisplayTextSuffix}`; -type DisplayTextBase = - // Keys matching `GetDisplayTextKey` must be a function returning - // a display `string` or `undefined` for flexibility - | Record string | undefined> - // Keys matching `DisplayTextKey` must be a `string`` - | Record; +// example: `someText` +type DisplayTextStringKey = `${DisplayTextBody}${DisplayTextSuffix}`; + +// example: `(isCopied: boolean) => string` +type DisplayTextFunction = (...args: any) => string | undefined; + +// keys matching `DisplayTextFunctionKey` must be a function returning +// a display `string` or `undefined` for flexibility +type DisplayTextFunctions = Record; + +// keys matching `DisplayTextStringKey` must be a `string`` +type DisplayTextStrings = Record; + +type IsDisplayTextFunction = T extends DisplayTextFunctionKey + ? K extends DisplayTextFunction + ? T + : never + : never; + +type IsDisplayTextString = T extends DisplayTextStringKey + ? K extends string + ? T + : never + : never; + +type FilterDisplayText = + | IsDisplayTextFunction + | IsDisplayTextString; /** * Display Text type utility for creating standardized `DisplayText` interfaces @@ -81,6 +104,7 @@ type DisplayTextBase = * type SomeComponentDisplayText = DisplayTextTemplate<{ * getCopyButtonText?: GetCopyButtonText; * submitButtonText?: string; + * usernameFieldLabel?: string; * }>; * * // default interface @@ -89,11 +113,14 @@ type DisplayTextBase = * // default values * const someComponentDisplayText: SomeComponentDisplayTextDefault = { * getCopyButtonText: (hasCopied) => (hasCopied ? 'Copied' : 'Copy'), - * submitButtonText: 'Submit' + * submitButtonText: 'Submit', + * usernameFieldLabel: 'Username', * }; * ``` */ -export type DisplayTextTemplate = { - // filter out disallowed keys - [K in keyof T]: K extends GetDisplayTextKey | DisplayTextKey ? T[K] : never; +export type DisplayTextTemplate< + T extends DisplayTextFunctions | DisplayTextStrings, +> = { + // remove disallowed `displayText` keys + [K in keyof T as FilterDisplayText]: T[K]; };