Repository URL to install this package:
|
Version:
7.0.2 ▾
|
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
}