Skip to content

Commit a2d0985

Browse files
authored
Merge pull request #208 from atomic-state/feats/transform
feats(transform):
2 parents 39bb419 + e48576c commit a2d0985

5 files changed

Lines changed: 338 additions & 337 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "http-react",
3-
"version": "3.7.8",
3+
"version": "3.7.9",
44
"description": "React hooks for data fetching",
55
"main": "dist/index.js",
66
"scripts": {

src/hooks/others.ts

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ export function useFetchConfig(id?: unknown) {
4141
return thisConfig as FetchInit & FetchContextType
4242
}
4343

44-
export function useFetchSuspense<FetchDataType = any, BodyType = any>(
45-
init: FetchConfigType<FetchDataType, BodyType> | string,
46-
options?: FetchConfigTypeNoUrl<FetchDataType, BodyType>
44+
export function useFetchSuspense<FetchDataType = any>(
45+
init: FetchConfigType<FetchDataType> | string,
46+
options?: FetchConfigTypeNoUrl<FetchDataType>
4747
) {
4848
let o =
4949
typeof init === 'string'
@@ -224,13 +224,13 @@ export function useSuccess(id: any) {
224224
* @deprecated - Use the useFetch hook instead
225225
* Get everything from a `useFetch` call using its id
226226
*/
227-
export function useFetchId<ResponseType = any, BodyType = any>(id: any) {
227+
export function useFetchId<ResponseType = any>(id: any) {
228228
const defaultsKey = serialize({
229229
idString: serialize(id)
230230
})
231231
const def = fetcherDefaults.get(defaultsKey)
232232

233-
return useFetch<ResponseType, BodyType>({
233+
return useFetch<ResponseType>({
234234
id,
235235
default: def
236236
})
@@ -289,9 +289,9 @@ export function useResolve<ResponseType = any, VT = any>(
289289
/**
290290
* User a `GET` request
291291
*/
292-
export function useGET<FetchDataType = any, BodyType = any>(
293-
init: FetchConfigType<FetchDataType, BodyType> | string,
294-
options?: FetchConfigTypeNoUrl<FetchDataType, BodyType>
292+
export function useGET<FetchDataType = any>(
293+
init: FetchConfigType<FetchDataType> | string,
294+
options?: FetchConfigTypeNoUrl<FetchDataType>
295295
) {
296296
return useFetch(init, {
297297
...options,
@@ -302,9 +302,9 @@ export function useGET<FetchDataType = any, BodyType = any>(
302302
/**
303303
* Use a `DELETE` request
304304
*/
305-
export function useDELETE<FetchDataType = any, BodyType = any>(
306-
init: FetchConfigType<FetchDataType, BodyType> | string,
307-
options?: FetchConfigTypeNoUrl<FetchDataType, BodyType>
305+
export function useDELETE<FetchDataType = any>(
306+
init: FetchConfigType<FetchDataType> | string,
307+
options?: FetchConfigTypeNoUrl<FetchDataType>
308308
) {
309309
return useFetch(init, {
310310
...options,
@@ -315,9 +315,9 @@ export function useDELETE<FetchDataType = any, BodyType = any>(
315315
/**
316316
* Use a `HEAD` request
317317
*/
318-
export function useHEAD<FetchDataType = any, BodyType = any>(
319-
init: FetchConfigType<FetchDataType, BodyType> | string,
320-
options?: FetchConfigTypeNoUrl<FetchDataType, BodyType>
318+
export function useHEAD<FetchDataType = any>(
319+
init: FetchConfigType<FetchDataType> | string,
320+
options?: FetchConfigTypeNoUrl<FetchDataType>
321321
) {
322322
return useFetch(init, {
323323
...options,
@@ -328,9 +328,9 @@ export function useHEAD<FetchDataType = any, BodyType = any>(
328328
/**
329329
* Use an `OPTIONS` request
330330
*/
331-
export function useOPTIONS<FetchDataType = any, BodyType = any>(
332-
init: FetchConfigType<FetchDataType, BodyType> | string,
333-
options?: FetchConfigTypeNoUrl<FetchDataType, BodyType>
331+
export function useOPTIONS<FetchDataType = any>(
332+
init: FetchConfigType<FetchDataType> | string,
333+
options?: FetchConfigTypeNoUrl<FetchDataType>
334334
) {
335335
return useFetch(init, {
336336
...options,
@@ -342,9 +342,9 @@ export function useOPTIONS<FetchDataType = any, BodyType = any>(
342342
/**
343343
* Use a `POST` request
344344
*/
345-
export function usePOST<FetchDataType = any, BodyType = any>(
346-
init: FetchConfigType<FetchDataType, BodyType> | string,
347-
options?: FetchConfigTypeNoUrl<FetchDataType, BodyType>
345+
export function usePOST<FetchDataType = any>(
346+
init: FetchConfigType<FetchDataType> | string,
347+
options?: FetchConfigTypeNoUrl<FetchDataType>
348348
) {
349349
return useFetch(init, {
350350
...options,
@@ -355,9 +355,9 @@ export function usePOST<FetchDataType = any, BodyType = any>(
355355
/**
356356
* Use a `PUT` request
357357
*/
358-
export function usePUT<FetchDataType = any, BodyType = any>(
359-
init: FetchConfigType<FetchDataType, BodyType> | string,
360-
options?: FetchConfigTypeNoUrl<FetchDataType, BodyType>
358+
export function usePUT<FetchDataType = any>(
359+
init: FetchConfigType<FetchDataType> | string,
360+
options?: FetchConfigTypeNoUrl<FetchDataType>
361361
) {
362362
return useFetch(init, {
363363
...options,
@@ -368,9 +368,9 @@ export function usePUT<FetchDataType = any, BodyType = any>(
368368
/**
369369
* Use a `PATCH` request
370370
*/
371-
export function usePATCH<FetchDataType = any, BodyType = any>(
372-
init: FetchConfigType<FetchDataType, BodyType> | string,
373-
options?: FetchConfigTypeNoUrl<FetchDataType, BodyType>
371+
export function usePATCH<FetchDataType = any>(
372+
init: FetchConfigType<FetchDataType> | string,
373+
options?: FetchConfigTypeNoUrl<FetchDataType>
374374
) {
375375
return useFetch(init, {
376376
...options,
@@ -381,9 +381,9 @@ export function usePATCH<FetchDataType = any, BodyType = any>(
381381
/**
382382
* Use a `PURGE` request
383383
*/
384-
export function usePURGE<FetchDataType = any, BodyType = any>(
385-
init: FetchConfigType<FetchDataType, BodyType> | string,
386-
options?: FetchConfigTypeNoUrl<FetchDataType, BodyType>
384+
export function usePURGE<FetchDataType = any>(
385+
init: FetchConfigType<FetchDataType> | string,
386+
options?: FetchConfigTypeNoUrl<FetchDataType>
387387
) {
388388
return useFetch(init, {
389389
...options,
@@ -394,9 +394,9 @@ export function usePURGE<FetchDataType = any, BodyType = any>(
394394
/**
395395
* Use a `LINK` request
396396
*/
397-
export function useLINK<FetchDataType = any, BodyType = any>(
398-
init: FetchConfigType<FetchDataType, BodyType> | string,
399-
options?: FetchConfigTypeNoUrl<FetchDataType, BodyType>
397+
export function useLINK<FetchDataType = any>(
398+
init: FetchConfigType<FetchDataType> | string,
399+
options?: FetchConfigTypeNoUrl<FetchDataType>
400400
) {
401401
return useFetch(init, {
402402
...options,
@@ -407,9 +407,9 @@ export function useLINK<FetchDataType = any, BodyType = any>(
407407
/**
408408
* Use an `UNLINK` request
409409
*/
410-
export function useUNLINK<FetchDataType = any, BodyType = any>(
411-
init: FetchConfigType<FetchDataType, BodyType> | string,
412-
options?: FetchConfigTypeNoUrl<FetchDataType, BodyType>
410+
export function useUNLINK<FetchDataType = any>(
411+
init: FetchConfigType<FetchDataType> | string,
412+
options?: FetchConfigTypeNoUrl<FetchDataType>
413413
) {
414414
return useFetch(init, {
415415
...options,
@@ -420,9 +420,9 @@ export function useUNLINK<FetchDataType = any, BodyType = any>(
420420
/**
421421
* Use a request without making it automatically
422422
*/
423-
export function useManualFetch<FetchDataType = any, BodyType = any>(
424-
init: FetchConfigType<FetchDataType, BodyType> | string,
425-
options?: FetchConfigTypeNoUrl<FetchDataType, BodyType>
423+
export function useManualFetch<FetchDataType = any>(
424+
init: FetchConfigType<FetchDataType> | string,
425+
options?: FetchConfigTypeNoUrl<FetchDataType>
426426
) {
427427
return useFetch(init, {
428428
...options,
@@ -433,15 +433,13 @@ export function useManualFetch<FetchDataType = any, BodyType = any>(
433433
/**
434434
* Get a blob of the response. You can pass an `objectURL` property that will convet that blob into a string using `URL.createObjectURL`
435435
*/
436-
export function useFetchBlob<FetchDataType = string, BodyType = any>(
437-
init:
438-
| (FetchConfigType<FetchDataType, BodyType> & { objectURL?: boolean })
439-
| string,
440-
options?: FetchConfigTypeNoUrl<FetchDataType, BodyType> & {
436+
export function useFetchBlob<FetchDataType = string>(
437+
init: (FetchConfigType<FetchDataType> & { objectURL?: boolean }) | string,
438+
options?: FetchConfigTypeNoUrl<FetchDataType> & {
441439
objectURL?: boolean
442440
}
443441
) {
444-
return useFetch<FetchDataType, BodyType>(init, {
442+
return useFetch<FetchDataType>(init, {
445443
...options,
446444
async resolver(res) {
447445
const blob = await res.blob()
@@ -463,11 +461,11 @@ export function useFetchBlob<FetchDataType = string, BodyType = any>(
463461
/**
464462
* Get a text of the response
465463
*/
466-
export function useFetchText<FetchDataType = string, BodyType = any>(
467-
init: FetchConfigType<string, BodyType> | string,
468-
options?: FetchConfigTypeNoUrl<string, BodyType>
464+
export function useFetchText<FetchDataType = string>(
465+
init: FetchConfigType<string> | string,
466+
options?: FetchConfigTypeNoUrl<string>
469467
) {
470-
return useFetch<string, BodyType>(init, {
468+
return useFetch<string>(init, {
471469
...options,
472470
async resolver(res) {
473471
const text = await res.text()
@@ -503,21 +501,21 @@ export function useRequestEnd(id: any) {
503501
/**
504502
* Debounce a fetch by the time given
505503
*/
506-
export function useDebounceFetch<FetchDataType = any, BodyType = any>(
504+
export function useDebounceFetch<FetchDataType = any>(
507505
init:
508-
| (Omit<FetchConfigType<FetchDataType, BodyType>, 'debounce'> & {
506+
| (Omit<FetchConfigType<FetchDataType>, 'debounce'> & {
509507
debounce?: TimeSpan
510508
})
511509
| string
512510
| Request,
513-
options?: Omit<FetchConfigTypeNoUrl<FetchDataType, BodyType>, 'debounce'> & {
511+
options?: Omit<FetchConfigTypeNoUrl<FetchDataType>, 'debounce'> & {
514512
debounce?: TimeSpan
515513
}
516514
) {
517515
// @ts-ignore - auto can be present in the first arg
518516
const canDebounce = init?.auto ?? options?.auto ?? true
519517

520-
const res = useFetch<FetchDataType, BodyType>(init, {
518+
const res = useFetch<FetchDataType>(init, {
521519
...options,
522520
auto: false
523521
})
@@ -550,7 +548,7 @@ export function useGql<T = any, VT = { [k: string]: any }>(
550548
value: T
551549
variables: VT
552550
},
553-
cfg: FetchConfigTypeNoUrl<T, any> & {
551+
cfg: FetchConfigTypeNoUrl<T> & {
554552
/**
555553
* GraphQL variables
556554
*/

src/hooks/use-fetch.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ const temporaryFormData = new Map()
8282
/**
8383
* Fetch hook
8484
*/
85-
export function useFetch<FetchDataType = any, BodyType = any>(
86-
init: FetchConfigType<FetchDataType, BodyType> | string | Request,
87-
options?: FetchConfigTypeNoUrl<FetchDataType, BodyType>
85+
export function useFetch<FetchDataType = any, TransformData = any>(
86+
init: FetchConfigType<FetchDataType, TransformData> | string | Request,
87+
options?: FetchConfigTypeNoUrl<FetchDataType, TransformData>
8888
) {
8989
const $ctx = useHRFContext()
9090

@@ -129,7 +129,7 @@ export function useFetch<FetchDataType = any, BodyType = any>(
129129
...options,
130130
// @ts-expect-error
131131
id: init?.id ?? init?.key
132-
} as FetchConfigType<FetchDataType, BodyType>)
132+
} as Required<FetchConfigType<FetchDataType, TransformData>>)
133133

134134
const {
135135
onOnline = ctx.onOnline,
@@ -143,7 +143,7 @@ export function useFetch<FetchDataType = any, BodyType = any>(
143143
method = isRequest ? init.method : (METHODS.GET as HTTP_METHODS),
144144
headers = {} as Headers,
145145
body = undefined as unknown as Body,
146-
formatBody = e => JSON.stringify(e),
146+
formatBody = (e: any) => JSON.stringify(e),
147147
resolver = isFunction(ctx.resolver) ? ctx.resolver : DEFAULT_RESOLVER,
148148
onError,
149149
auto = isDefined(ctx.auto) ? ctx.auto : true,
@@ -523,7 +523,7 @@ export function useFetch<FetchDataType = any, BodyType = any>(
523523

524524
const fetchData = useCallback(
525525
async function fetchData(
526-
c: { headers?: any; body?: BodyType; query?: any; params?: any } = {}
526+
c: { headers?: any; body?: any; query?: any; params?: any } = {}
527527
) {
528528
const rawUrl =
529529
(hasBaseUrl(url)
@@ -1790,7 +1790,7 @@ Learn more: https://httpr.vercel.app/docs/api#suspense
17901790
loadingFirst: boolean
17911791
isLoadingFirst: boolean
17921792
expiration: Date
1793-
data: FetchDataType
1793+
data: 0 extends 1 & TransformData ? FetchDataType : TransformData
17941794
isPending?: boolean
17951795
loading: boolean
17961796
isLoading: boolean
@@ -1806,7 +1806,7 @@ Learn more: https://httpr.vercel.app/docs/api#suspense
18061806
) => FetchDataType
18071807
fetcher: ImperativeFetch
18081808
abort: () => void
1809-
config: FetchConfigType<FetchDataType, BodyType> & {
1809+
config: Required<FetchConfigType<FetchDataType, TransformData>> & {
18101810
baseUrl: string
18111811
url: string
18121812
rawUrl: string

0 commit comments

Comments
 (0)