@duongtrungnguyen/nestro
Version:
Service registry for Nest JS
60 lines • 2.37 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __decorateClass = (decorators, target, key, kind) => {
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
for (var i = decorators.length - 1, decorator; i >= 0; i--)
if (decorator = decorators[i])
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
if (kind && result) __defProp(target, key, result);
return result;
};
var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index);
import { HttpStatus, Inject, Injectable } from "@nestjs/common";
import { RouteHandleService } from "./route-handle.service";
import { HttpProxyService } from "./http-proxy.service";
import { WsProxyService } from "./ws-proxy.service";
import { isSocketRequest } from "../utils";
let GatewayService = class {
constructor(httpProxyService, wsProxyService, routeHandleService) {
this.httpProxyService = httpProxyService;
this.wsProxyService = wsProxyService;
this.routeHandleService = routeHandleService;
}
async proxyRequest(req, res) {
const routeConfig = this.routeHandleService.findMatchingRoute(req.path);
if (!routeConfig) {
res.status(HttpStatus.NOT_FOUND).json({
message: "Proxy failed because route not found",
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
statusCode: HttpStatus.NOT_FOUND
});
return;
}
const isAllowed = await this.routeHandleService.checkGuards(routeConfig.guards, req, res);
if (!isAllowed) return;
if (isSocketRequest(req)) {
return await this.wsProxyService.proxyRequest(req, res, routeConfig);
}
await this.httpProxyService.proxyRequest(req, res, routeConfig);
}
executeWithOptions(req, res, options) {
if (isSocketRequest(req)) {
this.wsProxyService.handleProxy(req, res, options);
return;
}
this.httpProxyService.handleProxy(req, res, {
buffer: this.httpProxyService.getRequestBuffer(req),
...options
});
}
};
GatewayService = __decorateClass([
Injectable(),
__decorateParam(0, Inject(HttpProxyService)),
__decorateParam(1, Inject(WsProxyService)),
__decorateParam(2, Inject(RouteHandleService))
], GatewayService);
export {
GatewayService
};
//# sourceMappingURL=gateway.service.js.map