Skip to content

Commit 5cb64f8

Browse files
committed
refactor: add typescript support
1 parent 21e160d commit 5cb64f8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+2671
-1430
lines changed

.babelrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"exclude": ["@babel/plugin-transform-regenerator"]
99
}
1010
],
11+
"@babel/preset-typescript",
1112
"@babel/react"
1213
],
1314
"plugins": ["babel-plugin-transform-async-to-promises"],

.eslintrc

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
11
{
2-
"parser": "babel-eslint",
3-
"extends": ["react-app", "prettier"],
2+
"parser": "@typescript-eslint/parser",
3+
"plugins": ["@typescript-eslint"],
4+
"extends": [
5+
"plugin:@typescript-eslint/eslint-recommended",
6+
"plugin:@typescript-eslint/recommended",
7+
"react-app",
8+
"prettier"
9+
],
410
"env": {
511
"es6": true
612
},
713
"parserOptions": {
814
"sourceType": "module"
15+
},
16+
"rules": {
17+
"prefer-const": "off",
18+
"@typescript-eslint/ban-ts-comment": "off",
19+
"@typescript-eslint/explicit-module-boundary-types": "off",
20+
"@typescript-eslint/no-empty-interface": "off",
21+
"@typescript-eslint/no-explicit-any": "off",
22+
"@typescript-eslint/no-non-null-assertion": "off",
23+
"@typescript-eslint/ban-types": "off"
924
}
1025
}

package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
"test:dev": "jest --watch",
2020
"test:ci": "jest && yarn dtslint",
2121
"test:coverage": "yarn test:ci; open coverage/lcov-report/index.html",
22+
"test:types": "tsc",
23+
"test:eslint": "eslint --ext .ts,.tsx ./src",
24+
"gen:types": "tsc --project ./tsconfig.types.json",
2225
"build": "NODE_ENV=production rollup -c",
2326
"now-build": "yarn && cd www && yarn && yarn build",
2427
"start": "rollup -c -w",
@@ -56,10 +59,14 @@
5659
"@babel/core": "^7.10.2",
5760
"@babel/preset-env": "^7.10.2",
5861
"@babel/preset-react": "^7.10.1",
62+
"@babel/preset-typescript": "^7.10.4",
5963
"@rollup/plugin-replace": "^2.3.3",
6064
"@svgr/rollup": "^5.4.0",
61-
"@testing-library/react": "^10.2.1",
65+
"@testing-library/react": "^10.4.7",
66+
"@types/jest": "^26.0.4",
6267
"@types/react": "^16.9.41",
68+
"@typescript-eslint/eslint-plugin": "^3.6.1",
69+
"@typescript-eslint/parser": "^3.6.1",
6370
"babel-eslint": "^10.1.0",
6471
"babel-jest": "^26.0.1",
6572
"babel-plugin-transform-async-to-promises": "^0.8.15",

rollup.config.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ const globals = {
1313
react: 'React',
1414
}
1515

16-
const inputSrc = 'src/react/index.js'
16+
const inputSrc = 'src/index.ts'
17+
18+
const extensions = ['.js', '.jsx', '.es6', '.es', '.mjs', '.ts', '.tsx']
19+
const babelConfig = { extensions }
20+
const resolveConfig = { extensions }
1721

1822
export default [
1923
{
@@ -24,7 +28,12 @@ export default [
2428
sourcemap: true,
2529
},
2630
external,
27-
plugins: [resolve(), babel(), commonJS(), externalDeps()],
31+
plugins: [
32+
resolve(resolveConfig),
33+
babel(babelConfig),
34+
commonJS(),
35+
externalDeps(),
36+
],
2837
},
2938
{
3039
input: inputSrc,
@@ -34,7 +43,13 @@ export default [
3443
sourcemap: true,
3544
},
3645
external,
37-
plugins: [resolve(), babel(), commonJS(), externalDeps(), terser()],
46+
plugins: [
47+
resolve(resolveConfig),
48+
babel(babelConfig),
49+
commonJS(),
50+
externalDeps(),
51+
terser(),
52+
],
3853
},
3954
{
4055
input: inputSrc,
@@ -46,7 +61,12 @@ export default [
4661
globals,
4762
},
4863
external,
49-
plugins: [resolve(), babel(), commonJS(), externalDeps()],
64+
plugins: [
65+
resolve(resolveConfig),
66+
babel(babelConfig),
67+
commonJS(),
68+
externalDeps(),
69+
],
5070
},
5171
{
5272
input: inputSrc,
@@ -60,8 +80,8 @@ export default [
6080
external,
6181
plugins: [
6282
replace({ 'process.env.NODE_ENV': `"production"`, delimiters: ['', ''] }),
63-
resolve(),
64-
babel(),
83+
resolve(resolveConfig),
84+
babel(babelConfig),
6585
commonJS(),
6686
externalDeps(),
6787
terser(),
Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { noop, stableStringify, identity, deepEqual } from './utils'
2+
import { ReactQueryConfig, ArrayQueryKey, QueryKey } from './types'
23

3-
export const DEFAULT_CONFIG = {
4+
export interface ReactQueryConfigRef {
5+
current: ReactQueryConfig
6+
}
7+
8+
export const DEFAULT_CONFIG: ReactQueryConfig = {
49
shared: {
510
suspense: false,
611
},
@@ -10,7 +15,7 @@ export const DEFAULT_CONFIG = {
1015
initialStale: undefined,
1116
enabled: true,
1217
retry: 3,
13-
retryDelay: attemptIndex => Math.min(1000 * 2 ** attemptIndex, 30000),
18+
retryDelay: attemptIndex => Math.min(1000 * 2 ** attemptIndex!, 30000),
1419
staleTime: 0,
1520
cacheTime: 5 * 60 * 1000,
1621
refetchOnWindowFocus: true,
@@ -33,29 +38,27 @@ export const DEFAULT_CONFIG = {
3338
},
3439
}
3540

36-
export const defaultConfigRef = {
41+
export const defaultConfigRef: ReactQueryConfigRef = {
3742
current: DEFAULT_CONFIG,
3843
}
3944

40-
export function defaultQueryKeySerializerFn(queryKey) {
41-
if (!queryKey) {
42-
return []
43-
}
45+
export function defaultQueryKeySerializerFn(
46+
queryKey: QueryKey
47+
): [string, ArrayQueryKey] {
48+
let arrayQueryKey: ArrayQueryKey = Array.isArray(queryKey)
49+
? queryKey
50+
: [queryKey]
4451

45-
if (!Array.isArray(queryKey)) {
46-
queryKey = [queryKey]
47-
}
48-
49-
if (queryKey.some(d => typeof d === 'function')) {
52+
if (
53+
arrayQueryKey[0] === '' ||
54+
typeof arrayQueryKey[0] !== 'string' ||
55+
arrayQueryKey.some(d => typeof d === 'function')
56+
) {
5057
throw new Error('A valid query key is required!')
5158
}
5259

53-
const queryHash = stableStringify(queryKey)
54-
queryKey = JSON.parse(queryHash)
55-
56-
if (!queryHash) {
57-
return []
58-
}
60+
const queryHash = stableStringify(arrayQueryKey)
61+
arrayQueryKey = JSON.parse(queryHash)
5962

60-
return [queryHash, queryKey]
63+
return [queryHash, arrayQueryKey]
6164
}
File renamed without changes.

0 commit comments

Comments
 (0)