add example for POST

This commit is contained in:
Kaan Barmore-Genç 2022-05-22 01:14:16 -04:00
parent 29bd394ebe
commit 5c181a098e

View file

@ -1,16 +1,30 @@
import { endpoint } from "../src"; import { endpoint } from "../src";
import { startServer } from "../src/core/server"; import { startServer } from "../src/core/server";
import { JSONRequest, JSONResponse } from "../src/json";
import { route } from "../src/routing/router"; import { route } from "../src/routing/router";
type Data = { type Data = {
name: string; name: string;
}; };
type DataIn = {
name: string;
nickname: string;
};
type DataOut = {
message: string;
};
startServer( startServer(
endpoint<void, Data>( endpoint(
route([ // 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) => { app: async (req: JSONRequest<void>): Promise<JSONResponse<Data>> => {
return { return {
status: 200, status: 200,
body: { body: {
@ -22,7 +36,7 @@ startServer(
url: "/", url: "/",
}, },
{ {
app: async (req) => { app: async (req: JSONRequest<void>): Promise<JSONResponse<Data>> => {
return { return {
status: 200, status: 200,
body: { body: {
@ -33,6 +47,20 @@ startServer(
method: "GET", method: "GET",
url: "/marco", 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",
},
]), ]),
), ),
); );