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

typed-fetch-decorator

v1.0.0-3

Published

An experimental typed decorator for fetch.

Downloads

2

Readme

typed-fetch-decorator

An experimental almost zero-cost decorator for fetch with your API specification.

Why?

  • types for each API calls with simple declarations for API, which means:
    • code auto completion by API declarations.
    • type validation.
  • familiar and handy API for both browser & NodeJS.
  • almost zero overhead for runtime.

Getting Started

Installation via npm:

npm i typed-fetch-decorator

Declare your API. E.g. the GET /test/:name end point:

import decoratedFetch from "typed-fetch-decorator";

// declare your API.
type YourAPI = {
  "/test/:name": {
    GET: {
      req: {
        route: {
          name: string;
        };
      };
      output: {
        resCode: string;
        result: {
          name: string;
        };
      };
    };
  };
};

// the decorated fetch function.
export default decoratedFetch<YourAPI>(fetch);

Then, you can use it anywhere you like via import and get auto completion and validation for the fetch.

If you're using NodeJS, install node-fetch to provide the fetch implementation.

API

It's quite simple. As you can see in the example above, the API includes two parts: the API declaration and the decorated fetch function.

API declaration

The API definition is like the Open API specification, but in a more simpler form of typescript declarations.

export type APISpec = {
  // URL or path of the API endpoint.
  [path: string]: {
    // HTTP Methods.
    [M in Methods]?: {
      // Request Specification.
      req?: {
        // Path parameters
        route?: PathParameter;
        // Headers
        headers?: Record<string, string>;
        // Request body.
        body?: Json;
      };
      // Result of the response in JSON format.
      output: Json;
    };
  };
};

The declaration is just for development only and will be swipped in runtime. That's why we call it zero-overhead.

The decorated fetch function

The decorator itself is complicatedly typed due to some flaws of typescript. Here we simplified the decorated fetch function as:

function fetch(url: string, opt: {
  headers: YOU_API_HEADERS,
  body: YOUR API_BODY,
  // ... other fetch options.
}): Promise<Reponse<YOUR_API_OUTPUT>>

In short, you can just use the decorated fetch function like the original fetch with types.

for more details, see the project declaration files.

Limitations

The decorator is not a one-to-one typed decorator to fetch. It just add type declarations to the JSON-like API, which means the input and output are mostly JSON. Anything outside the JSON-like API is not considered well.

Help is welcomed!

License

MIT.