Skip to content

Latest commit

 

History

65 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sift

A small routing and utility library for building HTTP servers on Deno.

JSR ci

Install

deno add jsr:@satya/sift

Or import it directly:

import { serve } from "jsr:@satya/sift";

serve()

serve() maps URL patterns to handlers and starts a server. Paths are matched with URLPattern; the matched groups are passed to the handler as the third argument. The 404 route handles requests that nothing else matches.

import { serve } from "@satya/sift";

serve({
  "/": () => new Response("hello world"),
  "/blog/:slug": (_request, _info, params) => {
    return new Response(`you visited ${params?.slug}`);
  },
  404: () => new Response("not found", { status: 404 }),
});

A handler may return a Response or a JSX element. serve() returns the Deno.HttpServer, so you can await server.shutdown() when you're done.

serveStatic()

Serve files from disk, resolved relative to baseUrl (usually import.meta.url). To serve a directory, end the route with :filename+. Files under 10MB are cached in memory unless you pass cache: false.

import { serve, serveStatic } from "@satya/sift";

serve({
  // A single file.
  "/": serveStatic("public/index.html", { baseUrl: import.meta.url }),
  // A directory of files.
  "/assets/:filename+": serveStatic("public", { baseUrl: import.meta.url }),
  // Modify the response before it's sent.
  "/style.css": serveStatic("style.css", {
    baseUrl: import.meta.url,
    intervene: (_request, response) => response,
  }),
});

json()

Serialize a value to JSON and return it as an application/json response.

import { json, serve } from "@satya/sift";

serve({
  "/": () => json({ message: "hello world" }),
  "/create": () => json({ message: "created" }, { status: 201 }),
});

jsx()

Render a JSX element to HTML and return it as a text/html response. Configure the JSX runtime in your deno.json:

{
  "compilerOptions": {
    "jsx": "react-jsx",
    "jsxImportSource": "preact"
  }
}
import { jsx, serve } from "@satya/sift";

const App = () => <h1>Hello world!</h1>;

serve({
  "/": () => jsx(<App />),
  404: () => jsx(<h1>Page not found</h1>, { status: 404 }),
});

validateRequest()

Check that a request uses an allowed method and includes the named headers, query params and body fields. The parsed body is returned so you don't have to read it again.

import { serve, validateRequest } from "@satya/sift";

serve({
  "/": async (request) => {
    const { error, body } = await validateRequest(request, {
      POST: { body: ["name"] },
    });
    if (error) return new Response(error.message, { status: error.status });
    return new Response(`hello ${body?.name}`);
  },
});

License

MIT

About

Sift is a routing and utility library for Deno Deploy.

Topics

Resources

Stars

168 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages