简体   繁体   中英

What should I do to use function interface on Class property method

As you can see, in the Request Class, there are get(), put(), delete(), post() methods, and each of them has quite a few parameters. So I just want to use one interface.

interface fetchParam {
  (
    url: string,
    params?: string | object | [],
    alwaysBack?: boolean,
    options?: object,
    resolveResult?: boolean,
  ): {}
}

class Request {
  constructor() {}

  fetchApi(url, method, params, alwaysBack, options, resolveResult) {
     return new Promise((res, rej) => { 
       // do somthing
     })
  }

  get(url, params, alwaysBack, options, resolveResult) {
    return this.fetchApi(url, 'get', params, alwaysBack, options, resolveResult)
  }

  post(url, params, alwaysBack, options, resolveResult) {
    return this.fetchApi(url, 'post', params, alwaysBack, options, resolveResult)
  }

  put(url, params, alwaysBack, options, resolveResult) {
    return this.fetchApi(url, 'put', params, alwaysBack, options, resolveResult)
  }

  delete(url, params, alwaysBack, options, resolveResult) {
    return this.fetchApi(url, 'delete', params, alwaysBack, options, resolveResult)
  }


}

I do not want to like this, too cumbersome

get(url: string, params?: string | object | [], alwaysBack?: boolean) {
    // do somthing
}

so, what should I do?

my idea is to wrap it into an object

get({ url, params, alwaysBack }: fetchParam) {
// do something
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM