From a1badd4c5328fcc0829a5f94e7491d137011187e Mon Sep 17 00:00:00 2001 From: Pedro Palhari Date: Fri, 25 Sep 2020 08:21:14 -0300 Subject: [PATCH] feat: add typescript types (#33) * Initial type adding, mapping current structures and adding props * Finishing typing, based on the props and types currently identified by typescript and on the README at 24/09/2020. (fix #32) (#8) --- package.json | 1 + types/errors.d.ts | 1 + types/getDeviceId.d.ts | 3 + types/havePropsChanged.d.ts | 2 + types/index.d.ts | 125 ++++++++++++++++++++++++++++++++++++ types/worker.d.ts | 0 6 files changed, 132 insertions(+) create mode 100644 types/errors.d.ts create mode 100644 types/getDeviceId.d.ts create mode 100644 types/havePropsChanged.d.ts create mode 100644 types/index.d.ts create mode 100644 types/worker.d.ts diff --git a/package.json b/package.json index de5f0ec..9d4e8c9 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "0.0.10", "description": "A react component for reading QR codes from the webcam.", "main": "./lib/index.js", + "types": "./types", "scripts": { "storybook": "start-storybook -p 9001", "storybook:export": "build-storybook -c .storybook -o ./docs", diff --git a/types/errors.d.ts b/types/errors.d.ts new file mode 100644 index 0000000..f34c6e7 --- /dev/null +++ b/types/errors.d.ts @@ -0,0 +1 @@ +export function NoVideoInputDevicesError(): void; diff --git a/types/getDeviceId.d.ts b/types/getDeviceId.d.ts new file mode 100644 index 0000000..a82d772 --- /dev/null +++ b/types/getDeviceId.d.ts @@ -0,0 +1,3 @@ +export default getDeviceId; +declare function getDeviceId(facingMode: any, chooseDeviceId?: typeof defaultDeviceIdChooser): Promise; +declare function defaultDeviceIdChooser(filteredDevices: any, videoDevices: any): any; diff --git a/types/havePropsChanged.d.ts b/types/havePropsChanged.d.ts new file mode 100644 index 0000000..5730707 --- /dev/null +++ b/types/havePropsChanged.d.ts @@ -0,0 +1,2 @@ +export default havePropsChanged; +declare function havePropsChanged(prevProps: any, nextProps: any, keys: any): any[]; diff --git a/types/index.d.ts b/types/index.d.ts new file mode 100644 index 0000000..d4fe8e9 --- /dev/null +++ b/types/index.d.ts @@ -0,0 +1,125 @@ +export default Reader; + +interface ReaderProps { + /** + * Function to call when an error occurs such as: + * + * - Not supported platform + * - The lack of available devices + */ + onError: (error: any) => any; + + /** + * Scan event handler. Called every scan with the decoded value or null if no QR code was found. + */ + onScan: (result: string) => any; + + /** + * Called when the component is ready for use. + */ + onLoad?: () => any; + + /** + * Called when the image in legacyMode is loaded. + */ + onImageLoad?: () => any; + + /** + * The delay between scans in milliseconds. To disable the interval pass in false. + * + * default: `500` + */ + delay?: number | true; + + /** + * Specify which camera direction should be used (if available). Options: `front` and `rear`. + */ + facingMode?: "rear" | "front"; + + /** + * If the device does not allow camera access (e.g. IOS Browsers, Safari) you can enable legacyMode to allow the user to take a picture (On a mobile device) or use an existing one. To trigger the image dialog just call the method openImageDialog from the parent component. Warning You must call the method from a user action (eg. click event on some element). + * + * default: `false` + */ + legacyMode?: boolean; + + /** + * If legacyMode is active then the image will be downscaled to the given value while keepings its aspect ratio. Allowing larger images will increase the accuracy but it will also slow down the processing time. + * + * default: `1500` + */ + maxImageSize?: number; + + /** + * Styling for the preview element. This will be a video or an img when legacymode is true. Warning The preview will keep its aspect ratio, to disable this set the CSS property `objectFit` to `fill`. + */ + style?: React.CSSProperties; + + /** + * ClassName for the container element. + */ + className?: string; + + /** + * Called when choosing which device to use for scanning. By default chooses the first video device matching facingMode, if no devices match the first video device found is choosen. + */ + chooseDeviceId?: ( + filteredDevices: MediaDeviceInfo[], + videoDevices?: MediaDeviceInfo[] + ) => MediaDeviceInfo[]; + + /** + * Existing MediaStream to use initially. + */ + initialStream?: MediaStream; +} + +declare class Reader extends Component { + constructor(props: any); + els: {}; + initialStreamStarted: boolean; + initiate( + props?: Readonly & + Readonly<{ + children?: import("react").ReactNode; + }> + ): void; + initiateLegacyMode(): void; + check(): void; + handleVideo(stream: any): void; + handleLoadStart(): void; + handleInputChange(e: any): void; + clearComponent(): void; + handleReaderLoad(e: any): void; + openImageDialog(): void; + handleWorkerMessage(e: any): void; + setRefFactory(key: any): (element: any) => void; + worker: Worker; + timeout: NodeJS.Timeout; + stopCamera: any; + reader: FileReader; +} +declare namespace Reader { + namespace propTypes { + const onScan: any; + const onError: any; + const onLoad: any; + const onImageLoad: any; + const delay: any; + const facingMode: any; + const legacyMode: any; + const maxImageSize: any; + const style: any; + const className: any; + const chooseDeviceId: any; + } + namespace defaultProps { + const delay_1: number; + export { delay_1 as delay }; + const maxImageSize_1: number; + export { maxImageSize_1 as maxImageSize }; + const facingMode_1: string; + export { facingMode_1 as facingMode }; + } +} +import { Component } from "react"; diff --git a/types/worker.d.ts b/types/worker.d.ts new file mode 100644 index 0000000..e69de29