thx / rapper

一个自带类型的请求库
https://www.yuque.com/rap/rapper/readme
96 stars 17 forks source link

无法区分query和body的参数 #4

Closed AtarisMio closed 4 years ago

AtarisMio commented 4 years ago

无法区分query和body的参数

无论在rap2上设置query或者body他都作为params进入

AtarisMio commented 4 years ago

1.0.4新增了path variable,也无法区分

mochen666 commented 4 years ago

目前Rap平台录入的大量接口都没有区分query和body,很多接口的body参数录入在query上,而query参数录入在body上

所以,在发送请求时就没有做区分,默认将 get 请求的所有参数作为query,将 post 请求的所有参数作为 body(在业务中极少数接口会同时有query和body)

AtarisMio commented 4 years ago

但是path variable的情况会有很多啊

mochen666 commented 4 years ago

但是path variable的情况会有很多啊

没理解你说的意思,贴一下代码或者截图吧

AtarisMio commented 4 years ago

@mochen666

WX20200728-163807

/* md5: 4fff71867e974e46c10fa31c956bac40 */
/* Rap仓库id: 41 */
/* Rapper版本: 1.1.0-beta.1 */
/* eslint-disable */
/* tslint:disable */

/**
 * 本文件由 Rapper 同步 Rap 平台接口,自动生成,请勿修改
 * Rap仓库 地址: https://rap2.lima-tech.com/repository/editor?id=41
 */

import * as commonLib from 'rap/runtime/commonLib'

export interface IModels {
  /**
   * 接口名:商品列表接口
   * Rap 地址: https://rap2.lima-tech.com/repository/editor?id=41&mod=112&itf=1031
   */
  'GET/api/products/{productType}': {
    Req: {
      /**
       * 页码
       */
      page?: number
      /**
       * 分页大小
       */
      pageSize?: number
      productType?: string
    }
    Res: {
      id: string
      name: string
    }[]
  }
}

type ResSelector<T> = T

export interface IResponseTypes {
  'GET/api/products/{productType}': ResSelector<IModels['GET/api/products/{productType}']['Res']>
}

export function createFetch(fetchConfig: commonLib.RequesterOption, extraConfig?: {fetchType?: commonLib.FetchType}) {
  if (!extraConfig?.fetchType) {
    console.warn(
      'Rapper Warning: createFetch API will be deprecated, if you want to customize fetch, please use overrideFetch instead, since new API guarantees better type consistency during frontend lifespan. See detail https://www.yuque.com/rap/rapper/overridefetch'
    )
  }
  const rapperFetch = commonLib.getRapperRequest(fetchConfig)

  return {
    /**
     * 接口名:商品列表接口
     * Rap 地址: https://rap2.lima-tech.com/repository/editor?id=41&mod=112&itf=1031
     * @param req 请求参数
     * @param extra 请求配置项
     */
    'GET/api/products/{productType}': (
      req?: IModels['GET/api/products/{productType}']['Req'],
      extra?: commonLib.IExtra
    ) => {
      return rapperFetch({
        url: '/api/products/{productType}',
        method: 'GET',
        params: req,
        extra,
      }) as Promise<IResponseTypes['GET/api/products/{productType}']>
    },
  }
}

这里我希望是能够区分path parameter以及query parameter ,以及body parameter ,然后可以在createFetch或者通过其他手段,将path parameter直接替换url中的path variable。

import querystring from 'querystring';
import {createFetch} from './utils/rapper';

// 只是示意,写的没有很完整 主要在于url的处理
const fetch = createFetch(({ url, method, params: { path, query, body }, extra }) => {
    return window.fetch(`${Object.entries(path).reduce((u, [k, v]) => u.replace('{'+k+'}', v).replace(':'+k, v), url)}${query ? '?' + querystring.stringify(query) : ''}`, { method, body: body ? JSON.stringify(body) : undefined, headers: { 'Content-Type': 'application/json' } })
});

// 比如这样调用的时候,希望能够自动替换url上的path variable
fetch['GET/api/products/{productType}']({productType: 'toys', page: 1, pageSize: 2})
mochen666 commented 4 years ago

@mochen666

WX20200728-163807

/* md5: 4fff71867e974e46c10fa31c956bac40 */
/* Rap仓库id: 41 */
/* Rapper版本: 1.1.0-beta.1 */
/* eslint-disable */
/* tslint:disable */

/**
 * 本文件由 Rapper 同步 Rap 平台接口,自动生成,请勿修改
 * Rap仓库 地址: https://rap2.lima-tech.com/repository/editor?id=41
 */

import * as commonLib from 'rap/runtime/commonLib'

export interface IModels {
  /**
   * 接口名:商品列表接口
   * Rap 地址: https://rap2.lima-tech.com/repository/editor?id=41&mod=112&itf=1031
   */
  'GET/api/products/{productType}': {
    Req: {
      /**
       * 页码
       */
      page?: number
      /**
       * 分页大小
       */
      pageSize?: number
      productType?: string
    }
    Res: {
      id: string
      name: string
    }[]
  }
}

type ResSelector<T> = T

export interface IResponseTypes {
  'GET/api/products/{productType}': ResSelector<IModels['GET/api/products/{productType}']['Res']>
}

export function createFetch(fetchConfig: commonLib.RequesterOption, extraConfig?: {fetchType?: commonLib.FetchType}) {
  if (!extraConfig?.fetchType) {
    console.warn(
      'Rapper Warning: createFetch API will be deprecated, if you want to customize fetch, please use overrideFetch instead, since new API guarantees better type consistency during frontend lifespan. See detail https://www.yuque.com/rap/rapper/overridefetch'
    )
  }
  const rapperFetch = commonLib.getRapperRequest(fetchConfig)

  return {
    /**
     * 接口名:商品列表接口
     * Rap 地址: https://rap2.lima-tech.com/repository/editor?id=41&mod=112&itf=1031
     * @param req 请求参数
     * @param extra 请求配置项
     */
    'GET/api/products/{productType}': (
      req?: IModels['GET/api/products/{productType}']['Req'],
      extra?: commonLib.IExtra
    ) => {
      return rapperFetch({
        url: '/api/products/{productType}',
        method: 'GET',
        params: req,
        extra,
      }) as Promise<IResponseTypes['GET/api/products/{productType}']>
    },
  }
}

这里我希望是能够区分path parameter以及query parameter ~,以及body parameter~ ,然后可以在createFetch或者通过其他手段,将path parameter直接替换url中的path variable。

import querystring from 'querystring';
import {createFetch} from './utils/rapper';

// 只是示意,写的没有很完整 主要在于url的处理
const fetch = createFetch(({ url, method, params: { path, query, body }, extra }) => {
    return window.fetch(`${Object.entries(path).reduce((u, [k, v]) => u.replace('{'+k+'}', v).replace(':'+k, v), url)}${query ? '?' + querystring.stringify(query) : ''}`, { method, body: body ? JSON.stringify(body) : undefined, headers: { 'Content-Type': 'application/json' } })
});

// 比如这样调用的时候,希望能够自动替换url上的path variable
fetch['GET/api/products/{productType}']({productType: 'toys', page: 1, pageSize: 2})

现在已经支持 RestfulAPI,且在这种场景 path parameter 和 query parameter 肯定是不可能重复的,所以,无需区分