Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
Fix types error for api-controller and demoDataSetup (#169)
Browse files Browse the repository at this point in the history
* fix types error for api-controller and demoDataSetup

* handle undefined connectionPromise
  • Loading branch information
JameelB committed Oct 27, 2017
1 parent 308318e commit 2511dca
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
4 changes: 3 additions & 1 deletion cloud/wfm-rest-api/src/impl/ApiController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as express from 'express';
import { NextFunction, Request, RequestHandler, Response, Router } from 'express';
import { ApiError } from '../data-api/ApiError';
import { defaultPaginationEngine } from '../data-api/MongoPaginationEngine';
import { PageResponse } from '../data-api/PageResponse';
import { PagingDataRepository } from '../data-api/PagingDataRepository';
import * as errorCodes from './ErrorCodes';

Expand Down Expand Up @@ -172,7 +173,8 @@ export class ApiController<T> {
return router;
}

private buildExpressHandler(handlerFn: (this: this, req: Request) => Bluebird<T | T[] | undefined>): RequestHandler {
private buildExpressHandler(handlerFn: (this: this, req: Request) =>
Bluebird<PageResponse<T> | T | T[] | undefined| null>): RequestHandler {
return (req, res, next) => handlerFn.bind(this)(req)
.then((data: T | T[] | undefined) => data ? res.json(data) : res.status(204).end())
.catch(next);
Expand Down
28 changes: 18 additions & 10 deletions demo/server/src/modules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,24 @@ function wfmApiSetup(app: express.Router, connectionPromise: Promise<any>) {
const role = config.security.adminRole;
app.use('/api', portalsecurityMiddleware.protect(role));
app.use('/api', api.createWFMRouter());
connectionPromise.then(function(mongo: Db) {
// Fix compilation problem with different version of Db.
api.setDb(mongo as any);
});
if (!connectionPromise) {
getLogger().error('Failed to connect to a database');
} else {
connectionPromise.then(function(mongo: Db) {
// Fix compilation problem with different version of Db.
api.setDb(mongo as any);
});
}
}

function demoDataSetup(connectionPromise: Promise<Db>) {
connectionPromise.then(function(mongo: Db) {
if (config.seedDemoData) {
initData(mongo);
}
});
function demoDataSetup(connectionPromise: Promise<any>) {
if (!connectionPromise) {
getLogger().error('Failed to connect to a database');
} else {
connectionPromise.then(function(mongo: Db) {
if (config.seedDemoData) {
initData(mongo);
}
});
}
}

0 comments on commit 2511dca

Please sign in to comment.