@@ -11,25 +11,25 @@ import {
11
11
Ref
12
12
} from '@vue/composition-api'
13
13
import { Params } from './utils'
14
+ import { AnyData , ModelStatic , Model , Id } from './service-module/types'
14
15
15
- interface UseGetOptions {
16
- model : Function
16
+ interface UseGetOptions < T > {
17
+ model : ModelStatic < T >
17
18
id : null | string | number | Ref < null > | Ref < string > | Ref < number >
18
19
params ?: Params | Ref < Params >
19
- queryWhen ?: Ref < Function >
20
+ queryWhen ?: Ref < boolean >
20
21
local ?: boolean
21
22
lazy ?: boolean
22
23
}
23
- interface UseGetState {
24
- item : Ref < any >
24
+ interface UseGetState < T > {
25
25
isPending : boolean
26
26
hasBeenRequested : boolean
27
27
hasLoaded : boolean
28
28
error : null | Error
29
29
isLocal : boolean
30
30
}
31
- interface UseGetData {
32
- item : Ref < any >
31
+ interface UseGetData < T > {
32
+ item : Ref < Readonly < Model < T > | null > >
33
33
servicePath : Ref < string >
34
34
isPending : Ref < boolean >
35
35
hasBeenRequested : Ref < boolean >
@@ -39,8 +39,8 @@ interface UseGetData {
39
39
get : Function
40
40
}
41
41
42
- export default function get ( options : UseGetOptions ) : UseGetData {
43
- const defaults = {
42
+ export default function get < T extends AnyData = AnyData > ( options : UseGetOptions < T > ) : UseGetData < T > {
43
+ const defaults : UseGetOptions < T > = {
44
44
model : null ,
45
45
id : null ,
46
46
params : null ,
@@ -61,31 +61,30 @@ export default function get(options: UseGetOptions): UseGetData {
61
61
return isRef ( params ) ? params . value : params
62
62
}
63
63
64
- const servicePath = computed < string > ( ( ) => model . servicePath )
65
- const state = reactive < UseGetState > ( {
64
+ const state = reactive < UseGetState < T > > ( {
65
+ isPending : false ,
66
+ hasBeenRequested : false ,
67
+ hasLoaded : false ,
68
+ error : null ,
69
+ isLocal : local
70
+ } )
71
+
72
+ const computes = {
66
73
item : computed ( ( ) => {
67
74
const getterId = isRef ( id ) ? id . value : id
68
75
const getterParams = isRef ( params )
69
76
? Object . assign ( { } , params . value )
70
77
: params == null
71
78
? params
72
79
: { ...params }
73
- if ( getterParams != null ) {
74
- return model . getFromStore ( getterId , getterParams ) || null
75
- } else {
76
- return model . getFromStore ( getterId ) || null
77
- }
78
- // return model.store.state[model.servicePath].keyedById[id.value] || null
79
- // return model.getFromStore(id.value) || null
80
+ return model . getFromStore ( getterId , getterParams ) || null
80
81
} ) ,
81
- isPending : false ,
82
- hasBeenRequested : false ,
83
- hasLoaded : false ,
84
- error : null ,
85
- isLocal : local
86
- } )
82
+ servicePath : computed ( ( ) => model . servicePath )
83
+ }
84
+
85
+
87
86
88
- function get < T > ( id , params ?: Params ) : T {
87
+ function get ( id , params ?: Params ) : Promise < Model < T > | undefined > {
89
88
const idToUse = isRef ( id ) ? id . value : id
90
89
const paramsToUse = isRef ( params ) ? params . value : params
91
90
@@ -109,6 +108,8 @@ export default function get(options: UseGetOptions): UseGetData {
109
108
state . error = error
110
109
return error
111
110
} )
111
+ } else {
112
+ return Promise . resolve ( undefined )
112
113
}
113
114
}
114
115
@@ -132,8 +133,8 @@ export default function get(options: UseGetOptions): UseGetData {
132
133
}
133
134
134
135
return {
135
- servicePath,
136
136
...toRefs ( state ) ,
137
+ ...computes ,
137
138
get
138
139
}
139
140
}
0 commit comments