Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance paramsForServer with parameters with specific values. #607

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "feathers-vuex",
"description": "FeathersJS, Vue, and Nuxt for the artisan developer",
"version": "3.16.0",
"version": "3.17.1-0",
"homepage": "https:feathers-vuex.feathersjs-ecosystem.com",
"main": "dist/",
"module": "dist/",
Expand Down
51 changes: 42 additions & 9 deletions src/service-module/service-module.getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,32 @@ export default function makeServiceGetters() {
idField,
tempsById
} = state
const q = _omit(params.query || {}, paramsForServer)

const paramsForServerByValue = paramsForServer.filter(el =>
Array.isArray(el)
)

const q = paramsForServerByValue.reduce(
(acc, [key, valueOrPredicate]) => {
if (!acc[key] || (acc[key] && !valueOrPredicate)) return acc

if (
((typeof valueOrPredicate === 'string' ||
typeof valueOrPredicate === 'number') &&
acc[key] === valueOrPredicate) ||
(typeof valueOrPredicate === 'function' &&
valueOrPredicate(acc[key]))
) {
return _omit(acc, key)
}

return acc
},
_omit(
params.query || {},
paramsForServer.filter(el => typeof el === 'string')
)
)

const { query, filters } = filterQuery(q, {
operators: additionalOperators.concat(whitelist)
Expand Down Expand Up @@ -114,14 +139,22 @@ export default function makeServiceGetters() {
return copiesById[id]
},

isCreatePendingById: ({ isIdCreatePending }: ServiceState) => (id: Id) =>
isIdCreatePending.includes(id),
isUpdatePendingById: ({ isIdUpdatePending }: ServiceState) => (id: Id) =>
isIdUpdatePending.includes(id),
isPatchPendingById: ({ isIdPatchPending }: ServiceState) => (id: Id) =>
isIdPatchPending.includes(id),
isRemovePendingById: ({ isIdRemovePending }: ServiceState) => (id: Id) =>
isIdRemovePending.includes(id),
isCreatePendingById:
({ isIdCreatePending }: ServiceState) =>
(id: Id) =>
isIdCreatePending.includes(id),
isUpdatePendingById:
({ isIdUpdatePending }: ServiceState) =>
(id: Id) =>
isIdUpdatePending.includes(id),
isPatchPendingById:
({ isIdPatchPending }: ServiceState) =>
(id: Id) =>
isIdPatchPending.includes(id),
isRemovePendingById:
({ isIdRemovePending }: ServiceState) =>
(id: Id) =>
isIdRemovePending.includes(id),
isSavePendingById: (state: ServiceState, getters) => (id: Id) =>
getters.isCreatePendingById(id) ||
getters.isUpdatePendingById(id) ||
Expand Down
39 changes: 39 additions & 0 deletions test/service-module/service-module.getters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,45 @@ describe('Service Module - Getters', function () {
assert(results.total === 1, 'total was correct')
})

it('find with paramsForServer option with specific value as string', function () {
const { state } = this
state.paramsForServer = [['_$client', '1']]
const params = { query: { test: false, _$client: '1' } }
const results = find(state)(params)

assert(results.data.length === 1, 'the length was correct')
assert(results.data[0]._id === 3, 'the correct record was returned')
assert(results.limit === 0, 'limit was correct')
assert(results.skip === 0, 'skip was correct')
assert(results.total === 1, 'total was correct')
})

it('find with paramsForServer option with specific value as number', function () {
const { state } = this
state.paramsForServer = [['_$client', -1]]
const params = { query: { test: false, _$client: -1 } }
const results = find(state)(params)

assert(results.data.length === 1, 'the length was correct')
assert(results.data[0]._id === 3, 'the correct record was returned')
assert(results.limit === 0, 'limit was correct')
assert(results.skip === 0, 'skip was correct')
assert(results.total === 1, 'total was correct')
})

it('find with paramsForServer option with specific value as function', function () {
const { state } = this
state.paramsForServer = [['_$client', value => value === 1]]
const params = { query: { test: false, _$client: 1 } }
const results = find(state)(params)

assert(results.data.length === 1, 'the length was correct')
assert(results.data[0]._id === 3, 'the correct record was returned')
assert(results.limit === 0, 'limit was correct')
assert(results.skip === 0, 'skip was correct')
assert(results.total === 1, 'total was correct')
})

it('find with non-whitelisted custom operator fails', function () {
const { state } = this
const params = { query: { $client: 'test' } }
Expand Down