Skip to content

Fix types generate #1266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 6, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"experimentalObjectRestSpread": true
},
"allowImportExportEverywhere": true,
"project": ["**/tsconfig.json", "tsconfig.test.json"]
"project": ["**/tsconfig.json"]
},
"plugins": ["@typescript-eslint", "react-hooks"],
"extends": [
Expand Down
5 changes: 1 addition & 4 deletions immutable/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
// @ts-ignore
import useSWR from 'swr'

import useSWR, { Middleware, SWRHook } from 'swr'
import { withMiddleware } from '../src/utils/with-middleware'
import { Middleware, SWRHook } from '../src/types'

export const immutable: Middleware = useSWRNext => (key, fetcher, config) => {
// Always override all revalidate options.
Expand Down
5 changes: 2 additions & 3 deletions immutable/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
"rootDir": "..",
"outDir": "./dist"
},
"include": [".", "../src"],
"exclude": ["./dist"]
"include": ["./*.ts"]
}
9 changes: 3 additions & 6 deletions infinite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
// hook where `key` and return type are not like the normal `useSWR` types.

import { useRef, useState, useCallback } from 'react'
import useSWR, { SWRConfig } from 'swr'

// @ts-ignore
import useSWR from 'swr'

import defaultConfig from '../src/utils/config'
import { useIsomorphicLayoutEffect } from '../src/utils/env'
import { serialize } from '../src/utils/serialize'
import { isUndefined, UNDEFINED } from '../src/utils/helper'
Expand All @@ -20,12 +17,12 @@ import {
SWRInfiniteResponse,
MutatorCallback,
Middleware
} from '../src/types'
} from 'swr'

