Skip to content

Commit

Permalink
feat: add typescript types (#33)
Browse files Browse the repository at this point in the history
* 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)
  • Loading branch information
pedropalhari authored Sep 25, 2020
1 parent fd1f4fd commit a1badd4
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions types/errors.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export function NoVideoInputDevicesError(): void;
3 changes: 3 additions & 0 deletions types/getDeviceId.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default getDeviceId;
declare function getDeviceId(facingMode: any, chooseDeviceId?: typeof defaultDeviceIdChooser): Promise<any>;
declare function defaultDeviceIdChooser(filteredDevices: any, videoDevices: any): any;
2 changes: 2 additions & 0 deletions types/havePropsChanged.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export default havePropsChanged;
declare function havePropsChanged(prevProps: any, nextProps: any, keys: any): any[];
125 changes: 125 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -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<ReaderProps, any, any> {
constructor(props: any);
els: {};
initialStreamStarted: boolean;
initiate(
props?: Readonly<any> &
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";
Empty file added types/worker.d.ts
Empty file.

0 comments on commit a1badd4

Please sign in to comment.