@saihu/common 1.1.6 → 1.1.8

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.
@@ -1 +1 @@
1
- {"version":3,"file":"auth-guard.d.ts","sourceRoot":"","sources":["../../src/auth/auth-guard.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EACX,gBAAgB,EAIjB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAGzC,qBACa,SAAU,YAAW,WAAW;IAEzC,OAAO,CAAC,UAAU;IACI,OAAO,CAAC,QAAQ,CAAC,SAAS;IAChD,OAAO,CAAC,SAAS;gBAFT,UAAU,EAAE,UAAU,EACS,SAAS,EAAE,MAAM,EAChD,SAAS,EAAE,SAAS;IAGxB,WAAW,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,OAAO,CAAC;IA4B9D,OAAO,CAAC,sBAAsB;CAI/B"}
1
+ {"version":3,"file":"auth-guard.d.ts","sourceRoot":"","sources":["../../src/auth/auth-guard.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EACX,gBAAgB,EAIjB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAIzC,qBACa,SAAU,YAAW,WAAW;IAEzC,OAAO,CAAC,UAAU;IACI,OAAO,CAAC,QAAQ,CAAC,SAAS;IAChD,OAAO,CAAC,SAAS;gBAFT,UAAU,EAAE,UAAU,EACS,SAAS,EAAE,MAAM,EAChD,SAAS,EAAE,SAAS;IAGxB,WAAW,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,OAAO,CAAC;IA4B9D,OAAO,CAAC,sBAAsB;CAI/B"}
@@ -17,6 +17,7 @@ const common_1 = require("@nestjs/common");
17
17
  const jwt_1 = require("@nestjs/jwt");
18
18
  const core_1 = require("@nestjs/core");
19
19
  const decorators_1 = require("./decorators");
20
+ const dto_1 = require("../dto");
20
21
  let AuthGuard = class AuthGuard {
21
22
  constructor(jwtService, jwtSecret, reflector) {
22
23
  this.jwtService = jwtService;
@@ -35,7 +36,7 @@ let AuthGuard = class AuthGuard {
35
36
  const request = context.switchToHttp().getRequest();
36
37
  const token = this.extractTokenFromHeader(request);
37
38
  if (!token) {
38
- throw new common_1.UnauthorizedException();
39
+ throw new dto_1.BaseError(dto_1.BaseResponseDto.CODE_NOT_FOUND, '令牌缺失');
39
40
  }
40
41
  try {
41
42
  const payload = await this.jwtService.verifyAsync(token, {
@@ -46,7 +47,7 @@ let AuthGuard = class AuthGuard {
46
47
  request.user = payload;
47
48
  }
48
49
  catch (_a) {
49
- throw new common_1.UnauthorizedException();
50
+ throw new dto_1.BaseError(dto_1.BaseResponseDto.CODE_UNAUTHORIZED, '令牌无效');
50
51
  }
51
52
  return true;
52
53
  }
@@ -1,2 +1,3 @@
1
1
  export * from './base-error.filter';
2
+ export * from './validation-exception.filter';
2
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/filters/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/filters/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC;AACpC,cAAc,+BAA+B,CAAC"}
@@ -15,3 +15,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./base-error.filter"), exports);
18
+ __exportStar(require("./validation-exception.filter"), exports);
@@ -0,0 +1,5 @@
1
+ import { ExceptionFilter, ArgumentsHost, BadRequestException } from '@nestjs/common';
2
+ export declare class ValidationExceptionFilter implements ExceptionFilter {
3
+ catch(exception: BadRequestException, host: ArgumentsHost): void;
4
+ }
5
+ //# sourceMappingURL=validation-exception.filter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validation-exception.filter.d.ts","sourceRoot":"","sources":["../../src/filters/validation-exception.filter.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EAEf,aAAa,EACb,mBAAmB,EAEpB,MAAM,gBAAgB,CAAC;AAGxB,qBACa,yBAA0B,YAAW,eAAe;IAC/D,KAAK,CAAC,SAAS,EAAE,mBAAmB,EAAE,IAAI,EAAE,aAAa;CA2B1D"}
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ 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;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.ValidationExceptionFilter = void 0;
10
+ const common_1 = require("@nestjs/common");
11
+ let ValidationExceptionFilter = class ValidationExceptionFilter {
12
+ catch(exception, host) {
13
+ const ctx = host.switchToHttp();
14
+ const response = ctx.getResponse();
15
+ const exceptionResponse = exception.getResponse();
16
+ // Check if it's a validation error (has array of messages)
17
+ if (exceptionResponse.message && Array.isArray(exceptionResponse.message)) {
18
+ // Format validation errors in your BaseResponse format
19
+ const errorResponse = {
20
+ code: 400, // BaseResponseDto.CODE_BAD_REQUEST
21
+ msg: exceptionResponse.message.join(', '),
22
+ data: null,
23
+ };
24
+ response.status(common_1.HttpStatus.BAD_REQUEST).json(errorResponse);
25
+ }
26
+ else {
27
+ // Handle other BadRequestExceptions
28
+ const errorResponse = {
29
+ code: 400,
30
+ msg: exceptionResponse.message || 'Bad Request',
31
+ data: null,
32
+ };
33
+ response.status(common_1.HttpStatus.BAD_REQUEST).json(errorResponse);
34
+ }
35
+ }
36
+ };
37
+ exports.ValidationExceptionFilter = ValidationExceptionFilter;
38
+ exports.ValidationExceptionFilter = ValidationExceptionFilter = __decorate([
39
+ (0, common_1.Catch)(common_1.BadRequestException)
40
+ ], ValidationExceptionFilter);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saihu/common",
3
- "version": "1.1.6",
3
+ "version": "1.1.8",
4
4
  "description": "Common utilities for NestJS applications",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",