Skip to content

Commit 7e47bf5

Browse files
committed
refactor: add typescript support
1 parent 2c49b5d commit 7e47bf5

Some content is hidden

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

54 files changed

+2902
-1690
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: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
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+
"@typescript-eslint/ban-types": "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"
923
}
1024
}

package.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616
"sideEffects": false,
1717
"scripts": {
1818
"test": "is-ci \"test:ci\" \"test:dev\"",
19-
"test:dev": "jest --watch",
20-
"test:ci": "jest && yarn dtslint",
19+
"test:dev": "npm run test:types && npm run test:eslint && jest --watch",
20+
"test:ci": "npm run test:types && npm run test:eslint && 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",
@@ -53,10 +56,14 @@
5356
"@babel/core": "^7.10.2",
5457
"@babel/preset-env": "^7.10.2",
5558
"@babel/preset-react": "^7.10.1",
59+
"@babel/preset-typescript": "^7.10.4",
5660
"@rollup/plugin-replace": "^2.3.3",
5761
"@svgr/rollup": "^5.4.0",
58-
"@testing-library/react": "^10.2.1",
62+
"@testing-library/react": "^10.4.7",
63+
"@types/jest": "^26.0.4",
5964
"@types/react": "^16.9.41",
65+
"@typescript-eslint/eslint-plugin": "^3.6.1",
66+
"@typescript-eslint/parser": "^3.6.1",
6067
"babel-eslint": "^10.1.0",
6168
"babel-jest": "^26.0.1",
6269
"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(),

src/core/config.js renamed to src/core/config.ts

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,35 @@
11
import { noop, stableStringify, identity, deepEqual } from './utils'
2+
import {
3+
ArrayQueryKey,
4+
QueryKey,
5+
QueryKeySerializerFunction,
6+
ReactQueryConfig,
7+
} from './types'
28

3-
export const DEFAULT_CONFIG = {
9+
// TYPES
10+
11+
export interface ReactQueryConfigRef {
12+
current: ReactQueryConfig
13+
}
14+
15+
// CONFIG
16+
17+
export const defaultQueryKeySerializerFn: QueryKeySerializerFunction = (
18+
queryKey: QueryKey
19+
): [string, ArrayQueryKey] => {
20+
try {
21+
let arrayQueryKey: ArrayQueryKey = Array.isArray(queryKey)
22+
? queryKey
23+
: [queryKey]
24+
const queryHash = stableStringify(arrayQueryKey)
25+
arrayQueryKey = JSON.parse(queryHash)
26+
return [queryHash, arrayQueryKey]
27+
} catch {
28+
throw new Error('A valid query key is required!')
29+
}
30+
}
31+
32+
export const DEFAULT_CONFIG: ReactQueryConfig = {
433
shared: {
534
suspense: false,
635
},
@@ -33,29 +62,6 @@ export const DEFAULT_CONFIG = {
3362
},
3463
}
3564

36-
export const defaultConfigRef = {
65+
export const defaultConfigRef: ReactQueryConfigRef = {
3766
current: DEFAULT_CONFIG,
3867
}
39-
40-
export function defaultQueryKeySerializerFn(queryKey) {
41-
if (!queryKey) {
42-
return []
43-
}
44-
45-
if (!Array.isArray(queryKey)) {
46-
queryKey = [queryKey]
47-
}
48-
49-
if (queryKey.some(d => typeof d === 'function')) {
50-
throw new Error('A valid query key is required!')
51-
}
52-
53-
const queryHash = stableStringify(queryKey)
54-
queryKey = JSON.parse(queryHash)
55-
56-
if (!queryHash) {
57-
return []
58-
}
59-
60-
return [queryHash, queryKey]
61-
}

src/core/index.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/core/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export { queryCache, queryCaches, makeQueryCache } from './queryCache'
2+
export { setFocusHandler } from './setFocusHandler'
3+
export { stableStringify, setConsole, deepIncludes } from './utils'
4+
5+
// Types
6+
export * from './types'
7+
export type { Query } from './query'
8+
export type { QueryCache } from './queryCache'
9+
export type { ConsoleObject } from './utils'

0 commit comments

Comments
 (0)