@ts-rest/nest
Advanced tools
Comparing version 3.9.0 to 3.10.0
44
index.js
@@ -1,3 +0,3 @@ | ||
import { createParamDecorator, BadRequestException, Injectable, HttpException, applyDecorators, UseInterceptors, Put, Patch, Post, Get, Delete } from '@nestjs/common'; | ||
import { getPathParamsFromUrl, checkZodSchema } from '@ts-rest/core'; | ||
import { createParamDecorator, BadRequestException, Injectable, applyDecorators, SetMetadata, UseInterceptors, Put, Patch, Post, Get, Delete } from '@nestjs/common'; | ||
import { checkZodSchema } from '@ts-rest/core'; | ||
import { map } from 'rxjs'; | ||
@@ -35,23 +35,11 @@ | ||
function __metadata(metadataKey, metadataValue) { | ||
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue); | ||
} | ||
const getQueryParams = (url) => { | ||
const searchParams = new URLSearchParams(url.split('?')[1]); | ||
const queryParams = {}; | ||
for (const [key, value] of searchParams) { | ||
queryParams[key] = value; | ||
} | ||
return queryParams; | ||
}; | ||
const tsRestAppRouteMetadataKey = Symbol('ts-rest-app-route'); | ||
const ApiDecorator = createParamDecorator((_, ctx) => { | ||
const req = ctx.switchToHttp().getRequest(); | ||
const appRoute = req.appRoute; | ||
const appRoute = Reflect.getMetadata(tsRestAppRouteMetadataKey, ctx.getHandler()); | ||
if (!appRoute) { | ||
throw new BadRequestException('Make sure your route is decorated with @Api()'); | ||
// this will respond with a 500 error without revealing this error message in the response body | ||
throw new Error('Make sure your route is decorated with @Api()'); | ||
} | ||
const pathParams = getPathParamsFromUrl(req.url, appRoute); | ||
const queryParams = getQueryParams(req.url); | ||
const queryResult = checkZodSchema(queryParams, appRoute.query); | ||
const queryResult = checkZodSchema(req.query, appRoute.query); | ||
if (!queryResult.success) { | ||
@@ -64,3 +52,3 @@ throw new BadRequestException(queryResult.error); | ||
} | ||
const pathParamsResult = checkZodSchema(pathParams, appRoute.pathParams, { | ||
const pathParamsResult = checkZodSchema(req.params, appRoute.pathParams, { | ||
passThroughExtraKeys: true, | ||
@@ -92,8 +80,4 @@ }); | ||
let ApiRouteInterceptor = class ApiRouteInterceptor { | ||
constructor(appRoute) { | ||
this.appRoute = appRoute; | ||
} | ||
intercept(context, next) { | ||
const req = context.switchToHttp().getRequest(); | ||
req.appRoute = this.appRoute; | ||
const res = context.switchToHttp().getResponse(); | ||
return next.handle().pipe(map((value) => { | ||
@@ -103,3 +87,4 @@ if (typeof value === 'object' && | ||
value.body !== undefined) { | ||
throw new HttpException(value === null || value === void 0 ? void 0 : value.body, value.status); | ||
res.status(value.status); | ||
return value.body; | ||
} | ||
@@ -111,12 +96,9 @@ return value; | ||
ApiRouteInterceptor = __decorate([ | ||
Injectable(), | ||
__metadata("design:paramtypes", [Object]) | ||
Injectable() | ||
], ApiRouteInterceptor); | ||
const Api = (appRoute) => { | ||
const methodDecorator = getMethodDecorator(appRoute); | ||
return applyDecorators(methodDecorator, | ||
// Apply the interceptor to populate req.appRoute | ||
UseInterceptors(new ApiRouteInterceptor(appRoute))); | ||
return applyDecorators(SetMetadata(tsRestAppRouteMetadataKey, appRoute), methodDecorator, UseInterceptors(ApiRouteInterceptor)); | ||
}; | ||
export { Api, ApiDecorator, ApiRouteInterceptor, initNestServer }; |
{ | ||
"name": "@ts-rest/nest", | ||
"version": "3.9.0", | ||
"version": "3.10.0", | ||
"description": "Nest server integration for @ts-rest", | ||
@@ -34,4 +34,7 @@ "license": "MIT", | ||
"zod": "3.x.x", | ||
"@ts-rest/core": "3.9.0" | ||
"@ts-rest/core": "3.10.0" | ||
}, | ||
"devDependencies": { | ||
"express-serve-static-core": "^0.1.1" | ||
}, | ||
"module": "./index.js", | ||
@@ -38,0 +41,0 @@ "main": "./index.cjs", |
@@ -12,4 +12,2 @@ import { CallHandler, ExecutionContext, NestInterceptor } from '@nestjs/common'; | ||
export declare class ApiRouteInterceptor implements NestInterceptor { | ||
private readonly appRoute; | ||
constructor(appRoute: AppRoute); | ||
intercept(context: ExecutionContext, next: CallHandler): Observable<any>; | ||
@@ -16,0 +14,0 @@ } |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12601
1
222