snap-on-openapi 1.0.9 → 1.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/OpenApi.js CHANGED
@@ -185,19 +185,19 @@ export class OpenApi {
185
185
  const queryValidator = route.validators.query?.strict() ?? z.object({});
186
186
  const query = queryValidator.safeParse(req.query);
187
187
  if (!query.success) {
188
- throw new ValidationError(query.error, ValidationLocation.Query);
188
+ throw new ValidationError(query.error, ValidationLocation.Query, req.query);
189
189
  }
190
190
  const pathvalidator = route.validators.path?.strict() ?? z.object({});
191
191
  const path = pathvalidator.safeParse(req.params);
192
192
  if (!path.success) {
193
- throw new ValidationError(path.error, ValidationLocation.Path);
193
+ throw new ValidationError(path.error, ValidationLocation.Path, req.params);
194
194
  }
195
195
  let response;
196
196
  const containsBody = route.method !== Method.GET;
197
197
  if (containsBody && route.validators.body) {
198
198
  const body = route.validators.body.safeParse(req.body);
199
199
  if (!body.success) {
200
- throw new ValidationError(body.error, ValidationLocation.Body);
200
+ throw new ValidationError(body.error, ValidationLocation.Body, req.body);
201
201
  }
202
202
  const context = await this.config.routes[route.type].contextFactory({
203
203
  route: route,
@@ -241,9 +241,13 @@ export class OpenApi {
241
241
  body: route.validators.response ?? z.undefined(),
242
242
  headers: route.validators.responseHeaders?.strict() ?? z.object({}),
243
243
  });
244
+ if (this.config.disableResponseValidation) {
245
+ this.logger.info('Response: 200', finalResponse);
246
+ return { status: 200, body: finalResponse.body, headers: finalResponse.headers };
247
+ }
244
248
  const validated = finalResponseValidator.safeParse(finalResponse);
245
249
  if (!validated.success) {
246
- throw new ValidationError(validated.error, ValidationLocation.Response);
250
+ throw new ValidationError(validated.error, ValidationLocation.Response, finalResponse);
247
251
  }
248
252
  this.logger.info('Response: 200', validated.data);
249
253
  return { status: 200, body: validated.data.body, headers: validated.data.headers };
@@ -255,7 +259,7 @@ export class OpenApi {
255
259
  handleError(e, req) {
256
260
  this.logger.error('Error during request openAPI route handling', e);
257
261
  try {
258
- const response = this.config.handleError ? this.config.handleError(e, req) : this.config.defaultError;
262
+ const response = this.config.handleError ? this.config.handleError(e, { request: req, logger: this.logger }) : this.config.defaultError;
259
263
  const status = this.config.errors[response.code].status;
260
264
  const valid = this.config.errors[response.code].responseValidator.safeParse(response.body);
261
265
  if (!valid.success) {
@@ -10,6 +10,7 @@ import { RouteContextMap } from './RouteContextMap.js';
10
10
  import { RouteExtraPropsMap } from './RouteExtraPropsMap.js';
11
11
  import { Server } from './Server.js';
12
12
  export type Config<TRouteTypes extends string, TErrorCodes extends string, TErrorConfigMap extends ErrorConfigMap<TErrorCodes>, TRouteParamMap extends RouteExtraPropsMap<TRouteTypes>, TRouteContextMap extends RouteContextMap<TRouteTypes, TRouteParamMap>, TRouteConfigMap extends RouteConfigMap<TRouteTypes, TErrorCodes, TRouteParamMap, TRouteContextMap>> = {
13
+ disableResponseValidation?: boolean;
13
14
  logger?: Logger;
14
15
  basePath: RoutePath;
15
16
  routes: TRouteConfigMap;
@@ -27,7 +28,10 @@ export type Config<TRouteTypes extends string, TErrorCodes extends string, TErro
27
28
  apiVersion?: string;
28
29
  servers?: Server[];
29
30
  logLevel?: LogLevel;
30
- handleError?: (e: unknown, req: Request) => ErrorResponse<TErrorCodes, TErrorConfigMap>;
31
+ handleError?: (e: unknown, context: {
32
+ request: Request;
33
+ logger: Logger;
34
+ }) => ErrorResponse<TErrorCodes, TErrorConfigMap>;
31
35
  middleware?: <T extends TRouteTypes>(route: AnyRoute<T>, ctx: Awaited<ReturnType<TRouteContextMap[T]>>) => Promise<{
32
36
  body?: unknown;
33
37
  status?: number;
@@ -2,9 +2,11 @@ import { ZodError } from 'zod';
2
2
  import { ValidationLocation } from '../../enums/ValidationLocations.js';
3
3
  import { BuiltInError } from './BuiltInError.js';
4
4
  export declare class ValidationError extends BuiltInError {
5
- private error;
6
- private location;
7
- constructor(error: ZodError<unknown>, location: ValidationLocation);
5
+ protected error: ZodError<unknown>;
6
+ protected location: ValidationLocation;
7
+ protected data?: unknown;
8
+ constructor(error: ZodError<unknown>, location: ValidationLocation, data: unknown);
8
9
  getZodError(): ZodError<unknown>;
9
10
  getLocation(): ValidationLocation;
11
+ getData(): unknown;
10
12
  }
@@ -3,10 +3,12 @@ import { BuiltInError } from './BuiltInError.js';
3
3
  export class ValidationError extends BuiltInError {
4
4
  error;
5
5
  location;
6
- constructor(error, location) {
6
+ data;
7
+ constructor(error, location, data) {
7
8
  super(ErrorCode.ValidationFailed);
8
9
  this.error = error;
9
10
  this.location = location;
11
+ this.data = data;
10
12
  }
11
13
  getZodError() {
12
14
  return this.error;
@@ -14,4 +16,7 @@ export class ValidationError extends BuiltInError {
14
16
  getLocation() {
15
17
  return this.location;
16
18
  }
19
+ getData() {
20
+ return this.data;
21
+ }
17
22
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "snap-on-openapi",
3
3
  "author": "Alex Sarychev",
4
- "version": "1.0.9",
4
+ "version": "1.0.11",
5
5
  "description": "Swiftly build type-checked OpenAPI applications with Zod and TypeScript",
6
6
  "type": "module",
7
7
  "license": "ISC",