0.0.11 • Published 2 months ago

@bolt-ts/core v0.0.11

Weekly downloads
-
License
MIT
Repository
-
Last release
2 months ago

Bolt TS - Modular, typed REST API framework for NodeJS

npm version npm monthly downloads npm total downloads bundle size

Bolt is in early development. Expect breaking changes until v1.0.0, and use at your own risk.

Getting started

Installation

Core (required)

yarn add @bolt-ts/core

Fastify (you'll need at least one server implementation)

yarn add @bolt-ts/fastify

Plexus (you'll need at least one client implementation)

yarn add @bolt-ts/plexus @plexusjs/napi

Basic usage

First, you'll need to define some routes. Bolt uses a simple route definition format:

import { route } from '@bolt-ts/core';
// We're using zod, but you can use other validation/serialization libraries, as long as they're compatible with bolt
import { z } from 'zod';

const myRoute = route('/hello')
  // Define the input body schema
  .body(z.object({
    name: z.string()
  }))
  // Define the output body schema
  .output(z.object({
    message: z.string()
  }))

// Other methods include:
// .method()
// .headers()
// .query()
// .params()

Once you've defined your routes, you'll need to create a server. Bolt uses a modular server implementation, so you can use any server implementation you want. For this example, we'll use Fastify, which we primarily support.

import { BoltServer } from '@bolt-ts/fastify';
import fastify from 'fastify';

const app = fastify();

const server = new BoltServer({ myRoute }, app);

// Now we can define our route handlers
server.registerHandlers({
  myRoute: async (req, res) => {
    return {
      message: `Hello ${req.body.name}!`
    }
  }
});

// This will attach all bolt routes to the fastify server
server.attachRoutes();

// You can now start the server
app.listen({
  port: 3000,
});

Now that we have a server running, we can make requests to it. Just like for the backend, we use a modular client implementation. For this example, we'll use our Plexus API wrapper.

import { createPlexusRouter } from '@bolt-ts/plexus';
import { api } from '@bolt-ts/napi';

const client = api('http://localhost:3000')

const router = createPlexusRouter({ myRoute }, client);

// Now we can make requests to the server
const response = await router.myRoute({
  body: {
    name: 'Nico'
  }
});

console.log(response.message); // Hello Nico!

Alternatives

0.0.12-canary.27

2 months ago

0.0.12-canary.29

2 months ago

0.0.12-canary.28

2 months ago

0.0.12-canary.26

3 months ago

0.0.12-canary.14

5 months ago

0.0.12-canary.2

6 months ago

0.0.12-canary.13

6 months ago

0.0.12-canary.3

6 months ago

0.0.12-canary.16

5 months ago

0.0.12-canary.4

6 months ago

0.0.12-canary.15

5 months ago

0.0.12-canary.5

6 months ago

0.0.12-canary.18

5 months ago

0.0.12-canary.17

5 months ago

0.0.12-canary.0

6 months ago

0.0.12-canary.19

5 months ago

0.0.12-canary.10

6 months ago

0.0.12-canary.12

6 months ago

0.0.12-canary.11

6 months ago

0.0.12-canary.6

6 months ago

0.0.12-canary.7

6 months ago

0.0.12-canary.8

6 months ago

0.0.12-canary.9

6 months ago

0.0.12-canary.24

5 months ago

0.0.12-canary.21

5 months ago

0.0.12-canary.20

5 months ago

0.0.12-canary.23

5 months ago

0.0.12-canary.22

5 months ago

0.0.11

7 months ago

0.0.10

7 months ago

0.0.9

7 months ago

0.0.8

7 months ago

0.0.7

8 months ago

0.0.6

9 months ago

0.0.5

9 months ago

0.0.3

9 months ago

0.0.2

9 months ago

0.0.1

9 months ago