@athenna/http 5.47.0 → 5.48.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/context/Response.d.ts +1 -0
- package/src/context/Response.js +10 -0
- package/src/exceptions/ResponseValidationException.d.ts +5 -0
- package/src/exceptions/ResponseValidationException.js +11 -0
- package/src/handlers/HttpExceptionHandler.js +1 -1
- package/src/router/RouteSchema.js +6 -55
- package/src/server/ServerImpl.js +1 -9
package/package.json
CHANGED
package/src/context/Response.js
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
*/
|
|
9
9
|
import { View } from '@athenna/view';
|
|
10
10
|
import { Macroable } from '@athenna/common';
|
|
11
|
+
import { parseResponseWithZod } from '#src/router/RouteSchema';
|
|
11
12
|
export class Response extends Macroable {
|
|
12
13
|
constructor(response, request) {
|
|
13
14
|
super();
|
|
@@ -126,10 +127,19 @@ export class Response extends Macroable {
|
|
|
126
127
|
* ```
|
|
127
128
|
*/
|
|
128
129
|
async send(data) {
|
|
130
|
+
const zodSchemas = this.getRouteZodSchemas();
|
|
131
|
+
if (zodSchemas) {
|
|
132
|
+
data = await parseResponseWithZod(this.response, data, zodSchemas);
|
|
133
|
+
}
|
|
129
134
|
await this.response.send(data);
|
|
130
135
|
this.response.body = data;
|
|
131
136
|
return this;
|
|
132
137
|
}
|
|
138
|
+
getRouteZodSchemas() {
|
|
139
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
140
|
+
// @ts-ignore
|
|
141
|
+
return this.response.request?.routeOptions?.config?.zod || null;
|
|
142
|
+
}
|
|
133
143
|
/**
|
|
134
144
|
* Terminate the request sending a file.
|
|
135
145
|
*
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { HttpException } from '#src/exceptions/HttpException';
|
|
2
|
+
export class ResponseValidationException extends HttpException {
|
|
3
|
+
constructor(error) {
|
|
4
|
+
const name = 'ResponseValidationException';
|
|
5
|
+
const code = 'E_RESPONSE_VALIDATION_ERROR';
|
|
6
|
+
const status = 500;
|
|
7
|
+
const message = 'The server failed to generate a valid response.';
|
|
8
|
+
const details = error.issues;
|
|
9
|
+
super({ name, message, status, code, details });
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -51,7 +51,7 @@ export class HttpExceptionHandler extends ExceptionHandler {
|
|
|
51
51
|
if (!isDebugMode) {
|
|
52
52
|
delete body.stack;
|
|
53
53
|
}
|
|
54
|
-
response.status(body.statusCode).send(body);
|
|
54
|
+
await response.status(body.statusCode).send(body);
|
|
55
55
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
56
56
|
// @ts-ignore
|
|
57
57
|
await super.handle({ error, response });
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
*/
|
|
9
9
|
import { Is } from '@athenna/common';
|
|
10
10
|
import { ZodValidationException } from '#src/exceptions/ZodValidationException';
|
|
11
|
+
import { ResponseValidationException } from '#src/exceptions/ResponseValidationException';
|
|
11
12
|
export function normalizeRouteSchema(options) {
|
|
12
13
|
const request = {};
|
|
13
14
|
const response = {};
|
|
@@ -45,10 +46,10 @@ export async function parseRequestWithZod(req, schemas) {
|
|
|
45
46
|
req.headers = await parseSchema(requestSchemas.headers, req.headers);
|
|
46
47
|
}
|
|
47
48
|
if (requestSchemas.params) {
|
|
48
|
-
req.params = await parseSchema(requestSchemas.params,
|
|
49
|
+
req.params = await parseSchema(requestSchemas.params, req.params);
|
|
49
50
|
}
|
|
50
51
|
if (requestSchemas.querystring) {
|
|
51
|
-
req.query = await parseSchema(requestSchemas.querystring,
|
|
52
|
+
req.query = await parseSchema(requestSchemas.querystring, req.query);
|
|
52
53
|
}
|
|
53
54
|
}
|
|
54
55
|
export async function parseResponseWithZod(reply, payload, schemas) {
|
|
@@ -56,7 +57,9 @@ export async function parseResponseWithZod(reply, payload, schemas) {
|
|
|
56
57
|
if (!schema) {
|
|
57
58
|
return payload;
|
|
58
59
|
}
|
|
59
|
-
return parseSchema(schema, payload)
|
|
60
|
+
return parseSchema(schema, payload).catch(error => {
|
|
61
|
+
throw new ResponseValidationException(error);
|
|
62
|
+
});
|
|
60
63
|
}
|
|
61
64
|
function getResponseSchema(statusCode, schemas) {
|
|
62
65
|
return (schemas[statusCode] ||
|
|
@@ -85,58 +88,6 @@ function toJsonSchema(schema, io) {
|
|
|
85
88
|
delete jsonSchema.$schema;
|
|
86
89
|
return jsonSchema;
|
|
87
90
|
}
|
|
88
|
-
function coerceDataForValidation(schema, data) {
|
|
89
|
-
return coerceDataByJsonSchema(toJsonSchema(schema, 'input'), data);
|
|
90
|
-
}
|
|
91
|
-
function coerceDataByJsonSchema(schema, data) {
|
|
92
|
-
if (Is.Undefined(data) || Is.Null(data) || !schema) {
|
|
93
|
-
return data;
|
|
94
|
-
}
|
|
95
|
-
if (schema.anyOf) {
|
|
96
|
-
return coerceWithAlternatives(schema.anyOf, data);
|
|
97
|
-
}
|
|
98
|
-
if (schema.oneOf) {
|
|
99
|
-
return coerceWithAlternatives(schema.oneOf, data);
|
|
100
|
-
}
|
|
101
|
-
if (schema.type === 'object' && Is.Object(data)) {
|
|
102
|
-
const coerced = { ...data };
|
|
103
|
-
const properties = schema.properties || {};
|
|
104
|
-
Object.entries(properties).forEach(([key, childSchema]) => {
|
|
105
|
-
if (!Object.hasOwn(coerced, key)) {
|
|
106
|
-
return;
|
|
107
|
-
}
|
|
108
|
-
coerced[key] = coerceDataByJsonSchema(childSchema, coerced[key]);
|
|
109
|
-
});
|
|
110
|
-
return coerced;
|
|
111
|
-
}
|
|
112
|
-
if (schema.type === 'array' && Is.Array(data) && schema.items) {
|
|
113
|
-
return data.map(item => coerceDataByJsonSchema(schema.items, item));
|
|
114
|
-
}
|
|
115
|
-
if (schema.type === 'number' || schema.type === 'integer') {
|
|
116
|
-
return coerceNumber(data, schema.type === 'integer');
|
|
117
|
-
}
|
|
118
|
-
return data;
|
|
119
|
-
}
|
|
120
|
-
function coerceWithAlternatives(schemas, data) {
|
|
121
|
-
let coerced = data;
|
|
122
|
-
schemas.forEach(schema => {
|
|
123
|
-
coerced = coerceDataByJsonSchema(schema, coerced);
|
|
124
|
-
});
|
|
125
|
-
return coerced;
|
|
126
|
-
}
|
|
127
|
-
function coerceNumber(value, integerOnly) {
|
|
128
|
-
if (!Is.String(value) || value.trim() === '') {
|
|
129
|
-
return value;
|
|
130
|
-
}
|
|
131
|
-
const parsed = integerOnly ? Number.parseInt(value, 10) : Number(value);
|
|
132
|
-
if (Number.isNaN(parsed)) {
|
|
133
|
-
return value;
|
|
134
|
-
}
|
|
135
|
-
if (integerOnly && !Number.isInteger(parsed)) {
|
|
136
|
-
return value;
|
|
137
|
-
}
|
|
138
|
-
return parsed;
|
|
139
|
-
}
|
|
140
91
|
function isZodSchema(value) {
|
|
141
92
|
return (Is.Defined(value) &&
|
|
142
93
|
Is.Function(value.parse) &&
|
package/src/server/ServerImpl.js
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* file that was distributed with this source code.
|
|
8
8
|
*/
|
|
9
9
|
import fastify from 'fastify';
|
|
10
|
-
import {
|
|
10
|
+
import { parseRequestWithZod, normalizeRouteSchema } from '#src/router/RouteSchema';
|
|
11
11
|
import { Options, Macroable, Is } from '@athenna/common';
|
|
12
12
|
import { FastifyHandler } from '#src/handlers/FastifyHandler';
|
|
13
13
|
export class ServerImpl extends Macroable {
|
|
@@ -196,7 +196,6 @@ export class ServerImpl extends Macroable {
|
|
|
196
196
|
onSend: [],
|
|
197
197
|
preValidation: [],
|
|
198
198
|
preHandler: [],
|
|
199
|
-
preSerialization: [],
|
|
200
199
|
onResponse: [],
|
|
201
200
|
url: options.url,
|
|
202
201
|
method: options.methods,
|
|
@@ -213,9 +212,6 @@ export class ServerImpl extends Macroable {
|
|
|
213
212
|
}
|
|
214
213
|
if (zodSchemas) {
|
|
215
214
|
route.preValidation = [async (req) => parseRequestWithZod(req, zodSchemas)];
|
|
216
|
-
route.preSerialization = [
|
|
217
|
-
async (_, reply, payload) => parseResponseWithZod(reply, payload, zodSchemas)
|
|
218
|
-
];
|
|
219
215
|
}
|
|
220
216
|
if (options.data && Is.Array(route.preHandler)) {
|
|
221
217
|
route.preHandler?.unshift((req, _, done) => {
|
|
@@ -231,10 +227,6 @@ export class ServerImpl extends Macroable {
|
|
|
231
227
|
...this.toRouteHooks(route.preValidation),
|
|
232
228
|
...this.toRouteHooks(fastifyOptions.preValidation)
|
|
233
229
|
];
|
|
234
|
-
fastifyOptions.preSerialization = [
|
|
235
|
-
...this.toRouteHooks(route.preSerialization),
|
|
236
|
-
...this.toRouteHooks(fastifyOptions.preSerialization)
|
|
237
|
-
];
|
|
238
230
|
}
|
|
239
231
|
this.fastify.route({ ...route, ...fastifyOptions });
|
|
240
232
|
}
|