Skip to content

Commit

Permalink
Move query key definition out of parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
jstayton committed Jul 23, 2019
1 parent 1f19103 commit bf66b69
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 21 deletions.
3 changes: 2 additions & 1 deletion src/filterer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Filterer {
}

get queryKey() {
return FilterParser.QUERY_KEY
return 'filter'
}

get query() {
Expand Down Expand Up @@ -42,6 +42,7 @@ class Filterer {
parse() {
if (!this._filters) {
const parser = new FilterParser(
this.queryKey,
this.query || this.querier.defaultFilter,
this.querier.schema,
{
Expand Down
3 changes: 2 additions & 1 deletion src/pager.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Pager {
}

get queryKey() {
return PageParser.QUERY_KEY
return 'page'
}

get query() {
Expand Down Expand Up @@ -50,6 +50,7 @@ class Pager {
this._page = false
} else if (this._page === null) {
const parser = new PageParser(
this.queryKey,
this.query || this.querier.defaultPage,
this.querier.schema,
this.querier.pageDefaults
Expand Down
9 changes: 3 additions & 6 deletions src/parsers/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ const NotImplementedError = require('../errors/not_implemented')
const ValidationError = require('../errors/validation')

class BaseParser {
constructor(query, schema, defaults = {}) {
constructor(queryKey, query, schema, defaults = {}) {
this.queryKey = queryKey
this.query = query
this.schema = schema
this.defaults = defaults
}

static get QUERY_KEY() {
throw new NotImplementedError()
}

buildValidationSchema(schema) {
throw new NotImplementedError()
}
Expand Down Expand Up @@ -49,7 +46,7 @@ class BaseParser {
const path = detail.path.reduce(
(accumulator, value, index) =>
index === 0 ? `${accumulator}:${value}` : `${accumulator}[${value}]`,
this.constructor.QUERY_KEY
this.queryKey
)

const message = detail.message.replace(/^".*?" /, '')
Expand Down
4 changes: 0 additions & 4 deletions src/parsers/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ const is = require('is')
const BaseParser = require('./base')

class FilterParser extends BaseParser {
static get QUERY_KEY() {
return 'filter'
}

static get DEFAULTS() {
return {
field: null,
Expand Down
4 changes: 0 additions & 4 deletions src/parsers/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ const is = require('is')
const BaseParser = require('./base')

class PageParser extends BaseParser {
static get QUERY_KEY() {
return 'page'
}

static get DEFAULTS() {
return {
size: 20,
Expand Down
4 changes: 0 additions & 4 deletions src/parsers/sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ const is = require('is')
const BaseParser = require('./base')

class SortParser extends BaseParser {
static get QUERY_KEY() {
return 'sort'
}

static get DEFAULTS() {
return {
field: null,
Expand Down
3 changes: 2 additions & 1 deletion src/sorter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Sorter {
}

get queryKey() {
return SortParser.QUERY_KEY
return 'sort'
}

get query() {
Expand Down Expand Up @@ -42,6 +42,7 @@ class Sorter {
parse() {
if (!this._sorts) {
const parser = new SortParser(
this.queryKey,
this.query || this.querier.defaultSort,
this.querier.schema,
this.querier.sortDefaults
Expand Down

0 comments on commit bf66b69

Please sign in to comment.