67 lines
1.5 KiB
TypeScript
67 lines
1.5 KiB
TypeScript
import { endpoint } from "../src";
|
|
import { startServer } from "../src/core/server";
|
|
import { JSONRequest, JSONResponse } from "../src/json";
|
|
import { route } from "../src/routing/router";
|
|
|
|
type Data = {
|
|
name: string;
|
|
};
|
|
|
|
type DataIn = {
|
|
name: string;
|
|
nickname: string;
|
|
};
|
|
|
|
type DataOut = {
|
|
message: string;
|
|
};
|
|
|
|
startServer(
|
|
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>([
|
|
{
|
|
app: async (req: JSONRequest<void>): Promise<JSONResponse<Data>> => {
|
|
return {
|
|
status: 200,
|
|
body: {
|
|
name: "foo",
|
|
},
|
|
};
|
|
},
|
|
method: "GET",
|
|
url: "/",
|
|
},
|
|
{
|
|
app: async (req: JSONRequest<void>): Promise<JSONResponse<Data>> => {
|
|
return {
|
|
status: 200,
|
|
body: {
|
|
name: "polo",
|
|
},
|
|
};
|
|
},
|
|
method: "GET",
|
|
url: "/marco",
|
|
},
|
|
{
|
|
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",
|
|
},
|
|
]),
|
|
),
|
|
);
|