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 { 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<void, Data>(
route([
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) => {
app: async (req: JSONRequest<void>): Promise<JSONResponse<Data>> => {
return {
status: 200,
body: {
@ -22,7 +36,7 @@ startServer(
url: "/",
},
{
app: async (req) => {
app: async (req: JSONRequest<void>): Promise<JSONResponse<Data>> => {
return {
status: 200,
body: {
@ -33,6 +47,20 @@ startServer(
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",
},
]),
),
);