0.1.9 • Published 6 months ago

erstwhile-backend v0.1.9

Weekly downloads
-
License
GPLv3
Repository
-
Last release
6 months ago

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.jsonfile. 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.

0.1.9

6 months ago

0.1.8

9 months ago

0.1.7

9 months ago

0.1.6

9 months ago

0.1.5

9 months ago

0.1.4

10 months ago

0.1.3

10 months ago

0.1.2

10 months ago

0.1.1

10 months ago

0.1.0

10 months ago