2.6.1 • Published 1 year ago

lanza v2.6.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Lanza

CI

Lanza is a launch-ready server tools package to create APIs

Server

import { Server } from 'lanza';

const server = new Server();
await server.start();

Without any configuration this server already has:

  • Initialized in the 8000 port
  • /health endpoint
  • 404 errors
  • Error handler (no app crashes)
  • Disabled x-powered-by header
  • Incoming JSON requests parsed
  • CORS

Installation

You will have to install the lanza package:

npm install lanza

Options

OptionDefaultDescription
hostnameHostname for the API
port8000Port to access the API
corstrueCORS enabled to accept requests from anywhere
versions[]API versions. Check below for more details
healthChecknullCustom /health endpoint response
error404nullCustom 404 error response
maxBodySize200mbMax body size the API can receive
rawBodyfalseOption to store the unparsed request in req.rawBody
loggernullCustom logger to log errors
appMiddlewaresnullExpress' application-level middlewares

Versions

You can use various API versions in Lanza. You have to pass the array of versions to the versions option of the server.

OptionDescription
pathPath for the versions (e.g. /v1)
routesArray of routes. Check below for more details
middlewaresArray of conditional middlewares for the routes. Check below for more details

Routes

OptionDescription
pathPath of the resource without the version path (e.g. /users)
methodMethod of the request (e.g. post)
handlerFunction that will handle the request
dtoFunction that will get your response and transform it before returning it to the client

Middlewares

OptionDescription
conditionFunction to check if the path fulfills the condition to execute the middleware
handlerFunction that will act as a middleware

Note: The order of the middlewares matter

Responses

There is no need to use res.status(200).send(...) anymore (although you still can).

You can directly return booleans, text, and objects and Lanza will handle the responses for you:

TypeResponse
boolean{ "success": true } or { "success": false }
stringWill return the string
JSONWill return the JSON with its content type

Errors

If an error occurs and it is thrown, Lanza will return the error in a specific format:

{
  "error": {
    "message": "Custom error message"
  }
}

Example

import { Server } from 'lanza';

const server = new Server({
  port: 8008,
  rawBody: true,
  versions: [
    {
      path: '/v1',
      middlewares: [
        {
          condition: (route) => !!route.executeMiddleware,
          handler: (req, _res, next) => {
            req.middlewareExecuted = true;
            next();
          }
        }
      ],
      routes: [
        {
          method: 'get',
          path: '/test',
          executeMiddleware: true,
          handler: (req) => {
            console.log(req.middlewareExecuted);
            return true;
          }
        }
      ],
    }
  ]
});

await server.start();

Making a request to /test would log a true and the client would receive a { "success": true }

2.6.1

1 year ago

2.5.2

2 years ago

2.6.0

1 year ago

2.3.0

2 years ago

2.5.0

2 years ago

2.4.0

2 years ago

2.5.1

2 years ago

1.4.1

2 years ago

0.0.0

2 years ago

2.1.0

2 years ago

2.0.0

2 years ago

1.4.0

2 years ago

1.2.0

2 years ago

1.1.0

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago