arv-rest
TypeScript icon, indicating that this package has built-in type declarations

0.0.12 • Public • Published

arv-rest

Publish

A very easy rest (express like) api tool which allows you to create http api's with bun

A very simple example of how you can use it:

import { BodyType, RestAPI } from "arv-rest";

const api = new RestAPI()
api.get('/', async (c) => {

    c.setHeader('content-type', 'text/html')
    const page = await Bun.file('home.html').text();

    return c.send(page, 200);
});

api.start(3030)

More examples

import { RestAPI } from "arv-rest";

const api = new RestAPI();
// add default headers to all responses
api.setDefaultHeader('powered-by', 'arv-rest');
api.notFound((c) => c.send('Not found!', 404));
api.use((c) => {

    const auth = c.headers.get('authorization');
    if(!auth || !auth.startsWith('Basic')) { return c.send('Unauthorized!', 401)}
    //! do some logic here
    return c.next();
})
api.get('/', async (c) => {

    c.setHeader('content-type', 'text/html')
    const page = await Bun.file('home.html').text();

    return c.send(page, 200);
})
.post('/', async (c) => {

    const body = await c.body(BodyType.ArrayBuffer);

    return c.send('OK'); // will use status 200 by default
})
.put('/', async (c) => c.send('Too lazy to add full example'))
.delete('/posts/:post_id', (c) => {

    const { post_id } = c.params();
    const { is_mock } = c.query();

    return c.send('OK');
})
api.start(3030)

Readme

Keywords

none

Package Sidebar

Install

npm i arv-rest

Weekly Downloads

1

Version

0.0.12

License

none

Unpacked Size

15.2 kB

Total Files

4

Last publish

Collaborators

  • arthurvanl