From bf66b69128500d8e683824f956d99c8008b6a766 Mon Sep 17 00:00:00 2001 From: Justin Stayton Date: Tue, 23 Jul 2019 10:24:30 -0400 Subject: [PATCH] Move query key definition out of parsers --- src/filterer.js | 3 ++- src/pager.js | 3 ++- src/parsers/base.js | 9 +++------ src/parsers/filter.js | 4 ---- src/parsers/page.js | 4 ---- src/parsers/sort.js | 4 ---- src/sorter.js | 3 ++- 7 files changed, 9 insertions(+), 21 deletions(-) diff --git a/src/filterer.js b/src/filterer.js index 5fe61e2..ae517dc 100644 --- a/src/filterer.js +++ b/src/filterer.js @@ -8,7 +8,7 @@ class Filterer { } get queryKey() { - return FilterParser.QUERY_KEY + return 'filter' } get query() { @@ -42,6 +42,7 @@ class Filterer { parse() { if (!this._filters) { const parser = new FilterParser( + this.queryKey, this.query || this.querier.defaultFilter, this.querier.schema, { diff --git a/src/pager.js b/src/pager.js index e0eaa44..7811098 100644 --- a/src/pager.js +++ b/src/pager.js @@ -8,7 +8,7 @@ class Pager { } get queryKey() { - return PageParser.QUERY_KEY + return 'page' } get query() { @@ -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 diff --git a/src/parsers/base.js b/src/parsers/base.js index 621d81d..e7c7b0e 100644 --- a/src/parsers/base.js +++ b/src/parsers/base.js @@ -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() } @@ -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(/^".*?" /, '') diff --git a/src/parsers/filter.js b/src/parsers/filter.js index b2c44d3..cd72068 100644 --- a/src/parsers/filter.js +++ b/src/parsers/filter.js @@ -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, diff --git a/src/parsers/page.js b/src/parsers/page.js index 06ae445..8dd6ef8 100644 --- a/src/parsers/page.js +++ b/src/parsers/page.js @@ -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, diff --git a/src/parsers/sort.js b/src/parsers/sort.js index c1de7bd..911b179 100644 --- a/src/parsers/sort.js +++ b/src/parsers/sort.js @@ -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, diff --git a/src/sorter.js b/src/sorter.js index 81d5026..bada0fa 100644 --- a/src/sorter.js +++ b/src/sorter.js @@ -8,7 +8,7 @@ class Sorter { } get queryKey() { - return SortParser.QUERY_KEY + return 'sort' } get query() { @@ -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