Skip to content

Commit

Permalink
BUGFIX: Cannot set statuscode for express.js #94
Browse files Browse the repository at this point in the history
  • Loading branch information
isman-usoh committed May 19, 2017
1 parent d5808c5 commit 804ed90
Show file tree
Hide file tree
Showing 14 changed files with 440 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/interfaces/controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

export class Controller {
private statusCode?: number = undefined;
public statusCode?: number = undefined;

public getStatus() {
return this.statusCode;
Expand Down
6 changes: 2 additions & 4 deletions src/routeGeneration/templates/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,9 @@ export function RegisterRoutes(app: any) {
return promise
.then((data: any) => {
if (data) {
response.json(data);
response.status(statusCode || 200);
response.status(statusCode || 200).json(data);;
} else {
response.status(statusCode || 204);
response.end();
response.status(statusCode || 204).end();
}
})
.catch((error: any) => next(error));
Expand Down
42 changes: 42 additions & 0 deletions tests/fixtures/controllers/testController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {
Route, Get, Controller
} from '../../../src';
import { TestModel } from '../../fixtures/testModel';
import { ModelService } from '../services/modelService';

@Route('Controller')
export class TestController extends Controller {

@Get('normalStatusCode')
public async normalStatusCode(): Promise<TestModel> {
return Promise.resolve(new ModelService().getModel());
}

@Get('customNomalStatusCode')
public async customNomalStatusCode(): Promise<TestModel> {
const that = this;
const service = new ModelService();
const promise = service.getModelPromise();

return new Promise<TestModel>(resolve => {
that.statusCode = 201;
resolve(promise);
});
}

@Get('noContentStatusCode')
public async noContentStatusCode(): Promise<void> {
return Promise.resolve();
}

@Get('customNoContentStatusCode')
public async customNoContentStatusCode(): Promise<void> {
const that = this;
const promise = Promise.resolve();

return new Promise<void>(resolve => {
that.statusCode = 201;
resolve(promise);
});
}
}
95 changes: 91 additions & 4 deletions tests/fixtures/express/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { MethodController } from './../controllers/methodController';
import { ParameterController } from './../controllers/parameterController';
import { SecurityTestController } from './../controllers/securityController';
import { ValidateController } from './../controllers/validateController';
import { TestController } from './../controllers/testController';
import { set } from 'lodash';
import { expressAuthentication } from './authentication';

Expand Down Expand Up @@ -1743,6 +1744,94 @@ export function RegisterRoutes(app: any) {
}
promiseHandler(promise, statusCode, response, next);
});
app.get('/v1/Controller/normalStatusCode',
function(request: any, response: any, next: any) {
const args = {
};

let validatedArgs: any[] = [];
try {
validatedArgs = getValidatedArgs(args, request);
} catch (err) {
return next(err);
}

const controller = new TestController();


const promise = controller.normalStatusCode.apply(controller, validatedArgs);
let statusCode: any;
if (controller instanceof Controller) {
statusCode = (controller as Controller).getStatus();
}
promiseHandler(promise, statusCode, response, next);
});
app.get('/v1/Controller/customNomalStatusCode',
function(request: any, response: any, next: any) {
const args = {
};

let validatedArgs: any[] = [];
try {
validatedArgs = getValidatedArgs(args, request);
} catch (err) {
return next(err);
}

const controller = new TestController();


const promise = controller.customNomalStatusCode.apply(controller, validatedArgs);
let statusCode: any;
if (controller instanceof Controller) {
statusCode = (controller as Controller).getStatus();
}
promiseHandler(promise, statusCode, response, next);
});
app.get('/v1/Controller/noContentStatusCode',
function(request: any, response: any, next: any) {
const args = {
};

let validatedArgs: any[] = [];
try {
validatedArgs = getValidatedArgs(args, request);
} catch (err) {
return next(err);
}

const controller = new TestController();


const promise = controller.noContentStatusCode.apply(controller, validatedArgs);
let statusCode: any;
if (controller instanceof Controller) {
statusCode = (controller as Controller).getStatus();
}
promiseHandler(promise, statusCode, response, next);
});
app.get('/v1/Controller/customNoContentStatusCode',
function(request: any, response: any, next: any) {
const args = {
};

let validatedArgs: any[] = [];
try {
validatedArgs = getValidatedArgs(args, request);
} catch (err) {
return next(err);
}

const controller = new TestController();


const promise = controller.customNoContentStatusCode.apply(controller, validatedArgs);
let statusCode: any;
if (controller instanceof Controller) {
statusCode = (controller as Controller).getStatus();
}
promiseHandler(promise, statusCode, response, next);
});

function authenticateMiddleware(name: string, scopes: string[] = []) {
return (request: any, response: any, next: any) => {
Expand All @@ -1761,11 +1850,9 @@ export function RegisterRoutes(app: any) {
return promise
.then((data: any) => {
if (data) {
response.json(data);
response.status(statusCode || 200);
response.status(statusCode || 200).json(data);;
} else {
response.status(statusCode || 204);
response.end();
response.status(statusCode || 204).end();
}
})
.catch((error: any) => next(error));
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/express/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import '../controllers/methodController';
import '../controllers/parameterController';
import '../controllers/securityController';
import '../controllers/validateController';
import '../controllers/testController';

import { RegisterRoutes } from './routes';

Expand Down
117 changes: 111 additions & 6 deletions tests/fixtures/hapi/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { MethodController } from './../controllers/methodController';
import { ParameterController } from './../controllers/parameterController';
import { SecurityTestController } from './../controllers/securityController';
import { ValidateController } from './../controllers/validateController';
import { TestController } from './../controllers/testController';
import { set } from 'lodash';
import { hapiAuthentication } from './authentication';

Expand Down Expand Up @@ -1388,8 +1389,8 @@ export function RegisterRoutes(server: hapi.Server) {
pre: [
{
method: authenticateMiddleware('api_key'
)
}
)
}
],
handler: (request: any, reply) => {
const args = {
Expand Down Expand Up @@ -1421,8 +1422,8 @@ export function RegisterRoutes(server: hapi.Server) {
{
method: authenticateMiddleware('tsoa_auth'
, ["write:pets", "read:pets"]
)
}
)
}
],
handler: (request: any, reply) => {
const args = {
Expand Down Expand Up @@ -1720,8 +1721,8 @@ export function RegisterRoutes(server: hapi.Server) {
pre: [
{
method: authenticateMiddleware('api_key'
)
}
)
}
],
handler: (request: any, reply) => {
const args = {
Expand Down Expand Up @@ -2029,6 +2030,110 @@ export function RegisterRoutes(server: hapi.Server) {
}
}
});
server.route({
method: 'get',
path: '/v1/Controller/normalStatusCode',
config: {
handler: (request: any, reply) => {
const args = {
};

let validatedArgs: any[] = [];
try {
validatedArgs = getValidatedArgs(args, request);
} catch (err) {
return reply(err).code(err.status || 500);
}

const controller = new TestController();

const promise = controller.normalStatusCode.apply(controller, validatedArgs);
let statusCode: any;
if (controller instanceof Controller) {
statusCode = (controller as Controller).getStatus();
}
return promiseHandler(promise, statusCode, request, reply);
}
}
});
server.route({
method: 'get',
path: '/v1/Controller/customNomalStatusCode',
config: {
handler: (request: any, reply) => {
const args = {
};

let validatedArgs: any[] = [];
try {
validatedArgs = getValidatedArgs(args, request);
} catch (err) {
return reply(err).code(err.status || 500);
}

const controller = new TestController();

const promise = controller.customNomalStatusCode.apply(controller, validatedArgs);
let statusCode: any;
if (controller instanceof Controller) {
statusCode = (controller as Controller).getStatus();
}
return promiseHandler(promise, statusCode, request, reply);
}
}
});
server.route({
method: 'get',
path: '/v1/Controller/noContentStatusCode',
config: {
handler: (request: any, reply) => {
const args = {
};

let validatedArgs: any[] = [];
try {
validatedArgs = getValidatedArgs(args, request);
} catch (err) {
return reply(err).code(err.status || 500);
}

const controller = new TestController();

const promise = controller.noContentStatusCode.apply(controller, validatedArgs);
let statusCode: any;
if (controller instanceof Controller) {
statusCode = (controller as Controller).getStatus();
}
return promiseHandler(promise, statusCode, request, reply);
}
}
});
server.route({
method: 'get',
path: '/v1/Controller/customNoContentStatusCode',
config: {
handler: (request: any, reply) => {
const args = {
};

let validatedArgs: any[] = [];
try {
validatedArgs = getValidatedArgs(args, request);
} catch (err) {
return reply(err).code(err.status || 500);
}

const controller = new TestController();

const promise = controller.customNoContentStatusCode.apply(controller, validatedArgs);
let statusCode: any;
if (controller instanceof Controller) {
statusCode = (controller as Controller).getStatus();
}
return promiseHandler(promise, statusCode, request, reply);
}
}
});

function authenticateMiddleware(name: string, scopes: string[] = []) {
return (request: hapi.Request, reply: hapi.IReply) => {
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/hapi/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import '../controllers/methodController';
import '../controllers/parameterController';
import '../controllers/securityController';
import '../controllers/validateController';
import '../controllers/testController';

import { RegisterRoutes } from './routes';

Expand Down
6 changes: 2 additions & 4 deletions tests/fixtures/inversify/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,9 @@ export function RegisterRoutes(app: any) {
return promise
.then((data: any) => {
if (data) {
response.json(data);
response.status(statusCode || 200);
response.status(statusCode || 200).json(data);;
} else {
response.status(statusCode || 204);
response.end();
response.status(statusCode || 204).end();
}
})
.catch((error: any) => next(error));
Expand Down

0 comments on commit 804ed90

Please sign in to comment.