0.1.1 • Published 2 years ago

@umijs/request2type v0.1.1

Weekly downloads
-
License
-
Repository
-
Last release
2 years ago

request2type

a tool for generating type of request params/payload/query, works in http-proxy-middleware.

Install

$ yarn add @umijs/request2type --dev

Usage

In Umi

  1. add handler in umi config file.
import { EventHandler } from '@umijs/request2type';

export default {
  proxy: {
    '/api': {
      target: 'http://localhost:8080',
      onProxyReq: EventHandler.onProxyReq(proxyReq, req, res),
      onProxyRes: EventHandler.onProxyRes(proxyRes, req, res),
    },
  },
};
  1. add type file to tsconfig.json
{
  "typeRoots": ["./types"]
}
  1. start with node env
$ REQUEST_LISTEN=1 umi dev
  1. use type in request, all types export from namespace API
const getUserInfo = (query: API.GET_API_USER_INFO_QUERY) => {
  return axios.get<typeof query, AxiosResponse<API.GET_API_USER_INFO_RES>>(
    `/api/userInfo?${stringify(query)}`
  );
};