Skip to content

Commit 892e0af

Browse files
Merge pull request #199 from feathers-plus/data-component
v1.7.0 - Data components & mixins
2 parents 77d46b8 + ed56f12 commit 892e0af

30 files changed

+5438
-2889
lines changed

package-lock.json

Lines changed: 3482 additions & 2525 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 31 additions & 28 deletions
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": "1.6.2",
4+
"version": "1.7.0-pre.57",
55
"homepage": "https://github.com/feathersjs/feathers-vuex",
66
"main": "lib/",
77
"keywords": [
@@ -11,7 +11,7 @@
1111
"license": "MIT",
1212
"repository": {
1313
"type": "git",
14-
"url": "git://github.com/feathers-plus/feathers-vuex.git"
14+
"url": "git://github.com/feathersjs/feathers-vuex.git"
1515
},
1616
"author": {
1717
"name": "Marshall Thompson",
@@ -66,46 +66,49 @@
6666
"lib": "lib"
6767
},
6868
"dependencies": {
69-
"@feathersjs/commons": "^3.0.1",
70-
"@feathersjs/errors": "^3.3.4",
71-
"debug": "^3.2.5",
69+
"@feathersjs/adapter-commons": "^2.0.0",
70+
"@feathersjs/commons": "^4.0.0",
71+
"@feathersjs/errors": "^3.3.6",
72+
"debug": "^4.1.1",
7273
"deep-diff": "^1.0.2",
73-
"fast-copy": "^1.2.2",
74+
"fast-copy": "^1.2.3",
7475
"inflection": "^1.12.0",
7576
"jwt-decode": "^2.2.0",
7677
"lodash.isobject": "^3.0.2",
78+
"lodash.isplainobject": "^4.0.6",
7779
"lodash.merge": "^4.6.1",
80+
"lodash.omit": "^4.5.0",
7881
"lodash.trim": "^4.5.1",
79-
"serialize-error": "^2.1.0",
80-
"sift": "^6.0.0"
82+
"serialize-error": "^3.0.0",
83+
"sift": "^7.0.1"
8184
},
8285
"devDependencies": {
83-
"@feathersjs/authentication-client": "^1.0.6",
84-
"@feathersjs/authentication-jwt": "^2.0.5",
85-
"@feathersjs/client": "^3.7.3",
86-
"@feathersjs/feathers": "^3.2.3",
87-
"@feathersjs/rest-client": "^1.4.5",
88-
"@feathersjs/socketio-client": "^1.1.4",
89-
"axios": "^0.16.2",
86+
"@feathersjs/authentication-client": "^1.0.10",
87+
"@feathersjs/authentication-jwt": "^2.0.9",
88+
"@feathersjs/client": "^3.7.7",
89+
"@feathersjs/feathers": "^3.3.1",
90+
"@feathersjs/rest-client": "^1.4.7",
91+
"@feathersjs/socketio-client": "^1.2.1",
92+
"axios": "^0.18.0",
9093
"babel-cli": "^6.26.0",
9194
"babel-core": "^6.26.3",
92-
"babel-eslint": "^7.2.3",
93-
"babel-plugin-add-module-exports": "^0.2.1",
95+
"babel-eslint": "^10.0.1",
96+
"babel-plugin-add-module-exports": "^1.0.0",
9497
"babel-preset-es2015": "^6.24.1",
9598
"babel-preset-stage-2": "^6.24.1",
9699
"body-parser": "^1.18.3",
97-
"can-fixture-socket": "^0.7.2",
98-
"chai": "^3.5.0",
99-
"feathers-memory": "^2.2.0",
100+
"can-fixture-socket": "^2.0.0",
101+
"chai": "^4.2.0",
102+
"feathers-memory": "^3.0.1",
100103
"istanbul": "^1.1.0-alpha.1",
101-
"mocha": "^3.5.3",
102-
"shx": "^0.2.2",
103-
"socket.io-client": "^1.7.4",
104-
"standard": "^10.0.3",
105-
"steal": "^1.12.5",
106-
"steal-mocha": "^1.0.0",
107-
"testee": "^0.7.0",
108-
"vue": "^2.5.17",
104+
"mocha": "^5.2.0",
105+
"shx": "^0.3.2",
106+
"socket.io-client": "^2.2.0",
107+
"standard": "^12.0.1",
108+
"steal": "^2.1.13",
109+
"steal-mocha": "^2.0.1",
110+
"testee": "^0.8.1",
111+
"vue": "^2.5.22",
109112
"vuex": "^3.0.1"
110113
}
111114
}

