Skip to content

Commit 13703f5

Browse files
committed
add Model typing to useGet
1 parent 3f37964 commit 13703f5

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

src/useGet.ts

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,25 @@ import {
1111
Ref
1212
} from '@vue/composition-api'
1313
import { Params } from './utils'
14+
import { AnyData, ModelStatic, Model, Id } from './service-module/types'
1415

15-
interface UseGetOptions {
16-
model: Function
16+
interface UseGetOptions<T> {
17+
model: ModelStatic<T>
1718
id: null | string | number | Ref<null> | Ref<string> | Ref<number>
1819
params?: Params | Ref<Params>
19-
queryWhen?: Ref<Function>
20+
queryWhen?: Ref<boolean>
2021
local?: boolean
2122
lazy?: boolean
2223
}
23-
interface UseGetState {
24-
item: Ref<any>
24+
interface UseGetState<T> {
2525
isPending: boolean
2626
hasBeenRequested: boolean
2727
hasLoaded: boolean
2828
error: null | Error
2929
isLocal: boolean
3030
}
31-
interface UseGetData {
32-
item: Ref<any>
31+
interface UseGetData<T> {
32+
item: Ref<Readonly<Model<T> | null>>
3333
servicePath: Ref<string>
3434
isPending: Ref<boolean>
3535
hasBeenRequested: Ref<boolean>
@@ -39,8 +39,8 @@ interface UseGetData {
3939
get: Function
4040
}
4141

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> = {
4444
model: null,
4545
id: null,
4646
params: null,
@@ -61,31 +61,30 @@ export default function get(options: UseGetOptions): UseGetData {
6161
return isRef(params) ? params.value : params
6262
}
6363

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 = {
6673
item: computed(() => {
6774
const getterId = isRef(id) ? id.value : id
6875
const getterParams = isRef(params)
6976
? Object.assign({}, params.value)
7077
: params == null
7178
? params
7279
: { ...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
8081
}),
81-
isPending: false,
82-
hasBeenRequested: false,
83-
hasLoaded: false,
84-
error: null,
85-
isLocal: local
86-
})
82+
servicePath: computed(() => model.servicePath)
83+
}
84+
85+
8786

88-
function get<T>(id, params?: Params): T {
87+
function get(id, params?: Params): Promise<Model<T> | undefined> {
8988
const idToUse = isRef(id) ? id.value : id
9089
const paramsToUse = isRef(params) ? params.value : params
9190

@@ -109,6 +108,8 @@ export default function get(options: UseGetOptions): UseGetData {
109108
state.error = error
110109
return error
111110
})
111+
} else {
112+
return Promise.resolve(undefined)
112113
}
113114
}
114115

@@ -132,8 +133,8 @@ export default function get(options: UseGetOptions): UseGetData {
132133
}
133134

134135
return {
135-
servicePath,
136136
...toRefs(state),
137+
...computes,
137138
get
138139
}
139140
}

0 commit comments

Comments
 (0)