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

frapi

v0.1.26

Published

Automatically create client-side functions to consume your API

Readme

License: MIT

Introduction

Frapi is middleware and router for Express providing the following features:

  • validation of request payload and query parameters,
  • validation of response payload,
  • automatic generation of an API client library,
  • fully blown Typescript support (both on backend and in the generated client library),
  • catching errors in asynchronous request handlers.

You can find a working example presenting all of the above in this CodeSandbox.

Installation

Install the package as a dependency with npm or yarn:

  • npm install frapi
  • yarn add frapi

Basic usage

import express from "express";
import bodyParser from "body-parser";
import { Router } from "frapi";

const app = express().use(bodyParser.json());

// Create frapi router and attach it to the express app
const routes = new Router();
app.use(routes);

routes.get(
    {
        path: "/user/:id",
        // Define expected result shape. 
        // You can also define shapes of request payload and query parameters.
        response: { fullName: String, age: Number } 
    },
    (req, res) => {
        res.sendResponse({ fullName: "John Smith", age: 12 });
    }
);

app.listen(3000);

Generating client library

You can generate a strongly typed client library based on the endpoints definition. Let's take the following example:

import express from "express";
import bodyParser from "body-parser";
import { Router, saveEndpointsToFile } from "frapi";

const app = express().use(bodyParser.json());

// Create frapi router and attach it to the express app
const routes = new Router();
app.use(routes);

routes.post(
    {
        path: "/user/:id",
        name: 'createUser',
        body: { fullName: String, age: Number },
        response: { id: String, fullName: String, age: Number } 
    },
    (req, res) => {
        res.sendResponse({ ...req.body, id: req.params.id });
    }
);

// This traverses all the registered endpoints in the app and 
// generates a strongly typed client library
saveEndpointsToFile(app, "./api.ts", "ts")

app.listen(3000);

The code above will generate the following client library (api.ts):

export async function createUser(id: string, body: { fullName: string; age: number }) {
    const response = await fetch(`/user/${id}`, { method: 'post',  headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body), });
    const responseBody = (await response.json()) as { id: string; fullName: string; age: number };
    return { ok: response.ok, status: response.status, body: responseBody, headers: response.headers, response };
}

Both, request payload and response body are strongly typed. This establishes the contract between your backend and frontend.

:warning: The saveEndpointsToFile function implementation is currently in the proof-of-concept state. All contributions are highly appreciated!

Validation syntax

The validation API uses String, Boolean and Number constructors to define primitive types. Nested structures are described as nested objects. For example:

const User = {
    name: String,
    age: Number,
    isAdult: Boolean,
    address: {
        firstLine: String,
        secondLine: String,
    }
}

Optional fields are defined by adding ? to their name. In the example below both name and surname are optional (can be undefined):

const User = {
    "name?": String,
    "surname?": String,
}

There's a number of helpers to define complex types:

  • ArrayOf(Type) defines an array of objects of given types (Type[] in TypeScript).
  • MapOf(Type) defines an map with values of provided type (Record<String, Type> in TypeScript).
  • AnyOf(A, B, C) defines a union of types (A | B | C in TypeScript).
  • AllOf(A, B, C) defines an intersection of types (A & B & C in TypeScript).

Example:

import { ArrayOf, AnyOf } from 'frapi';

const Book = {
    title: String,
    author: String,
};

const User = {
    name: String,
    books: ArrayOf(Book),                       
    country: AnyOf('US' as const, 'UK' as const) 
}

Custom validators

You can define custom validation logic with the following syntax:

const NonEmptyString = {
  // The underlying type for TypeScript
  $type: String,
  // Validation method. Returns true, if the object is valid and false otherwise.
  // Can also throw an exception with validation error deatils 
  $validate: (text: string) => text.trim().length > 0
};

Remarks

Fields starting with $ are ignored during validation - you can't expect e.g. to successfully validate an payload with a field $name.

Frapi rejects objects with fields that are not defined in the validation type. E.g. the following code will fail:

import { validate } from 'frapi';
    
const User = { name: String, age: Number };

// Error - the field `city` is not in the type definition. 
validate(User, { name: "John", age: 25, city: 'London '})

Acknowledgements

This project is built based on express-list-endpoints and express-async-router.