1- import type { MutatorCallback } from "swr" ;
1+ import useSWR , { type MutatorCallback , mutate } from "swr" ;
22import type { MutatorOptions } from "swr/_internal" ;
3-
4- import { customMutate , customSWR } from "./SWRUtils" ;
3+ import useSWRInfinite from "swr/infinite" ;
54import type { Fetcher , SWRModelEndpointConfig , SWRModelEndpointConfigOverride } from "./types" ;
6- import { convertObjectValuesToString , jsonFetcher } from "./utils" ;
5+ import { convertObjectValuesToString , getJson , jsonFetcher } from "./utils" ;
76
87export class SWRModelEndpoint < T > {
9- private readonly config : SWRModelEndpointConfig ;
8+ private readonly config : SWRModelEndpointConfig < T > ;
109
11- constructor ( config : SWRModelEndpointConfig ) {
10+ constructor ( config : SWRModelEndpointConfig < T > ) {
1211 this . config = config ;
1312 }
1413
15- private _configMerge ( config ?: SWRModelEndpointConfigOverride ) {
14+ private _configMerge ( config ?: SWRModelEndpointConfigOverride < T > ) {
1615 return {
1716 ...this . config ,
1817 ...config ,
1918 } ;
2019 }
2120
22- public endpoint ( config ?: SWRModelEndpointConfigOverride ) : string | null {
21+ public endpoint ( config ?: SWRModelEndpointConfigOverride < T > ) : string | null {
2322 if ( Array . isArray ( config ?. id ) ) {
2423 throw new Error ( "id can be array only in ssr fallback." ) ;
2524 }
@@ -45,15 +44,15 @@ export class SWRModelEndpoint<T> {
4544 return c ?. trailingSlash && ! r . endsWith ( "/" ) ? `${ r } /${ p ? `?${ p } ` : "" } ` : `${ r } ${ p ? `?${ p } ` : "" } ` ;
4645 }
4746
48- public fetch < R = T > ( fetcher : Fetcher , config ?: SWRModelEndpointConfigOverride ) {
47+ public fetch < R = T > ( fetcher : Fetcher , config ?: SWRModelEndpointConfigOverride < T > ) {
4948 const endpoint = this . endpoint ( config ) ;
5049 if ( endpoint === null ) {
5150 return Promise . resolve ( null ) ;
5251 }
5352 return fetcher < R > ( endpoint ) ;
5453 }
5554
56- public async fetchFallback < R = T > ( fetcher : Fetcher , config ?: SWRModelEndpointConfigOverride ) {
55+ public async fetchFallback < R = T > ( fetcher : Fetcher , config ?: SWRModelEndpointConfigOverride < T > ) {
5756 const c = this . _configMerge ( config ) ;
5857 const fallbackPairs : { [ url : string ] : R } = { } ;
5958 for ( const id of Array . isArray ( c ?. id ) ? c . id : [ c ?. id ] ) {
@@ -68,18 +67,18 @@ export class SWRModelEndpoint<T> {
6867 return fallbackPairs ;
6968 }
7069
71- public mutate < Data = unknown , T = Data > (
72- config ?: SWRModelEndpointConfigOverride ,
73- data ?: T | Promise < T > | MutatorCallback < T > ,
74- opts ?: boolean | MutatorOptions < Data , T > ,
70+ public mutate < Data = unknown , R = Data > (
71+ config ?: SWRModelEndpointConfigOverride < T > ,
72+ data ?: R | Promise < R > | MutatorCallback < R > ,
73+ opts ?: boolean | MutatorOptions < Data , R > ,
7574 ) {
76- return customMutate ( this . endpoint ( config ) , data , opts ) ;
75+ return mutate ( this . endpoint ( config ) , data , opts ) ;
7776 }
7877
79- public async update < T extends object | object [ ] > (
80- data : T | Promise < T > | MutatorCallback < T > ,
78+ public async update < R extends object | object [ ] > (
79+ data : R | Promise < R > | MutatorCallback < R > ,
8180 onSuccess ?: ( response : Response ) => void ,
82- config ?: SWRModelEndpointConfigOverride ,
81+ config ?: SWRModelEndpointConfigOverride < T > ,
8382 ) {
8483 const c = this . _configMerge ( config ) ;
8584 return this . mutate ( c , data , {
@@ -100,7 +99,19 @@ export class SWRModelEndpoint<T> {
10099 } ) ;
101100 }
102101
103- public use < R = T > ( config ?: SWRModelEndpointConfigOverride ) {
104- return customSWR < R > ( this . endpoint ( config ) , this . _configMerge ( config ) . swrConfig ) ;
102+ public use < R = T > ( config ?: SWRModelEndpointConfigOverride < T > ) {
103+ return useSWR < R > ( this . endpoint ( config ) , getJson , this . _configMerge ( config ) . swrConfig ) ;
104+ }
105+
106+ public useInfinite ( config ?: SWRModelEndpointConfigOverride < T > ) {
107+ const c = this . _configMerge ( config ) ;
108+ return useSWRInfinite < T > ( ( index , previousPageData ) => {
109+ if ( previousPageData && ! c . pagination ?. hasMore ( previousPageData ) ) return null ;
110+ const params = {
111+ ...c . params ,
112+ ...( c . pagination ? c . pagination . getParams ( index , previousPageData ) : { } ) ,
113+ } ;
114+ return this . endpoint ( { ...c , params } ) ;
115+ } ) ;
105116 }
106117}
0 commit comments