1.0.9 • Published 3 years ago

littless v1.0.9

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

Littless

Build Status

This project is a helper for serverless services that is achieved through the usage of adapters.

Inspired by express middlewares and the idea was to be able to reuse some of them.

Install

npm install littless -S

Install an adapter

npm install littless-adapter-netlify -S

or

npm install littless-adapter-vercel -S

Usage

Netlify example

Create a functions/test.js

const littless = require('littless').default;
const adapter = require('littless-adapter-netlify').default;
const cookieParser = require('cookie-parser');
const {json} = require('body-parser');
const app = littless();

app.useAdapter(adapter);

app.use(json({
  extended: true
}));
app.use(cookieParser());
app.useAfter((req, res) => {
  if (!res.shouldOutput()) {
    res.status(500)
      .send({
        error: true,
        message: 'No body was sent up until this point'
      })
  }
});

exports.handler = app.any(async (req, res, next) => {
  console.dir(req);
  res.send({
    path: req.path,
    cookies: req.cookies,
    query: req.query,
    body: req.body
  });

  req.on('data', (chunck) => {
    console.log(chunck);
  })

  next();
});

Vercel example

Create a api/test.js

const littless = require('littless').default;
const adapter = require('littless-adapter-vercel').default;
const cookieParser = require('cookie-parser');
const app = littless();

app.useAdapter(adapter);
app.use(cookieParser());
app.useAfter((req, res) => {
  if (!res.shouldOutput()) {
    res.status(500)
      .send({
        error: true,
        message: 'No body was sent up until this point'
      })
  }
});

module.exports = app.any(async (req, res, next) => {
  res.send({
    path: req.path,
    cookies: req.cookies,
    query: req.query,
    body: req.body
  });

  next();
});

Please note that vercel already parses the body so no need to use the body-parser

Specify the method to use

You can use the already made middlewares to filter the allowed methods

app.get(/* ... */)
app.post(/* ... */)
app.put(/* ... */)
app.delete(/* ... */)

Or you can use only or except to construct your own

app.only(['POST', 'GET'], (req, res, next) => {
  /* ... */
})
app.except(['DELETE'], (req, res, next) => {
  /* ... */
})

Using adapters

Adapters can be used with .useAdapter

Available adapters

For now the available parsers are for netlify and vercel but you can inspect the adapters code and create your own.

If you do create a new parser please reach out to me so I can include it in the official list.

App Available Methods

MethodArgumentsDescription
useAdapterAdapter Class(Required) Specify which adapter to be used to parse information from serverless service
useFunction(req, res, nextMiddleware)Append middleware
useAfterFunction(req, res, nextMiddleware)Append middleware to be used after the callback function
useBeforeFunction(req, res, nextMiddleware)Preprend middleware
postFunction(req, res, nextMiddleware)Only allow POST and OPTIONS requests and calls back the first argument
getFunction(req, res, nextMiddleware)Only allow GET and OPTIONS requests and calls back the first argument
deleteFunction(req, res, nextMiddleware)Only allow DELETE and OPTIONS requests and calls back the first argument
putFunction(req, res, nextMiddleware)Only allow PUT and OPTIONS requests and calls back the first argument
anyFunction(req, res, nextMiddleware)Allows any requests and calls back the first argument
onlyMethods (Array), Function(req, res, nextMiddleware)Allows user to specify methods to allow and calls back the second argument
exceptMethods (Array), Function(req, res, nextMiddleware)Allows user to specify methods to block and calls back the second argument

Request Api

Method / PropertyTypeDescription
headersObjectRequest headers are accessible from this property
queryObjectQuery params are accessible from this property
httpMethodStringThe method of the request
pathStringThe url path
bodyString / ObjectNormally a string but can be parsed depending on the service or the adapter
rawObjectThe original event sent by the service

Response Api

MethodArgumentsDescription
statuscode:NumberSpecify the response code
setkey:String, value:StringSets a header for the response i.e res.set('Content-Type', 'application/json')
headerskey:String, value:StringAlias for set
sendbody:AnySets the response body

Note: Possibly the response should have a way of setting a cookie but this can be achieved with the headers or creating a middleware for that which might be an option for the future.

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.1

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.0

3 years ago