{"slug": "every-response-from-my-api-looks-the-same-and-it-s-two-small-files-nestjs-in-a", "title": "Every response from my API looks the same, and it's two small files: NestJS interceptors in a real project", "summary": "A junior full-stack developer in Alexandria, Egypt, documented how FaceMark, a face-recognition attendance system built on a NestJS API and a Python AI service, uses two NestJS interceptors to standardize every API response and log each request. The TransformInterceptor wraps successful handler output in a uniform { success, data, timestamp } envelope, while the LoggingInterceptor emits one line per request with method, URL, status, IP and duration, which the developer used to compare a 40ms cache hit against multi-second face-recognition calls. The developer notes the transform only covers successful responses, since errors pass through a separate exception filter.", "body_md": "I'm a junior full-stack developer from Alexandria, Egypt. This is another post about FaceMark, my face-recognition attendance system (a NestJS API plus a Python AI service), and it's about the two smallest files in the repo that touch every request: the interceptors.\n\nAn API with twenty endpoints drifts. One controller returns the entity, another returns `{ message: \"ok\" }`, a third returns whatever the service felt like. The frontend ends up with a different parsing rule per screen, and nobody knows how slow anything is until a user complains.\n\nNestJS's answer is interceptors: code that wraps every handler, sees the request going in and the response going out, and can transform either. FaceMark uses two.\n\n```\n// src/common/interceptors/transform.interceptor.ts\nexport interface Response<T> {\n  success: boolean;\n  data: T;\n  timestamp: string;\n}\n\n@Injectable()\nexport class TransformInterceptor<T> implements NestInterceptor<T, Response<T>> {\n  intercept(context: ExecutionContext, next: CallHandler): Observable<Response<T>> {\n    return next.handle().pipe(\n      map((data) => ({\n        success: true,\n        data: data || null,\n        timestamp: new Date().toISOString(),\n      })),\n    );\n  }\n}\n```\n\nThirty lines, and every successful response from every endpoint now looks like `{ success: true, data: ..., timestamp: ... }`. The frontend writes its parsing logic once. `next.handle()` is the actual controller doing its work; the interceptor just maps over what comes back.\n\nThe honest limitation I learned while writing this: it only wraps successful responses. Errors go through the exception filter instead, which is a separate piece with its own shape. So \"every response looks the same\" really means \"every response has a defined shape\", and there are two of them.\n\n```\n// src/common/interceptors/logging.interceptor.ts\n@Injectable()\nexport class LoggingInterceptor implements NestInterceptor {\n  private readonly logger = new Logger('HTTP');\n\n  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {\n    const request = context.switchToHttp().getRequest();\n    const { method, url, ip } = request;\n    const now = Date.now();\n\n    return next.handle().pipe(\n      tap((data) => {\n        const response = context.switchToHttp().getResponse();\n        this.logger.log(\n          `${method} ${url} ${response.statusCode} - ${ip} - ${Date.now() - now}ms`,\n        );\n      }),\n    );\n  }\n}\n```\n\nEvery request gets one log line: method, URL, status, IP, and milliseconds. No APM tool, no dashboard, no config. When I was tuning the face-recognition call, the one that goes out to the Python service, these lines were how I saw the difference between a 40ms cache hit and a multi-second AI call without touching the endpoints themselves.\n\nThat's the real pitch for interceptors as a junior: cross-cutting concerns (logging, shaping, timing) live in one place, and your controllers stay about the business.\n\nIf you copy-paste response formatting into a second controller, stop and write an interceptor. The second copy is when it becomes a convention; the third is when it becomes a bug farm.\n\n`tap` vs `map` matters. `tap` observes (logging), `map` changes (transform). Mixing them up is the classic first interceptor bug: logging inside a `map` that forgets to return the data.\n\nInterceptors run outside-in, then inside-out. If you stack two, the first one registered sees the request first and the response last. Worth knowing before you wonder why your log line includes or excludes the transform step.\n\nEverything above is from the real repo.\n\nI'm a junior full-stack developer (Angular, NestJS, TypeScript, Python) in Alexandria, Egypt, open to junior roles and freelance work. More posts from this project:\n\nThe NestJS-to-Python boundary: [https://dev.to/ziadtaha/my-nestjs-api-talks-to-a-python-ai-microservice-heres-how-i-keep-the-boundary-honest-with-real-160g](https://dev.to/ziadtaha/my-nestjs-api-talks-to-a-python-ai-microservice-heres-how-i-keep-the-boundary-honest-with-real-160g)\n\nGitHub: [https://github.com/ZiadtahaM](https://github.com/ZiadtahaM)\n\nEmail: [ziad.taha3456@gmail.com](mailto:ziad.taha3456@gmail.com)\n\ndev.to: [https://dev.to/ziadtaha](https://dev.to/ziadtaha)", "url": "https://wpnews.pro/news/every-response-from-my-api-looks-the-same-and-it-s-two-small-files-nestjs-in-a", "canonical_source": "https://dev.to/ziadtaha/every-response-from-my-api-looks-the-same-and-its-two-small-files-nestjs-interceptors-in-a-real-4b46", "published_at": "2026-11-24 21:42:12+00:00", "updated_at": "2026-11-24 21:44:09.398922+00:00", "lang": "en", "topics": ["developer-tools", "ai-tools"], "entities": ["FaceMark", "NestJS", "Angular", "TypeScript", "Python"], "also_reported_by": [], "takeaway": "Wrapping every NestJS handler in a TransformInterceptor for a uniform { success, data, timestamp } envelope and a LoggingInterceptor using tap for per-request timing keeps cross-cutting concerns out of controllers, but errors still need a separate exception filter.", "recommendation": {"advice": "Move duplicated response shaping and request logging into NestJS interceptors: use map to transform responses and tap to log method, URL, status, IP and duration, and handle errors in a separate exception filter.", "playbook": "production-ops", "playbook_url": "https://wpnews.pro/playbooks/production-ops"}, "alternates": {"html": "https://wpnews.pro/news/every-response-from-my-api-looks-the-same-and-it-s-two-small-files-nestjs-in-a", "markdown": "https://wpnews.pro/news/every-response-from-my-api-looks-the-same-and-it-s-two-small-files-nestjs-in-a.md", "text": "https://wpnews.pro/news/every-response-from-my-api-looks-the-same-and-it-s-two-small-files-nestjs-in-a.txt", "jsonld": "https://wpnews.pro/news/every-response-from-my-api-looks-the-same-and-it-s-two-small-files-nestjs-in-a.jsonld"}}