From d754ec6adf0bb382bf7f7d7442d0927ad5b0653e Mon Sep 17 00:00:00 2001 From: Samuel Goulart Date: Tue, 19 Aug 2025 16:23:16 -0300 Subject: [PATCH] change(web-server): add content-type parsers for JSON and x-www-form-urlencoded --- TEMPLATE-CHANGELOG.md | 6 +++++ src/infra/http/util/web-server/web-server.ts | 26 ++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/TEMPLATE-CHANGELOG.md b/TEMPLATE-CHANGELOG.md index e77d61c..0e07689 100755 --- a/TEMPLATE-CHANGELOG.md +++ b/TEMPLATE-CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [2.4.1] - 2025-08-19 + +### Changed + +- Add content-type parsers for JSON and x-www-form-urlencoded. + ## [2.4.0] - 2025-05-08 ### Added diff --git a/src/infra/http/util/web-server/web-server.ts b/src/infra/http/util/web-server/web-server.ts index 3f2f81c..d96a7eb 100755 --- a/src/infra/http/util/web-server/web-server.ts +++ b/src/infra/http/util/web-server/web-server.ts @@ -90,6 +90,32 @@ export class WebServer { this.connections.add(socket); socket.on('close', () => this.connections.delete(socket)); }); + + fastify.addContentTypeParser( + 'application/json', + { parseAs: 'string' }, + function (_req, body, done) { + if (!body) return done(null, {}); + try { + done(null, JSON.parse(String(body))); + } catch (error) { + done(error as Error, undefined); + } + } + ); + + fastify.addContentTypeParser( + 'application/x-www-form-urlencoded', + { parseAs: 'string' }, + function (_req, body, done) { + try { + const parsed = Object.fromEntries(new URLSearchParams(String(body))); + done(null, parsed); + } catch (error) { + done(error as Error, undefined); + } + } + ); } public getWebsocketServer = () => this.socketIO;