diff --git a/package.json b/package.json index e4c67255..cb9cc9f4 100644 --- a/package.json +++ b/package.json @@ -19,9 +19,11 @@ "registry": "https://registry.npmjs.org/" }, "main": "lib/index.js", + "typings": "typings/index.d.ts", "files": [ "lib", - "src" + "src", + "typings" ], "scripts": { "build": "babel src -d lib", diff --git a/typings/index.d.ts b/typings/index.d.ts new file mode 100644 index 00000000..326145e7 --- /dev/null +++ b/typings/index.d.ts @@ -0,0 +1,38 @@ +import * as React from "react" + +type AsyncChildren = ((state: AsyncState) => React.ReactNode) | React.ReactNode + +interface AsyncProps { + promiseFn?: (props: object) => Promise + deferFn?: (...args) => Promise + watch?: any + initialValue?: T + onResolve?: (data: T) => void + onError?: (error: Error) => void + children?: AsyncChildren +} + +interface AsyncState { + initialValue?: T + data?: T + error?: Error + isLoading: boolean + startedAt?: Date + finishedAt?: Date + cancel: () => void + run: (...args) => Promise + reload: () => void + setData: (data: T, callback?: () => void) => T + setError: (error: Error, callback?: () => void) => Error +} + +declare class Async extends React.Component, AsyncState> { + static Pending: React.FunctionComponent<{ children?: AsyncChildren; persist?: boolean }> + static Loading: React.FunctionComponent<{ children?: AsyncChildren; initial?: boolean }> + static Resolved: React.FunctionComponent<{ children?: AsyncChildren; persist?: boolean }> + static Rejected: React.FunctionComponent<{ children?: AsyncChildren; persist?: boolean }> +} + +declare function createInstance(defaultProps?: AsyncProps): Async + +export default createInstance