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

request-schema

v0.1.2

Published

Request Schema for TypeScript Intellisense

Downloads

8

Readme

Request Schema make possible to define your schema in one place and then use request() without the need to specify type for each request.

schema object is a mix of plain JavaScript and TypeScript definition. That is possible with the powerful as keyword.

For now, request-schema only support axios, but agnostic HTTP client is coming soon.

npm i [email protected]

Example

import { createAxiosInstance } from 'request-schema'

const schema = {
  'GET users': {
    url: 'users',
    method: 'GET',
    params: {} as {
      page?: number,
      pageSize?: number,
    },
    data: null,
    response: {} as {
      id: string,
      username: string,
      email: string
    }[]
  },
  'PATCH users/:id': {
    url: (params: { id: string }) => `users/${params.id}`,
    method: 'PATCH',
    params: null,
    data: {} as {
      username?: string,
      email?: string
    },
    response: {} as {
      id: string,
      username: string,
      email: string
    }
  },
}

const api = createAxiosInstance({ baseURL: '...' }, schema)

// GET
const user = await api.request({
  routeName: 'GET users',
  params: {
    page: 1,
    pageSize: 10
  },
})

// PATCH
const user = await api.request({
  routeName: 'PATCH users/:id',
  urlParams: {
    id: '1'
  },
  data: {
    username: 'John Doe'
  }
})

Properties url and method are used as JavaScript value

Properties params, data, response and url(params) are used as TypeScript definition.

TypeScript IntelliSense

  • routeName
    Route Name

  • data
    Data

  • params
    Params

  • urlParams
    URL Params

  • response
    Response

Things to know

Required and omited request properties

request-schema required 2 new properties on axios request config and omit 2 others.

It require:

  • routeName - use to get route config and definition
  • urlParams - object with params for url

It omit:

  • url
  • method

Theses 2 keys are already define in the schema

Schema keys names

Each keys of the schema object can be named like you want. In examples, names are GET users, GET users/:id, but you can named it GET_users, users get, retrieve users, update users/id, etc. Keys are use by TypeScript to find the correct route schema, so it's completely arbitrary

url, urlParams and method

Theses properties are handled by an axios interceptor in order to convert schema route to plain axios config.

:warning: Axios compatibility version

Not compatible with [email protected] due to breaking changes with custom config, see axios/issues/1718.

Use axios [email protected] instead