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

@typified-web/request

v1.0.0-beta.2

Published

A type-safe http client with a focus on zero runtime cost.

Downloads

1

Readme

@typified-web/request

A tiny request library to provide compile-time type-safe with your API specification.

Features

  • Types for each API calls with simple declarations for API,

    which means:

    • Code completion by API declarations with supported editor (like VSCode).

    • Type validation at compile-time (design-time).

  • Natural API to request resources.

  • Tiny footprint for runtime of browser and NodeJS (about 4kb uncompressed).

Getting Started

Installation via npm:

npm i @typified-web/request

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

type YourAPI = {
  '/greet/:name': {
    get: {
      input: {
        route: {
          name: string;
        };
      };
      output: {
        body: {
          resCode: string;
          result: {
            name: string;
          };
        };
      };
    };
  };
};

Then create your API request client:

import { createRequest } from '@typified-web/request';

// the decorated reuqest function.
export default createRequest<YourAPI>();

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

import fetch from 'node-fetch';

export default createRequest<YourAPI>({
  fetch: (path, opt) => {
    return fetch(`${host}${path}`, opt);
  },
});

Then, you can use it anywhere you like via import and get auto completion and validation for each request:

import request from './the/newly/created/request';

// send a request.
request.endpoint('/greet/:name').get({ route: { name: 'Jack' } });

Code-completion and type validation works like a charm.

API

The API includes two parts: the API declaration for design-time and the decorated request client for runtime.

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 Method]?: {
      // Request Specification.
      input?: {
        // Path parameters
        route?: PathParameter;
        // Path parameters
        query?: PathParameter;
        // Headers
        headers?: Record<string, string>;
        // Request body.
        body?: Json;
      };
      // Response Specification.
      output: {
        headers?: Record<string, string>;
        body?: Json;
      };
    };
  };
};

The declaration is just for design-time only and will be swiped in runtime. That's why we call it almost zero-overhead.

The request client

To create a request client, you can specify your customized fetch implemenation for easy extention:

import fetch from 'node-fetch';

const request = createRequest<YourAPI>({
  fetch: (path, opt) => {
    return fetch(`${host}${path}`, opt);
  },
});

The request client itself is complicatedly typed as it uses generics massively.

However, the usage is quite simple.

request.endpoint('PATH').method({ params });

The path is the resource key defined in your API specification and method is the supported method(get/post/put/patch/delete) for your endpoint declaration. The parameters will be inferred for you.

Limitations

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.