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

0.22.6 • Public • Published

Nixle logo

Universal server-side framework.
Backend for everyone and everywhere.

Overview

Nixle is a framework for building HTTP servers. It is designed to be simple, fast, and extensible. It is built on top of existing frameworks, such as Express, Fastify, and Nitro.

  • ✨ Simple and intuitive API.
  • 🚀 Supports multiple providers such as Express, Fastify, and Hono.
  • 🌐 Supports SSR frameworks such as Nuxt.
  • 💪 Incredible TypeScript support.
  • 🎯 Easy to use and extend.

Documentation

You can find the documentation on the website.

Installation

npm install nixle

Usage

To set up your app, use the createApp function. Create a router with the createRouter function. Additionally, you can create services with module-specific logic using the createService function.

// usersRouter.ts
import { createRouter, createService } from 'nixle';

declare global {
  namespace Nixle {
    interface Env {
      USERS_SERVICE: string;
    }
  }
}

const usersService = createService('users', ({ log, env, ofetch }) => {
  const getUsers = async (limit: number) => {
    log.info('Getting users...');

    const users = await ofetch<{ name: string; email: string }[]>(`${env.USERS_SERVICE}/users`);

    log.success(`Got ${users.length} users`);

    return users;
  };

  return {
    getUsers,
  };
});

export const usersRouter = createRouter('/users', ({ route, zodObject }) => [
  route.get('/', {
    queryValidation: zodObject({
      limit: zod.number().default(10),
    }).validate,
    handler: ({ query }) => {
      return usersService().getUsers(query.limit);
    },
  }),
]);

Providers

We have several providers that you can use to create your app such as Nuxt, Express, Fastify, and Elysia (Bun). Choose the one that suits you best and install packages for it. More information about providers can be found in the docs.

For example, if you want to use Fastify, install the @nixle/fastify package besides the nixle package:

npm install @nixle/fastify

Then, import the fastifyProvider function and pass it to the createApp function:

import fastify from 'fastify';
import { createApp } from 'nixle';
import { fastifyProvider } from '@nixle/fastify';
import { zodPlugin } from '@nixle/zod';
import { ofetchPlugin } from '@nixle/ofetch';
import { usersRouter } from './usersRouter';

const { app, $inferRouters } = createApp({
  provider: fastifyProvider(fastify()),
  router: [usersRouter],
  plugins: [zodPlugin, ofetchPlugin()],
});

app.listen({ port: 4000 });

type NixleRouters = typeof $inferRouters;
// {
//   '/users': {
//     '/': {
//       GET: {
//         query: {
//           limit: number;
//         };
//         response: { name: string; email: string }[]
//       }
//     }
//   };
// }

Author

© letstri, released under the MIT license.

Package Sidebar

Install

npm i nixle

Weekly Downloads

431

Version

0.22.6

License

MIT

Unpacked Size

63.1 kB

Total Files

28

Last publish

Collaborators

  • letstri