Skip to content

Instantly share code, notes, and snippets.

@bpinedah
bpinedah / res-function-send.js
Last active March 17, 2020 06:33
Routes article
send: function send(data) {
// this is an incomplete example
const resp = {
statusCode,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true
},
body: JSON.stringify(data)
@bpinedah
bpinedah / res-function.js
Created March 17, 2020 06:17
Routes article
/**
* @description Resolve function to exit from pipeline
* @param signal Flag to continue or not on pipeline
* @returns {*}
* @private
*/
const _res = signal => () => ({
statusCode: 200,
headers: {},
setHeader: function setHeader(header) {
app.post('/test', async (req, res, next) => {
req.body.auth = true;
return next(req);
}, async (req, res) => {
console.log("other func", req.body.auth); // true
return res.send(req.body.auth);
});
app.get('/test/(.*)', async (req, res, next) => {
req.test = { pre: true };
return next(req);
});
@bpinedah
bpinedah / next-function.js
Created March 17, 2020 05:38
Routes article
**
* @description Function to send to the next middleware function
* @param idx Current function index
* @param length Total of middlewares functions
* @private
*/
const _next = (idx, length) => e =>
Promise.resolve(idx === length - 1 && length > 2 ? e : [e]);
@bpinedah
bpinedah / pipeline-create.js
Created March 17, 2020 05:33
Routes article
/**
* @description Create function with middlewares
* @param f Middleware functions to execute
* @returns {*}
* @private
*/
const _createPipeline = f => {
const signal = { continue: true };
return f.length > 1 ? f.reduce((before, after, idx) =>
/**
* @description Get a flat array
* @param arr Array to flat
* @private
*/
const _toFlat = arr => [].concat(...arr);
const functions = routesFound.map(i => ([...i.middlewares]));
const fns = _toFlat(functions);
const findPredicate = this._findAndComposeRoute(method.toLowerCase(), routerPath);
const routesFound = this._routes.filter(findPredicate);
@bpinedah
bpinedah / serverless.yml
Created March 17, 2020 01:17
Router article
v2-app:
handler: router-test.main
events:
- http:
path: v2/{proxy+}
method: ANY
cors: true
authorizer: aws_iam
/**
* @description Function to generate handler for lambda
* @returns {function(*)}
*/
toLambda() {
return async event => {
// FUNCTIONS HELPERS
/**
* @description Get a flat array
* @param arr Array to flat