src/FeathersVuexFind.js

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
export default {
2+
props: {
3+
service: {
4+
type: String,
5+
required: true
6+
},
7+
query: {
8+
type: Object,
9+
default: null
10+
},
11+
queryWhen: {
12+
type: [ Boolean, Function ],
13+
default: true
14+
},
15+
// If a separate query is desired to fetch data, use fetchQuery
16+
// The watchers will automatically be updated, so you don't have to write 'fetchQuery.propName'
17+
fetchQuery: {
18+
type: Object
19+
},
20+
watch: {
21+
type: [ String, Array ],
22+
default () {
23+
return []
24+
}
25+
},
26+
local: {
27+
type: Boolean,
28+
default: false
29+
},
30+
editScope: {
31+
type: Function,
32+
default (scope) {
33+
return scope
34+
}
35+
},
36+
qid: {
37+
type: String,
38+
default () {
39+
return randomString(10)
40+
}
41+
}
42+
},
43+
data: () => ({
44+
isFindPending: false
45+
}),
46+
computed: {
47+
items () {
48+
const { query, service, $store } = this
49+
50+
return query ? $store.getters[`${service}/find`]({ query }).data : []
51+
},
52+
pagination () {
53+
return this.$store.state[this.service].pagination[this.qid]
54+
},
55+
scope () {
56+
const { items, isFindPending, pagination } = this
57+
const defaultScope = { isFindPending, pagination, items }
58+
59+
return this.editScope(defaultScope) || defaultScope
60+
}
61+
},
62+
methods: {
63+
findData () {
64+
const query = this.fetchQuery || this.query
65+
66+
if (typeof this.queryWhen === 'function' ? this.queryWhen(this.query) : this.queryWhen) {
67+
this.isFindPending = true
68+
69+
if (query) {
70+
const params = { query }
71+
72+
if (this.qid) {
73+
params.qid = params.qid || this.qid
74+
}
75+
return this.$store.dispatch(`${this.service}/find`, params)
76+
.then(() => {
77+
this.isFindPending = false
78+
})
79+
}
80+
}
81+
},
82+
fetchData () {
83+
if (!this.local) {
84+
if (this.query) {
85+
return this.findData()
86+
} else {
87+
// TODO: access debug boolean from from the store config, somehow.
88+
console.log(`No query and no id provided, so no data will be fetched.`)
89+
}
90+
}
91+
}
92+
},
93+
created () {
94+
if (!this.$FeathersVuex) {
95+
throw new Error(`You must first Vue.use the FeathersVuex plugin before using the 'feathers-vuex-find' component.`)
96+
}
97+
if (!this.$store.state[this.service]) {
98+
throw new Error(`The '${this.service}' plugin cannot be found. Did you register the service with feathers-vuex?`)
99+
}
100+
101+
const watch = Array.isArray(this.watch) ? this.watch : [ this.watch ]
102+
103+
if (this.fetchQuery || this.query) {
104+
watch.forEach(prop => {
105+
if (typeof prop !== 'string') {
106+
throw new Error(`Values in the 'watch' array must be strings.`)
107+
}
108+
if (this.fetchQuery) {
109+
if (prop.startsWith('query')) {
110+
prop.replace('query', 'fetchQuery')
111+
}
112+
}
113+
this.$watch(prop, this.fetchData)
114+
})
115+
116+
this.fetchData()
117+
}
118+
},
119+
render () {
120+
return this.$scopedSlots.default(this.scope)
121+
}
122+
}
123+
124+
function randomString (length) {
125+
let text = ''
126+
let possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
127+
128+
for (let i = 0; i < length; i++) {
129+
text += possible.charAt(Math.floor(Math.random() * possible.length))
130+
}
131+
132+
return text
133+
}

