Skip to content
Snippets Groups Projects
Commit ba55296e authored by michael.minelli's avatar michael.minelli
Browse files

Add manager of params (f.e. help get value from an id)

parent 92d9fd7e
No related branches found
No related tags found
No related merge requests found
import { Express } from 'express-serve-static-core';
import ApiRequest from '../models/ApiRequest';
import express from 'express';
import { StatusCodes } from 'http-status-codes';
class ParamsCallbackManager {
private static _instance: ParamsCallbackManager;
private constructor() { }
public static get instance(): ParamsCallbackManager {
if ( !ParamsCallbackManager._instance ) {
ParamsCallbackManager._instance = new ParamsCallbackManager();
}
return ParamsCallbackManager._instance;
}
protected listenParam(paramName: string, backend: Express, context: any, functionName: string, indexName: string) {
backend.param(paramName, (req: ApiRequest, res: express.Response, next: express.NextFunction, id: number | string) => {
(context[functionName] as (id: number | Array<number>) => Promise<any>)(typeof id === 'string' ? JSON.parse(id) as Array<number> : id).then(result => {
if ( result ) {
this.initBoundParams(req);
(req.boundParams as any)[indexName] = result;
next();
} else {
req.session.sendResponse(res, StatusCodes.NOT_FOUND, {}, 'Param bounding failed: ' + paramName);
}
});
});
}
initBoundParams(req: ApiRequest) {
if ( !req.boundParams ) {
req.boundParams = {};
}
}
register(backend: Express) {
}
}
export default ParamsCallbackManager.instance;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment