REST(Representational State Transfer) RESTful is always used in HTTP api design.
Here is a tool of using it in common service design, and of course support typescript.
just like define a server router
import { defineRoute, merge } from "typed-restful";
// const routeA: {
// topic: {
// ":id": {
// GET: () => void;
// };
// };
// }
const route1 = defineRoute("GET", "/topic/:id", (request: string) => {
return 123;
});
const route2 = defineRoute("POST", "/topic/:id", () => {});
const route3 = defineRoute("GET", "/topic", () => {});
const route4 = defineRoute("GET", "/file", (req?: {}) => {});
const routes = merge(route1, route2, route3, route4);
from routes create service
import { createService } from "typed-restful";
const service = createService(routes);
service.run("GET", "/file", undefined);
service.get("/file", undefined);
// @ts-expect-error
service.post("/file", undefined);
service.run("GET", "/topic/123", "123").then((v) => {});
// @ts-expect-error
service.run("GET", "/topic/123", 123).then((v) => {});
service.get("/topic/123", "123").then((v) => {});