Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@skava/request / src / deps / toOptions.ts
Size: Mime:
import { stringify } from 'chain-able-boost'
import * as qs from 'query-string'

export interface OptionsArg {
  body: any
  method: any
  headers?: Headers
}

/**
 * https://github.com/niftylettuce/frisbee/blob/master/src/index.js#L142
 * in order to support Android POST requests
 * we must allow an empty body to be sent
 * https://github.com/facebook/react-native/issues/4890
 */
export function toOptions(opts: OptionsArg) {
  const headers: Headers = opts.headers || new Headers()
  let path = ''

  if (typeof opts.body === 'undefined' && opts.method === 'POST') {
    opts.body = ''
  } else if (typeof opts.body === 'object' || Array.isArray(opts.body)) {
    if (opts.method === 'GET' || opts.method === 'DELETE') {
      path += `?${qs.stringify(opts.body, { arrayFormat: true })}`
      delete opts.body
    } else if (
      headers.get('Content-Type') &&
      headers.get('Content-Type').split(';')[0] === 'application/json'
    ) {
      opts.body = stringify(opts.body)
    }
  }

  // @@todo
  return undefined
}