Skip to content

Commit 9865f36

Browse files
committed
add explicit typing in make-base-model to ensure Model interface is implemented
1 parent c9f2969 commit 9865f36

File tree

3 files changed

+35
-25
lines changed

3 files changed

+35
-25
lines changed

src/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ import {
2424
ModelStatic,
2525
ModelSetupContext,
2626
Model,
27-
ModelClone
27+
ModelClone,
28+
Id
2829
} from './service-module/types'
2930
import { initAuth, hydrateApi } from './utils'
3031
import { FeathersVuex } from './vue-plugin/vue-plugin'
@@ -109,9 +110,10 @@ export {
109110
clients,
110111
useFind,
111112
useGet,
113+
Id,
112114
Model,
113115
ModelClone,
114116
ModelStatic,
115-
ServiceState,
116-
ModelSetupContext
117+
ModelSetupContext,
118+
ServiceState
117119
}

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

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,20 @@ eslint
55
*/
66
import {
77
FeathersVuexOptions,
8+
Id,
89
ModelInstanceOptions,
10+
Model,
911
ModelStatic,
10-
ModelInstanceClone
12+
ModelInstanceClone,
13+
ModelClone
1114
} from './types'
1215
import { globalModels, prepareAddModel } from './global-models'
13-
import { mergeWithAccessors, checkNamespace, getId } from '../utils'
16+
import { mergeWithAccessors, checkNamespace, getId, Params } from '../utils'
1417
import _merge from 'lodash/merge'
1518
import _get from 'lodash/get'
1619
import { EventEmitter } from 'events'
1720
import { FeathersVuexStoreState, FeathersVuexGlobalModels } from '..'
21+
import { ModelSetupContext } from './types'
1822
import { Store } from 'vuex'
1923

