import { isEither, ITraceable, PenguenoRequest, PenguenoResponse, ResponseOpts, ServerTrace, } from '@emprespresso/pengueno'; type Jsonable = any; export class JsonResponse extends PenguenoResponse { constructor(req: ITraceable, e: Jsonable, _opts: ResponseOpts) { const opts = { ..._opts, headers: { ..._opts.headers, 'Content-Type': 'application/json' } }; if (isEither(e)) { super( req, JSON.stringify( e.fold( (error) => ({ error, ok: undefined }), (ok) => ({ ok }), ), ), opts, ); return; } super(req, JSON.stringify(Math.floor(opts.status / 100) > 4 ? { error: e } : { ok: e }), opts); } }