leblebijs/example/index.ts

67 lines
1.5 KiB
TypeScript
Raw Permalink Normal View History

2022-05-21 21:36:48 -05:00
import { endpoint } from "../src";
import { startServer } from "../src/core/server";
2022-05-22 00:14:16 -05:00
import { JSONRequest, JSONResponse } from "../src/json";
2022-05-21 23:33:51 -05:00
import { route } from "../src/routing/router";
2022-05-21 02:31:26 -05:00
2022-05-21 21:36:48 -05:00
type Data = {
name: string;
};
2022-05-22 00:14:16 -05:00
type DataIn = {
name: string;
nickname: string;
};
type DataOut = {
message: string;
};
2022-05-21 21:36:48 -05:00
startServer(
2022-05-22 00:14:16 -05:00
endpoint(
// TODO this is ugly... I wonder if there's some way to reconcile the types
// here. Problem is input types not matching, so I think I need some way to
// tell TypeScript that it's cool and I'm going to match the input types
// myself.
route<any, any>([
2022-05-21 23:33:51 -05:00
{
2022-05-22 00:14:16 -05:00
app: async (req: JSONRequest<void>): Promise<JSONResponse<Data>> => {
2022-05-21 23:33:51 -05:00
return {
status: 200,
body: {
name: "foo",
},
};
},
method: "GET",
url: "/",
2022-05-21 21:36:48 -05:00
},
2022-05-21 23:33:51 -05:00
{
2022-05-22 00:14:16 -05:00
app: async (req: JSONRequest<void>): Promise<JSONResponse<Data>> => {
2022-05-21 23:33:51 -05:00
return {
status: 200,
body: {
name: "polo",
},
};
},
method: "GET",
url: "/marco",
},
2022-05-22 00:14:16 -05:00
{
app: async (
req: JSONRequest<DataIn>,
): Promise<JSONResponse<DataOut>> => {
return {
status: 200,
body: {
message: `Hello ${req.body.name}, your codename is ${req.body.nickname}`,
},
};
},
method: "POST",
url: "/hello",
},
2022-05-21 23:33:51 -05:00
]),
),
2022-05-21 21:36:48 -05:00
);