Skip to content

v0.6.0

Compare
Choose a tag to compare
@icebob icebob released this 04 Jan 12:25
· 491 commits to master since this release

Breaking changes

Alias custom function arguments is changed

The route first argument is removed. The new signature of function is function(req, res) {}. To access to route use the req.$route property.
However you can use an array of Function for aliases. With it you can call middlewares. In this case the third argument is next. I.e.: function(req, res, next) {}.

Other changes

  • better error handling. Always returns with JSON error response.
  • The charset is UTF-8 for application/json responses.
  • logRequestParams setting to log the request parameters. Use log level value i.e. "debug", "info" or null to disable.
  • logResponseData setting to log the response data. Use log level value i.e. "debug", "info" or null to disable.
  • req.$service & res.$service is pointed to the service instance.
  • req.$route & res.$route is pointed to the route definition.
  • req.$params is pointed to the resolved parameters (from query string & post body)
  • req.$alias is pointed to the alias definition.
  • req.$endpoint is pointed to the resolved action endpoint. It contains action and nodeID.

Middlewares

Support middlewares in global, routes & aliases.

broker.createService({
    mixins: [ApiService],
    settings: {
        // Global middlewares. Applied to all routes.
        use: [
            cookieParser(),
            helmet()
        ],

        routes: [
            {
                path: "/",

                // Route-level middlewares.
                use: [
                    compression(),
                    
                    passport.initialize(),
                    passport.session(),

                    serveStatic(path.join(__dirname, "public"))
                ],
                
                aliases: {
                    "GET /secret": [
                        // Alias-level middlewares.
                        auth.isAuthenticated(),
                        auth.hasRole("admin"),
                        "top.secret" // Call the `top.secret` action
                    ]
                }
            }
        ]
    }
});

Custom response headers

It supports custom response headers to define in action definition.

module.exports = {
    name: "export",
    actions: {
        downloadCSV: 
            responseHeaders: {
                "Content-Disposition": "attachment; filename=\"data.csv\"",
                "Content-Type": "text/csv"
            },
            handler() {
                return "...";
            }
        }
    }
}

Error handlers

You can add route & global custom error handlers.

broker.createService({
    mixins: [ApiService],
    settings: {

        routes: [{
            path: "/api",

            // Route error handler
            onError(req, res, err) {
                res.setHeader("Content-Type", "application/json; charset=utf-8");
                res.writeHead(500);
                res.end(JSON.stringify(err));
            }
        }],

        // Global error handler
        onError(req, res, err) {
            res.setHeader("Content-Type", "text/plain");
            res.writeHead(501);
            res.end("Global error: " + err.message);
        }		
    }
}

New examples to serve client-side developing with Webpack