From e421d75960f694091b27ab7ee7b2e82f429b31af Mon Sep 17 00:00:00 2001 From: Harvmaster Date: Mon, 3 Aug 2026 03:13:30 +0000 Subject: [PATCH 1/3] Add constants for status codes --- source/constants.ts | 17 +++++++++++++++++ source/services/route-stream.ts | 3 ++- 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 source/constants.ts diff --git a/source/constants.ts b/source/constants.ts new file mode 100644 index 0000000..140fe80 --- /dev/null +++ b/source/constants.ts @@ -0,0 +1,17 @@ +/** + * Success response status code. + */ +export const HTTP_STATUS_CODE_SUCCESS = 200; + +/** + * Created response status code. + */ +export const HTTP_STATUS_CODE_CREATED = 201; + +/** + * No content response status code. + */ +export const HTTP_STATUS_CODE_NO_CONTENT = 204; + + + diff --git a/source/services/route-stream.ts b/source/services/route-stream.ts index 9c41221..e3b1b6c 100644 --- a/source/services/route-stream.ts +++ b/source/services/route-stream.ts @@ -1,5 +1,6 @@ import type { RouteSendOptions, RouteStream } from '../routes/types.ts'; import type { BaseStream } from './stream/base-stream.ts'; +import { HTTP_STATUS_CODE_NO_CONTENT, HTTP_STATUS_CODE_SUCCESS } from '../constants.ts'; /** * Binds one application request to a connection-level stream. @@ -44,7 +45,7 @@ export class ApplicationRouteStream implements RouteStream { await this.connection.send({ ...(this.requestId === undefined ? {} : { id: this.requestId }), type, - statusCode: options.statusCode ?? (data === undefined ? 204 : 200), + statusCode: options.statusCode ?? (data === undefined ? HTTP_STATUS_CODE_NO_CONTENT : HTTP_STATUS_CODE_SUCCESS), body: data ?? null, }); From ed28dfbebbe5f8cb747c34b0b39792587f7ea35e Mon Sep 17 00:00:00 2001 From: Harvmaster Date: Mon, 3 Aug 2026 03:18:18 +0000 Subject: [PATCH 2/3] Add BAD_REQUEST status code --- source/constants.ts | 5 +++++ source/services/transport/http-transport.ts | 14 ++++++++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/source/constants.ts b/source/constants.ts index 9eff4c8..37b4606 100644 --- a/source/constants.ts +++ b/source/constants.ts @@ -13,6 +13,11 @@ export const HTTP_STATUS_CODE_CREATED = 201; */ export const HTTP_STATUS_CODE_NO_CONTENT = 204; +/** + * Bad request response status code. + */ +export const HTTP_STATUS_CODE_BAD_REQUEST = 400; + /** * HTTP status code for "Not Acceptable" error. */ diff --git a/source/services/transport/http-transport.ts b/source/services/transport/http-transport.ts index 5d7908c..ec8b2a6 100644 --- a/source/services/transport/http-transport.ts +++ b/source/services/transport/http-transport.ts @@ -3,13 +3,15 @@ import { streamSSE } from 'hono/streaming'; import { toExtendedJson, fromExtendedJson } from '@xo-cash/utils'; import type { Logger } from '../../utils/logger.ts'; -import { ApplicationError, normalizePublicError } from '../../errors/index.ts'; import type { ApplicationRequest, ApplicationRouter } from '../router.ts'; -import { HonoSSEStream } from '../stream/hono-sse-stream.ts'; -import { HttpRequestStream } from '../stream/http-request-stream.ts'; import type { StreamResponse } from '../stream/base-stream.ts'; import type { AppEnv, TransportRouter } from './transport-router.ts'; +import { ApplicationError, normalizePublicError } from '../../errors/index.ts'; +import { HTTP_STATUS_CODE_BAD_REQUEST, HTTP_STATUS_CODE_NO_CONTENT } from '../../constants.ts'; +import { HonoSSEStream } from '../stream/hono-sse-stream.ts'; +import { HttpRequestStream } from '../stream/http-request-stream.ts'; + /** Hono context key where decoded Extended JSON bodies are stored. */ const PARSED_BODY_KEY = 'parsedBody'; @@ -90,7 +92,7 @@ export class HttpTransportRouter implements TransportRouter { c.set(PARSED_BODY_KEY, parsed); } catch (error) { debug('invalid Extended JSON request: %O', error); - throw new ApplicationError(400, 'Invalid JSON in request body'); + throw new ApplicationError(HTTP_STATUS_CODE_BAD_REQUEST, 'Invalid JSON in request body'); } } @@ -209,8 +211,8 @@ export class HttpTransportRouter implements TransportRouter { * @param response - Buffered response from HttpRequestStream, if any. */ private static toResponse(response: StreamResponse | undefined): Response { - if (!response || response.statusCode === 204) { - return new Response(null, { status: 204 }); + if (!response || response.statusCode === HTTP_STATUS_CODE_NO_CONTENT) { + return new Response(null, { status: HTTP_STATUS_CODE_NO_CONTENT }); } return new Response(toExtendedJson(response.body), { From c17565ab24cbef05404d698de9890800002de7df Mon Sep 17 00:00:00 2001 From: Harvmaster Date: Mon, 3 Aug 2026 03:19:48 +0000 Subject: [PATCH 3/3] Add BAD_REQUEST status code --- source/services/transport/ws-transport.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/source/services/transport/ws-transport.ts b/source/services/transport/ws-transport.ts index 71a60f7..0f34bdd 100644 --- a/source/services/transport/ws-transport.ts +++ b/source/services/transport/ws-transport.ts @@ -7,11 +7,13 @@ import { toExtendedJson, fromExtendedJson } from '@xo-cash/utils'; import { z } from 'zod'; import type { Logger } from '../../utils/logger.ts'; -import { ApplicationError, normalizePublicError } from '../../errors/index.ts'; import type { ApplicationRequest, ApplicationRouter } from '../router.ts'; -import { WSStream } from '../stream/ws-stream.ts'; import type { AppEnv, UpgradeTransportRouter } from './transport-router.ts'; +import { ApplicationError, normalizePublicError } from '../../errors/index.ts'; +import { HTTP_STATUS_CODE_BAD_REQUEST } from '../../constants.ts'; +import { WSStream } from '../stream/ws-stream.ts'; + /** Default WebSocket upgrade path for application messages. */ const WS_ROUTE = '/ws'; @@ -198,7 +200,7 @@ export class WsTransportRouter implements UpgradeTransportRouter { try { decoded = fromExtendedJson(payload); } catch { - throw new ApplicationError(400, 'Invalid JSON in WebSocket message'); + throw new ApplicationError(HTTP_STATUS_CODE_BAD_REQUEST, 'Invalid JSON in WebSocket message'); } const envelope = wsRequestSchema.parse(decoded);