erstwhile-backend
v0.1.10
Published
This is the backend portion for the Erstwhile Framework. Currently focused on the API description.
Downloads
7
Readme
erstwhile-backend
erstwhile-backend
is a Node module meant to help build ExpressJS backends that are compatible with the Erstwhile Javascript framework.
When Erstwhile uses an API, it looks for a JSON file that describes the endpoints available, data strucures used by the endpoints, authentication information, and other details. This package helps backend applications publish that file, and also provides for an HTML display of that file that can serve as built-in developer documentation when looking at a development server.
It also provides some convenient helper functions for validating data based on that API description.
Installation
npm install --save erstwhile-backend
Usage
Required Environment Variables
ERSTWHILE_DESCRIPTION_PATH=./config/description.json
ERSTWHILE_SESSION_KEY=session
ERSTWHILE_DESCRIPTION_PATH
should point to your description.json
file.
ERSTWHILE_SESSION_KEY
should be the key on the request object where you store the logged in user's user_id
. This presence of (by default) req['session'].user_id
is used to determine if the user is authenticated.
Publishing description.json
Within your application's app.js
:
const {handleDescribe} = require("erstwhile-backend");
if(process.env.ENVIRONMENT != 'production') {
app.get('/describe', handleDescribe)
}
The path /describe
is the normal place to put this file, but you can also use something else if that doesnt' work in your environment. To see the HTML version of the description try /describe?format=html
in the brower.
Validating Requests
const { validateRequest } = require("erstwhile-backend");
/*
* If the request itself contains everything you need to properly
* validate, you can use the short form of the method.
let validRequest1 = validateRequest(req);
/*
* For updates, you may want to include the existing object as well
* as the request, as requests might not contain all of the keys needed
* to properly validate.
*/
let validRequest2 = validateRequest(req, existingObject);
/*
* If the request is invalid, validRequest conains a properly formatted
* Erstwhile standard response you can pass back to the user.
*/
if(validRequest.success) {
// continue
else {
res.send(validRequest)
}
Please note that if req[process.env.ERSTWHILE_SESSION_KEY || 'session']?.user_id
is falsy, the authenticated
key in the standard response will be false.