Skip to content

Commit 73f2a9f

Browse files
committed
allow users to augment type options to disable readonly model data
1 parent b4e33ff commit 73f2a9f

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ import {
2727
ModelClone,
2828
Id,
2929
FeathersVuexStoreState,
30-
FeathersVuexGlobalModels
30+
FeathersVuexGlobalModels,
31+
FeathersVuexTypeOptions
3132
} from './service-module/types'
3233
import { initAuth, hydrateApi } from './utils'
3334
import { FeathersVuex } from './vue-plugin/vue-plugin'
@@ -119,5 +120,6 @@ export {
119120
ModelSetupContext,
120121
ServiceState,
121122
FeathersVuexGlobalModels,
122-
FeathersVuexStoreState
123+
FeathersVuexStoreState,
124+
FeathersVuexTypeOptions
123125
}

src/service-module/types.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,26 @@ export interface ModelInstanceOptions {
112112

113113
type AnyData = { [k: string]: any }
114114

115+
// eslint-disable-next-line @typescript-eslint/no-empty-interface
116+
export interface FeathersVuexTypeOptions {
117+
// 'model-readonly': true
118+
}
119+
120+
type GetOption<T, K, Default = false> = K extends keyof T ? T[K] : Default
121+
122+
// ModelData is readonly unless user explicitly says `model-readonly` is false
123+
type ModelData<D> = GetOption<
124+
FeathersVuexTypeOptions,
125+
'model-readonly',
126+
true
127+
> extends false
128+
? D
129+
: Readonly<D>
130+
115131
/**
116132
* FeathersVuex Model with readonly data props
117133
*/
118-
export type Model<D extends {} = AnyData> = ModelInstance<D> & Readonly<D>
134+
export type Model<D extends {} = AnyData> = ModelInstance<D> & ModelData<D>
119135

120136
/**
121137
* FeathersVuex Model clone with writeable data props

0 commit comments

Comments
 (0)