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

ParamsCallback => Add callback on enonce name or url

parent 43738359
No related branches found
No related tags found
No related merge requests found
...@@ -2,6 +2,7 @@ import { Express } from 'express-serve-static-core'; ...@@ -2,6 +2,7 @@ import { Express } from 'express-serve-static-core';
import ApiRequest from '../models/ApiRequest'; import ApiRequest from '../models/ApiRequest';
import express from 'express'; import express from 'express';
import { StatusCodes } from 'http-status-codes'; import { StatusCodes } from 'http-status-codes';
import EnonceManager from '../managers/EnonceManager';
class ParamsCallbackManager { class ParamsCallbackManager {
...@@ -18,8 +19,8 @@ class ParamsCallbackManager { ...@@ -18,8 +19,8 @@ class ParamsCallbackManager {
} }
protected listenParam(paramName: string, backend: Express, context: any, functionName: string, indexName: string) { 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) => { backend.param(paramName, (req: ApiRequest, res: express.Response, next: express.NextFunction, id: string | number) => {
(context[functionName] as (id: number | Array<number>) => Promise<any>)(typeof id === 'string' ? JSON.parse(id) as Array<number> : id).then(result => { (context[functionName] as (id: string | number) => Promise<any>)(id).then(result => {
if ( result ) { if ( result ) {
this.initBoundParams(req); this.initBoundParams(req);
(req.boundParams as any)[indexName] = result; (req.boundParams as any)[indexName] = result;
...@@ -34,11 +35,14 @@ class ParamsCallbackManager { ...@@ -34,11 +35,14 @@ class ParamsCallbackManager {
initBoundParams(req: ApiRequest) { initBoundParams(req: ApiRequest) {
if ( !req.boundParams ) { if ( !req.boundParams ) {
req.boundParams = {}; req.boundParams = {
enonce: null
};
} }
} }
register(backend: Express) { register(backend: Express) {
this.listenParam('enonceNameOrUrl', backend, EnonceManager, 'get', 'enonce');
} }
} }
......
import express from 'express'; import express from 'express';
import Session from '../controllers/Session'; import Session from '../controllers/Session';
import Enonce from './Enonce';
type ApiRequest = express.Request & { type ApiRequest = express.Request & {
session: Session, boundParams: {} session: Session, boundParams: {
enonce: Enonce
}
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment