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-web-api/client

v1.1.1

Published

Client side library to infer the return type of fetch requests based on the corresponding Api's types declaration

Downloads

3

Readme

@typed-web-api/client

Client library to infer the return type of fetch requests based on a web API's type declaration generated via @typed-web-api/common.

Example

Given the following sample fetch call:

/* ... */

const response = await fetch(`/users`, { method: 'get' });
const users = await response.json(); // Inferred type => any

This is how to get typed response payloads by using typedFetch (given a WebApiTypes types declaration):

import { getTypedFetch } from '@typed-web-api/client';
import { WebApiTypes } from '...';

const typedFetch = getTypedFetch<WebApiTypes>();

/* ... */

const response = await typedFetch('/users_get');
const users = await response.json(); // Inferred type => User[]

API

getTypedFetch<TApi>([options])

Returns

An instance of typedFetch, configured for the TApi type.

Arguments

  • TApi: Web API's type declaration.

  • options:

    | name | type | default | description | | ------- | ---------------- | ------------ | ---------------------------------------------------------------- | | baseUrl | string? | undefined | A string that will be prepended to the URL of all fetch requests | | fetch | Window['fetch']? | this.fetch | A function that issues Http requests |

Examples

const typedFetch = getTypedFetch<MyApiType>();
const typedFetch = getTypedFetch<MyApiType>({ baseUrl: '/api' });
const typedFetch = getTypedFetch<MyApiType>({ fetch: nodeFetch });

typedFetch(endpointName, [options])

Returns

A promise of an Http response, with a .json() method typed according to the TApi provided in the parent function (i.e. getTypedFetch).

Arguments

  • endpointName: The target web API endpoint name (e.g. /users_get).

  • options:

    | name | type | default | description | | --------- | ------------ | ----------- | --------------------------------------------------------------------------------------------------------------------------------------------------- | | init | RequestInit? | undefined | RequestInit properties that will be passed to the fetch call (except for the method and conditional overrides depending on the provided options). | | urlPrefix | string? | undefined | A string that will be prepended to the endpoint URL. |

    For endpoints with typed request payload (i.e. endpoint definitions that use JsonBody, QueryString and/or UrlParams), additional parameters are available:

    | name | type | default | description | | ----------- | ------------------------------------ | ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ | | jsonBody | extends any | undefined | Object that will be stringified and sent as the request body. When provided, the Content-Type header will be will set/overwritten with application/json. | | queryString | extends { [key: string]: string }? | undefined | Key-value dictionary with query string parameters to be added to the URL. | | urlParams | extends { [key: string]: string }? | undefined | Key-value dictionary with parameters to be replaced in the URL. |

Examples

const users = await typedFetch('/users_get');
const usersPage = await typedFetch('/users_get', { queryString: { limit: '30', skip: '30' } });

const loginResponse = await typedFetch('/login_post', { jsonBody: { email: '', password: '' } });

const user = await typedFetch('/users/:userId_get', { urlParams: { userId: 'xyz' } });