Socket
Book a DemoInstallSign in
Socket

@pagopa/handler-kit-express

Package Overview
Dependencies
Maintainers
5
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@pagopa/handler-kit-express

`@pagopa/handler-kit-express` adapter for `Express`

latest
Source
npmnpm
Version
2.0.0
Version published
Maintainers
5
Created
Source

@pagopa/handler-kit-express

@pagopa/handler-kit-express adapter for Express

How to use it

import { expressHandler } from "@pagopa/handler-kit-express";

// Given an Handler
// (from the @pagopa/handler README example)

const GetMovies = H.of((req: H.HttpRequest) =>
  pipe(
    req.body,
    // perform a refinement with io-ts, and returns a ValidationError
    // that represents a 422 HTTP response
    H.parse(GetMoviesBody),
    E.map(({ genre }) => genre),
    RTE.fromEither,
    RTE.chainTaskEither(getMoviesByGenre),
    RTE.map((movies) => ({ items: movies })),
    // wrap in a 200 HTTP response, with content-type JSON
    RTE.map(H.successJson),
    // convert Error instances to problem json (RFC 7808) objects
    RTE.orElseW(flow(H.toProblemJson, H.problemJson))
  )
);

// instead of wiring manually the dependencies

/*
GetMovies({
  input: ...,
  inputDecoder: ...,
  logger: ...,
  movies: ...
})*/

// just use "expressHandler"
const GetMoviesExpress = expressHandler(GetMovies)({
  movies,
});

const ConsoleLogger: L.Logger = {
  log: (r) => () => console.log(r),
  format: L.format.json,
};

// now GetMoviesRoute can be called by the Express runtime
const app = express.default();
app.use(express.json());

// decorate "req" with "log" function
app.use(logger(ConsoleLogger));

// enable HTTP request logging
app.use(access());

app.post("/", GetMoviesExpress);

app.listen(3001, () => {
  // eslint-disable-next-line no-console
  console.log("Server ready on port 3001");
});

See the unit tests for other examples

FAQs

Package last updated on 20 Apr 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts