Skip to content

Commit fab53b2

Browse files
committed
Merge branch 'master' of https://github.com/feathersjs-ecosystem/feathers-vuex into find-copies
2 parents ed2c456 + 528e04e commit fab53b2

File tree

10 files changed

+364
-117
lines changed

10 files changed

+364
-117
lines changed

docs/3.0-major-release.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,33 @@ Version 3.0 of Feathers-Vuex is the Vue Composition API release! There were qui
1616

1717
And now it has become the best way to perform queries with Feathers-Vuex. To find out how to take advantage of the new functionality in your apps, read the [Feather-Vuex Composition API docs](./composition-api.md).
1818

19+
## New `extend` option for `makeServicePlugin` <Badge text="3.9.0+" />
20+
21+
The `makeServicePlugin` now supports an `extend` method that allows customizing the store and gives access to the actual Vuex `store` object, as shown in this example:
22+
23+
```js
24+
import { makeServicePlugin } from ‘feathers-vuex’
25+
import { feathersClient } from ‘./feathers-client.js’
26+
27+
class Todo { /* truncated */ }
28+
29+
export default makeServicePlugin({
30+
Model: Todo,
31+
service: feathersClient.service(‘todos’),
32+
extend({ store, module }) {
33+
// Listen to other parts of the store
34+
store.watch(/* truncated */)
35+
36+
return {
37+
state: {},
38+
getters: {},
39+
mutations: {},
40+
actions: {}
41+
}
42+
}
43+
})
44+
```
45+
1946
## Partial data on patch <Badge text="3.9.0+" />
2047
As of version 3.9.0, you can provide an object as `params.data`, and Feathers-Vuex will use `params.data` as the patch data. This change was made to the service-module, itself, so it will work for `patch` across all of feathers-vuex. Here's an example of patching with partial data:
2148

docs/service-plugin.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,36 @@ The `find` getter queries data from the local store using the same Feathers quer
733733

734734
## Customizing a Service's Default Store
735735

736-
As shown in the example below, the service module allows you to customize its store:
736+
### New `extend` option for `makeServicePlugin` <Badge text="3.14.0+" />
737+
738+
As of version `3.14.0`, the `makeServicePlugin` now supports an `extend` method that allows customizing the store and gives access to the actual Vuex `store` object, as shown in this example:
739+
740+
```js
741+
import { makeServicePlugin } from ‘feathers-vuex’
742+
import { feathersClient } from ‘./feathers-client.js’
743+
744+
class Todo { /* truncated */ }
745+
746+
export default makeServicePlugin({
747+
Model: Todo,
748+
service: feathersClient.service(‘todos’),
749+
extend({ store, module }) {
750+
// Listen to other parts of the store
751+
store.watch(/* truncated */)
752+
753+
return {
754+
state: {},
755+
getters: {},
756+
mutations: {},
757+
actions: {}
758+
}
759+
}
760+
})
761+
```
762+
763+
### Deprecated options for customizing the store
764+
765+
Before version `3.14.0`, you can customize the store using the options for `state`, `getters`, `mutations`, and `actions`, as shown below. This method is now deprecated and will be removed from Feathers-Vuex 4.0.
737766

