Advertisement
Guest User

Untitled

a guest
Aug 15th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. WooCommerceAPI.prototype._request = async function(method, endpoint, data) {
  2.   const url = this.url + endpoint
  3.  
  4.   const params = {
  5.     url,
  6.     method,
  7.     encoding: this.encoding,
  8.     timeout: this.timeout,
  9.   }
  10.  
  11.   // if (this.isSsl) {
  12.   //   if (this.queryStringAuth) {
  13.   //     params.qs = {
  14.   //       consumer_key: this.consumerKey,
  15.   //       consumer_secret: this.consumerSecret,
  16.   //       ...data,
  17.   //     }
  18.   //   } else {
  19.   //     params.auth = {
  20.   //       user: this.consumerKey,
  21.   //       pass: this.consumerSecret,
  22.   //     }
  23.   //   }
  24.  
  25.   //   if (this.verifySsl) {
  26.   //     params.strictSSL = this.verifySsl
  27.   //   }
  28.   // } else if (method == 'GET') {
  29.   //   params.qs = this._getOAuth().authorize({
  30.   //     url,
  31.   //     method,
  32.   //     data,
  33.   //   })
  34.   // } else if (method == 'POST') {
  35.   //   params.qs = this._getOAuth().authorize({
  36.   //     url,
  37.   //     method,
  38.   //   })
  39.   // }
  40.  
  41.   // console.log(params.url);
  42.  
  43.   // encode the oauth_signature to make sure it not remove + charactor
  44.   // params.qs.oauth_signature = encodeURIComponent(params.qs.oauth_signature)
  45.  
  46.  
  47.  
  48.   params.url = `${params.url}`
  49.  
  50.   if (method == 'GET') {
  51.  
  52.     let paramsArray = [];
  53.     //拼接参数
  54.     if (data) {
  55.       Object.keys(data).forEach(key => paramsArray.push(key + '=' + data[key]))
  56.       if (params.url.search(/\?/) === -1) {
  57.           params.url += '?' + paramsArray.join('&')
  58.       } else {
  59.           params.url += '&' + paramsArray.join('&')
  60.       }
  61.     }
  62.  
  63.     params.headers = { 'Cache-Control': 'no-cache' }
  64.   } else if (method == 'POST') {
  65.     params.headers = {
  66.       Accept: 'application/json',
  67.       'Content-Type': 'application/json',
  68.     }
  69.     params.body = JSON.stringify(data)
  70.   }
  71.   // console.log(params.url);
  72.   return await fetch(params.url, params)
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement