npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@fastify/funky

v3.1.0

Published

Support for fastify routes returning functional structures, such as fp-ts Either, Task, TaskEither or plain javascript parameterless functions

Downloads

1,428

Readme

@fastify/funky

NPM Version Build Status js-standard-style

Support for Fastify routes returning functional structures, such as fp-ts Either, Task, TaskEither, or plain JavaScript parameterless functions. Let's go funky, let's go functional!

Getting started

First install the package:

npm i @fastify/funky

Next, set up the plugin:

const { fastifyFunky } = require('@fastify/funky')
const fastify = require('fastify');

fastify.register(fastifyFunky);

@fastify/funky plugin is executed during preSerialization response lifecycle phase.

Supported structures

While the most convenient way to use this plugin is with fp-ts library, it is not required. @fastify/funky supports the following data structures:

Parameterless functions:

app.get('/', (req, reply) => {
  // This will result in a response 200: { id: 1}
  return () => { return { id: 1} }
  
  // Same as above
  return () => { return { right:
  { id: 1 }
  }}

  // Same as above
  return () => { return Promise.resolve({ id: 1 })}

  // Same as above
  return () => { return Promise.resolve({ right:
      { id: 1 }
  })}

  // Plugin will pass-through this value without doing anything
  return (id) => { return Promise.resolve({ id })}
});

If the function returns an Either object, it will be handled in the same way as if you returned that Either object directly.

If the function returns a Promise, it will be resolved. If Promise resolves to an Either object, it will be handled in the same way as if you returned that Either object directly.

If the function directly returns anything else, or if its Promise resolves to anything else, that result is passed further along the chain as the plugin execution result.

Note that functions with parameters will be ignored by the plugin and passed-through as-is.

Either objects:

Right value is passed further along the chain as the plugin execution result:

app.get('/', (req, reply) => {
  // This will result in a response 200: { id: 1}
  return { right: 
           { id: 1 }
         } 
});

Left value is passed to the error handler:

app.get('/', (req, reply) => {
  // This will propagate to fastify error handler, which by default will result in a response 500: Internal server error
  return { left: new Error('Invalid state') } 
});

Using with fp-ts

With the plugin registered, you can start returning entities of type Either, Task, or plain parameterless functions as router method results:

const { either, task, taskEither } = require('fp-ts')

app.get('/', (req, reply) => {
  // This will result in a response 200: { id: 1}
  return either.right({ id: 1})

  // Same as above
  return task.of(Promise.resolve({ id: 1}))

  // Same as above
  return taskEither.fromEither(either.right({ id: 1}))

  // Same as above
  return taskEither.fromTask(task.of(Promise.resolve({ id: 1})))

  // Same as above
  return () => { return { id: 1} } 

  // This will propagate to fastify error handler, which by default will result in a response 500: Internal server error
  return either.left(new Error('Invalid state'))

  // Same as above
  return taskEither.fromEither(either.left(new Error('Invalid state')))
});