Skip to content

Commit 0ab21cb

Browse files
committed
simplify: combine Model and ModelClone interfaces
this gives better typing for decendent classes
1 parent 8d49748 commit 0ab21cb

File tree

4 files changed

+18
-33
lines changed

4 files changed

+18
-33
lines changed

src/index.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,12 @@ import {
2424
ModelStatic,
2525
ModelSetupContext,
2626
Model,
27-
ModelClone,
2827
Id,
2928
FeathersVuexStoreState,
3029
FeathersVuexGlobalModels,
3130
FeathersVuexTypeOptions,
3231
GlobalModels,
33-
ModelInstance,
34-
ModelInstanceClone
32+
ModelInstance
3533
} from './service-module/types'
3634
import { initAuth, hydrateApi } from './utils'
3735
import { FeathersVuex } from './vue-plugin/vue-plugin'
@@ -113,10 +111,8 @@ export {
113111
AuthState,
114112
Id,
115113
Model,
116-
ModelClone,
117114
ModelStatic,
118115
ModelInstance,
119-
ModelInstanceClone,
120116
ModelSetupContext,
121117
ServiceState,
122118
FeathersVuexGlobalModels,

src/service-module/make-base-model.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ import {
99
ModelInstanceOptions,
1010
Model,
1111
ModelStatic,
12-
ModelInstanceClone,
13-
ModelClone,
1412
GlobalModels,
15-
StoreState
13+
StoreState,
14+
ModelInstance
1615
} from './types'
1716
import { globalModels, prepareAddModel } from './global-models'
1817
import { mergeWithAccessors, checkNamespace, getId, Params } from '../utils'
@@ -63,7 +62,7 @@ export default function makeBaseModel(options: FeathersVuexOptions) {
6362
return ExistingBaseModel as ModelStatic<D>
6463
}
6564

66-
abstract class BaseModel implements ModelInstanceClone<D> {
65+
abstract class BaseModel implements ModelInstance<D> {
6766
// Think of these as abstract static properties
6867
public static servicePath: string
6968
public static namespace: string
@@ -90,8 +89,8 @@ export default function makeBaseModel(options: FeathersVuexOptions) {
9089

9190
public static readonly models = globalModels as GlobalModels // Can access other Models here
9291
public static copiesById: {
93-
[key: string]: ModelClone<D> | undefined
94-
[key: number]: ModelClone<D> | undefined
92+
[key: string]: Model<D> | undefined
93+
[key: number]: Model<D> | undefined
9594
} = {}
9695

9796
public __id: string
@@ -269,7 +268,7 @@ export default function makeBaseModel(options: FeathersVuexOptions) {
269268
/**
270269
* clone the current record using the `createCopy` mutation
271270
*/
272-
public clone(data: Partial<D>): ModelClone<D> {
271+
public clone(data: Partial<D>): this {
273272
const { idField, tempIdField } = this.constructor as typeof BaseModel
274273
if (this.__isClone) {
275274
throw new Error('You cannot clone a copy')
@@ -321,7 +320,7 @@ export default function makeBaseModel(options: FeathersVuexOptions) {
321320
/**
322321
* Update a store instance to match a clone.
323322
*/
324-
public commit(): Model<D> {
323+
public commit(): this {
325324
const { idField, tempIdField, _commit, _getters } = this
326325
.constructor as typeof BaseModel
327326
if (this.__isClone) {

src/service-module/service-module.state.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ eslint
66

77
import _omit from 'lodash/omit'
88

9-
import { MakeServicePluginOptions, AnyData, Model, ModelClone } from './types'
9+
import { MakeServicePluginOptions, AnyData, Model } from './types'
1010

1111
export interface ServiceStateExclusiveDefaults {
1212
ids: string[]
@@ -64,7 +64,7 @@ export interface ServiceState<D extends AnyData = AnyData> {
6464
[k: number]: Model<D>
6565
}
6666
copiesById: {
67-
[k: string]: ModelClone<D>
67+
[k: string]: Model<D>
6868
}
6969
whitelist: string[]
7070
paramsForServer: string[]

src/service-module/types.ts

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,6 @@ type ModelData<D> = GetOption<
137137
*/
138138
export type Model<D extends {} = AnyData> = ModelInstance<D> & ModelData<D>
139139

140-
/**
141-
* FeathersVuex Model clone with writeable data props
142-
*/
143-
export type ModelClone<D extends {} = AnyData> = ModelInstanceClone<D> & D
144-
145140
/** Static Model interface */
146141
export interface ModelStatic<D extends {} = AnyData> extends EventEmitter {
147142
/**
@@ -186,8 +181,8 @@ export interface ModelStatic<D extends {} = AnyData> extends EventEmitter {
186181
* All model copies created using `model.clone()`
187182
*/
188183
readonly copiesById: {
189-
[key: string]: ModelClone<D> | undefined
190-
[key: number]: ModelClone<D> | undefined
184+
[key: string]: Model<D> | undefined
185+
[key: number]: Model<D> | undefined
191186
}
192187

193188
/**
@@ -292,15 +287,18 @@ export interface ModelInstance<D extends {} = AnyData> {
292287
* model is temporary?
293288
*/
294289
readonly __isTemp?: boolean
295-
290+
/**
291+
* model is a clone?
292+
*/
293+
readonly __isClone?: boolean
296294
/**
297295
* Creates a deep copy of the record and stores it on
298296
* `Model.copiesById`. This allows you to make changes
299297
* to the clone and not update visible data until you
300298
* commit or save the data.
301299
* @param data Properties to modify on the cloned instance
302300
*/
303-
clone(data?: Partial<D>): ModelClone<D>
301+
clone(data?: Partial<D>): this
304302
/**
305303
* The create method calls the create action (service method)
306304
* using the instance data.
@@ -339,20 +337,12 @@ export interface ModelInstance<D extends {} = AnyData> {
339337
* @param params Params passed to the Feathers client request
340338
*/
341339
save(params?: Params): Promise<this>
342-
}
343340

344-
/** Model instance clone interface */
345-
export interface ModelInstanceClone<D extends {} = AnyData>
346-
extends Omit<ModelInstance<D>, 'clone'> {
347-
/**
348-
* model is a clone?
349-
*/
350-
readonly __isClone: true
351341

352342
/**
353343
* Commit changes from clone to original
354344
*/
355-
commit(): Model<D>
345+
commit(): this
356346

357347
/**
358348
* Discards changes made on this clone and syncs with the original

0 commit comments

Comments
 (0)