Skip to content

Commit

Permalink
Merge pull request #5 from danybeltran/feat-s-4
Browse files Browse the repository at this point in the history
Feat(RequestFn)
  • Loading branch information
danybeltran authored Apr 8, 2023
2 parents d568c26 + 1026ecb commit 19ce003
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "swr-micro",
"version": "0.0.9",
"version": "0.1.1",
"description": "React hooks for Data fetching",
"main": "dist/index.js",
"scripts": {
Expand Down
56 changes: 37 additions & 19 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -312,26 +312,44 @@ function setupSWR(url: string, config: any = {}) {
const useIsomorphicLayoutEffect =
typeof window === 'undefined' ? useEffect : useLayoutEffect

function useSWR<T = any>(
type CfType<_T> = Omit<RequestInit, 'body'> & {
body?: any
key?: any
query?: any
params?: any
default?: _T
suspense?: boolean
auto?: boolean
revalidateInterval?: TimeSpan
fetcher?: (url: string, cfg: any) => any
revalidateOnFocus?: boolean
revalidateOnReconnect?: boolean
onStart?: (req: any) => void
onEnd?: (res: any) => void
attempts?: number
attemptInterval?: TimeSpan
}

export type RequestFn = <T = any>(
url: string,
config: Omit<RequestInit, 'body'> & {
body?: any
key?: any
query?: any
params?: any
default?: T
suspense?: boolean
auto?: boolean
revalidateInterval?: TimeSpan
fetcher?: (url: string, cfg: any) => any
revalidateOnFocus?: boolean
revalidateOnReconnect?: boolean
onStart?: (req: any) => void
onEnd?: (res: any) => void
attempts?: number
attemptInterval?: TimeSpan
} = {}
) {
config?: CfType<T>
) => {
key: any
revalidate: () => void
cancelRequest: () => void
mutate: <Returns = any>(args?: T) => Returns
data: T
loading: boolean
error: boolean
revalidating: boolean
start: Date
end: Date
status: number
success: boolean
responseTime: number
}

function useSWR<T = any>(url: string, config: CfType<T> = {}) {
const { auto = true } = config

const key = url ? setupSWR(url, config) : serialize(config.key)
Expand Down

0 comments on commit 19ce003

Please sign in to comment.