src/FeathersVuexGet.js

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
export default {
2+
props: {
3+
service: {
4+
type: String,
5+
required: true
6+
},
7+
query: {
8+
type: Object,
9+
default: null
10+
},
11+
queryWhen: {
12+
type: [ Boolean, Function ],
13+
default: true
14+
},
15+
// For get requests
16+
id: {
17+
type: [ Number, String ],
18+
default: null
19+
},
20+
// If a separate query is desired to fetch data, use fetchQuery
21+
// The watchers will automatically be updated, so you don't have to write 'fetchQuery.propName'
22+
fetchQuery: {
23+
type: Object
24+
},
25+
watch: {
26+
type: [ String, Array ],
27+
default () {
28+
return []
29+
}
30+
},
31+
local: {
32+
type: Boolean,
33+
default: false
34+
},
35+
editScope: {
36+
type: Function,
37+
default (scope) {
38+
return scope
39+
}
40+
}
41+
},
42+
data: () => ({
43+
isFindPending: false,
44+
isGetPending: false
45+
}),
46+
computed: {
47+
item () {
48+
const getArgs = this.getArgs(this.query)
49+
50+
if (this.id) {
51+
return this.$store.getters[`${this.service}/get`](getArgs.length === 1 ? this.id : getArgs) || null
52+
} else {
53+
return null
54+
}
55+
},
56+
scope () {
57+
const { item, isGetPending } = this
58+
const defaultScope = { item, isGetPending }
59+
60+
return this.editScope(defaultScope) || defaultScope
61+
}
62+
},
63+
methods: {
64+
getArgs (queryToUse) {
65+
const query = queryToUse || this.fetchQuery || this.query
66+
const getArgs = [this.id]
67+
68+
if (query && Object.keys(query).length > 0) {
69+
getArgs.push({ query })
70+
}
71+
72+
return getArgs
73+
},
74+
getData () {
75+
const getArgs = this.getArgs()
76+
77+
if (typeof this.queryWhen === 'function' ? this.queryWhen(...getArgs) : this.queryWhen) {
78+
this.isGetPending = true
79+
80+
if (this.id) {
81+
return this.$store.dispatch(`${this.service}/get`, getArgs.length === 1 ? this.id : getArgs)
82+
.then(() => {
83+
this.isGetPending = false
84+
})
85+
}
86+
}
87+
},
88+
fetchData () {
89+
if (!this.local) {
90+
if (this.id !== null && this.id !== undefined && !this.query && !this.fetchQuery) {
91+
return this.getData()
92+
} else {
93+
console.log(`No query and no id provided, so no data will be fetched.`)
94+
}
95+
}
96+
}
97+
},
98+
created () {
99+
if (!this.$FeathersVuex) {
100+
throw new Error(`You must first Vue.use the FeathersVuex plugin before using the 'feathers-vuex-get' component.`)
101+
}
102+
if (!this.$store.state[this.service]) {
103+
throw new Error(`The '${this.service}' plugin cannot be found. Did you register the service with feathers-vuex?`)
104+
}
105+
106+
const watch = Array.isArray(this.watch) ? this.watch : [ this.watch ]
107+
108+
if (this.fetchQuery || this.query || (this.id !== null && this.id !== undefined)) {
109+
watch.forEach(prop => {
110+
if (typeof prop !== 'string') {
111+
throw new Error(`Values in the 'watch' array must be strings.`)
112+
}
113+
if (this.fetchQuery) {
114+
if (prop.startsWith('query')) {
115+
prop.replace('query', 'fetchQuery')
116+
}
117+
}
118+
this.$watch(prop, this.fetchData)
119+
})
120+
121+
this.fetchData()
122+
}
123+
},
124+
render () {
125+
return this.$scopedSlots.default(this.scope)
126+
}
127+
}

src/auth-module/actions.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ export default function makeAuthActions (feathersClient, globalModels) {
1818
commit('setPayload', payload)
1919

2020
// Populate the user if the userService was provided
21-
if (state.userService && payload.hasOwnProperty('userId')) {
22-
return dispatch('populateUser', payload.userId)
21+
if (state.userService && payload.hasOwnProperty(state.entityIdField)) {
22+
return dispatch('populateUser', payload[state.entityIdField])
2323
.then(() => {
2424
commit('unsetAuthenticatePending')
2525
return response
@@ -49,7 +49,7 @@ export default function makeAuthActions (feathersClient, globalModels) {
4949
})
5050
},
5151

52-
logout ({commit}) {
52+
logout ({ commit }) {
5353
commit('setLogoutPending')
5454
return feathersClient.logout()
5555
.then(response => {

src/auth-module/auth-module.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import setupActions from './actions'
66
const defaults = {
77
namespace: 'auth',
88
userService: '', // Set this to automatically populate the user (using an additional request) on login success.
9-
state: {}, // for custom state
10-
getters: {}, // for custom getters
9+
state: {}, // for custom state
10+
getters: {}, // for custom getters
1111
mutations: {}, // for custom mutations
12-
actions: {} // for custom actions
12+
actions: {} // for custom actions
1313
}
1414

1515
export default function authPluginInit (feathersClient, globalOptions = {}, globalModels = {}) {

src/auth-module/state.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export default function setupAuthState ({ userService }) {
22
const state = {
33
accessToken: null, // The JWT
44
payload: null, // The JWT payload
5+
entityIdField: 'userId',
56

67
isAuthenticatePending: false,
78
isLogoutPending: false,

0 commit comments

Comments
 (0)