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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@superfunctions/http-fastify

v0.1.1

Published

Fastify adapter for @superfunctions/http

Readme

@superfunctions/http-fastify

Fastify adapter for @superfunctions/http - Use framework-agnostic routers with Fastify.

Installation

npm install @superfunctions/http @superfunctions/http-fastify fastify

Quick Start

import Fastify from 'fastify';
import { createRouter } from '@superfunctions/http';
import { toFastify } from '@superfunctions/http-fastify';

// Define your router (framework-agnostic)
const apiRouter = createRouter({
  routes: [
    {
      method: 'GET',
      path: '/users/:id',
      handler: async (req, ctx) => {
        return Response.json({ id: ctx.params.id });
      },
    },
  ],
});

// Use with Fastify
const fastify = Fastify();
await fastify.register(toFastify(apiRouter), { prefix: '/api' });
await fastify.listen({ port: 3000 });

API

toFastify(router)

Converts a @superfunctions/http router to a Fastify plugin.

Parameters:

  • router: Router instance from @superfunctions/http

Returns: FastifyPluginAsync

Example:

import { toFastify } from '@superfunctions/http-fastify';

await fastify.register(toFastify(myRouter), { 
  prefix: '/api' 
});

Usage Examples

With Plugin Options

import Fastify from 'fastify';
import { toFastify } from '@superfunctions/http-fastify';

const fastify = Fastify();

// Register with prefix
await fastify.register(toFastify(apiRouter), { 
  prefix: '/api/v1' 
});

// Routes accessible at: /api/v1/*
await fastify.listen({ port: 3000 });

With Middleware

import { createRouter } from '@superfunctions/http';
import { toFastify } from '@superfunctions/http-fastify';

const router = createRouter({
  middleware: [
    async (req, ctx, next) => {
      // Auth middleware
      const token = req.headers.get('Authorization');
      if (!token) {
        return Response.json({ error: 'Unauthorized' }, { status: 401 });
      }
      return next();
    },
  ],
  routes: [
    {
      method: 'GET',
      path: '/protected',
      handler: async () => Response.json({ data: 'secret' }),
    },
  ],
});

await fastify.register(toFastify(router), { prefix: '/api' });

With Custom Context

interface AppContext {
  db: Database;
}

const router = createRouter<AppContext>({
  context: { db: myDatabase },
  routes: [
    {
      method: 'GET',
      path: '/users',
      handler: async (req, ctx) => {
        const users = await ctx.db.findMany({ model: 'users' });
        return Response.json(users);
      },
    },
  ],
});

await fastify.register(toFastify(router), { prefix: '/api' });

Multiple Routers

const usersRouter = createRouter({ routes: [/* user routes */] });
const postsRouter = createRouter({ routes: [/* post routes */] });

await fastify.register(toFastify(usersRouter), { prefix: '/api/users' });
await fastify.register(toFastify(postsRouter), { prefix: '/api/posts' });

With Fastify Decorators

You can combine with other Fastify plugins:

import Fastify from 'fastify';
import fastifyCors from '@fastify/cors';
import { toFastify } from '@superfunctions/http-fastify';

const fastify = Fastify();

// Register CORS plugin
await fastify.register(fastifyCors, {
  origin: 'https://example.com'
});

// Register your router
await fastify.register(toFastify(apiRouter), { prefix: '/api' });

await fastify.listen({ port: 3000 });

Important Notes

Plugin Registration

The adapter returns a Fastify plugin that must be registered with await fastify.register():

// ✅ Correct - await registration
await fastify.register(toFastify(router));

// ❌ Incorrect - missing await
fastify.register(toFastify(router));

Prefix Handling

The adapter automatically handles the prefix from plugin options:

const router = createRouter({
  routes: [
    { method: 'GET', path: '/hello', handler: () => Response.json({ msg: 'hi' }) }
  ]
});

// Route accessible at: /api/hello
await fastify.register(toFastify(router), { prefix: '/api' });

JSON Serialization

Fastify automatically serializes JSON responses. The adapter handles this by:

  • Parsing JSON from Web Response
  • Sending parsed objects to Fastify
  • Letting Fastify handle serialization

Body Parsing

Fastify automatically parses JSON and form data. The adapter accesses the parsed body via request.body.

Testing

Use Fastify's built-in inject method for testing:

const response = await fastify.inject({
  method: 'GET',
  url: '/api/hello'
});

expect(response.statusCode).toBe(200);
expect(response.json()).toEqual({ message: 'Hello' });

TypeScript

Full TypeScript support with proper types:

import type { Router } from '@superfunctions/http';
import type { FastifyPluginAsync } from 'fastify';

const myRouter: Router = createRouter({ routes: [...] });
const plugin: FastifyPluginAsync = toFastify(myRouter);

await fastify.register(plugin, { prefix: '/api' });

Performance

The adapter is designed for minimal overhead:

  • Uses catch-all route to delegate to the router
  • Minimal request/response translation
  • Leverages Fastify's fast JSON serialization

Compatibility

  • Fastify 4.x ✅
  • Fastify 5.x ✅

License

MIT