Skip to content

Commit 3d43921

Browse files
author
Alex Cory
committed
Merge branch 'master' of github.com:alex-cory/react-usefetch
2 parents 1e11aa0 + f3757cd commit 3d43921

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

src/types.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,26 +103,26 @@ export type RetryOpts = { attempt: number, error?: Error, response?: Response }
103103

104104
export type NoArgs = () => Promise<any>
105105

106-
export type FetchData = (
106+
export type FetchData<TData> = (
107107
routeOrBody?: string | BodyInit | object,
108108
body?: BodyInit | object,
109-
) => Promise<any>
109+
) => Promise<TData>
110110

111111
export type RequestInitJSON = RequestInit & {
112112
headers: {
113113
'Content-Type': string
114114
}
115115
}
116116

117-
export interface ReqMethods {
118-
get: (route?: string) => Promise<any>
119-
post: FetchData
120-
patch: FetchData
121-
put: FetchData
122-
del: FetchData
123-
delete: FetchData
124-
query: (query: string, variables?: BodyInit | object) => Promise<any>
125-
mutate: (mutation: string, variables?: BodyInit | object) => Promise<any>
117+
export interface ReqMethods<TData> {
118+
get: (route?: string) => Promise<TData>
119+
post: FetchData<TData>
120+
patch: FetchData<TData>
121+
put: FetchData<TData>
122+
del: FetchData<TData>
123+
delete: FetchData<TData>
124+
query: (query: string, variables?: BodyInit | object) => Promise<TData>
125+
mutate: (mutation: string, variables?: BodyInit | object) => Promise<TData>
126126
abort: () => void
127127
}
128128

@@ -141,7 +141,7 @@ export interface Res<TData> extends Response {
141141
data?: TData | undefined
142142
}
143143

144-
export type Req<TData = any> = ReqMethods & ReqBase<TData>
144+
export type Req<TData = any> = ReqMethods<TData> & ReqBase<TData>
145145

146146
export type UseFetchArgs = [(string | IncomingOptions | OverwriteGlobalOptions)?, (IncomingOptions | OverwriteGlobalOptions | any[])?, any[]?]
147147

@@ -153,7 +153,7 @@ export type UseFetchArrayReturn<TData> = [
153153
]
154154

155155
export type UseFetchObjectReturn<TData> = ReqBase<TData> &
156-
ReqMethods & {
156+
ReqMethods<TData> & {
157157
request: Req<TData>
158158
response: Res<TData>
159159
}

src/useFetch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function useFetch<TData = any>(...args: UseFetchArgs): UseFetch<TData> {
6464
const [loading, setLoading] = useRefState(defaults.loading)
6565
const forceUpdate = useReducer(() => ({}), [])[1]
6666

67-
const makeFetch = useDeepCallback((method: HTTPMethod): FetchData => {
67+
const makeFetch = useDeepCallback((method: HTTPMethod): FetchData<TData> => {
6868

6969
const doFetch = async (routeOrBody?: RouteOrBody, body?: UFBody): Promise<any> => {
7070
if (isServer) return // for now, we don't do anything on the server
@@ -231,7 +231,7 @@ function useFetch<TData = any>(...args: UseFetchArgs): UseFetch<TData> {
231231
mounted.current = true
232232
if (Array.isArray(dependencies)) {
233233
const methodName = requestInit.method || HTTPMethod.GET
234-
const methodLower = methodName.toLowerCase() as keyof ReqMethods
234+
const methodLower = methodName.toLowerCase() as keyof ReqMethods<TData>
235235
const req = request[methodLower] as NoArgs
236236
req()
237237
}

0 commit comments

Comments
 (0)