2024
// A hack to prevent error with this.constructor.preferUpdate
@@ -64,14 +68,14 @@ export default function makeBaseModel(options: Required<FeathersVuexOptions>) {
6468
public static namespace: string
6569
public static keepCopiesInStore = options.keepCopiesInStore
6670
// eslint-disable-next-line
67-
public static instanceDefaults(data, { models, store }) {
71+
public static instanceDefaults(data: Partial<D>, ctx: ModelSetupContext) {
6872
return data
6973
}
7074
// eslint-disable-next-line
71-
public static setupInstance(data, { models, store }) {
75+
public static setupInstance(data: Partial<D>, ctx: ModelSetupContext) {
7276
return data
7377
}
74-
public static diffOnPatch(data) {
78+
public static diffOnPatch(data: Partial<D>) {
7579
return data
7680
}
7781

@@ -84,7 +88,10 @@ export default function makeBaseModel(options: Required<FeathersVuexOptions>) {
8488
public static serverAlias: string = options.serverAlias
8589

8690
public static readonly models = globalModels as FeathersVuexGlobalModels // Can access other Models here
87-
public static copiesById = {}
91+
public static copiesById: {
92+
[key: string]: ModelClone<D> | undefined
93+
[key: number]: ModelClone<D> | undefined
94+
} = {}
8895

8996
public __id: string
9097
public __isClone: boolean
@@ -93,7 +100,7 @@ export default function makeBaseModel(options: Required<FeathersVuexOptions>) {
93100
public static merge = mergeWithAccessors
94101
public static modelName = 'BaseModel'
95102

96-
public constructor(data, options: ModelInstanceOptions) {
103+
public constructor(data: Partial<D>, options: ModelInstanceOptions) {
97104
// You have to pass at least an empty object to get a tempId.
98105
data = data || {}
99106
options = Object.assign({}, defaultOptions, options)
@@ -182,23 +189,23 @@ export default function makeBaseModel(options: Required<FeathersVuexOptions>) {
182189
return getId(record, idField)
183190
}
184191

185-
public static find(params) {
192+
public static find(params?: Params) {
186193
return this._dispatch('find', params)
187194
}
188195

189-
public static findInStore(params) {
196+
public static findInStore(params?: Params) {
190197
return this._getters('find', params)
191198
}
192199

193-
public static get(id, params) {
200+
public static get(id: Id, params?: Params) {
194201
if (params) {
195202
return this._dispatch('get', [id, params])
196203
} else {
197204
return this._dispatch('get', id)
198205
}
199206
}
200207

201-
public static getFromStore(id, params?) {
208+
public static getFromStore(id: Id, params?: Params) {
202209
return this._getters('get', id, params)
203210
}
204211

@@ -261,7 +268,7 @@ export default function makeBaseModel(options: Required<FeathersVuexOptions>) {
261268
/**
262269
* clone the current record using the `createCopy` mutation
263270
*/
264-
public clone(data) {
271+
public clone(data: Partial<D>): ModelClone<D> {
265272
const { idField, tempIdField } = this.constructor as typeof BaseModel
266273
if (this.__isClone) {
267274
throw new Error('You cannot clone a copy')
@@ -294,7 +301,7 @@ export default function makeBaseModel(options: Required<FeathersVuexOptions>) {
294301
/**
295302
* Reset a clone to match the instance in the store.
296303
*/
297-
public reset() {
304+
public reset(): this {
298305
const { idField, tempIdField, _commit } = this
299306
.constructor as typeof BaseModel
300307

@@ -313,7 +320,7 @@ export default function makeBaseModel(options: Required<FeathersVuexOptions>) {
313320
/**
314321
* Update a store instance to match a clone.
315322
*/
316-
public commit() {
323+
public commit(): Model<D> {
317324
const { idField, tempIdField, _commit, _getters } = this
318325
.constructor as typeof BaseModel
319326
if (this.__isClone) {
@@ -333,7 +340,7 @@ export default function makeBaseModel(options: Required<FeathersVuexOptions>) {
333340
* A shortcut to either call create or patch/update
334341
* @param params
335342
*/
336-
public save(params) {
343+
public save(params?: Params): Promise<this> {
337344
const { idField, preferUpdate } = this.constructor as typeof BaseModel
338345
const id = getId(this, idField)
339346
if (id != null) {
@@ -346,7 +353,7 @@ export default function makeBaseModel(options: Required<FeathersVuexOptions>) {
346353
* Calls service create with the current instance data
347354
* @param params
348355
*/
349-
public create(params) {
356+
public create(params?: Params): Promise<this> {
350357
const { _dispatch } = this.constructor as typeof BaseModel
351358
const data = Object.assign({}, this)
352359
if (data[options.idField] === null) {
@@ -359,7 +366,7 @@ export default function makeBaseModel(options: Required<FeathersVuexOptions>) {
359366
* Calls service patch with the current instance data
360367
* @param params
361368
*/
362-
public patch(params?) {
369+
public patch(params?: Params): Promise<this> {
363370
const { idField, _dispatch } = this.constructor as typeof BaseModel
364371
const id = getId(this, idField)
365372

@@ -376,7 +383,7 @@ export default function makeBaseModel(options: Required<FeathersVuexOptions>) {
376383
* Calls service update with the current instance data
377384
* @param params
378385
*/
379-
public update(params) {
386+
public update(params?: Params): Promise<this> {
380387
const { idField, _dispatch } = this.constructor as typeof BaseModel
381388
const id = getId(this, idField)
382389

@@ -393,7 +400,7 @@ export default function makeBaseModel(options: Required<FeathersVuexOptions>) {
393400
* Calls service remove with the current instance id
394401
* @param params
395402
*/
396-
public remove(params) {
403+
public remove(params?: Params): Promise<this> {
397404
const { idField, tempIdField, _dispatch, _commit } = this
398405
.constructor as typeof BaseModel
399406
const id = getId(this, idField)

src/service-module/types.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { Service } from '@feathersjs/feathers'
22
import { Params, Paginated } from '../utils'
3-
import { Id } from '@feathersjs/feathers'
43
import { EventEmitter } from 'events'
54
import { FeathersVuexStoreState, FeathersVuexGlobalModels } from '..'
65
import { Store } from 'vuex'
76

7+
export type Id = number | string
8+
89
/*
910
eslint
1011
@typescript-eslint/no-explicit-any: 0
@@ -166,8 +167,8 @@ export interface ModelStatic<D extends {} = {}> extends EventEmitter {
166167
* All model copies created using `model.clone()`
167168
*/
168169
readonly copiesById: {
169-
[key: string]: Model<D> | undefined
170-
[key: number]: Model<D> | undefined
170+
[key: string]: ModelClone<D> | undefined
171+
[key: number]: ModelClone<D> | undefined
171172
}
172173

173174
/**

0 commit comments

Comments
 (0)