Skip to content

Commit d83fb17

Browse files
authored
Fix types generate (#1266)
* fix: types generation * replace resolved swr in tests * avoid emit swr types in subpath module * use paths for type imports * revert few changes
1 parent a019c4f commit d83fb17

32 files changed

+60
-59
lines changed

.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"experimentalObjectRestSpread": true
99
},
1010
"allowImportExportEverywhere": true,
11-
"project": ["**/tsconfig.json", "tsconfig.test.json"]
11+
"project": ["**/tsconfig.json"]
1212
},
1313
"plugins": ["@typescript-eslint", "react-hooks"],
1414
"extends": [

immutable/index.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
// @ts-ignore
2-
import useSWR from 'swr'
3-
1+
import useSWR, { Middleware, SWRHook } from 'swr'
42
import { withMiddleware } from '../src/utils/with-middleware'
5-
import { Middleware, SWRHook } from '../src/types'
63

74
export const immutable: Middleware = useSWRNext => (key, fetcher, config) => {
85
// Always override all revalidate options.

immutable/tsconfig.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
{
22
"extends": "../tsconfig.json",
33
"compilerOptions": {
4-
"outDir": "./dist",
54
"rootDir": "..",
5+
"outDir": "./dist"
66
},
7-
"include": [".", "../src"],
8-
"exclude": ["./dist"]
7+
"include": ["./*.ts"]
98
}

infinite/index.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22
// hook where `key` and return type are not like the normal `useSWR` types.
33

44
import { useRef, useState, useCallback } from 'react'
5+
import useSWR, { SWRConfig } from 'swr'
56

6-
// @ts-ignore
7-
import useSWR from 'swr'
8-
9-
import defaultConfig from '../src/utils/config'
107
import { useIsomorphicLayoutEffect } from '../src/utils/env'
118
import { serialize } from '../src/utils/serialize'
129
import { isUndefined, UNDEFINED } from '../src/utils/helper'
@@ -20,12 +17,12 @@ import {
2017
SWRInfiniteResponse,
2118
MutatorCallback,
2219
Middleware
23-
} from '../src/types'
20+
} from 'swr'
2421

2522
export const infinite = ((<Data, Error>(useSWRNext: SWRHook) => (
2623
getKey: KeyLoader<Data>,
2724
fn: Fetcher<Data> | null,
28-
config: typeof defaultConfig & SWRInfiniteConfiguration<Data, Error>
25+
config: typeof SWRConfig.default & SWRInfiniteConfiguration<Data, Error>
2926
): SWRInfiniteResponse<Data, Error> => {
3027
const {
3128
cache,

infinite/tsconfig.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
{
22
"extends": "../tsconfig.json",
33
"compilerOptions": {
4-
"outDir": "./dist",
54
"rootDir": "..",
5+
"outDir": "./dist"
66
},
7-
"include": [".", "../src"],
8-
"exclude": ["./dist"]
7+
"include": ["./*.ts"]
98
}

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = {
99
},
1010
globals: {
1111
'ts-jest': {
12-
tsconfig: 'tsconfig.test.json'
12+
tsconfig: 'test/tsconfig.json'
1313
}
1414
}
1515
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"homepage": "https://swr.vercel.app",
3535
"license": "MIT",
3636
"scripts": {
37+
"clean": "rm -rf dist infinite/dist immutable/dist",
3738
"build": "yarn build:core && yarn build:infinite && yarn build:immutable",
3839
"watch": "concurrently \"yarn watch:core\" \"yarn watch:infinite\" \"yarn watch:immutable\"",
3940
"watch:core": "bunchee src/index.ts --watch",
@@ -42,7 +43,7 @@
4243
"build:core": "bunchee src/index.ts -m --no-sourcemap",
4344
"build:infinite": "bunchee index.ts --cwd infinite -m --no-sourcemap",
4445
"build:immutable": "bunchee index.ts --cwd immutable -m --no-sourcemap",
45-
"prepublishOnly": "rm -rf dist infinite/dist immutable/dist && yarn build",
46+
"prepublishOnly": "yarn clean && yarn build",
4647
"types:check": "tsc --noEmit",
4748
"format": "prettier --write \"{src,infinite,immutable,test,examples}/**/*.{ts,tsx}\"",
4849
"lint": "eslint \"{src,infinite,immutable,test,examples}/**/*.{ts,tsx}\"",

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ export {
1616
KeyLoader,
1717
SWRResponse,
1818
Cache,
19+
SWRHook,
20+
Fetcher,
21+
MutatorCallback,
1922
Middleware,
2023
// Legacy, for backwards compatibility
2124
ConfigInterface,

src/types.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ export interface Configuration<
4747

4848
compare: (a: Data | undefined, b: Data | undefined) => boolean
4949

50+
isOnline: () => boolean
51+
isDocumentVisible: () => boolean
52+
registerOnFocus: (cb: () => void) => void
53+
registerOnReconnect: (cb: () => void) => void
54+
5055
/**
5156
* @deprecated `revalidateOnMount` will be removed. Please considering using the `revalidateWhenStale` option.
5257
*/
@@ -65,13 +70,6 @@ export type SWRHook = <Data = any, Error = any>(
6570
]
6671
) => SWRResponse<Data, Error>
6772

68-
export interface Preset {
69-
isOnline: () => boolean
70-
isDocumentVisible: () => boolean
71-
registerOnFocus?: (cb: () => void) => void
72-
registerOnReconnect?: (cb: () => void) => void
73-
}
74-
7573
// Middlewares guarantee that a SWRHook receives a key, fetcher, and config as the argument
7674
type SWRHookWithMiddleware = <Data = any, Error = any>(
7775
key: Key,

src/use-swr.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
SWRResponse,
1919
RevalidatorOptions,
2020
Updater,
21+
Configuration,
2122
SWRConfiguration,
2223
Cache,
2324
ScopedMutator,
@@ -681,7 +682,7 @@ export function useSWRHandler<Data = any, Error = any>(
681682
export const SWRConfig = Object.defineProperty(ConfigProvider, 'default', {
682683
value: defaultConfig
683684
}) as typeof ConfigProvider & {
684-
default: SWRConfiguration
685+
default: Configuration
685686
}
686687

687688
export const mutate = internalMutate.bind(

src/utils/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function onErrorRetry(
3636
}
3737

3838
// Default config
39-
const defaultConfig = {
39+
const defaultConfig: Configuration = {
4040
// events
4141
onLoadingSlow: noop,
4242
onSuccess: noop,

test/tsconfig.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "../tsconfig.json",
3+
"compilerOptions": {
4+
"rootDir": "..",
5+
"strict": false
6+
},
7+
"include": [
8+
".",
9+
"../jest-setup.ts"
10+
]
11+
}

test/use-swr-cache.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { act, fireEvent, render, screen } from '@testing-library/react'
22
import React, { useState } from 'react'
3-
import useSWR, { createCache, SWRConfig } from '../src'
3+
import useSWR, { createCache, SWRConfig } from 'swr'
44
import { sleep, createKey } from './utils'
55

66
describe('useSWR - cache', () => {

test/use-swr-concurrent-rendering.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('useSWR - concurrent rendering', () => {
2020
React = require('react')
2121
ReactDOM = require('react-dom')
2222
act = require('react-dom/test-utils').act
23-
useSWR = require('../src').default
23+
useSWR = require('swr').default
2424
})
2525

2626
it('should fetch data in concurrent rendering', async () => {

test/use-swr-config-callbacks.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { act, render, screen, fireEvent } from '@testing-library/react'
22
import React from 'react'
3-
import useSWR from '../src'
3+
import useSWR from 'swr'
44
import { sleep, createResponse } from './utils'
55

66
describe('useSWR - config callbacks', () => {

test/use-swr-configs.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { act, render, screen, fireEvent } from '@testing-library/react'
22
import React, { useEffect, useState } from 'react'
3-
import useSWR, { mutate, SWRConfig } from '../src'
3+
import useSWR, { mutate, SWRConfig } from 'swr'
44
import { sleep } from './utils'
55

66
describe('useSWR - configs', () => {

test/use-swr-context-config.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { act, render, screen } from '@testing-library/react'
22
import React from 'react'
3-
import useSWR, { mutate } from '../src'
3+
import useSWR, { mutate } from 'swr'
44
import { createResponse } from './utils'
55

66
describe('useSWR - context configs', () => {

test/use-swr-error.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { act, fireEvent, render, screen } from '@testing-library/react'
22
import React, { useEffect, useState } from 'react'
3-
import useSWR, { mutate } from '../src'
3+
import useSWR, { mutate } from 'swr'
44
import { sleep, createResponse } from './utils'
55

66
describe('useSWR - error', () => {

test/use-swr-focus.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { act, fireEvent, render, screen } from '@testing-library/react'
22
import React, { useState } from 'react'
3-
import useSWR from '../src'
3+
import useSWR from 'swr'
44
import { sleep } from './utils'
55

66
const waitForNextTick = () => act(() => sleep(1))

test/use-swr-immutable.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { render, screen, act, fireEvent } from '@testing-library/react'
22
import React, { useState } from 'react'
3-
import useSWR from '../src'
4-
import useSWRImmutable from '../immutable'
3+
import useSWR from 'swr'
4+
import useSWRImmutable from 'swr/immutable'
55
import { sleep, createKey } from './utils'
66

77
const waitForNextTick = () => act(() => sleep(1))

test/use-swr-infinite.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useEffect, useState } from 'react'
22
import { render, fireEvent, act, screen } from '@testing-library/react'
3-
import { mutate } from '../src'
4-
import useSWRInfinite from '../infinite'
3+
import { mutate } from 'swr'
4+
import useSWRInfinite from 'swr/infinite'
55
import { sleep, createResponse } from './utils'
66

77
describe('useSWRInfinite', () => {

test/use-swr-integration.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { act, render, screen, fireEvent } from '@testing-library/react'
22
import React, { useState, useEffect } from 'react'
3-
import useSWR from '../src'
3+
import useSWR from 'swr'
44
import { createResponse, sleep } from './utils'
55

66
const waitForNextTick = () => act(() => sleep(1))

test/use-swr-key.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { act, fireEvent, render, screen } from '@testing-library/react'
22
import React, { useState, useEffect } from 'react'
3-
import useSWR from '../src'
3+
import useSWR from 'swr'
44
import { createResponse, sleep } from './utils'
55

66
describe('useSWR - key', () => {

test/use-swr-loading.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { act, render, screen } from '@testing-library/react'
22
import React from 'react'
3-
import useSWR from '../src'
3+
import useSWR from 'swr'
44
import { createResponse, sleep } from './utils'
55

66
describe('useSWR - loading', () => {

test/use-swr-local-mutation.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { act, render, screen, fireEvent } from '@testing-library/react'
22
import React, { useEffect, useState } from 'react'
3-
import useSWR, { mutate, createCache, SWRConfig } from '../src'
3+
import useSWR, { mutate, createCache, SWRConfig } from 'swr'
44
import { serialize } from '../src/utils/serialize'
55
import { createResponse, sleep } from './utils'
66

test/use-swr-middlewares.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { act, render, screen } from '@testing-library/react'
22
import React, { useState, useEffect, useRef } from 'react'
3-
import useSWR, { Middleware, SWRConfig } from '../src'
3+
import useSWR, { Middleware, SWRConfig } from 'swr'
44
import { createResponse, sleep, createKey } from './utils'
55

66
describe('useSWR - middlewares', () => {

test/use-swr-offline.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { act, fireEvent, render, screen } from '@testing-library/react'
22
import React from 'react'
3-
import useSWR from '../src'
3+
import useSWR from 'swr'
44
import { sleep } from './utils'
55

66
const waitForNextTick = () => act(() => sleep(1))

test/use-swr-refresh.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { act, fireEvent, render, screen } from '@testing-library/react'
22
import React, { useState } from 'react'
3-
import useSWR, { createCache, SWRConfig } from '../src'
3+
import useSWR, { createCache, SWRConfig } from 'swr'
44
import { sleep } from './utils'
55

66
// This has to be an async function to wait a microtask to flush updates

test/use-swr-revalidate.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { act, fireEvent, render, screen } from '@testing-library/react'
22
import React from 'react'
3-
import useSWR from '../src'
3+
import useSWR from 'swr'
44
import { createResponse, sleep } from './utils'
55

66
const waitForNextTick = () => act(() => sleep(1))

test/use-swr-suspense.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { act, fireEvent, render, screen } from '@testing-library/react'
22
import React, { ReactNode, Suspense, useEffect, useState } from 'react'
3-
import useSWR, { mutate } from '../src'
3+
import useSWR, { mutate } from 'swr'
44
import { createResponse, sleep } from './utils'
55

66
class ErrorBoundary extends React.Component<{ fallback: ReactNode }> {

tsconfig.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
"rootDir": "src",
1616
"strict": true,
1717
"target": "es5",
18+
"baseUrl": ".",
19+
"paths": {
20+
"swr": ["./src/index.ts"],
21+
"swr/infinite": ["./infinite/index.ts"],
22+
"swr/immutable": ["./immutable/index.ts"]
23+
},
1824
"typeRoots": ["./src/types", "./node_modules/@types"]
1925
},
2026
"include": ["src"],

tsconfig.test.json

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

0 commit comments

Comments
 (0)