738767
```js
739768
// src/store/services/users.js

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "feathers-vuex",
33
"description": "FeathersJS, Vue, and Nuxt for the artisan developer",
4-
"version": "3.13.0",
4+
"version": "3.14.0",
55
"homepage": "https:feathers-vuex.feathersjs-ecosystem.com",
66
"main": "dist/",
77
"module": "dist/",

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

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,37 @@ eslint
33
@typescript-eslint/explicit-function-return-type: 0,
44
@typescript-eslint/no-explicit-any: 0
55
*/
6+
import _pick from 'lodash/pick'
7+
import _merge from 'lodash/merge'
68
import makeDefaultState from './service-module.state'
79
import makeGetters from './service-module.getters'
810
import makeMutations from './service-module.mutations'
911
import makeActions from './service-module.actions'
1012
import { Service } from '@feathersjs/feathers'
1113
import { MakeServicePluginOptions } from './types'
14+
import { Store } from 'vuex'
1215

1316
export default function makeServiceModule(
1417
service: Service<any>,
15-
options: MakeServicePluginOptions
18+
options: MakeServicePluginOptions,
19+
store: Store<any>
1620
) {
17-
const defaultState = makeDefaultState(options)
18-
const defaultGetters = makeGetters()
19-
const defaultMutations = makeMutations()
20-
const defaultActions = makeActions(service)
21-
22-
return {
21+
const defaults = {
2322
namespaced: true,
24-
state: Object.assign({}, defaultState, options.state),
25-
getters: Object.assign({}, defaultGetters, options.getters),
26-
mutations: Object.assign({}, defaultMutations, options.mutations),
27-
actions: Object.assign({}, defaultActions, options.actions)
23+
state: makeDefaultState(options),
24+
getters: makeGetters(),
25+
mutations: makeMutations(),
26+
actions: makeActions(service)
2827
}
28+
const fromOptions = _pick(options, [
29+
'state',
30+
'getters',
31+
'mutations',
32+
'actions'
33+
])
34+
const merged = _merge({}, defaults, fromOptions)
35+
const extended = options.extend({ store, module: merged })
36+
const finalModule = _merge({}, merged, extended)
37+
38+
return finalModule
2939
}

src/service-module/make-service-plugin.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ eslint
33
@typescript-eslint/explicit-function-return-type: 0,
44
@typescript-eslint/no-explicit-any: 0
55
*/
6-
import { FeathersVuexOptions, MakeServicePluginOptions } from './types'
6+
import {
7+
FeathersVuexOptions,
8+
MakeServicePluginOptions,
9+
ServicePluginExtendOptions
10+
} from './types'
11+
712
import makeServiceModule from './make-service-module'
813
import { globalModels, prepareAddModel } from './global-models'
914
import enableServiceEvents from './service-module.events'
@@ -13,6 +18,14 @@ import _get from 'lodash/get'
1318
interface ServiceOptionsDefaults {
1419
servicePath: string
1520
namespace: string
21+
extend: (
22+
options: ServicePluginExtendOptions
23+
) => {
24+
state: any
25+
getters: any
26+
mutations: any
27+
actions: any
28+
}
1629
state: {}
1730
getters: {}
1831
mutations: {}
@@ -25,6 +38,7 @@ interface ServiceOptionsDefaults {
2538
const defaults: ServiceOptionsDefaults = {
2639
namespace: '', // The namespace for the Vuex module. Will generally be derived from the service.path, service.name, when available. Otherwise, it must be provided here, explicitly.
2740
servicePath: '',
41+
extend: ({ module }) => module, // for custom plugin (replaces state, getters, mutations, and actions)
2842
state: {}, // for custom state
2943
getters: {}, // for custom getters
3044
mutations: {}, // for custom mutations
@@ -91,7 +105,7 @@ export default function prepareMakeServicePlugin(
91105
return store => {
92106
// (1^) Create and register the Vuex module
93107
options.namespace = makeNamespace(namespace, servicePath, nameStyle)
94-
const module = makeServiceModule(service, options)
108+
const module = makeServiceModule(service, options, store)
95109
// Don't preserve state if reinitialized (prevents state pollution in SSR)
96110
store.registerModule(options.namespace, module, { preserveState: false })
97111

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ export default function makeDefaultState(options: MakeServicePluginOptions) {
107107
'instanceDefaults',
108108
'setupInstance',
109109
'handleEvents',
110+
'extend',
110111
'state',
111112
'getters',
112113
'mutations',

src/service-module/types.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ export interface HandleEvents {
3737
removed?: Function
3838
}
3939

40+
export interface ServicePluginExtendOptions {
41+
store: Store<any>
42+
module: any
43+
}
44+
4045
export interface MakeServicePluginOptions {
4146
Model: any
4247
service: Service<any>
@@ -65,6 +70,15 @@ export interface MakeServicePluginOptions {
6570
instanceDefaults?: () => {}
6671
setupInstance?: (data: any, { models, store }) => {}
6772
handleEvents?: HandleEvents
73+
74+
extend?: (
75+
options: ServicePluginExtendOptions
76+
) => {
77+
state?: any
78+
getters?: any
79+
mutations?: any
80+
actions?: any
81+
}
6882
state?: {}
6983
getters?: {}
7084
mutations?: {}

test/fixtures/feathers-client.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,40 +11,50 @@ const baseUrl = 'http://localhost:3030'
1111

1212
// These are fixtures used in the service-modulet.test.js under socket events.
1313
let id = 0
14-
mockServer.on('things::create', function (data) {
14+
mockServer.on('things::create', function (data, params, cb) {
1515
data.id = id
1616
id++
1717
mockServer.emit('things created', data)
18+
cb(null, data)
1819
})
19-
mockServer.on('things::patch', function (id, data) {
20+
mockServer.on('things::patch', function (id, data, params, cb) {
2021
Object.assign(data, { id, test: true })
2122
mockServer.emit('things patched', data)
23+
cb(null, data)
2224
})
23-
mockServer.on('things::update', function (id, data) {
25+
mockServer.on('things::update', function (id, data, params, cb) {
2426
Object.assign(data, { id, test: true })
2527
mockServer.emit('things updated', data)
28+
cb(null, data)
2629
})
27-
mockServer.on('things::remove', function (id) {
28-
mockServer.emit('things removed', { id, test: true })
30+
mockServer.on('things::remove', function (id, obj, cb) {
31+
const response = { id, test: true }
32+
mockServer.emit('things removed', response)
33+
cb(null, response)
2934
})
3035

3136
let idDebounce = 0
3237

33-
mockServer.on('things-debounced::create', function (data) {
38+
mockServer.on('things-debounced::create', function (data, obj, cb) {
3439
data.id = idDebounce
3540
idDebounce++
3641
mockServer.emit('things-debounced created', data)
42+
cb(null, data)
3743
})
38-
mockServer.on('things-debounced::patch', function (id, data) {
44+
mockServer.on('things-debounced::patch', function (id, data, params, cb) {
3945
Object.assign(data, { id, test: true })
4046
mockServer.emit('things-debounced patched', data)
47+
cb(null, data)
4148
})
42-
mockServer.on('things-debounced::update', function (id, data) {
49+
mockServer.on('things-debounced::update', function (id, data, params, cb) {
4350
Object.assign(data, { id, test: true })
4451
mockServer.emit('things-debounced updated', data)
52+
cb(null, data)
4553
})
46-
mockServer.on('things-debounced::remove', function (id) {
47-
mockServer.emit('things-debounced removed', { id, test: true })
54+
mockServer.on('things-debounced::remove', function (id, params, cb) {
55+
const response = { id, test: true }
56+
mockServer.emit('things-debounced removed', response)
57+
cb(null, response)
4858
})
4959

5060
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type

0 commit comments

Comments
 (0)