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

Improve coding style #194

Open
wants to merge 1 commit 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
69 changes: 28 additions & 41 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks like debug-code 😉

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)
})
})
}

Expand All @@ -338,7 +325,7 @@ class JsonApi {
url: this.urlFor({model: modelName, id: id}),
model: modelName,
data: {},
params: params
params
}
return this.runMiddleware(req)
}
Expand All @@ -348,7 +335,7 @@ class JsonApi {
method: 'GET',
url: this.urlFor({model: modelName}),
model: modelName,
params: params,
params,
data: {}
}
return this.runMiddleware(req)
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
16 changes: 6 additions & 10 deletions src/middleware/json-api/req-patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
15 changes: 5 additions & 10 deletions src/middleware/json-api/req-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down