0.0.2 • Published 2 years ago

typed-restful v0.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

typed-restful

purpose

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.

usage

defineRoute

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);

createSerivce

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) => {});