Skip to content

claeusdev/raptur

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

7 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ¦– Raptur

     __    _                   
    / _)  / \        /\  /\    
   /(_)(  \_/       /  \/  \   
  (____)\  _    ___/   /\   \  
       U  (_)  (___/   \/   /  
           _    _  \_      /   
          (____(__  \_____/    

Raptur Router is a simple, TypeScript-first HTTP router for Node.js. Built with developer experience in mind, it provides a clean, chainable API for building web applications.

Features

  • πŸš€ Simple routing
  • πŸ’ͺ Built with TypeScript
  • 🎯 Type safety
  • ⚑️ Async/await support
  • πŸ” URL parameter parsing
  • πŸ“¦ Zero dependencies
  • πŸ›  Chainable API
  • πŸ¦• Prehistoric power!

Installation

Still WIP so not on npm yet.

npm install raptur

Quick Start

import { Raptur } from 'raptur';

const app = new Raptur(); // can pass optional port, default is :3000

app
  .get('/api/hello', (req, res) => {
    res.json({ message: 'Hello from Raptur! πŸ¦–' });
  })
  .get('/api/users/:id', async (req, res) => {
    const { id } = req.params;
    res.json({ userId: id });
  })
  .post('/api/users', async (req, res) => {
    const body = await req.json();
    res.status(201).json({ message: 'User created', data: body });
  });

app.start(() => {
  console.log('πŸ¦– Raptur is hunting on port 3000');
});

API Reference

Creating a Router

import { Raptur } from 'raptur';
const app = new Raptur();

Route Methods

app.get(path: string, handler: RouteHandler);
app.post(path: string, handler: RouteHandler);
app.put(path: string, handler: RouteHandler);
app.delete(path: string, handler: RouteHandler);

Request Object

interface RapturRequest {
  params: Record<string, string>;    // URL parameters
  query: Record<string, string>;     // Query string parameters
  headers: http.IncomingHttpHeaders; // Request headers
  json(): Promise<any>;              // Parse JSON body
}

Response Object

interface RapturResponse {
  status(code: number): RapturResponse;
  json(data: any): void;
  send(data: string): void;
  setHeader(name: string, value: string): RapturResponse;
}

URL Parameters

Raptur supports dynamic URL parameters with the :param syntax:

app.get('/api/users/:id/posts/:postId', (req, res) => {
  const { id, postId } = req.params;
  res.json({ userId: id, postId });
});

Query Parameters

Access query parameters through the query object:

// GET /api/search?q=raptor&sort=desc
app.get('/api/search', (req, res) => {
  const { q, sort } = req.query;
  res.json({ searchTerm: q, sortOrder: sort });
});

Body Parsing

Parse JSON request bodies with the json() method:

app.post('/api/data', async (req, res) => {
  const body = await req.json();
  res.json({ received: body });
});

Error Handling

Raptur automatically handles route errors:

app.get('/api/error', async (req, res) => {
  throw new Error('Something went wrong');
  // Automatically returns 500 Internal Server Error
});

Examples

Basic REST API

app
  .get('/api/items', async (req, res) => {
    const items = await getItems();
    res.json(items);
  })
  .post('/api/items', async (req, res) => {
    const body = await req.json();
    const newItem = await createItem(body);
    res.status(201).json(newItem);
  })
  .get('/api/items/:id', async (req, res) => {
    const item = await getItem(req.params.id);
    if (!item) {
      return res.status(404).json({ error: 'Item not found' });
    }
    res.json(item);
  })
  .delete('/api/items/:id', async (req, res) => {
    await deleteItem(req.params.id);
    res.status(204).send('');
  });

Contributing

We welcome contributions! Please feel free to submit a Pull Request. Check out our contributing guidelines for more information.

License

MIT Β© [Nana Adjei Manu]

Credits

ASCII art logo generated with love and prehistoric power! πŸ¦–


Happy routing with Raptur! πŸ¦•

About

Simple express inspired router

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published