Comparing version 0.5.0 to 0.6.0
@@ -11,2 +11,3 @@ /** | ||
body: request.body || {}, | ||
errorBody: null, | ||
params: request.params || {}, | ||
@@ -13,0 +14,0 @@ query: request.query || {}, |
@@ -9,15 +9,28 @@ /** | ||
const { extractFromRequest } = require('./container'); | ||
const PipelineError = require('./PipelineError'); | ||
const executeChunk = async (chunk, originalContainer, req) => { | ||
let container = originalContainer; | ||
for (let i = 0; i < chunk.length; i += 1) { | ||
if (Array.isArray(chunk[i])) { | ||
// eslint-disable-next-line no-await-in-loop | ||
container = await executeChunk(chunk[i], container, req); | ||
} else { | ||
// eslint-disable-next-line no-await-in-loop | ||
container = await chunk[i](container, req, executeChunk); | ||
try { | ||
for (let i = 0; i < chunk.length; i += 1) { | ||
if (Array.isArray(chunk[i])) { | ||
// eslint-disable-next-line no-await-in-loop | ||
container = await executeChunk(chunk[i], container, req); | ||
} else { | ||
// eslint-disable-next-line no-await-in-loop | ||
container = await chunk[i](container, req, executeChunk); | ||
} | ||
} | ||
return container; | ||
} catch (err) { | ||
let error = err; | ||
if (!(error instanceof PipelineError)) { | ||
error = new PipelineError(err.message); | ||
error.setContainer({ ...container, statusCode: 500 }); | ||
} | ||
if (!error.container) { | ||
error.setContainer(container); | ||
} | ||
throw error; | ||
} | ||
return container; | ||
}; | ||
@@ -27,7 +40,20 @@ | ||
let container = extractFromRequest(req); | ||
container = await executeChunk(beforeEach, container, req); | ||
container = await executeChunk(route.pipeline, container, req); | ||
res.status(container.statusCode).send(container.body); | ||
try { | ||
container = await executeChunk(beforeEach, container, req); | ||
container = await executeChunk(route.pipeline, container, req); | ||
res.status(container.statusCode).send(container.body); | ||
} catch (err) { | ||
if (route.onError) { | ||
container = route.onError(err); | ||
res.status(container.statusCode).send(container.body); | ||
return; | ||
} | ||
if (err.container.errorBody) { | ||
res.status(err.container.statusCode).send(err.container.errorBody); | ||
return; | ||
} | ||
res.sendStatus(err.container.statusCode); | ||
} | ||
}; | ||
module.exports = { execute }; |
@@ -8,17 +8,25 @@ /** | ||
const { fromJS } = require('immutable'); | ||
const { cloneDeep, merge, set } = require('lodash'); | ||
const PipelineError = require('../entities/PipelineError'); | ||
const request = require('../services/request'); | ||
const urlBuilder = require('../services/urlBuilder'); | ||
module.exports = (method, url, key) => { | ||
module.exports = (method, url, path) => { | ||
const buildedUrl = urlBuilder(url); | ||
return async (container) => { | ||
const { body, statusCode } = await request(container)[method](buildedUrl); | ||
const containerMap = fromJS(container); | ||
if (!key) { | ||
return containerMap.mergeDeep(fromJS({ body, statusCode })).toJS(); | ||
try { | ||
const { body, statusCode } = await request(container)[method](buildedUrl); | ||
if (path) { | ||
return merge(cloneDeep(container), { statusCode, body: set({}, path, body) }); | ||
} | ||
return merge(cloneDeep(container), { statusCode, body }); | ||
} catch (err) { | ||
const error = new PipelineError(err, err.response); | ||
error.setContainer({ | ||
...container, | ||
errorBody: err.response.body, | ||
}); | ||
throw error; | ||
} | ||
return containerMap.mergeDeep(fromJS({ body: { [key]: body }, statusCode })).toJS(); | ||
}; | ||
}; |
@@ -8,10 +8,7 @@ /** | ||
const { fromJS } = require('immutable'); | ||
const { cloneDeep, pick } = require('lodash'); | ||
module.exports = properties => (container) => { | ||
const containerMap = fromJS(container); | ||
return containerMap.update( | ||
'body', | ||
map => map.filter((_, key) => properties.includes(key)), | ||
).toJS(); | ||
}; | ||
module.exports = paths => container => ({ | ||
...cloneDeep(container), | ||
body: pick(container.body, paths), | ||
}); |
@@ -10,4 +10,7 @@ /** | ||
const filter = require('./filter'); | ||
const forwardedhost = require('./forwardedhost'); | ||
const waitfor = require('./waitfor'); | ||
const forwardedHost = require('./forwardedHost'); | ||
const mergeBody = require('./mergeBody'); | ||
const routeMatch = require('./routeMatch'); | ||
const statusCode = require('./statusCode'); | ||
const waitFor = require('./waitFor'); | ||
@@ -17,4 +20,7 @@ module.exports = { | ||
filter, | ||
forwardedhost, | ||
waitfor, | ||
forwardedHost, | ||
mergeBody, | ||
routeMatch, | ||
statusCode, | ||
waitFor, | ||
}; |
{ | ||
"name": "nodegate", | ||
"description": "API gateway made simple, fast and easy to configure.", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"author": "Julien Martin <martin.julien82@gmail.com>", | ||
@@ -17,3 +17,3 @@ "license": "MIT", | ||
"express": "^4.16.4", | ||
"immutable": "^4.0.0-rc.12", | ||
"lodash": "^4.17.11", | ||
"request": "^2.88.0", | ||
@@ -20,0 +20,0 @@ "request-promise-native": "^1.0.7" |
![nodegate](assets/images/logo-readme.png) | ||
The easy plugin to programmatically manage a JSON API Gateway. Builded on top of | ||
The easy plugin to programmatically manage a horizontally scalable JSON API Gateway. Builded on top of | ||
[Express](url-express) & [Request](url-request). | ||
@@ -5,0 +5,0 @@ |
@@ -20,3 +20,3 @@ /** | ||
modifiers: { | ||
waitfor: { | ||
waitFor: { | ||
delay: 300, | ||
@@ -23,0 +23,0 @@ tentatives: 10, |
@@ -8,4 +8,6 @@ /** | ||
const at = require('lodash/at'); | ||
const getVariables = (url) => { | ||
const regExp = /({([a-zA-Z0-9.]+)})/gm; | ||
const regExp = /({([a-zA-Z0-9.[\]]+)})/gm; | ||
const matches = []; | ||
@@ -16,3 +18,3 @@ let match; | ||
if (match) { | ||
matches.push(match[2].split('.')); | ||
matches.push(match[2]); | ||
} | ||
@@ -31,3 +33,3 @@ } while (match !== null); | ||
details.parts = details.parts.concat( | ||
part.split(`{${variable.join('.')}}`), | ||
part.split(`{${variable}}`), | ||
); | ||
@@ -39,11 +41,9 @@ }); | ||
const getValue = (container, path) => path.reduce( | ||
(value, current) => { | ||
if (value[current]) { | ||
return value[current]; | ||
} | ||
throw new Error(`Missing value for {${path.join('.')}}`); | ||
}, | ||
container, | ||
); | ||
const getValue = (container, path) => { | ||
const value = at(container, path)[0]; | ||
if (value === undefined) { | ||
throw new Error(`Missing value for {${path}}`); | ||
} | ||
return value; | ||
}; | ||
@@ -50,0 +50,0 @@ const mergeUrl = (container, parsedUrl) => { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17679
19
423
+ Addedlodash@^4.17.11
- Removedimmutable@^4.0.0-rc.12
- Removedimmutable@4.3.7(transitive)