@helloandre/tini
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

Tini

A tiny web framework

Installation

  • npm i @helloandre/tini

API

tini(fn)

a function that expects a function as it's only parameter, and is passed an object containing the routing api.

import tini from '@helloandre/tini'

tini(router => {
  router.get('/:key', req => req.params.key);
})

Router

Convenience methods

  • get(route: string, ...callbacks: Function)
  • post(route: string, ...callbacks: Function)
  • put(route: string, ...callbacks: Function)
  • del(route: string, ...callbacks: Function)
  • route(method: string, route: string, ...callbacks: Function) - a generic catch all for any other methods you may need to support

Nested Routers

  • with(router: Router)

Examples

For more in depth route path documentation, see path-to-regexp

Return String

tini(router => {
  router.get('/someroute', req => {
    return 'Hello, World!';
  });
});

Route Parameters + Query String

// url: /myKey?p=1
tini(router => {
  router.get('/:key', req => {
    // outputs "myKey, 1"
    return `${req.params.key}, ${req.query.p}`;
  });
});

Return JSON

tini(router => {
  router.get('/someroute', req => {
    return { hello: 'world' };
  });
});

Return A Promise

tini(router => {
  router.get('/someroute', req => {
    return Promise.resolve('hello, world');
  });
});

Return A Response

tini(router => {
  router.get('/someroute', req => {
    return new Response("Not Found", { status: 404 });
  });
});

Middleware

tini(router => {
  router.get('/someroute',
    req => {
      req.intermediateValue = 'somevalue';

      if (req.query.secret !== 'mysecret') {
        return new Response('Unauthorized', { status: 401 });
      }
    },
    req => {
      return req.intermediateValue;
    }
  );
});

Nested Routers

tini(router => {
  const api = new TiniRouter(`/api/v1`);
  api.get('/:name', (req) => ({ params: req.params, query: req.query }));
  router.with(api);

  router.get('(.*)', () => new Response("Not Found", { status: 404 }));
});

License

MIT

Dependencies (1)

Dev Dependencies (5)

Package Sidebar

Install

npm i @helloandre/tini

Weekly Downloads

1

Version

2.0.0

License

MIT

Unpacked Size

23.7 kB

Total Files

9

Last publish

Collaborators

  • helloandre