Skip to content

Commit 37565e7

Browse files
committed
Add TS typings
1 parent d08bff7 commit 37565e7

File tree

5 files changed

+76
-3
lines changed

5 files changed

+76
-3
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
React hook for conveniently use Fetch API.
88

9+
Both **Flow** and **TS** types included.
10+
911
```javascript
1012
import React from "react";
1113
import { useFetch } from "react-fetch-hook";

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
{
22
"name": "react-fetch-hook",
3-
"version": "1.4.2",
3+
"version": "1.5.0",
44
"description": "React fetch hook",
55
"main": "./dist/index.js",
66
"scripts": {
77
"flow": "flow",
88
"flow:check": "flow check",
9+
"typescript": "tsc -p tsconfig.json --noEmit",
10+
"copy:ts": "cp ./src/index.d.ts ./dist/index.d.ts",
911
"test": "jest --silent",
1012
"prettier": "prettier \"*/**/*.js\" --ignore-path ./.prettierignore --write && git add . && git status",
11-
"build": "npm run build:clean && npm run build:lib && npm run build:flow",
13+
"build": "npm run build:clean && npm run build:lib && npm run build:flow && npm run copy:ts",
1214
"build:clean": "rimraf dist",
1315
"build:lib": "cross-env BABEL_ENV=production babel src --out-dir dist --ignore '**/__tests__/**'",
1416
"build:flow": "gen-flow-files src --out-dir dist",
@@ -68,6 +70,7 @@
6870
"react-dom": "^16.8.0",
6971
"react-test-renderer": "^16.8.0",
7072
"react-testing-library": "^6.0.0",
71-
"rimraf": "^2.6.2"
73+
"rimraf": "^2.6.2",
74+
"typescript": "^3.4.5"
7275
}
7376
}

src/index.d.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
interface FetchResult<T> {
3+
data?: T,
4+
isLoading: boolean,
5+
error?: any
6+
}
7+
8+
interface HookOptions extends RequestInit {
9+
depends?: Array<any>
10+
}
11+
12+
interface HookOptionsWithFormatter<T> extends HookOptions {
13+
formatter(response: Response): Promise<T>
14+
}
15+
16+
export function useFetch<T>(path: RequestInfo,
17+
options?: HookOptions | HookOptionsWithFormatter<T>,
18+
specialOptions?: HookOptions): FetchResult<T>;
19+
20+
type requestFunction<T> = (params: {
21+
limit: number,
22+
offset: number,
23+
}) => Promise<Array<T>>
24+
25+
export function usePaginatedRequest<T>(request: requestFunction<T>, limit: number): {
26+
data: Array<T>,
27+
loadMore?: () => any,
28+
hasMore: boolean,
29+
};
30+
31+
type TUsePromiseResult<T> = {
32+
data?: T,
33+
isLoading: boolean,
34+
error: any,
35+
};
36+
export function flattenInput(...inputs: Array<any>): Array<any>;
37+
export function usePromise<T, I extends Array<any>>(callFunction?: (...args: I) => Promise<T>, ...inputs: I): TUsePromiseResult<T>;

tsconfig.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"compilerOptions": {
3+
"allowSyntheticDefaultImports": true,
4+
"jsx": "react",
5+
"lib": [
6+
"dom",
7+
"es2018"
8+
],
9+
"module": "esnext",
10+
"moduleResolution": "node",
11+
"noImplicitAny": false,
12+
"noImplicitReturns": true,
13+
"noUnusedLocals": true,
14+
"noUnusedParameters": true,
15+
"strict": true,
16+
"strictFunctionTypes": false,
17+
"stripInternal": true
18+
},
19+
"include": [
20+
"**/*.d.ts"
21+
],
22+
"exclude": [
23+
"node_modules"
24+
]
25+
}

0 commit comments

Comments
 (0)