-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin_server.ts
More file actions
138 lines (129 loc) · 3.76 KB
/
Copy pathplugin_server.ts
File metadata and controls
138 lines (129 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import * as D from "@baetheus/fun/decoder";
import * as Effect from "@baetheus/fun/effect";
import * as Either from "@baetheus/fun/either";
import { pipe } from "@baetheus/fun/fn";
import * as Router from "./router.ts";
import * as Tokens from "./tokens.ts";
import * as Builder from "./builder.ts";
/**
* Configuration options for the server plugin.
*
* @since 0.1.0
*/
export type ServerPluginOptions = {
readonly name: string;
readonly middleware: Router.Middleware<unknown>[];
readonly include_extensions: string[];
};
export function apply_schema_validation(
partial: Tokens.PartialRoute,
): Tokens.PartialRoute {
const { schema_handler, params_schema, body_schema } = partial;
if (!schema_handler && !body_schema && !params_schema) {
return partial;
}
const validated_handler: Router.Handler = async (req, params, ctx) => {
let decoded_params = params;
if (params_schema) {
const decoder = params_schema(D.SchemableDecoder);
const result = decoder(params);
if (Either.isLeft(result)) {
return [Either.left(Router.text(
D.draw(result.left),
Router.STATUS_CODE.BadRequest,
))];
}
decoded_params = result.right as URLPatternResult;
}
if (schema_handler) {
let decoded_body: unknown = undefined;
if (body_schema) {
let raw: unknown;
try {
raw = await req.json();
} catch {
return [Either.left(Router.text(
"Invalid JSON body",
Router.STATUS_CODE.BadRequest,
))];
}
const decoder = body_schema(D.SchemableDecoder);
const result = decoder(raw);
if (Either.isLeft(result)) {
return [Either.left(Router.text(
D.draw(result.left),
Router.STATUS_CODE.BadRequest,
))];
}
decoded_body = result.right;
}
const response = await schema_handler(
req,
decoded_params,
decoded_body,
ctx,
);
return [Either.right(response)];
}
// Plain handler with only params validation
return partial.handler(req, decoded_params, ctx);
};
return { ...partial, handler: validated_handler };
}
/**
* Creates a server plugin that scans files for exported PartialRoute tokens
* and converts them into full routes for the router.
*
* When a PartialRoute declares `params_schema` or `body_schema`, the plugin
* automatically validates incoming requests:
* - Invalid JSON body → 400 with parse error
* - Schema mismatch → 400 with decode error details
* - Valid request → handler receives decoded params and body
*
* @example
* ```ts
* import { server_plugin } from "@baetheus/pick/plugin_server";
*
* const plugin = server_plugin({
* name: "ApiPlugin",
* middleware: [authMiddleware],
* include_extensions: [".ts"],
* });
* ```
*
* @since 0.1.0
*/
export function server_plugin(
{
name = "DefaultServerPlugin",
middleware = [],
include_extensions = [".ts", ".tsx"],
}: Partial<ServerPluginOptions>,
): Builder.Plugin {
return {
name,
process_file: (file_entry) => {
if (!include_extensions.includes(file_entry.parsed_path.ext)) {
return Effect.right([]);
}
return pipe(
Builder.safe_import(file_entry.parsed_path),
Effect.map((exports) =>
Object.values(exports)
.filter(Tokens.is_partial_route)
.map((partial_route) =>
Builder.from_partial_route(
name,
file_entry,
Builder.wrap_partial_route(
apply_schema_validation(partial_route),
middleware,
),
)
)
),
);
},
process_build: (_routes) => Effect.right([]),
};
}