GithubHelp home page GithubHelp logo

oak-hooks's Introduction

oak-hooks

A wrapper of the oak middleware framework which exposes a functional hook-based API.

This library removes the need to manually manage the Application and Router instances in your code, lets you group middlewares into custom hooks, and lets you instantiate components in different ways according to you needs.

Most importantly, this library is interoperable with existing oak code, so you can use it with existing projects.

Example

The ubiquitous Hello World! program:

import { use, listen } from "https://deno.land/x/oak_hooks/mod.ts";

const App = () => {
  use((ctx) => {
    ctx.response.body = "Hello World!";
  });
}

await listen(App, { port: 8080 });

You would then run this script in Deno like:

> deno run --allow-net helloWorld.ts

listen() and routes()

With oak-hooks, there is no distinction between an Application and a Router component. That distinction is only made when the component is instantiated. Using listen() will instanciate the component as an Application, and using routes() will instanciate the component as a Router.

For convenience, you can use the get, post, head and other HTTP method hooks with the listen() function. Under the hood, a router will be instantiated and automatically used by the application instance.

Altough you shouldn't need to use them, we also provide the appify() and routerify() functions as escape hatches if you need to access the Application and Router instances directly.

An example:

import { get, use, routes, allowedMethods, listen } from "https://deno.land/x/oak_hooks/mod.ts";

const books = new Map<string, any>();
books.set("1", {
  id: "1",
  title: "The Hound of the Baskervilles",
  author: "Conan Doyle, Arthur",
});

const Router = () => {
  get("/book", (context) => {
    context.response.body = Array.from(books.values());
  });
  get("/book/:id", (context) => {
    if (context.params && context.params.id && books.has(context.params.id)) {
      context.response.body = books.get(context.params.id);
    }
  });
}

const App = () => {
  get("/", (context) => {
    context.response.body = "Hello world!";
  });
  
  use(routes(Router));
  use(allowedMethods(Router));
}

await listen(App, { port: 8080});

There are currently plans to provide an interface that could allow anyone to create their own instantiation functions. This could let people instantiate components into mock objects for testing, OpenAPI documentation, or anything else.

API

Hooks

Name Description
use() Register a middleware to be used with every request.
all() Register a middleware when the GET, POST, PUT or DELETE method is requested.
get() Register a middleware when the GET method is requested.
head() Register a middleware when the HEAD method is requested.
post() Register a middleware when the POST method is requested.
put() Register a middleware when the PUT method is requested.
del() Register a middleware when the DELETE method is requested.
connect() Register a middleware when the CONNECT method is requested.
options() Register a middleware when the OPTIONS method is requested.
patch() Register a middleware when the PATCH method is requested.
redirect() Register a redirection middleware from and to the specified paths.
prefix() Set the prefix for a router.

Instantiation functions

Name Description
listen() Creates a new Application instance and listen to the specified port.
routes() Creates a new Router instance and returns the registered routes.
allowedMethods() Creates a new Router instance and returns the allowed routes.
appify() Creates a new Application instance.
routerify() Creates a new Router instance.

oak-hooks's People

Contributors

tjosepo avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.