@zenstackhq/server
Version:
ZenStack server-side adapters
72 lines • 3.74 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApiHandlerService = void 0;
const common_1 = require("@nestjs/common");
const core_1 = require("@nestjs/core");
const shared_1 = require("../shared");
const rpc_1 = require("../api/rpc");
const zenstack_constants_1 = require("./zenstack.constants");
/**
* The ZenStack API handler service for NestJS. The service is used to handle API requests
* and forward them to the ZenStack API handler. It is platform agnostic and can be used
* with any HTTP adapter.
*/
let ApiHandlerService = class ApiHandlerService {
constructor(httpAdapterHost, prisma, request) {
this.httpAdapterHost = httpAdapterHost;
this.prisma = prisma;
this.request = request;
}
async handleRequest(options) {
const { modelMeta, zodSchemas } = (0, shared_1.loadAssets)(options || {});
const requestHandler = options?.handler || (0, rpc_1.RPCApiHandler)();
const hostname = this.httpAdapterHost.httpAdapter.getRequestHostname(this.request);
const requestUrl = this.httpAdapterHost.httpAdapter.getRequestUrl(this.request);
// prefix with http:// to make a valid url accepted by URL constructor
const url = new URL(`http://${hostname}${requestUrl}`);
const method = this.httpAdapterHost.httpAdapter.getRequestMethod(this.request);
const path = options?.baseUrl && url.pathname.startsWith(options.baseUrl) ? url.pathname.slice(options.baseUrl.length) : url.pathname;
const searchParams = url.searchParams;
const query = Object.fromEntries(searchParams);
const requestBody = this.request.body;
const response = await requestHandler({
method,
path,
query,
requestBody,
prisma: this.prisma,
modelMeta,
zodSchemas,
logger: options?.logger,
});
// handle handler error
// if response code >= 400 throw nestjs HttpException
// the error response will be generated by nestjs
// caller can use try/catch to deal with this manually also
if (response.status >= 400) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
throw new common_1.HttpException(response.body, response.status);
}
return response.body;
}
};
exports.ApiHandlerService = ApiHandlerService;
exports.ApiHandlerService = ApiHandlerService = __decorate([
(0, common_1.Injectable)({ scope: common_1.Scope.REQUEST }),
__param(1, (0, common_1.Inject)(zenstack_constants_1.ENHANCED_PRISMA)),
__param(2, (0, common_1.Inject)(core_1.REQUEST)),
__metadata("design:paramtypes", [core_1.HttpAdapterHost, Object, Object])
], ApiHandlerService);
//# sourceMappingURL=api-handler.service.js.map