From 5c181a098e7d38e88a7caa31e79195622b10fef4 Mon Sep 17 00:00:00 2001 From: Kaan Barmore-Genc Date: Sun, 22 May 2022 01:14:16 -0400 Subject: [PATCH] add example for POST --- example/index.ts | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/example/index.ts b/example/index.ts index 1d931d0..7375000 100644 --- a/example/index.ts +++ b/example/index.ts @@ -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( - 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([ { - app: async (req) => { + app: async (req: JSONRequest): Promise> => { return { status: 200, body: { @@ -22,7 +36,7 @@ startServer( url: "/", }, { - app: async (req) => { + app: async (req: JSONRequest): Promise> => { return { status: 200, body: { @@ -33,6 +47,20 @@ startServer( method: "GET", url: "/marco", }, + { + app: async ( + req: JSONRequest, + ): Promise> => { + return { + status: 200, + body: { + message: `Hello ${req.body.name}, your codename is ${req.body.nickname}`, + }, + }; + }, + method: "POST", + url: "/hello", + }, ]), ), );