forked from feathersjs-ecosystem/feathers-vuex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.ts
330 lines (300 loc) · 8.64 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import { Service } from '@feathersjs/feathers'
import { Params, Paginated } from '../utils'
import { EventEmitter } from 'events'
import { Store } from 'vuex'
export type Id = number | string
/*
eslint
@typescript-eslint/no-explicit-any: 0
*/
export interface FeathersVuexOptions {
serverAlias: string
addOnUpsert?: boolean
autoRemove?: boolean
debug?: boolean
enableEvents?: boolean
handleEvents?: HandleEvents
idField?: string
tempIdField?: string
keepCopiesInStore?: boolean
nameStyle?: string
paramsForServer?: string[]
preferUpdate?: boolean
replaceItems?: boolean
skipRequestIfExists?: boolean
whitelist?: string[]
}
export interface HandleEvents {
created?: Function
patched?: Function
updated?: Function
removed?: Function
}
export interface MakeServicePluginOptions {
Model: any
service: Service<any>
idField?: string
tempIdField?: string
addOnUpsert?: boolean
autoRemove?: boolean
debug?: boolean
enableEvents?: boolean
preferUpdate?: boolean
replaceItems?: boolean
skipRequestIfExists?: boolean
nameStyle?: string
servicePath?: string
namespace?: string
whitelist?: string[]
paramsForServer?: string[]
instanceDefaults?: () => {}
setupInstance?: (data: any, { models, store }) => {}
handleEvents?: HandleEvents
state?: {}
getters?: {}
mutations?: {}
actions?: {}
}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface FeathersVuexStoreState {
/** Allow clients to augment store state */
}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface FeathersVuexGlobalModels {
/** Allow clients to augment Global models */
}
interface PatchParams<D> extends Params {
data: Partial<D>
}
export interface ModelSetupContext {
/**
* The global Vuex store
*/
store: FeathersVuexStoreState
/**
* The global `models` object
*/
models: FeathersVuexGlobalModels
}
export interface ModelInstanceOptions {
/**
* Creating clone?
*
* Default: `false`
*/
clone?: boolean
/**
* Add to store
*
* Default: `true`
*/
commit?: boolean
/**
* Merge with existing?
*
* Default: `true`
*/
merge?: boolean
}
type AnyData = { [k: string]: any }
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface FeathersVuexTypeOptions {
// 'model-readonly': true
}
type GetOption<T, K, Default = false> = K extends keyof T ? T[K] : Default
// ModelData is readonly unless user explicitly says `model-readonly` is false
type ModelData<D> = GetOption<
FeathersVuexTypeOptions,
'model-readonly',
true
> extends false
? D
: Readonly<D>
/**
* FeathersVuex Model with readonly data props
*/
export type Model<D extends {} = AnyData> = ModelInstance<D> & ModelData<D>
/**
* FeathersVuex Model clone with writeable data props
*/
export type ModelClone<D extends {} = AnyData> = ModelInstanceClone<D> & D
/** Static Model interface */
export interface ModelStatic<D extends {} = AnyData> extends EventEmitter {
/**
* The path passed to `FeathersClient.service()` to create the service
*/
readonly servicePath: string
/**
* Holds the value that was used to register the module with Vuex.
* This will match the servicePath unless you've provided a custom
* namespace in the Service Module plugin options.
*/
readonly namespace: string
/**
* The global Vuex store
*/
readonly store: Store<FeathersVuexStoreState>
/**
* The field in each record that will contain the ID
*/
readonly idField: string
/**
* The field in each temporary record that contains the temporary ID
*/
readonly tempIdField: string
/**
* If `true`, calling `model.save()` will do an `update` instead of a `patch`.
*/
readonly preferUpdate: boolean
/**
* Server alias in the global `models` object
*/
readonly serverAlias: string
/**
* Model name used to circumvent Babel transpilation errors
*/
readonly modelName: string
/**
* The global `models` object
*/
readonly models: FeathersVuexGlobalModels
/**
* All model copies created using `model.clone()`
*/
readonly copiesById: {
[key: string]: ModelClone<D> | undefined
[key: number]: ModelClone<D> | undefined
}
/**
* Create new Model
* @param data partial model data
* @param options model instance options
*/
new (data?: Partial<D>, options?: ModelInstanceOptions): Model<D>
/**
* The instanceDefaults API was created in version 1.7 to prevent
* requiring to specify data for new instances created throughout
* the app. Depending on the complexity of the service's "business
* logic", it can save a lot of boilerplate. Notice that it is
* similar to the setupInstance method added in 2.0. The instanceDefaults
* method should ONLY be used to return default values for a new
* instance. Use setupInstance to handle other transformations on
* the data.
* @param data the instance data
* @param ctx setup context
*/
instanceDefaults(data: Partial<D>, ctx: ModelSetupContext): Partial<D>
/**
* A new setupInstance class method is now available in version 2.0.
* This method allows you to transform the data and setup the final
* instance based on incoming data. For example, you can access the
* models object to reference other service Model classes and create
* data associations.
* @param data the instance data
* @param ctx setup context
*/
setupInstance(data: Partial<D>, ctx: ModelSetupContext): Partial<D>
/**
* Gets called just before sending the data to the API server. It gets
* called with the data and must return the diffed data.
*
* Default: `data => data`
* @param data the instance data
*/
diffOnPatch(data: Partial<D>): Partial<D>
/**
* A proxy for the `find` action
* @param params Find params
*/
find(params?: Params): Promise<Model<D>[] | Paginated<Model<D>>>
/**
* A proxy for the `find` getter
* @param params Find params
*/
findInStore(params?: Params): Model<D>[] | Paginated<Model<D>>
/**
* A proxy for the `get` action
* @param id ID of record to retrieve
* @param params Get params
*/
get(id: Id, params?: Params): Promise<Model<D> | undefined>
/**
* A proxy for the `get` getter
* @param id ID of record to retrieve
* @param params Get params
*/
getFromStore(id: Id, params?: Params): Model<D> | undefined
}
/** Model instance interface */
export interface ModelInstance<D extends {} = AnyData> {
/**
* model's temporary ID
*/
readonly __id?: string
/**
* model is temporary?
*/
readonly __isTemp?: boolean
/**
* model is a clone?
*/
readonly __isClone?: boolean
/**
* Creates a deep copy of the record and stores it on
* `Model.copiesById`. This allows you to make changes
* to the clone and not update visible data until you
* commit or save the data.
* @param data Properties to modify on the cloned instance
*/
clone(data?: Partial<D>): ModelClone<D>
/**
* The create method calls the create action (service method)
* using the instance data.
* @param params Params passed to the Feathers client request
*/
create(params?: Params): Promise<this>
/**
* The patch method calls the patch action (service method)
* using the instance data. The instance's id field is used
* for the patch id.
*
* You can provide an object as `params.data`, and Feathers-Vuex
* will use `params.data` as the patch data. This allows patching
* with partial data.
* @param params Params passed to the Feathers client request
*/
patch(params?: PatchParams<D>): Promise<this>
/**
* The remove method calls the remove action (service method)
* using the instance data. The instance's id field is used
* for the remove id.
* @param params Params passed to the Feathers client request
*/
remove(params?: Params): Promise<this>
/**
* The update method calls the update action (service method)
* using the instance data. The instance's id field is used for
* the update id.
* @param params Params passed to the Feathers client request
*/
update(params?: Params): Promise<this>
/**
* The save method is a convenience wrapper for the create/patch
* methods, by default. If the records has no _id, the
* instance.create() method will be used.
* @param params Params passed to the Feathers client request
*/
save(params?: Params): Promise<this>
}
/** Model instance clone interface */
export interface ModelInstanceClone<D extends {} = AnyData>
extends ModelInstance<D> {
/**
* Commit changes from clone to original
*/
commit(): Model<D>
/**
* Discards changes made on this clone and syncs with the original
*/
reset(): this
}