node-byuapi-framework
A serverless framework that uses Swagger documents to produce web service schemas. Easy to integrate into unit tests, servers, AWS lambdas, etc.
Basic Usage
Example
const byuApi = require('node-byuapi-framework');
const api = byuApi({
controllers: './controllers',
development: true,
swagger: './swagger.json'
});
api.request({ method: 'GET', path: '/v1/path/to/call' })
.then(function(res) {
console.log(res.statusCode);
console.log(res.body);
});
Configuration
The API instance is generated using a configuration with the following properties:
-
controllers - [REQUIRED] The directory path to JavaScript files that contain the methods to execute to fulfill web service requests.
-
development - [OPTIONAL] If true then mocks will be used automatically when a controller does not exist and not all controllers must exist, otherwise all controllers must exist. Defaults to false
.
-
ignoreBasePath - [OPTIONAL] If true then the swagger base path will not be used in the routes. Defaults to false
.
-
sansServer - [OPTIONAL] A configuration to pass to sans-server. Defaults to {}
.
-
sansServerRouter - [OPTIONAL] A configuration to pass to the sans-server-router instance that this middleware employs. Defaults to: { paramFormat: 'handlebar' }
.
-
swagger - [REQUIRED] The swagger file that defines the services. This can be either a json or a yaml file.
As Middleware
This middleware that this framework uses to route requests through swagger can also be used as middleware.
const byuApi = require('node-byuapi-framework');
const Server = require('sans-server');
const server = Server();
server.use(function(req, res, next) {
next();
});
const middlewareConfig = {
controllers: './controllers',
development: true,
sansServerRouter: { passThrough: true },
swagger: './swagger.json'
};
server.use(byuApi.midddleware(middlewareConfig));
server.use(function(req, res, next) {
res.status(404);
res.send('Could not find what you were looking for.');
});
server.request({ method: 'GET', path: '/some/path' })
.then(function(res) {
console.log(res.statusCode);
console.log(res.body);
});