From 0224927eeaad7cfd1f20f0c649766a9031476072 Mon Sep 17 00:00:00 2001 From: Tomer Shalev Date: Fri, 19 Jul 2019 23:09:25 +0300 Subject: [PATCH] Improve coding style --- src/index.js | 69 +++++++++++----------------- src/middleware/json-api/req-patch.js | 16 +++---- src/middleware/json-api/req-post.js | 15 ++---- 3 files changed, 39 insertions(+), 61 deletions(-) diff --git a/src/index.js b/src/index.js index be8d27c..7e8a715 100644 --- a/src/index.js +++ b/src/index.js @@ -258,14 +258,16 @@ class JsonApi { } insertMiddleware (middlewareName, direction, newMiddleware) { - let middleware = this.middleware.filter(middleware => (middleware.name === middlewareName)) - if (middleware.length > 0) { - let index = this.middleware.indexOf(middleware[0]) - if (direction === 'after') { - index = index + 1 - } - this.middleware.splice(index, 0, newMiddleware) + let middleware = this.middleware + .filter(middleware => (middleware.name === middlewareName)) + if (middleware.length === 0) { + return + } + let index = this.middleware.indexOf(middleware[0]) + if (direction === 'after') { + index = index + 1 } + this.middleware.splice(index, 0, newMiddleware) } replaceMiddleware (middlewareName, newMiddleware) { @@ -284,46 +286,31 @@ class JsonApi { this.middleware = this._originalMiddleware.slice(0) } - applyRequestMiddleware (promise) { - let requestMiddlewares = this.middleware.filter(middleware => middleware.req) - requestMiddlewares.forEach((middleware) => { - promise = promise.then(middleware.req) - }) - return promise - } - - applyResponseMiddleware (promise) { - let responseMiddleware = this.middleware.filter(middleware => middleware.res) - responseMiddleware.forEach((middleware) => { - promise = promise.then(middleware.res) - }) - return promise - } - - applyErrorMiddleware (promise) { - let errorsMiddleware = this.middleware.filter(middleware => middleware.error) - errorsMiddleware.forEach((middleware) => { - promise = promise.then(middleware.error) - }) + applyMiddleware (method, promise) { + console.assert(['req', 'res', 'error'].includes(method)) + this.middleware + .filter(middleware => middleware[method]) + .forEach(middleware => { promise = promise.then(middleware[method]) }) return promise } runMiddleware (req) { - let payload = {req: req, jsonApi: this} + let payload = {req, jsonApi: this} let requestPromise = Promise.resolve(payload) - requestPromise = this.applyRequestMiddleware(requestPromise) - return requestPromise + + return this.applyMiddleware('req', requestPromise) .then((res) => { payload.res = res let responsePromise = Promise.resolve(payload) - return this.applyResponseMiddleware(responsePromise) + return this.applyMiddleware('res', responsePromise) }) .catch((err) => { Logger.error(err) let errorPromise = Promise.resolve(err) - return this.applyErrorMiddleware(errorPromise).then(err => { - return Promise.reject(err) - }) + return this.applyMiddleware('error', errorPromise) + .then(err => { + return Promise.reject(err) + }) }) } @@ -338,7 +325,7 @@ class JsonApi { url: this.urlFor({model: modelName, id: id}), model: modelName, data: {}, - params: params + params } return this.runMiddleware(req) } @@ -348,7 +335,7 @@ class JsonApi { method: 'GET', url: this.urlFor({model: modelName}), model: modelName, - params: params, + params, data: {} } return this.runMiddleware(req) @@ -359,9 +346,9 @@ class JsonApi { method: 'POST', url: this.urlFor({model: modelName}), model: modelName, - params: params, + params, data: payload, - meta: meta + meta } return this.runMiddleware(req) } @@ -372,8 +359,8 @@ class JsonApi { url: this.urlFor({model: modelName, id: payload.id}), model: modelName, data: payload, - params: params, - meta: meta + params, + meta } return this.runMiddleware(req) } diff --git a/src/middleware/json-api/req-patch.js b/src/middleware/json-api/req-patch.js index ff6143b..acb9d02 100644 --- a/src/middleware/json-api/req-patch.js +++ b/src/middleware/json-api/req-patch.js @@ -10,18 +10,14 @@ module.exports = { 'Content-Type': 'application/vnd.api+json', 'Accept': 'application/vnd.api+json' } - if (payload.req.data.constructor === Array) { - payload.req.data = { - data: serialize.collection.call(jsonApi, payload.req.model, payload.req.data), - meta: payload.req.meta - } - } else { - payload.req.data = { - data: serialize.resource.call(jsonApi, payload.req.model, payload.req.data), - meta: payload.req.meta - } + const serializer = payload.req.data.constructor === Array ? serialize.collection : serialize.resource + + payload.req.data = { + data: serializer.call(jsonApi, payload.req.model, payload.req.data), + meta: payload.req.meta } } + return payload } } diff --git a/src/middleware/json-api/req-post.js b/src/middleware/json-api/req-post.js index 8677efa..a438dee 100644 --- a/src/middleware/json-api/req-post.js +++ b/src/middleware/json-api/req-post.js @@ -10,16 +10,11 @@ module.exports = { 'Content-Type': 'application/vnd.api+json', 'Accept': 'application/vnd.api+json' } - if (payload.req.data.constructor === Array) { - payload.req.data = { - data: serialize.collection.call(jsonApi, payload.req.model, payload.req.data), - meta: payload.req.meta - } - } else { - payload.req.data = { - data: serialize.resource.call(jsonApi, payload.req.model, payload.req.data), - meta: payload.req.meta - } + const serializer = payload.req.data.constructor === Array ? serialize.collection : serialize.resource + + payload.req.data = { + data: serializer.call(jsonApi, payload.req.model, payload.req.data), + meta: payload.req.meta } }