Skip to content

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
PodaruDragos committed Nov 25, 2023
1 parent 574cee2 commit f7b3c46
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { BaseMiddleware, Controller } from './index';
import { getControllersFromMetadata, getControllersFromContainer, getControllerMetadata, getControllerMethodMetadata, getControllerParameterMetadata, instanceOfIHttpActionResult } from './utils';
import { TYPE, METADATA_KEY, DEFAULT_ROUTING_ROOT_PATH, PARAMETER_TYPE, DUPLICATED_CONTROLLER_NAME, } from './constants';
import { HttpResponseMessage } from './httpResponseMessage';
import { StreamContent } from './content/streamContent';

import type { AuthProvider, ConfigFunction, ControllerHandler, ControllerMethodMetadata, ExtractedParameters, HttpContext, Middleware, ParameterMetadata, Principal, RoutingConfig } from './interfaces';
import type { OutgoingHttpHeaders } from 'node:http';
Expand Down Expand Up @@ -57,7 +56,7 @@ export class InversifyExpressServer {
/**
* Sets the configuration function to be applied to the application.
* Note that the config function is not actually executed until a call to
* InversifyExpressServer.build().
* InversifyExpresServer.build().
*
* This method is chainable.
*
Expand All @@ -71,7 +70,7 @@ export class InversifyExpressServer {
/**
* Sets the error handler configuration function to be applied to the application.
* Note that the error config function is not actually executed until a call to
* InversifyExpressServer.build().
* InversifyExpresServer.build().
*
* This method is chainable.
*
Expand Down Expand Up @@ -136,7 +135,7 @@ export class InversifyExpressServer {
}

this._container.bind(TYPE.Controller)
.to(constructor as new (...args: Array<never>) => unknown)
.to(constructor as new (...args: never[]) => unknown)
.whenTargetNamed(name);
});

Expand All @@ -155,12 +154,12 @@ export class InversifyExpressServer {
);

if (controllerMetadata && methodMetadata) {
const controllerMiddleware = this.resolveMiddleware(
const controllerMiddleware = this.resolveMiddlewere(
...controllerMetadata.middleware,
);

methodMetadata.forEach((metadata: ControllerMethodMetadata) => {
let paramList: Array<ParameterMetadata> = [];
let paramList: ParameterMetadata[] = [];
if (parameterMetadata) {
paramList = parameterMetadata[metadata.key] || [];
}
Expand All @@ -170,7 +169,7 @@ export class InversifyExpressServer {
paramList,
);

const routeMiddleware = this.resolveMiddleware(
const routeMiddleware = this.resolveMiddlewere(
...metadata.middleware
);
this._router[metadata.method](
Expand All @@ -186,9 +185,9 @@ export class InversifyExpressServer {
this._app.use(this._routingConfig.rootPath, this._router);
}

private resolveMiddleware(
...middleware: Array<Middleware>
): Array<express.RequestHandler> {
private resolveMiddlewere(
...middleware: Middleware[]
): RequestHandler[] {
return middleware.map(middlewareItem => {
if (!this._container.isBound(middlewareItem)) {
return middlewareItem as express.RequestHandler;
Expand Down Expand Up @@ -237,15 +236,10 @@ export class InversifyExpressServer {
if (message.content !== undefined) {
this.copyHeadersTo(message.content.headers, res);

res.status(message.statusCode);

if (message.content instanceof StreamContent) {
(await message.content.readAsync()).pipe(res);
} {
res.status(message.statusCode)
// If the content is a number, ensure we change it to a string, else our content is
// treated as a statusCode rather than as the content of the Response
res.send(await message.content.readAsync());
}
.send(await message.content.readAsync());
} else {
res.sendStatus(message.statusCode);
}
Expand All @@ -254,7 +248,7 @@ export class InversifyExpressServer {
private handlerFactory(
controllerName: string,
key: string,
parameterMetadata: Array<ParameterMetadata>,
parameterMetadata: ParameterMetadata[],
): RequestHandler {
return async (
req: Request,
Expand Down Expand Up @@ -306,7 +300,7 @@ export class InversifyExpressServer {
): Promise<HttpContext> {
const principal = await this._getCurrentUser(req, res, next);
return {
// We use a childContainer for each request, so we can be
// We use a childContainer for each request so we can be
// sure that the binding is unique for each HTTP request
container: this._container.createChild(),
request: req,
Expand Down Expand Up @@ -336,9 +330,9 @@ export class InversifyExpressServer {
req: Request,
res: Response,
next: NextFunction,
params: Array<ParameterMetadata>,
params: ParameterMetadata[],
): ExtractedParameters {
const args: Array<unknown> = [];
const args: unknown[] = [];
if (!params || !params.length) {
return [req, res, next];
}
Expand Down Expand Up @@ -413,4 +407,4 @@ export class InversifyExpressServer {
private _getPrincipal(req: express.Request): Principal | null {
return this._getHttpContext(req).user;
}
}
}

0 comments on commit f7b3c46

Please sign in to comment.