Comparing version 0.5.1 to 1.0.0
86
index.js
@@ -1,20 +0,20 @@ | ||
const http = require('http'); | ||
const http = require('http') | ||
const methods = ['get', 'delete', 'patch', 'post', 'put', 'head', 'options']; | ||
const methods = ['get', 'delete', 'patch', 'post', 'put', 'head', 'options'] | ||
const extensions = { | ||
request: {}, | ||
response: require('./libs/response-extensions') | ||
}; | ||
const URL = require('url'); | ||
} | ||
const URL = require('url') | ||
module.exports = (options = {}) => { | ||
const server = options.server || http.createServer(); | ||
const server = options.server || http.createServer() | ||
server.on('request', (req, res) => { | ||
app.handler(req, res); | ||
}); | ||
const wayfarer = require('wayfarer')('/404'); | ||
wayfarer.on('/404', () => 404); | ||
app.handler(req, res) | ||
}) | ||
const wayfarer = require('wayfarer')('/404') | ||
wayfarer.on('/404', () => 404) | ||
const routes = {}; | ||
const middlewares = []; | ||
const routes = {} | ||
const middlewares = [] | ||
@@ -26,6 +26,6 @@ const app = { | ||
context | ||
}); | ||
}) | ||
}, | ||
route: (method, path, handler, ctx = {}) => { | ||
const key = `[${method.toUpperCase()}]${path}`; | ||
const key = `[${method.toUpperCase()}]${path}` | ||
if (!routes[key]) { | ||
@@ -37,30 +37,30 @@ routes[key] = { | ||
ctx | ||
}; | ||
} | ||
wayfarer.on(key, (params, req, res) => { | ||
try { | ||
req.params = params; | ||
const result = routes[key].handler.call(ctx, req, res, ctx); | ||
req.params = params | ||
const result = routes[key].handler.call(ctx, req, res, ctx) | ||
if (result instanceof Promise) { | ||
// async support | ||
result.catch(res.send); | ||
result.catch(res.send) | ||
} | ||
} catch (err) { | ||
res.send(err); | ||
res.send(err) | ||
} | ||
}); | ||
}) | ||
} else { | ||
routes[key].ctx = ctx; | ||
routes[key].handler = handler; | ||
routes[key].ctx = ctx | ||
routes[key].handler = handler | ||
} | ||
return routes[key]; | ||
return routes[key] | ||
}, | ||
handler: (req, res) => { | ||
for (const method of Object.keys(extensions.response)) { | ||
res[method] = extensions.response[method](req, res); | ||
res[method] = extensions.response[method](req, res) | ||
} | ||
const url = URL.parse(req.url); | ||
req.path = url.path; | ||
req.query = url.query; | ||
req.search = url.search; | ||
const url = URL.parse(req.url) | ||
req.path = url.path | ||
req.query = url.query | ||
req.search = url.search | ||
@@ -74,8 +74,8 @@ // calling middlewares | ||
handler: (req, res, next) => { | ||
const route = `[${req.method.toUpperCase()}]${req.path}`; | ||
const route = `[${req.method.toUpperCase()}]${req.path}` | ||
res.on('response', () => { | ||
next(); | ||
}); | ||
next() | ||
}) | ||
if (wayfarer(route, req, res) === 404) res.send(404); | ||
if (wayfarer(route, req, res) === 404) res.send(404) | ||
} | ||
@@ -86,3 +86,3 @@ } | ||
res | ||
)(); | ||
)() | ||
}, | ||
@@ -92,5 +92,5 @@ start: (port = 3000, host) => | ||
server.listen(port, host, (err) => { | ||
if (err) reject(err); | ||
resolve(server); | ||
}); | ||
if (err) reject(err) | ||
resolve(server) | ||
}) | ||
}), | ||
@@ -100,13 +100,13 @@ close: () => | ||
server.close((err) => { | ||
if (err) reject(err); | ||
resolve(); | ||
}); | ||
if (err) reject(err) | ||
resolve() | ||
}) | ||
}), | ||
routes: () => Object.keys(routes) | ||
}; | ||
} | ||
methods.forEach((method) => { | ||
app[method] = (path, handler, ctx) => app.route(method, path, handler, ctx); | ||
}); | ||
app[method] = (path, handler, ctx) => app.route(method, path, handler, ctx) | ||
}) | ||
return app; | ||
}; | ||
return app | ||
} |
@@ -1,10 +0,8 @@ | ||
/* eslint consistent-return:0 */ | ||
const next = (middlewares, req, res) => { | ||
const middleware = middlewares.shift(); | ||
const middleware = middlewares.shift() | ||
return (err) => { | ||
if (err) return res.send(err); | ||
if (err) return res.send(err) | ||
if (res.statusCode === 200 && !res.finished) { | ||
if (!middleware) return; | ||
if (!middleware) return | ||
@@ -17,16 +15,16 @@ try { | ||
next(middlewares, req, res) | ||
); | ||
) | ||
if (result instanceof Promise) { | ||
// async support | ||
result.catch(res.send); | ||
result.catch(res.send) | ||
} | ||
} catch (err) { | ||
res.send(err); | ||
res.send(err) | ||
} | ||
} else if (!res.finished) { | ||
res.end(); | ||
res.end() | ||
} | ||
}; | ||
}; | ||
} | ||
} | ||
module.exports = next; | ||
module.exports = next |
module.exports.send = (req, res) => (data = 200, code = 200, headers = {}) => { | ||
res.setHeader('content-type', 'text/plain'); | ||
res.setHeader('content-type', 'text/plain') | ||
Object.keys(headers).forEach((key) => { | ||
res.setHeader(key.toLowerCase(), headers[key]); | ||
}); | ||
res.setHeader(key.toLowerCase(), headers[key]) | ||
}) | ||
return new Promise(async (resolve, reject) => { | ||
if (data instanceof Promise) data = await data; | ||
if (data instanceof Promise) data = await data | ||
if (data instanceof Error) { | ||
code = data.status || data.code || 500; | ||
code = data.status || data.code || 500 | ||
data = { | ||
@@ -16,11 +16,11 @@ code, | ||
data: data.data | ||
}; | ||
} | ||
} | ||
if (typeof data === 'number') { | ||
code = parseInt(data, 10); | ||
data = res.body; | ||
code = parseInt(data, 10) | ||
data = res.body | ||
} | ||
if (typeof data === 'object') { | ||
res.setHeader('content-type', 'application/json'); | ||
data = JSON.stringify(data); | ||
res.setHeader('content-type', 'application/json') | ||
data = JSON.stringify(data) | ||
} | ||
@@ -34,11 +34,11 @@ | ||
code | ||
}; | ||
res.emit('response', params); | ||
} | ||
res.emit('response', params) | ||
res.writeHead(code); | ||
res.writeHead(code) | ||
res.end(params.data, 'utf-8', (err) => { | ||
if (err) reject(err); | ||
resolve(); | ||
}); | ||
}); | ||
}; | ||
if (err) reject(err) | ||
resolve() | ||
}) | ||
}) | ||
} |
{ | ||
"name": "restana", | ||
"version": "0.5.1", | ||
"version": "1.0.0", | ||
"description": "Super fast and minimalist web framework for building REST micro-services.", | ||
@@ -42,12 +42,6 @@ "main": "index.js", | ||
"morgan": "1.x.x", | ||
"standard": "^11.0.1", | ||
"supertest": "3.x.x", | ||
"winston": "2.x.x", | ||
"eslint": "^4.7.0", | ||
"eslint-config-airbnb": "^15.1.0", | ||
"eslint-plugin-import": "^2.7.0", | ||
"eslint-plugin-jsx-a11y": "^5.1.1", | ||
"eslint-plugin-node": "^5.1.1", | ||
"eslint-plugin-promise": "^3.5.0", | ||
"eslint-plugin-react": "^7.3.0" | ||
"winston": "2.x.x" | ||
} | ||
} |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
6
0
9580
162