@@ -2,7 +2,7 @@ import type { ApiRequest, IReturn, IReturnVoid, ApiState, IResponseError, IRespo
2
2
import type { JsonServiceClient } from "@servicestack/client"
3
3
import { inject , provide , ref } from "vue"
4
4
import { ResponseError , ResponseStatus , ApiResult , appendQueryString , nameOf } from "@servicestack/client"
5
- import { unRefs , setRef } from "./utils"
5
+ import { unRefs , setRef , swrApi } from "./utils"
6
6
import { Sole } from "./config"
7
7
8
8
export function useClient ( ) {
@@ -78,39 +78,11 @@ export function useClient() {
78
78
return api
79
79
}
80
80
81
- function swrCacheKey < TResponse > ( request :IReturn < TResponse > | ApiRequest , args ?: any ) {
82
- const key = appendQueryString ( `swr.${ nameOf ( request ) } ` , ! args ? request : Object . assign ( { } , request , args ) )
83
- return key
84
- }
85
- function fromCache ( key :string ) {
86
- const json = Sole . config . storage ! . getItem ( key )
87
- const ret = json
88
- ? JSON . parse ( json )
89
- : null
90
- return ret
91
- }
92
- function swrClear < TResponse > ( options :{ request :IReturn < TResponse > | ApiRequest , args ?: any } ) {
93
- if ( options . request ) {
94
- const key = swrCacheKey ( options . request , options . args )
95
- Sole . config . storage ! . removeItem ( key )
96
- }
97
- }
98
-
99
81
async function swr < TResponse > ( request :IReturn < TResponse > | ApiRequest , fn :( r :ApiResult < TResponse > ) => void , args ?: any , method ?: string ) {
100
- const key = swrCacheKey ( request , args )
101
-
102
- fn ( new ApiResult ( { response : fromCache ( key ) } ) )
103
- const api = await client . api ( request , args , method )
104
- if ( api . succeeded && api . response ) {
105
- api . response . _date = new Date ( ) . valueOf ( )
106
- const json = JSON . stringify ( api . response )
107
- Sole . config . storage ! . setItem ( key , json )
108
- fn ( api )
109
- }
110
- return api
82
+ return swrApi ( client , request , fn , args , method )
111
83
}
112
84
113
- let ctx :ApiState = { setError, addFieldError, loading, error, api, apiVoid, apiForm, apiFormVoid, swr, unRefs, setRef, swrCacheKey , swrClear }
85
+ let ctx :ApiState = { setError, addFieldError, loading, error, api, apiVoid, apiForm, apiFormVoid, swr, unRefs, setRef }
114
86
provide ( 'ApiState' , ctx )
115
87
return ctx
116
88
}
0 commit comments