yedra 0.19.2 → 0.19.3
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/dist/routing/app.js
CHANGED
|
@@ -272,7 +272,11 @@ yedra_request_duration_sum{method="${method}",status="${status}"} ${data?.durati
|
|
|
272
272
|
return;
|
|
273
273
|
}
|
|
274
274
|
try {
|
|
275
|
-
|
|
275
|
+
const headers = Object.fromEntries(Object.entries(req.headers).map(([key, value]) => [
|
|
276
|
+
key,
|
|
277
|
+
Array.isArray(value) ? value.join(',') : (value ?? ''),
|
|
278
|
+
]));
|
|
279
|
+
await result.endpoint.handle(url, result.params, headers, ws);
|
|
276
280
|
}
|
|
277
281
|
catch (error) {
|
|
278
282
|
if (error instanceof HttpError) {
|
|
@@ -32,26 +32,29 @@ declare class YedraWebSocket {
|
|
|
32
32
|
close(code?: number, reason?: string): void;
|
|
33
33
|
}
|
|
34
34
|
export type { YedraWebSocket };
|
|
35
|
-
type WebSocketOptions<Params extends Record<string, Schema<unknown>>, Query extends Record<string, Schema<unknown>>> = {
|
|
35
|
+
type WebSocketOptions<Params extends Record<string, Schema<unknown>>, Query extends Record<string, Schema<unknown>>, Headers extends Record<string, Schema<unknown>>> = {
|
|
36
36
|
category: string;
|
|
37
37
|
summary: string;
|
|
38
38
|
description?: string;
|
|
39
39
|
params: Params;
|
|
40
40
|
query: Query;
|
|
41
|
+
headers: Headers;
|
|
41
42
|
do: (ws: YedraWebSocket, req: {
|
|
42
43
|
url: string;
|
|
43
44
|
params: Typeof<ObjectSchema<Params>>;
|
|
44
45
|
query: Typeof<ObjectSchema<Query>>;
|
|
46
|
+
headers: Typeof<ObjectSchema<Headers>>;
|
|
45
47
|
}) => Promise<void> | void;
|
|
46
48
|
};
|
|
47
49
|
export declare abstract class WsEndpoint {
|
|
48
|
-
abstract handle(url: URL, params: Record<string, string>, ws: NodeWebSocket): Promise<void>;
|
|
50
|
+
abstract handle(url: URL, params: Record<string, string>, headers: Record<string, string>, ws: NodeWebSocket): Promise<void>;
|
|
49
51
|
}
|
|
50
|
-
export declare class Ws<Params extends Record<string, Schema<unknown>>, Query extends Record<string, Schema<unknown>>> extends WsEndpoint {
|
|
52
|
+
export declare class Ws<Params extends Record<string, Schema<unknown>>, Query extends Record<string, Schema<unknown>>, Headers extends Record<string, Schema<unknown>>> extends WsEndpoint {
|
|
51
53
|
private options;
|
|
52
54
|
private paramsSchema;
|
|
53
55
|
private querySchema;
|
|
54
|
-
|
|
55
|
-
|
|
56
|
+
private headersSchema;
|
|
57
|
+
constructor(options: WebSocketOptions<Params, Query, Headers>);
|
|
58
|
+
handle(url: URL, params: Record<string, string>, headers: Record<string, string>, ws: NodeWebSocket): Promise<void>;
|
|
56
59
|
documentation(): object;
|
|
57
60
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { paramDocs } from '../util/docs.js';
|
|
2
2
|
import { ValidationError } from '../validation/error.js';
|
|
3
|
-
import { object } from '../validation/object.js';
|
|
3
|
+
import { laxObject, object } from '../validation/object.js';
|
|
4
4
|
import { BadRequestError } from './errors.js';
|
|
5
5
|
class YedraWebSocket {
|
|
6
6
|
constructor(ws) {
|
|
@@ -66,13 +66,16 @@ export class Ws extends WsEndpoint {
|
|
|
66
66
|
this.options = options;
|
|
67
67
|
this.paramsSchema = object(options.params);
|
|
68
68
|
this.querySchema = object(options.query);
|
|
69
|
+
this.headersSchema = laxObject(options.headers);
|
|
69
70
|
}
|
|
70
|
-
async handle(url, params, ws) {
|
|
71
|
+
async handle(url, params, headers, ws) {
|
|
71
72
|
let parsedParams;
|
|
72
73
|
let parsedQuery;
|
|
74
|
+
let parsedHeaders;
|
|
73
75
|
try {
|
|
74
76
|
parsedParams = this.paramsSchema.parse(params);
|
|
75
77
|
parsedQuery = this.querySchema.parse(Object.fromEntries(url.searchParams));
|
|
78
|
+
parsedHeaders = this.headersSchema.parse(headers);
|
|
76
79
|
}
|
|
77
80
|
catch (error) {
|
|
78
81
|
if (error instanceof ValidationError) {
|
|
@@ -84,6 +87,7 @@ export class Ws extends WsEndpoint {
|
|
|
84
87
|
url: url.pathname,
|
|
85
88
|
params: parsedParams,
|
|
86
89
|
query: parsedQuery,
|
|
90
|
+
headers: parsedHeaders,
|
|
87
91
|
});
|
|
88
92
|
return undefined;
|
|
89
93
|
}
|
|
@@ -91,6 +95,7 @@ export class Ws extends WsEndpoint {
|
|
|
91
95
|
const parameters = [
|
|
92
96
|
...paramDocs(this.options.params, 'path', []),
|
|
93
97
|
...paramDocs(this.options.query, 'query', []),
|
|
98
|
+
...paramDocs(this.options.headers, 'header', []),
|
|
94
99
|
];
|
|
95
100
|
return {
|
|
96
101
|
tags: [this.options.category],
|