import "rxjs/add/observable/fromPromise" ; import { fromJS } from "immutable" ; import * as qs from "qs" ; import { get as getFromStorage } from "./localStorage" ; import { config } from "./config" ; import { fromPromise } from "rxjs/internal-compatibility" ; interface Args { endpoint ? : string ; params ? : Object ; fullEndpoint ? : string ; resHeader ? : boolean ; preventBody ? : boolean ; } interface ReqArgs extends Args { method : string ; } function makeUrl ( endpoint , url ? ) { const base = url || config . get ( "mainUrl" ) ; return endpoint ? base + `/${ endpoint }?` : base + "?" ; } const shallowRequest = ( method ) => method === "GET" || method === "HEAD" || method === "REMOVE" || method === "DELETE" ; export function request ( args ) { const shallow = shallowRequest ( args . method ) ; let body ; let url = makeUrl ( args . endpoint , args . fullEndpoint ) ; if ( ! shallow && ! args . preventBody ) { body = JSON . stringify ( args ) ; } if ( shallow || args . preventBody ) { url += qs . stringify ( args . params ) ; } let rawHeader = { Accept : "application/json" , "Content-Type" : "application/json" , "Accept-Charset" : "utf-8" } ; if ( getFromStorage ( "user" ) ) { rawHeader = Object . assign ( { } , rawHeader , { Authorization : `token ${ getFromStorage ( "user" ) . access_token }` } ) ; } const headers = new Headers ( rawHeader ) ; const req = new Request ( url , { method : args . method , headers , body } ) ; return fromPromise ( fetch ( req ) . then ( res => { if ( res . status >= 400 ) { throw res ; } return Promise . all ( [ res . json ( ) , Promise . resolve ( res . headers ) ] ) ; } ) . then ( x => { const res = fromJS ( x [ 0 ] ) ; return args . resHeader ? x [ 1 ] : res ; } ) ) ; } export function get ( args ) { return request ( Object . assign ( { } , args , { method : "GET" } ) ) ; } export function post ( args ) { return request ( Object . assign ( { } , args , { method : "POST" } ) ) ; } export function put ( args ) { return request ( Object . assign ( { } , args , { method : "PUT" } ) ) ; } export function remove ( args ) { return request ( Object . assign ( { } , args , { method : "DELETE" } ) ) ; } export default request ;