XYZ API

The XYZ API module exports the api function which serves as the entry point for all XYZ API requests.

A node.js express app will require the api module and reference the exported api method for all request routes.

const app = express()
const api = require('./api/api')
app.get(`/`, api)

Requires

Methods

(inner) api(req, res)

The API method will redirect requests with a request url length 1 and xyzEnv.DIR.

eg. A request to localhost:3000 with a DIR = "/mapp" will be redirected to localhost:3000/mapp

The request object itself or the request object url will be logged with the req or req_url keys in xyzEnv.LOGS.

Requests with the url matching the /saml/ path will be passed to the saml module.

Request parameter will be assigned once validated with the validateRequestParams method.

Requests with a logout parameter property will set the header cookie to null and return with a redirect to the application domain path [xyzEnv.DIR].

Requests with a login param or login property in the request body object will shortcircuit to the user/login module.

Requests with a register param or register property in the request body object will shortcircuit to the user/register module.

All other requests will passed to the async validateRequestAuth method.

Parameters:
NameTypeDescription
reqreq

HTTP request.

resres

HTTP response.

Properties
NameTypeDescription
req.paramsObject

The request params which will be parsed by the validateRequestParams method.

params.logoutBoolean

The request should destroy the user cookie and shortciircuit.

params.loginBoolean

The request should redirect to user/login.

params.registerBoolean

The request should redirect to user/register.

(inner) requestRouter(req, res)

The requestRouter switch tests the request URL for an API case.

By default requests will be passed to the View API module.

Parameters:
NameTypeDescription
reqreq

HTTP request.

resres

HTTP response.

Properties
NameTypeDescription
req.urlstring

The request url.

(async, inner) validateRequestAuth(req, res)

The async validateRequestAuth will wait for the user/auth module to return a user object.

Requests without authorization headers will be redirected to the login if the user authentication errs.

The user object will be assigned as to the req.params.

PRIVATE processes require user auth for all requests and will shortcircuit to the user/login if the user authentication failed to resolve a user object.

Parameters:
NameTypeDescription
reqreq

HTTP request.

resres

HTTP response.

Properties
NameTypeAttributesDescription
req.paramsObject

The request params which will be parsed by the validateRequestParams method.

req.headersObject

The request headers.

headers.authorizationObject<optional>

The request carries an authorization header.

req.urlstring

The request url.

(inner) validateRequestParams(req) → {Object}

The method assigns a params object from the request params and query objects.

The restricted params.user will be deleted. The params.user can only be assigned from a user object returned from the user/auth module.

The method will return an error if some params key contains non whitelisted character or if the restricted user param is detected.

The template param will be set from _template if not explicit. This is required for the vercel router logic which does not allow to use URL path parameter to have the same key as request parameter.

The params object will have a language property which is set to en if not explicit.

The params object properties will be iterated through to parse Object values [eg null, boolean, array], and remove undefined parameter properties.

Parameters:
NameTypeDescription
reqreq

HTTP request.

Properties
NameTypeDescription
req.paramsObject

The request params object.

req.queryObject

The request query object.

Returns:

Returns a validated params object.

Type: 
Object