npm.io
4.5.0 • Published yesterday

@tc-libs/request

Licence
Version
4.5.0
Deps
13
Size
133 kB
Vulns
1
Weekly
0

@tc-libs/request

Layer request globale per NestJS.

Centralizza:

  • validation pipe globale
  • timeout globale
  • throttling
  • middleware HTTP
  • decorator request-aware
  • custom validators class-validator

Registrazione

RequestModule.register(
  {
    request: {
      timeout: 15000,
      timestamp: { toleranceTimeInMs: 30000 },
      userAgent: {
        enabled: true,
        browser: ['Chrome', 'Firefox'],
        os: ['Windows', 'Mac OS', 'Linux'],
      },
      throttle: {
        ttl: 60000,
        limit: 100,
      },
      body: {
        urlencoded: { maxFileSize: 1048576 },
        json: { maxFileSize: 1048576 },
        raw: { maxFileSize: 1048576 },
        text: { maxFileSize: 1048576 },
      },
      cors: {
        allowOrigin: true,
        allowMethod: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'],
        allowHeader: ['Content-Type', 'Authorization'],
      },
    },
    app: {
      production: false,
      globalPrefix: 'api',
      repoVersion: '1.0.0',
      versioning: {
        enable: true,
        prefix: 'v',
        version: '1',
      },
    },
    file: {
      audio: { maxFileSize: 10485760 },
      excel: { maxFileSize: 10485760 },
      image: { maxFileSize: 10485760 },
      video: { maxFileSize: 10485760 },
    },
  },
  true,
);

Cosa registra

  • ValidationPipe globale con Api422ValidationError
  • RequestTimeoutInterceptor globale
  • ThrottlerGuard globale
  • middleware per helmet, request id, body parser, cors, version, user-agent, timestamp, timezone

Decorator utili

  • RequestId()
  • RequestTimestamp()
  • RequestXTimestamp()
  • RequestCustomLang()
  • RequestUserAgent()
  • Session()
  • RequestParamGuard(...)
  • RequestValidateUserAgent()
  • RequestValidateTimestamp()
  • RequestTimeout('30s')

Esempio:

@Get()
@RequestValidateTimestamp()
list(@RequestId() requestId: string) {}

Validator custom

Il package espone validator riusabili come:

  • password weak/medium/strong
  • only digits
  • safe string
  • max/min date rispetto a oggi
  • max/min greater than
  • max binary file per tipo

Note operative

  • Gli endpoint SSE vengono esclusi dal timeout globale.
  • I metadati raccolti qui vengono poi usati da response ed error-handler.

Sviluppo

nx build request
nx test request