-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokens.ts
More file actions
509 lines (482 loc) · 12.9 KB
/
Copy pathtokens.ts
File metadata and controls
509 lines (482 loc) · 12.9 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
import type { ComponentChildren, FunctionComponent } from "preact";
import type { Schema } from "@baetheus/fun/schemable";
import * as Option from "@baetheus/fun/option";
import * as Refinement from "@baetheus/fun/refinement";
import type { Handler, Methods } from "./router.ts";
const PartialRouteSymbol = "PARTIAL_ROUTE" as const;
type PartialRouteSymbol = typeof PartialRouteSymbol;
/**
* Config object for PartialRoute builders with typed params.
*
* @example
* ```ts
* import type { PartialRouteConfig } from "@baetheus/pick/tokens";
*
* const config: PartialRouteConfig<{ id: string }> = {
* params: schema(s => s.struct({ id: s.string() })),
* handler: myHandler,
* };
* ```
*
* @since 0.1.0
*/
export type PartialRouteConfig<P, B = unknown, O = unknown> = {
readonly params?: Schema<P>;
readonly body?: Schema<B>;
readonly output?: Schema<O>;
readonly handler: Handler;
};
/**
* A partial route definition containing method, handler, and optional schemas.
*
* @example
* ```ts
* import type { PartialRoute } from "@baetheus/pick/tokens";
* import { get } from "@baetheus/pick/tokens";
*
* const route: PartialRoute = get(myHandler);
* ```
*
* @since 0.1.0
*/
export type PartialRoute = {
readonly type: PartialRouteSymbol;
readonly method: Methods;
readonly handler: Handler;
readonly params_schema: Option.Option<Schema<unknown>>;
readonly body_schema: Option.Option<Schema<unknown>>;
readonly output_schema: Option.Option<Schema<unknown>>;
};
/**
* Creates a PartialRoute with the given method and handler.
*
* @example
* ```ts
* import { partial_route } from "@baetheus/pick/tokens";
* import * as Option from "@baetheus/fun/option";
*
* const route = partial_route("GET", myHandler, Option.none, Option.none, Option.none);
* ```
*
* @since 0.1.0
*/
export function partial_route(
method: Methods,
handler: Handler,
params_schema: Option.Option<Schema<unknown>> = Option.none,
body_schema: Option.Option<Schema<unknown>> = Option.none,
output_schema: Option.Option<Schema<unknown>> = Option.none,
): PartialRoute {
return {
type: PartialRouteSymbol,
method,
handler,
params_schema,
body_schema,
output_schema,
};
}
/**
* Type guard for PartialRoute.
*
* @example
* ```ts
* import { is_partial_route, get } from "@baetheus/pick/tokens";
*
* const route = get(myHandler);
* is_partial_route(route); // true
* is_partial_route({}); // false
* ```
*
* @since 0.1.0
*/
export function is_partial_route(value: unknown): value is PartialRoute {
return Refinement.isRecord(value) &&
"type" in value &&
value.type === PartialRouteSymbol;
}
/**
* Function type for building partial routes from handlers.
* Supports both simple handler form and config form with typed params.
*
* @example
* ```ts
* import type { MethodBuilder } from "@baetheus/pick/tokens";
* import { get, post } from "@baetheus/pick/tokens";
*
* // Both get and post are MethodBuilders
* const getRoute = get(myHandler);
* const postRoute = post({ params: mySchema, handler: myHandler });
* ```
*
* @since 0.2.0
*/
export type MethodBuilder = {
(handler: Handler): PartialRoute;
<P, B = unknown, O = unknown>(
config: PartialRouteConfig<P, B, O>,
): PartialRoute;
};
/**
* Checks if input is a PartialRouteConfig object.
*
* @since 0.1.0
*/
function is_config<P, B, O>(
input: Handler | PartialRouteConfig<P, B, O>,
): input is PartialRouteConfig<P, B, O> {
return Refinement.isRecord(input) && "handler" in input &&
("params" in input || "body" in input || "output" in input);
}
/**
* Creates a PartialRoute builder for the given HTTP method.
*
* Supports two calling conventions:
* - `method(handler)` - params is `unknown`
* - `method({ params, body, output, handler })` - schemas typed via schema
*
* @since 0.1.0
*/
function create_method_builder(method: Methods): MethodBuilder {
function builder(handler: Handler): PartialRoute;
function builder<P, B, O>(config: PartialRouteConfig<P, B, O>): PartialRoute;
function builder<P, B, O>(
input: Handler | PartialRouteConfig<P, B, O>,
): PartialRoute {
if (is_config(input)) {
return partial_route(
method,
input.handler,
input.params
? Option.some(input.params as Schema<unknown>)
: Option.none,
input.body ? Option.some(input.body as Schema<unknown>) : Option.none,
input.output
? Option.some(input.output as Schema<unknown>)
: Option.none,
);
}
return partial_route(method, input, Option.none, Option.none, Option.none);
}
return builder;
}
/**
* Creates a GET route handler.
*
* @example
* ```ts
* // Simple form - params is unknown
* export const get = B.get(E.gets((req, params, ctx) => {
* return R.text("Hello");
* }));
*
* // Config form - params is typed
* export const get = B.get({
* params: schema(s => s.struct({ id: s.string() })),
* handler: E.gets((req, params, ctx) => {
* return R.text(`ID: ${params.id}`);
* }),
* });
* ```
*
* @since 0.1.0
*/
export const get: MethodBuilder = create_method_builder("GET");
/**
* Creates a POST route handler.
*
* @example
* ```ts
* import { post } from "@baetheus/pick/tokens";
* import * as E from "@baetheus/fun/effect";
* import * as R from "@baetheus/pick/router";
*
* export const create = post(E.gets(async (req, params, ctx) => {
* const body = await req.json();
* return R.json(JSON.stringify({ created: body }), R.STATUS_CODE.Created);
* }));
* ```
*
* @since 0.1.0
*/
export const post: MethodBuilder = create_method_builder("POST");
/**
* Creates a PUT route handler.
*
* @example
* ```ts
* import { put } from "@baetheus/pick/tokens";
* import * as E from "@baetheus/fun/effect";
* import * as R from "@baetheus/pick/router";
*
* export const update = put(E.gets(async (req, params, ctx) => {
* const body = await req.json();
* return R.json(JSON.stringify({ updated: body }));
* }));
* ```
*
* @since 0.1.0
*/
export const put: MethodBuilder = create_method_builder("PUT");
/**
* Creates a DELETE route handler.
*
* @example
* ```ts
* import { del } from "@baetheus/pick/tokens";
* import * as E from "@baetheus/fun/effect";
* import * as R from "@baetheus/pick/router";
*
* export const remove = del(E.gets((req, params, ctx) => {
* return R.text("Deleted", R.STATUS_CODE.NoContent);
* }));
* ```
*
* @since 0.1.0
*/
export const del: MethodBuilder = create_method_builder("DELETE");
/**
* Creates a PATCH route handler.
*
* @example
* ```ts
* import { patch } from "@baetheus/pick/tokens";
* import * as E from "@baetheus/fun/effect";
* import * as R from "@baetheus/pick/router";
*
* export const modify = patch(E.gets(async (req, params, ctx) => {
* const updates = await req.json();
* return R.json(JSON.stringify({ patched: updates }));
* }));
* ```
*
* @since 0.1.0
*/
export const patch: MethodBuilder = create_method_builder("PATCH");
/**
* Creates a HEAD route handler.
*
* @example
* ```ts
* import { head } from "@baetheus/pick/tokens";
* import * as E from "@baetheus/fun/effect";
* import * as R from "@baetheus/pick/router";
*
* export const check = head(E.gets((req, params, ctx) => {
* return new Response(null, { status: 200 });
* }));
* ```
*
* @since 0.1.0
*/
export const head: MethodBuilder = create_method_builder("HEAD");
/**
* Creates an OPTIONS route handler.
*
* @example
* ```ts
* import { options } from "@baetheus/pick/tokens";
* import * as E from "@baetheus/fun/effect";
* import * as R from "@baetheus/pick/router";
*
* export const cors = options(E.gets((req, params, ctx) => {
* return new Response(null, {
* headers: { "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE" }
* });
* }));
* ```
*
* @since 0.1.0
*/
export const options: MethodBuilder = create_method_builder("OPTIONS");
const ClientPageSymbol = "CLIENT_PAGE" as const;
type ClientPageSymbol = typeof ClientPageSymbol;
/**
* Marker type for client page routes.
* Files with this as default export are included in the SPA router.
*
* @example
* ```ts
* import type { ClientPage } from "@baetheus/pick/tokens";
* import { client_route } from "@baetheus/pick/tokens";
*
* const MyPage = () => <div>Hello</div>;
* export const page: ClientPage<"ClientRoute"> = client_route.create(MyPage);
* ```
*
* @since 0.3.0
*/
export type ClientPage<T extends string = string, P = unknown> = {
readonly type: ClientPageSymbol;
readonly tag: T;
readonly component: FunctionComponent<P>;
};
// deno-lint-ignore no-explicit-any
type ClientPageFactory<T extends string, U = any> = {
readonly create: <P extends U>(
component: FunctionComponent<P>,
) => ClientPage<T, P>;
readonly refine: (value: unknown) => value is ClientPage<T, unknown>;
};
// deno-lint-ignore no-explicit-any
function create_client_page<T extends string, U = any>(
tag: T,
): ClientPageFactory<T, U> {
return {
create: <P extends U>(component: FunctionComponent<P>) => ({
type: ClientPageSymbol,
tag,
component,
}),
refine: Refinement.struct({
type: Refinement.literal(ClientPageSymbol),
tag: Refinement.literal(tag),
component: (c): c is FunctionComponent<unknown> =>
typeof c === "function",
}),
};
}
/**
* Factory for creating ClientRoute tagged pages.
*
* When a ClientRoute tagged ClientPage is exported from a file it designates to
* the client builder that the file should be built into the SPA client router.
*
* @example
* ```ts
* import { client_route } from "@baetheus/pick/tokens";
*
* const AboutPage = () => <div>About Us</div>;
* export const page = client_route.create(AboutPage);
* ```
*
* @since 0.3.0
*/
export const client_route: ClientPageFactory<"ClientRoute"> =
create_client_page(
"ClientRoute",
);
/**
* Factory for creating ClientDefaultRoute tagged pages.
*
* When a ClientDefaultRoute tagged ClientPage is exported from a file it
* designates that route as the fallback route for the SPA client.
*
* @example
* ```ts
* import { client_default } from "@baetheus/pick/tokens";
*
* const NotFoundPage = () => <div>404 - Page Not Found</div>;
* export const page = client_default.create(NotFoundPage);
* ```
*
* @since 0.3.0
*/
export const client_default: ClientPageFactory<"ClientDefaultRoute"> =
create_client_page(
"ClientDefaultRoute",
);
/**
* Parameters passed to ClientIndex components for rendering the HTML shell.
*
* @example
* ```ts
* import type { ClientIndexParameters } from "@baetheus/pick/tokens";
*
* const IndexPage = ({ scripts, styles, title }: ClientIndexParameters) => (
* <html>
* <head><title>{title}</title></head>
* <body><div id="app" /></body>
* </html>
* );
* ```
*
* @since 0.3.0
*/
export type ClientIndexParameters = {
readonly scripts: readonly string[];
readonly styles: readonly string[];
readonly title: string;
};
/**
* Factory for creating ClientIndex tagged pages.
*
* When a ClientIndex tagged ClientPage is exported from a file it designates to
* the client builder that the file should be used to construct the root
* index.html static file and the default / route for the builder application.
*
* @example
* ```ts
* import { client_index, ClientIndexParameters } from "@baetheus/pick/tokens";
*
* const IndexPage = ({ scripts, styles, title }: ClientIndexParameters) => (
* <html>
* <head>
* <title>{title}</title>
* {styles.map(s => <link rel="stylesheet" href={s} />)}
* </head>
* <body>
* <div id="app" />
* {scripts.map(s => <script type="module" src={s} />)}
* </body>
* </html>
* );
* export const index = client_index.create(IndexPage);
* ```
*
* @since 0.3.0
*/
export const client_index: ClientPageFactory<
"ClientIndex",
ClientIndexParameters
> = create_client_page(
"ClientIndex",
);
/**
* Parameters passed to ClientWrapper components for wrapping the SPA router.
*
* @example
* ```ts
* import type { ClientWrapperParameters } from "@baetheus/pick/tokens";
*
* const AppWrapper = ({ children }: ClientWrapperParameters) => (
* <div class="app-container">
* <nav>Navigation</nav>
* {children}
* <footer>Footer</footer>
* </div>
* );
* ```
*
* @since 0.3.0
*/
export type ClientWrapperParameters = {
readonly children: ComponentChildren;
};
/**
* Factory for creating ClientWrapper tagged pages.
*
* When a ClientWrapper tagged ClientPage is exported from a file it designates
* that the constructed SPA router should be nested within this component.
*
* @example
* ```ts
* import { client_wrapper, ClientWrapperParameters } from "@baetheus/pick/tokens";
*
* const AppWrapper = ({ children }: ClientWrapperParameters) => (
* <div class="app">
* <header>My App</header>
* <main>{children}</main>
* </div>
* );
* export const wrapper = client_wrapper.create(AppWrapper);
* ```
*
* @since 0.3.0
*/
export const client_wrapper: ClientPageFactory<
"ClientWrapper",
ClientWrapperParameters
> = create_client_page(
"ClientWrapper",
);