vndevteam / nestjs-boilerplate

NestJS boilerplate with TypeORM, ExpressJS, Fastify, Jest, I18n & Vuepress docs
https://vndevteam.github.io/nestjs-boilerplate/
MIT License
127 stars 42 forks source link

[Feature] Customize Ok/Failed Response, Tracing Request/Response Error, Logger #217

Closed ngoctienle closed 2 months ago

ngoctienle commented 2 months ago

Hi anh, em thấy hiện tại source mình chưa có custom cho response. Kiểu hiện tại em thấy các dự án lớn có một structure response cho Ok riêng, Error riêng để phía client có thể define đc Interface từ đầu.

Ngoài ra, việc bổ sung thêm Tracing (trace_id) ở request header và response, Logger là điều cần thiết.

https://medium.com/@alexanderknips/open-telemetry-with-multiple-nestjs-and-logs-in-tracing-62453e3e6cb2

khuongln-1346 commented 2 months ago

@ngoctienle Hiện tại đã có sẵn x-request-id tương tự trace_id rồi đó em. Nó có sẵn khi tích hợp pino-logger https://github.com/vndevteam/nestjs-boilerplate/blob/develop/src/utils/logger-factory.ts#L103

khuongln-1346 commented 2 months ago

@ngoctienle Về phần failed response thì đã có define ở error.dto.ts rồi nè em

Còn Ok response thì anh đang không hướng theo kiểu

data: []

do thấy hơi rườm rà

Không biết ý em có phải vậy không?

ngoctienle commented 2 months ago

Dạ vâng, thường thì em thấy

@ngoctienle Về phần failed response thì đã có define ở error.dto.ts rồi nè em

Còn Ok response thì anh đang không hướng theo kiểu

data: []

do thấy hơi rườm rà

Không biết ý em có phải vậy không?

@khuongln-1346 Dạ vâng, thường thì em thấy các dự án hay có structure response riêng kiểu kiểu như này, và Error cũng thế

interface OkResponse<T> {
success: true
data: T
message: string
}
lamngockhuong commented 2 months ago

@ngoctienle Cái này chỉ là optional thôi em, nhiều team họ sẽ thấy rườm rà mà bỏ đi, nhiều team thì lại muốn quy chuẩn

Anh cũng từng code theo kiểu này trong một số dự án rồi, và thấy nó bất tiện + thừa

  1. success: có thể check bằng http status code
  2. data: lồng vào vậy tốn thêm 1 level
  3. message: thường với case success thì chả cần thiết phải có, mà case error thì đã có object error riêng rồi

Vì phần này implement không khó, nên nếu muốn implement thêm ở các project thì ae tự thêm em nhé. Boilerplate này mình giữ common một tý

ngoctienle commented 2 months ago

@lamngockhuong Thanks anh đã giải đáp, em hiểu rồi ạ.