5

Resty: a tiny, radix-tree based library for building RESTful APIs

 3 years ago
source link: https://dev.to/brielov/resty-a-tiny-radix-tree-based-library-for-building-restful-apis-166a
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Resty: a tiny, radix-tree based library for building RESTful APIs

A few days ago I posted about a library I made called typed and it never happened to me before that people would actually use something I made. The thing is that I made typed to be a companion library to resty which is a thin wrapper around Node's http core module.

Resty is what I think is the best way I'd like to write RESTful APIs in Node. It borrows some of its syntax from the fetch API and it is of course type-safe. Let me show you how I'd like to write my endpoints.

Having an endpoint of about three lines of code is pretty nice to me, specially because there's type-safety involved. Take a closer look at request.json and request.query. They both require a typed function to be passed in. If the validation fails, a 400 Bad Request error is returned with a detailed explanation of what went wrong.

Also, postMovie and getMovies are typed because it is inferring the types from the Response object, so it knows that getMovies returns a Response<Movie[]> and postMovie returns a Response<Movie>.

The example above shows multiple endpoints being written in a single file. I personally don't don't use it like this (I prefer to write endpoints in separate files) but for demonstration purposes I'll leave it like this.

So how do we glue the endpoints together? Well, we can use the createHandler function to combine all our endpoints into a single request handler that can be use directly with http.createServer.

import { createServer } from "http";
import { createHandler } from "resty";
import { postMovie, getMovies } from "./endpoints";

const handler = createHandler(postMovie, getMovies);
const server = createServer(handler);

server.listen(4000, () => console.log("Listening on port 4000"));
Enter fullscreen modeExit fullscreen mode

Right now you're thinking "but this doesn't support middleware, how would I enable cors?" Well, resty is just a good old request handler, so you could actually combine it with connect.

// app.ts

import connect from "connect";
import logger from "morgan";
import cors from "cors";
import { createHandler } from "resty";
import { postMovie, getMovies } from "./endpoints";

const handler = createHandler(postMovie, getMovies);

export const app = connect();

app.use(logger("dev"));
app.use(cors());
app.use(handler);
Enter fullscreen modeExit fullscreen mode

The nice thing about doing things this way is that you can test your endpoints the same way you would test an express application with supertest.

I know all cool kids are writing Serverless APIs these days, but I still enjoy writing APIs the old way for simple, personal projects. Anyways, I thought of sharing this library too and hope somebody will benefit from it.

Cheers!

GitHub logo brielov / resty

Syntactic sugar for RESTful APIs

resty

Syntactic sugar for RESTful APIs.

resty adds a very thin layer on top of Node's core http module that provides a beautiful, simple, and easy to use API for building RESTful APIs. It is mostly syntactic sugar, but it also provides a few additional features.

  • A really fast, radix-tree based routing system that allows you to define routes in a declarative way.
  • A simple and powerful way to validate request parameters and JSON payloads using typed (direct dependency).
  • Built-in error handling that automatically generates error responses for you.
  • Separation of concerns: write your http handlers in a modular way, and use the createHandler function to glue them all together.
  • Type-safe request and response objects.

Note that at the time of writing resty has not been fully tested in the real world and is not recommended for production use (yet). You can play around with it on small, personal projects…


Recommend

  • 47

    The Adaptive Radix Tree: ARTful Indexing for Main-Memory Databases Leis et. al., ICDE 2013 [ paper ]

  • 55

    How Radix trees made blocking IPs 5000 times faster January 15, 2019 | InDev | ByÉmile-Hugo To most people, what you learn during a...

  • 76
    • my.oschina.net 6 years ago
    • Cache

    Redis radix tree源码解析 原 荐

    Redis实现了不定长压缩前缀的radix tree,用在集群模式下存储slot对应的的所有key信息。本文将详述在Redis中如何实现radix tree。 核心数据结构 raxNode是radix tree的核心数据结构,其结构体如下代码所示:...

  • 50
    • www.tuicool.com 5 years ago
    • Cache

    radix - a fast string sort algorithm

    Your basic radix sort A fast string sorting algorithm This is an optimized sorting algorithm equivalent to sort.Strings in the Go standard library. For string sorting, a carefully impl...

  • 31
    • Github github.com 4 years ago
    • Cache

    Adaptive Radix Tree library for Zig

    Features This library provides a zig implementation of the Adaptive Radix Tree or ART. The ART operates similar to a traditional radix tree but avoids the wasted space of internal nodes by changing the node size....

  • 12

    写了一款REST框架——RESTY 2010-12-04 关于REST的介绍可以参考我之前的文章,总体说来,REST是web发展的趋势,而PHP是web开发的利器,但我找了一遍,只找到了两个PHP REST框架(不包括那些以MVC为核心,同时又支持RE...

  • 4

    关于 REST 的介绍可以参考我之前的文章,总体说来,REST 是 web 发展的趋势,而 PHP 是 web 开发的利器,但我找了一遍,只找到了两个 PHP REST 框架(不包括那些以 MVC 为核心,同时又支持 RE...

  • 8

    Ranked #4 for todayRadix UIUnstyled React components for building UIRadix UI is a set of 26 unstyled React components for building...

  • 9
    • www.producthunt.com 1 year ago
    • Cache

    Radix UI icon library for React Native

    Radix UI icon library for React NativeIcons react native ui radixAn easy-to-use and customizable React Native icon library featuring Radix UI icons. Enhance your mobile applicatio...

  • 4

    Radix UI Icon Library for React Native Introduction This library provides a...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK