Skip to content

Commit

Permalink
Update typescript compiler (#344)
Browse files Browse the repository at this point in the history
* update all packages
update tsconfig

* add stricter tsconfig
fix all problems caused by the upgrade

* add some fixes

* got to green
  • Loading branch information
PodaruDragos authored Sep 1, 2021
1 parent 1f8e2f0 commit 2462cd2
Show file tree
Hide file tree
Showing 36 changed files with 252 additions and 276 deletions.
26 changes: 13 additions & 13 deletions src/base_http_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,55 +22,55 @@ import { OK } from "http-status-codes";

@injectable()
export class BaseHttpController {
@injectHttpContext protected readonly httpContext: interfaces.HttpContext;
@injectHttpContext protected readonly httpContext!: interfaces.HttpContext;

protected created<T>(location: string | URL, content: T) {
return new CreatedNegotiatedContentResult(location, content, this);
return new CreatedNegotiatedContentResult(location, content);
}

protected conflict() {
return new ConflictResult(this);
return new ConflictResult();
}

protected ok<T>(content: T): OkNegotiatedContentResult<T>;
protected ok(): OkResult;
protected ok<T>(content?: T) {
return content === undefined ?
new OkResult(this) :
new OkNegotiatedContentResult(content, this);
new OkResult() :
new OkNegotiatedContentResult(content);
}

protected badRequest(): BadRequestResult;
protected badRequest(message: string): BadRequestErrorMessageResult;
protected badRequest(message?: string) {
return message === undefined ?
new BadRequestResult(this) :
new BadRequestErrorMessageResult(message, this);
new BadRequestResult() :
new BadRequestErrorMessageResult(message);
}

protected internalServerError(): InternalServerErrorResult;
protected internalServerError(error: Error): ExceptionResult;
protected internalServerError(error?: Error) {
return error ? new ExceptionResult(error, this) : new InternalServerErrorResult(this);
return error ? new ExceptionResult(error) : new InternalServerErrorResult();
}

protected notFound() {
return new NotFoundResult(this);
return new NotFoundResult();
}

protected redirect(uri: string | URL) {
return new RedirectResult(uri, this);
return new RedirectResult(uri);
}

protected responseMessage(message: HttpResponseMessage) {
return new ResponseMessageResult(message, this);
return new ResponseMessageResult(message);
}

protected statusCode(statusCode: number) {
return new StatusCodeResult(statusCode, this);
return new StatusCodeResult(statusCode);
}

protected json(content: any, statusCode: number = OK) {
return new JsonResult(content, statusCode, this);
return new JsonResult(content, statusCode);
}
}
2 changes: 1 addition & 1 deletion src/base_middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { interfaces } from "./interfaces";
export abstract class BaseMiddleware implements BaseMiddleware {
// httpContext is initialized when the middleware is invoked
// see resolveMidleware in server.ts for more details
protected readonly httpContext: interfaces.HttpContext;
protected readonly httpContext!: interfaces.HttpContext;

protected bind<T>(serviceIdentifier: inversifyInterfaces.ServiceIdentifier<T>): inversifyInterfaces.BindingToSyntax<T> {
return this.httpContext.container.bind(serviceIdentifier);
Expand Down
14 changes: 14 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ export enum PARAMETER_TYPE {
PRINCIPAL
}

export enum HTTP_VERBS_ENUM {
all = "ALL",
connect = "CONNECT",
delete = "DELETE",
get = "GET",
head = "HEAD",
options = "OPTIONS",
patch = "PATCH",
post = "POST",
propfind = "PROPFIND",
put = "PUT",
trace = "TRACE"
}

export const DUPLICATED_CONTROLLER_NAME = (name: string) =>
`Two controllers cannot have the same name: ${name}`;

Expand Down
6 changes: 2 additions & 4 deletions src/content/jsonContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ const DEFAULT_MEDIA_TYPE = "application/json";
export class JsonContent extends HttpContent {
private content: string;

constructor(content: any);
constructor(content: any, mediaType: string);
constructor(content: any, private mediaType: string = DEFAULT_MEDIA_TYPE) {
constructor(content: any) {
super();

this.content = JSON.stringify(content);

this.headers["content-type"] = mediaType;
this.headers["content-type"] = DEFAULT_MEDIA_TYPE;
}

public readAsStringAsync() {
Expand Down
6 changes: 2 additions & 4 deletions src/content/stringContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ import { HttpContent } from "./httpContent";
const DEFAULT_MEDIA_TYPE = "text/plain";

export class StringContent extends HttpContent {
constructor(content: string);
constructor(content: string, mediaType: string);
constructor(private content: string, private mediaType: string = DEFAULT_MEDIA_TYPE) {
constructor(private content: string) {
super();

this.headers["content-type"] = mediaType;
this.headers["content-type"] = DEFAULT_MEDIA_TYPE;
}

public readAsStringAsync() {
Expand Down
6 changes: 3 additions & 3 deletions src/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
getControllerParameterMetadata
} from "./utils";

export function getRouteInfo(container: inversifyInterfaces.Container) {
export function getRouteInfo(container: inversifyInterfaces.Container): interfaces.RouteInfo[] {

const raw = getRawMetadata(container);

Expand Down Expand Up @@ -63,15 +63,15 @@ export function getRouteInfo(container: inversifyInterfaces.Container) {
}
}

const details = {
const details: interfaces.RouteDetails = {
route: `${method} ${controllerPath}${actionPath}`
};

if (args) {
details["args"] = args;
}

return details as { route: string, args?: string[] };
return details;

});

Expand Down
15 changes: 9 additions & 6 deletions src/decorators.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as express from "express";
import { inject, injectable, decorate } from "inversify";
import { interfaces } from "./interfaces";
import { TYPE, METADATA_KEY, PARAMETER_TYPE } from "./constants";
import { TYPE, METADATA_KEY, PARAMETER_TYPE, HTTP_VERBS_ENUM } from "./constants";

export const injectHttpContext = inject(TYPE.HttpContext);

Expand Down Expand Up @@ -67,7 +66,11 @@ export function httpDelete(path: string, ...middleware: interfaces.Middleware[])
return httpMethod("delete", path, ...middleware);
}

export function httpMethod(method: string, path: string, ...middleware: interfaces.Middleware[]): interfaces.HandlerDecorator {
export function httpMethod(
method: keyof typeof HTTP_VERBS_ENUM,
path: string,
...middleware: interfaces.Middleware[]
): interfaces.HandlerDecorator {
return function (target: any, key: string, value: any) {

let metadata: interfaces.ControllerMethodMetadata = {
Expand Down Expand Up @@ -107,7 +110,7 @@ function paramDecoratorFactory(parameterType: PARAMETER_TYPE): (name?: string) =
}

export function params(type: PARAMETER_TYPE, parameterName?: string) {
return function (target: Object, methodName: string, index: number) {
return function (target: Object, methodName: string | symbol, index: number) {

let metadataList: interfaces.ControllerParameterMetadata = {};
let parameterMetadataList: interfaces.ParameterMetadata[] = [];
Expand All @@ -122,11 +125,11 @@ export function params(type: PARAMETER_TYPE, parameterName?: string) {
} else {
metadataList = Reflect.getMetadata(METADATA_KEY.controllerParameter, target.constructor);
if (metadataList.hasOwnProperty(methodName)) {
parameterMetadataList = metadataList[methodName];
parameterMetadataList = metadataList[methodName as string] as interfaces.ParameterMetadata[];
}
parameterMetadataList.unshift(parameterMetadata);
}
metadataList[methodName] = parameterMetadataList;
metadataList[methodName as string] = parameterMetadataList;
Reflect.defineMetadata(METADATA_KEY.controllerParameter, metadataList, target.constructor);
};
}
4 changes: 2 additions & 2 deletions src/httpResponseMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { OutgoingHttpHeaders } from "http";
import { HttpContent } from "./content/httpContent";

export class HttpResponseMessage {
private _content: HttpContent;
private _content!: HttpContent;

private _headers: OutgoingHttpHeaders = {};

Expand All @@ -22,7 +22,7 @@ export class HttpResponseMessage {
this._content = value;
}

private _statusCode: number;
private _statusCode!: number;

public get statusCode(): number {
return this._statusCode;
Expand Down
10 changes: 7 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import "reflect-metadata";

import { InversifyExpressServer } from "./server";
import { controller, httpMethod, httpGet, httpPut, httpPost, httpPatch,
httpHead, all, httpDelete, request, response, requestParam, queryParam,
requestBody, requestHeaders, cookies, next, principal, injectHttpContext } from "./decorators";
import {
controller, httpMethod, httpGet, httpPut, httpPost, httpPatch,
httpHead, all, httpDelete, request, response, requestParam, queryParam,
requestBody, requestHeaders, cookies, next, principal, injectHttpContext
} from "./decorators";
import { TYPE } from "./constants";
import { interfaces } from "./interfaces";
import * as results from "./results";
Expand Down
14 changes: 12 additions & 2 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as express from "express";
import { interfaces as inversifyInterfaces } from "inversify";
import { PARAMETER_TYPE } from "./constants";
import { HTTP_VERBS_ENUM, PARAMETER_TYPE } from "./constants";
import { HttpResponseMessage } from "./httpResponseMessage";

namespace interfaces {
Expand All @@ -14,7 +14,7 @@ namespace interfaces {
}

export interface ControllerMethodMetadata extends ControllerMetadata {
method: string;
method: keyof typeof HTTP_VERBS_ENUM;
key: string;
}

Expand Down Expand Up @@ -70,6 +70,16 @@ namespace interfaces {
export interface IHttpActionResult {
executeAsync(): Promise<HttpResponseMessage>;
}

export interface RouteDetails {
route: string;
args?: string[];
}

export interface RouteInfo {
controller: any;
endpoints: RouteDetails[];
}
}

export { interfaces };
3 changes: 1 addition & 2 deletions src/results/BadRequestErrorMessageResult.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { HttpResponseMessage } from "../httpResponseMessage";
import { BAD_REQUEST } from "http-status-codes";
import { interfaces } from "../interfaces";
import { BaseHttpController } from "../base_http_controller";
import { StringContent } from "../content/stringContent";

export default class BadRequestErrorMessageResult implements interfaces.IHttpActionResult {
constructor(private message: string, private apiController: BaseHttpController) {}
constructor(private message: string) { }

public async executeAsync() {
const response = new HttpResponseMessage(BAD_REQUEST);
Expand Down
3 changes: 0 additions & 3 deletions src/results/BadRequestResult.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { HttpResponseMessage } from "../httpResponseMessage";
import { BAD_REQUEST } from "http-status-codes";
import { interfaces } from "../interfaces";
import { BaseHttpController } from "../base_http_controller";

export default class BadRequestResult implements interfaces.IHttpActionResult {
constructor(private apiController: BaseHttpController) {}

public async executeAsync() {
return new HttpResponseMessage(BAD_REQUEST);
}
Expand Down
3 changes: 0 additions & 3 deletions src/results/ConflictResult.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { HttpResponseMessage } from "../httpResponseMessage";
import { CONFLICT } from "http-status-codes";
import { interfaces } from "../interfaces";
import { BaseHttpController } from "../base_http_controller";

export default class ConflictResult implements interfaces.IHttpActionResult {
constructor(private apiController: BaseHttpController) {}

public async executeAsync() {
return new HttpResponseMessage(CONFLICT);
}
Expand Down
7 changes: 3 additions & 4 deletions src/results/CreatedNegotiatedContentResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ import { HttpResponseMessage } from "../httpResponseMessage";
import { CREATED } from "http-status-codes";
import { interfaces } from "../interfaces";
import { URL } from "url";
import { BaseHttpController } from "../base_http_controller";
import { StringContent } from "../content/stringContent";

export default class CreatedNegotiatedContentResult<T> implements interfaces.IHttpActionResult {
constructor(private location: string | URL, private content: T, private apiController: BaseHttpController) {}
constructor(private location: string | URL, private content: T) { }

public async executeAsync() {
const response = new HttpResponseMessage(CREATED);
response.content = new StringContent(JSON.stringify(this.content), "application/json");
response.headers.location = this.location.toString();
response.content = new StringContent(JSON.stringify(this.content));
response.headers["location"] = this.location.toString();
return response;
}
}
3 changes: 1 addition & 2 deletions src/results/ExceptionResult.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { HttpResponseMessage } from "../httpResponseMessage";
import { INTERNAL_SERVER_ERROR } from "http-status-codes";
import { interfaces } from "../interfaces";
import { BaseHttpController } from "../base_http_controller";
import { StringContent } from "../content/stringContent";

export default class ExceptionResult implements interfaces.IHttpActionResult {
constructor(private error: Error, private apiController: BaseHttpController) {}
constructor(private error: Error) { }

public async executeAsync() {
const response = new HttpResponseMessage(INTERNAL_SERVER_ERROR);
Expand Down
3 changes: 0 additions & 3 deletions src/results/InternalServerError.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { HttpResponseMessage } from "../httpResponseMessage";
import { INTERNAL_SERVER_ERROR } from "http-status-codes";
import { interfaces } from "../interfaces";
import { BaseHttpController } from "../base_http_controller";

export default class InternalServerErrorResult implements interfaces.IHttpActionResult {
constructor(private apiController: BaseHttpController) {}

public async executeAsync() {
return new HttpResponseMessage(INTERNAL_SERVER_ERROR);
}
Expand Down
6 changes: 1 addition & 5 deletions src/results/JsonResult.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { interfaces } from "../interfaces";
import { HttpResponseMessage } from "../httpResponseMessage";
import { JsonContent } from "../content/jsonContent";
import { BaseHttpController } from "../base_http_controller";

export default class JsonResult implements interfaces.IHttpActionResult {

constructor(
public readonly json: any,
public readonly statusCode: number,
private apiController: BaseHttpController) {}
constructor(public readonly json: any, public readonly statusCode: number) { }

public async executeAsync() {
const response = new HttpResponseMessage(this.statusCode);
Expand Down
3 changes: 0 additions & 3 deletions src/results/NotFoundResult.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { HttpResponseMessage } from "../httpResponseMessage";
import { NOT_FOUND } from "http-status-codes";
import { interfaces } from "../interfaces";
import { BaseHttpController } from "../base_http_controller";

export default class NotFoundResult implements interfaces.IHttpActionResult {
constructor(private apiController: BaseHttpController) {}

public async executeAsync() {
return new HttpResponseMessage(NOT_FOUND);
}
Expand Down
5 changes: 2 additions & 3 deletions src/results/OkNegotiatedContentResult.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { HttpResponseMessage } from "../httpResponseMessage";
import { OK } from "http-status-codes";
import { interfaces } from "../interfaces";
import { BaseHttpController } from "../base_http_controller";
import { StringContent } from "../content/stringContent";

export default class OkNegotiatedContentResult<T> implements interfaces.IHttpActionResult {
constructor(private content: T, private apiController: BaseHttpController) {}
constructor(private content: T) { }

public async executeAsync() {
const response = new HttpResponseMessage(OK);
response.content = new StringContent(JSON.stringify(this.content), "application/json");
response.content = new StringContent(JSON.stringify(this.content));
return response;
}
}
Loading

0 comments on commit 2462cd2

Please sign in to comment.