export const infinite = ((<Data, Error>(useSWRNext: SWRHook) => (
getKey: KeyLoader<Data>,
fn: Fetcher<Data> | null,
config: typeof defaultConfig & SWRInfiniteConfiguration<Data, Error>
config: typeof SWRConfig.default & SWRInfiniteConfiguration<Data, Error>
): SWRInfiniteResponse<Data, Error> => {
const {
cache,
Expand Down
5 changes: 2 additions & 3 deletions infinite/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
"rootDir": "..",
"outDir": "./dist"
},
"include": [".", "../src"],
"exclude": ["./dist"]
"include": ["./*.ts"]
}
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = {
},
globals: {
'ts-jest': {
tsconfig: 'tsconfig.test.json'
tsconfig: 'test/tsconfig.json'
}
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"homepage": "https://swr.vercel.app",
"license": "MIT",
"scripts": {
"clean": "rm -rf dist infinite/dist immutable/dist",
"build": "yarn build:core && yarn build:infinite && yarn build:immutable",
"watch": "concurrently \"yarn watch:core\" \"yarn watch:infinite\" \"yarn watch:immutable\"",
"watch:core": "bunchee src/index.ts --watch",
Expand All @@ -42,7 +43,7 @@
"build:core": "bunchee src/index.ts -m --no-sourcemap",
"build:infinite": "bunchee index.ts --cwd infinite -m --no-sourcemap",
"build:immutable": "bunchee index.ts --cwd immutable -m --no-sourcemap",
"prepublishOnly": "rm -rf dist infinite/dist immutable/dist && yarn build",
"prepublishOnly": "yarn clean && yarn build",
"types:check": "tsc --noEmit",
"format": "prettier --write \"{src,infinite,immutable,test,examples}/**/*.{ts,tsx}\"",
"lint": "eslint \"{src,infinite,immutable,test,examples}/**/*.{ts,tsx}\"",
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ export {
KeyLoader,
SWRResponse,
Cache,
SWRHook,
Fetcher,
MutatorCallback,
Middleware,
// Legacy, for backwards compatibility
ConfigInterface,
Expand Down
12 changes: 5 additions & 7 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ export interface Configuration<

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

isOnline: () => boolean
isDocumentVisible: () => boolean
registerOnFocus: (cb: () => void) => void
registerOnReconnect: (cb: () => void) => void

/**
* @deprecated `revalidateOnMount` will be removed. Please considering using the `revalidateWhenStale` option.
*/
Expand All @@ -65,13 +70,6 @@ export type SWRHook = <Data = any, Error = any>(
]
) => SWRResponse<Data, Error>

export interface Preset {
isOnline: () => boolean
isDocumentVisible: () => boolean
registerOnFocus?: (cb: () => void) => void
registerOnReconnect?: (cb: () => void) => void
}

// Middlewares guarantee that a SWRHook receives a key, fetcher, and config as the argument
type SWRHookWithdMiddleware = <Data = any, Error = any>(
key: Key,
Expand Down
3 changes: 2 additions & 1 deletion src/use-swr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
SWRResponse,
RevalidatorOptions,
Updater,
Configuration,
SWRConfiguration,
Cache,
ScopedMutator,
Expand Down Expand Up @@ -681,7 +682,7 @@ export function useSWRHandler<Data = any, Error = any>(
export const SWRConfig = Object.defineProperty(ConfigProvider, 'default', {
value: defaultConfig
}) as typeof ConfigProvider & {
default: SWRConfiguration
default: Configuration
}

export const mutate = internalMutate.bind(
Expand Down
2 changes: 1 addition & 1 deletion src/utils/cache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Cache } from '../types'
import { Cache } from 'swr'

export function wrapCache<Data = any>(provider: Cache<Data>): Cache {
// We might want to inject an extra layer on top of `provider` in the future,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/config-context.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createContext, createElement, useContext, FC } from 'react'

import mergeConfig from './merge-config'
import { SWRConfiguration } from '../types'
import { SWRConfiguration } from 'swr'

export const SWRConfigContext = createContext<SWRConfiguration>({})

Expand Down
5 changes: 3 additions & 2 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { dequal } from 'dequal/lite'
import { wrapCache } from './cache'
import webPreset from './web-preset'
import { slowConnection } from './env'
import { Configuration, RevalidatorOptions, Revalidator } from '../types'
import { RevalidatorOptions, Revalidator } from 'swr'
import { Configuration } from '../types'
import { UNDEFINED } from './helper'

const fetcher = (url: string) => fetch(url).then(res => res.json())
Expand Down Expand Up @@ -36,7 +37,7 @@ function onErrorRetry(
}

// Default config
const defaultConfig = {
const defaultConfig: Configuration = {
// events
onLoadingSlow: noop,
onSuccess: noop,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/normalize-args.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Key, Fetcher, SWRConfiguration } from '../types'
import { Key, Fetcher, SWRConfiguration } from 'swr'

export function normalize<KeyType = Key, Data = any>(
args:
Expand Down
2 changes: 1 addition & 1 deletion src/utils/serialize.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import hash from './hash'
import { Key } from '../types'
import { Key } from 'swr'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels like a hack, it seems to be working fine though.


export function serialize(key: Key): [string, any, string, string] {
if (typeof key === 'function') {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/with-middleware.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { normalize } from './normalize-args'

import { Key, Fetcher, Middleware, SWRConfiguration, SWRHook } from '../types'
import { Key, Fetcher, Middleware, SWRConfiguration, SWRHook } from 'swr'

// Create a custom hook with a middleware
export function withMiddleware(
Expand Down
5 changes: 3 additions & 2 deletions tsconfig.test.json → test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"extends": "./tsconfig.json",
"extends": "../tsconfig.json",
"compilerOptions": {
"rootDir": ".",
"rootDir": "..",
"strict": false
},
"include": [
"..",
"./test",
"./jest-setup.ts"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe:

Suggested change
"..",
"./test",
"./jest-setup.ts"
"../test",
"../jest-setup.ts"

]
Expand Down
2 changes: 1 addition & 1 deletion test/use-swr-cache.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { act, fireEvent, render, screen } from '@testing-library/react'
import React, { useState } from 'react'
import useSWR, { createCache, SWRConfig } from '../src'
import useSWR, { createCache, SWRConfig } from 'swr'
import { sleep, createKey } from './utils'

describe('useSWR - cache', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/use-swr-concurrent-rendering.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('useSWR - concurrent rendering', () => {
React = require('react')
ReactDOM = require('react-dom')
act = require('react-dom/test-utils').act
useSWR = require('../src').default
useSWR = require('swr').default
})

it('should fetch data in concurrent rendering', async () => {
Expand Down
2 changes: 1 addition & 1 deletion test/use-swr-config-callbacks.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { act, render, screen, fireEvent } from '@testing-library/react'
import React from 'react'
import useSWR from '../src'
import useSWR from 'swr'
import { sleep, createResponse } from './utils'

describe('useSWR - config callbacks', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/use-swr-configs.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { act, render, screen, fireEvent } from '@testing-library/react'
import React, { useEffect, useState } from 'react'
import useSWR, { mutate, SWRConfig } from '../src'
import useSWR, { mutate, SWRConfig } from 'swr'
import { sleep } from './utils'

describe('useSWR - configs', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/use-swr-context-config.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { act, render, screen } from '@testing-library/react'
import React from 'react'
import useSWR, { mutate } from '../src'
import useSWR, { mutate } from 'swr'
import { createResponse } from './utils'

describe('useSWR - context configs', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/use-swr-error.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { act, fireEvent, render, screen } from '@testing-library/react'
import React, { useEffect, useState } from 'react'
import useSWR, { mutate } from '../src'
import useSWR, { mutate } from 'swr'
import { sleep, createResponse } from './utils'

describe('useSWR - error', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/use-swr-focus.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { act, fireEvent, render, screen } from '@testing-library/react'
import React, { useState } from 'react'
import useSWR from '../src'
import useSWR from 'swr'
import { sleep } from './utils'

const waitForNextTick = () => act(() => sleep(1))
Expand Down
4 changes: 2 additions & 2 deletions test/use-swr-immutable.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { render, screen, act, fireEvent } from '@testing-library/react'
import React, { useState } from 'react'
import useSWR from '../src'
import useSWRImmutable from '../immutable'
import useSWR from 'swr'
import useSWRImmutable from 'swr/immutable'
import { sleep, createKey } from './utils'

const waitForNextTick = () => act(() => sleep(1))
Expand Down
4 changes: 2 additions & 2 deletions test/use-swr-infinite.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useEffect, useState } from 'react'
import { render, fireEvent, act, screen } from '@testing-library/react'
import { mutate } from '../src'
import useSWRInfinite from '../infinite'
import { mutate } from 'swr'
import useSWRInfinite from 'swr/infinite'
import { sleep, createResponse } from './utils'

describe('useSWRInfinite', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/use-swr-integration.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { act, render, screen, fireEvent } from '@testing-library/react'
import React, { useState, useEffect } from 'react'
import useSWR from '../src'
import useSWR from 'swr'
import { createResponse, sleep } from './utils'

const waitForNextTick = () => act(() => sleep(1))
Expand Down
2 changes: 1 addition & 1 deletion test/use-swr-key.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { act, fireEvent, render, screen } from '@testing-library/react'
import React, { useState, useEffect } from 'react'
import useSWR from '../src'
import useSWR from 'swr'
import { createResponse, sleep } from './utils'

describe('useSWR - key', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/use-swr-loading.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { act, render, screen } from '@testing-library/react'
import React from 'react'
import useSWR from '../src'
import useSWR from 'swr'
import { createResponse, sleep } from './utils'

describe('useSWR - loading', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/use-swr-local-mutation.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { act, render, screen, fireEvent } from '@testing-library/react'
import React, { useEffect, useState } from 'react'
import useSWR, { mutate, createCache, SWRConfig } from '../src'
import useSWR, { mutate, createCache, SWRConfig } from 'swr'
import { serialize } from '../src/utils/serialize'
import { createResponse, sleep } from './utils'

Expand Down
2 changes: 1 addition & 1 deletion test/use-swr-middlewares.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { act, render, screen } from '@testing-library/react'
import React, { useState, useEffect, useRef } from 'react'
import useSWR, { Middleware, SWRConfig } from '../src'
import useSWR, { Middleware, SWRConfig } from 'swr'
import { createResponse, sleep, createKey } from './utils'

describe('useSWR - middlewares', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/use-swr-offline.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { act, fireEvent, render, screen } from '@testing-library/react'
import React from 'react'
import useSWR from '../src'
import useSWR from 'swr'
import { sleep } from './utils'

const waitForNextTick = () => act(() => sleep(1))
Expand Down
2 changes: 1 addition & 1 deletion test/use-swr-refresh.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { act, fireEvent, render, screen } from '@testing-library/react'
import React, { useState } from 'react'
import useSWR, { createCache, SWRConfig } from '../src'
import useSWR, { createCache, SWRConfig } from 'swr'
import { sleep } from './utils'

// This has to be an async function to wait a microtask to flush updates
Expand Down
2 changes: 1 addition & 1 deletion test/use-swr-revalidate.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { act, fireEvent, render, screen } from '@testing-library/react'
import React from 'react'
import useSWR from '../src'
import useSWR from 'swr'
import { createResponse, sleep } from './utils'

const waitForNextTick = () => act(() => sleep(1))
Expand Down
2 changes: 1 addition & 1 deletion test/use-swr-suspense.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { act, fireEvent, render, screen } from '@testing-library/react'
import React, { ReactNode, Suspense, useEffect, useState } from 'react'
import useSWR, { mutate } from '../src'
import useSWR, { mutate } from 'swr'
import { createResponse, sleep } from './utils'

class ErrorBoundary extends React.Component<{ fallback: ReactNode }> {
Expand Down
6 changes: 6 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
"rootDir": "src",
"strict": true,
"target": "es5",
"baseUrl": ".",
"paths": {
"swr": ["./src/index.ts"],
"swr/infinite": ["./infinite/index.ts"],
"swr/immutable": ["./immutable/index.ts"]
},
"typeRoots": ["./src/types", "./node_modules/@types"]
},
"include": ["src"],
Expand Down