0.0.51 • Published 7 months ago

xirelta v0.0.51

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

Xirelta Web Framework

Xirelta is a lightweight web framework for building web applications in TypeScript with Bun. It is designed to be flexible and easy to use, allowing you to create web services and applications with minimal effort.

Installation

You can install Xirelta using Bun:

bun add xirelta

Getting Started

To get started with Xirelta, you need to create an instance of the Application class and define your routes and handlers. Here's a basic example:

import { Application } from 'xirelta';

const app = new Application();

app.get('/', (request) => {
  return 'Hello, Xirelta!';
});

app.start().then((serverInfo) => {
  console.log(`Server is running on port ${serverInfo.port}`);
});

In this example, we create a simple web server that listens on the root path and responds with "Hello, Xirelta!" when accessed.

Defining Routes

You can define routes for various HTTP methods using the following methods:

  • app.all(path, handler): Matches all HTTP methods.
  • app.get(path, handler): Matches GET requests.
  • app.post(path, handler): Matches POST requests.
  • app.put(path, handler): Matches PUT requests.
  • app.delete(path, handler): Matches DELETE requests.

The path parameter is a string that defines the URL pattern for the route, and the handler parameter is a function that handles the incoming requests and returns a response.

Handling Requests

The handler function takes a request object as its parameter, which contains information about the incoming request. You can access parameters, query strings, request bodies, and more from this object.

Here's an example of handling a GET request with a parameter:

app.get('/user/:id', (request) => {
  const userId = request.params.id;
  // Retrieve user data based on the userId
  const user = getUserById(userId);
  if (!user) {
    return new Response('User not found', { status: 404 });
  }
  return user;
});

In this example, we define a route with a parameter :id in the URL pattern. We then access this parameter using request.params.id and use it to retrieve user data.

Custom Responses

You can return custom responses using the Response class or plain JSON objects. Xirelta will handle serializing these responses appropriately.

app.get('/json', (request) => {
  return {
    message: 'This is a JSON response',
  };
});

app.get('/custom-response', (request) => {
  return new Response('Custom Response', { status: 200, headers: { 'Content-Type': 'text/plain' } });
});

Starting and Stopping the Server

To start the Xirelta web server, call the start method on your Application instance. It returns a promise that resolves with information about the server, including the port it's listening on.

app.start().then((serverInfo) => {
  console.log(`Server is running on port ${serverInfo.port}`);
});

To stop the server, use the stop method:

app.stop().then(() => {
  console.log('Server has stopped');
});

Configuration

Xirelta allows you to configure various options for your application, including the web server's port and logging. You can pass a configuration object when creating an Application instance:

const config = {
  web: {
    port: 8080, // Set the port to 8080
  },
  logger: {
    debug(message, options) {
      console.debug(message, options);
    },
    info(message, options) {
      console.info(message, options);
    },
  },
};

const app = new Application(config);

License

MIT

0.0.51

7 months ago

0.0.50

8 months ago

0.0.49

8 months ago

0.0.48

8 months ago

0.0.47

8 months ago

0.0.46

8 months ago

0.0.45

8 months ago

0.0.44

8 months ago

0.0.43

8 months ago

0.0.42

8 months ago

0.0.41

8 months ago

0.0.40

8 months ago

0.0.39

8 months ago

0.0.38

8 months ago

0.0.37

8 months ago

0.0.36

8 months ago

0.0.35

8 months ago

0.0.34

8 months ago

0.0.33

8 months ago

0.0.32

8 months ago

0.0.31

8 months ago

0.0.30

8 months ago

0.0.29

8 months ago

0.0.28

8 months ago

0.0.27

8 months ago

0.0.26

8 months ago

0.0.25

8 months ago

0.0.24

8 months ago

0.0.23

8 months ago

0.0.22

8 months ago

0.0.21

8 months ago

0.0.20

8 months ago

0.0.19

8 months ago

0.0.18

8 months ago

0.0.17

8 months ago

0.0.16

8 months ago

0.0.14

8 months ago

0.0.13

8 months ago

0.0.12

8 months ago

0.0.11

8 months ago

0.0.10

8 months ago

0.0.9

8 months ago

0.0.8

8 months ago

0.0.7

8 months ago

0.0.6

8 months ago

0.0.5

8 months ago

0.0.4

8 months ago

0.0.3

8 months ago

0.0.2

8 months ago

0.0.1

8 months ago