isomorphic-fetch-http
Advanced tools
Comparing version 0.1.4 to 0.2.0
@@ -6,6 +6,7 @@ 'use strict'; | ||
}); | ||
exports.$option = exports.$delete = exports.$put = exports.$post = exports.$get = undefined; | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _isomorphicFetch = require('isomorphic-fetch'); | ||
@@ -19,13 +20,7 @@ | ||
var httpConfig = { | ||
headers: { | ||
"Content-Type": "application/x-www-form-urlencoded", | ||
"Access-Control-Allow-Origin": "*", | ||
"Access-Control-Max-Age": "86400", | ||
"Access-Control-Allow-Methods": "POST, GET, OPTIONS, PUT, DELETE", | ||
"Access-Control-Allow-Headers": "token, host, x-real-ip, x-forwarded-ip, accept, content-type" | ||
}, | ||
credentials: "include", | ||
client_max_body_size: "2048m" | ||
}; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var config = Symbol('config'); | ||
var http = Symbol('http'); | ||
/* | ||
@@ -38,50 +33,95 @@ * Requests a URL, returning a promise. | ||
*/ | ||
var $http = function $http(url) { | ||
var option = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var header = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
return (0, _isomorphicFetch2.default)(header.prefix ? '' + header.prefix + url : url, _extends({}, httpConfig, { headers: _extends({}, httpConfig.headers, header) }, option)).then(function (resp) { | ||
if (resp.status >= 400) { | ||
throw new Error('400+Error'); | ||
var $http = function () { | ||
function $http() { | ||
_classCallCheck(this, $http); | ||
} | ||
_createClass($http, [{ | ||
key: 'contructor', | ||
value: function contructor() { | ||
// bind this | ||
this.prefix = ""; | ||
this.header = {}; | ||
this.fn = function (data) { | ||
return data; | ||
}; | ||
this.$get = this.$get.bind(this); | ||
this.$post = this.$post.bind(this); | ||
this.$put = this.$put.bind(this); | ||
this.$delete = this.$delete.bind(this); | ||
this.$option = this.$option.bind(this); | ||
this[config] = { | ||
headers: { | ||
"Content-Type": "application/x-www-form-urlencoded", | ||
"Access-Control-Allow-Origin": "*", | ||
"Access-Control-Max-Age": "86400", | ||
"Access-Control-Allow-Methods": "POST, GET, OPTIONS, PUT, DELETE", | ||
"Access-Control-Allow-Headers": "token, host, x-real-ip, x-forwarded-ip, accept, content-type" | ||
}, | ||
credentials: "include", | ||
client_max_body_size: "2048m" | ||
}; | ||
} | ||
return resp; | ||
}).then(function (resp) { | ||
try { | ||
return resp.json(); | ||
} catch (e) { | ||
throw new Error('JSONError'); | ||
}, { | ||
key: http, | ||
value: function value(url) { | ||
var option = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var header = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
return (0, _isomorphicFetch2.default)('' + this.prefix + url, _extends({}, this[config], { headers: _extends({}, this[config].headers, this.header, header) }, option)).then(function (resp) { | ||
if (resp.status >= 400) { | ||
throw new Error('400+Error'); | ||
} | ||
return resp; | ||
}).then(function (resp) { | ||
try { | ||
return resp.json(); | ||
} catch (e) { | ||
throw new Error('JSONError'); | ||
} | ||
}).then(this.fn).then(function (data) { | ||
return data; | ||
}); | ||
} | ||
}).then(function (data) { | ||
return data; | ||
}); | ||
}; | ||
}, { | ||
key: 'setup', | ||
value: function setup(prefix) { | ||
var header = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var fn = arguments[2]; | ||
var $get = exports.$get = function $get(url) { | ||
var param = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var header = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
return $http(url + '?' + (0, _qs.stringify)(param), header); | ||
}; | ||
this.prefix = prefix; | ||
this.header = header; | ||
this.fn = fn; | ||
} | ||
}, { | ||
key: '$get', | ||
value: function $get(url, param) { | ||
return this[http](url + '?' + (0, _qs.stringify)(param)); | ||
} | ||
}, { | ||
key: '$post', | ||
value: function $post(url, param) { | ||
return this[http](url, { method: 'POST', body: (0, _qs.stringify)(param) }); | ||
} | ||
}, { | ||
key: '$put', | ||
value: function $put(url, param) { | ||
return this[http](url, { method: 'PUT', body: (0, _qs.stringify)(param) }); | ||
} | ||
}, { | ||
key: '$delete', | ||
value: function $delete(url, param) { | ||
return this[http](url + '?' + (0, _qs.stringify)(param), { method: 'DELETE' }); | ||
} | ||
}, { | ||
key: '$option', | ||
value: function $option(url, param) { | ||
return $http(url, { method: 'POST', body: JSON.stringify(param) }, { "Content-Type": "application/json" }); | ||
} | ||
}]); | ||
var $post = exports.$post = function $post(url) { | ||
var param = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var header = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
return $http(url, { method: 'POST', body: (0, _qs.stringify)(param) }, header); | ||
}; | ||
return $http; | ||
}(); | ||
var $put = exports.$put = function $put(url) { | ||
var param = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var header = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
return $http(url, { method: 'PUT', body: (0, _qs.stringify)(param) }, header); | ||
}; | ||
var $delete = exports.$delete = function $delete(url) { | ||
var param = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var header = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
return $http(url + '?' + (0, _qs.stringify)(param), { method: 'DELETE' }, header); | ||
}; | ||
var $option = exports.$option = function $option(url) { | ||
var param = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var header = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
return $http(url, { method: 'POST', body: JSON.stringify(param) }, _extends({}, header, { "Content-Type": "application/json" })); | ||
}; | ||
exports.default = new $http(); |
{ | ||
"name": "isomorphic-fetch-http", | ||
"version": "0.1.4", | ||
"version": "0.2.0", | ||
"description": "react fetch by isomorphic-fetch", | ||
@@ -5,0 +5,0 @@ "main": "lib/isomorphic-fetch-http.js", |
import fetch from 'isomorphic-fetch'; | ||
import { stringify } from 'qs'; | ||
const httpConfig = { | ||
headers: { | ||
"Content-Type": "application/x-www-form-urlencoded", | ||
"Access-Control-Allow-Origin": "*", | ||
"Access-Control-Max-Age": "86400", | ||
"Access-Control-Allow-Methods": "POST, GET, OPTIONS, PUT, DELETE", | ||
"Access-Control-Allow-Headers": "token, host, x-real-ip, x-forwarded-ip, accept, content-type", | ||
}, | ||
credentials: "include", | ||
client_max_body_size: "2048m" | ||
}; | ||
const config = Symbol('config'); | ||
const http = Symbol('http'); | ||
/* | ||
@@ -22,4 +14,28 @@ * Requests a URL, returning a promise. | ||
*/ | ||
const $http = (url, option = {}, header = {}) => { | ||
return fetch(header.prefix ? `${header.prefix}${url}` : url, { ...httpConfig, headers: { ...httpConfig.headers, ...header }, ...option }) | ||
class $http { | ||
contructor() { | ||
// bind this | ||
this.prefix = ""; | ||
this.header = {}; | ||
this.fn = (data) => data; | ||
this.$get = this.$get.bind(this); | ||
this.$post = this.$post.bind(this); | ||
this.$put = this.$put.bind(this); | ||
this.$delete = this.$delete.bind(this); | ||
this.$option = this.$option.bind(this); | ||
this[config] = { | ||
headers: { | ||
"Content-Type": "application/x-www-form-urlencoded", | ||
"Access-Control-Allow-Origin": "*", | ||
"Access-Control-Max-Age": "86400", | ||
"Access-Control-Allow-Methods": "POST, GET, OPTIONS, PUT, DELETE", | ||
"Access-Control-Allow-Headers": "token, host, x-real-ip, x-forwarded-ip, accept, content-type", | ||
}, | ||
credentials: "include", | ||
client_max_body_size: "2048m" | ||
}; | ||
} | ||
[http](url, option = {}, header = {}) { | ||
return fetch(`${this.prefix}${url}`, { ...this[config], headers: { ...this[config].headers, ...this.header, ...header }, ...option }) | ||
.then((resp) => { | ||
@@ -38,13 +54,33 @@ if (resp.status >= 400) { | ||
}) | ||
.then((data) => data); | ||
}; | ||
.then(this.fn) | ||
.then((data) => data); | ||
} | ||
export const $get = (url, param = {}, header = {}) => ($http(`${url}?${stringify(param)}`, header)); | ||
setup(prefix, header = {}, fn) { | ||
this.prefix = prefix; | ||
this.header = header; | ||
this.fn = fn; | ||
} | ||
export const $post = (url, param = {}, header = {}) => ($http(url, { method: 'POST', body: stringify(param) }, header)); | ||
$get(url, param) { | ||
return this[http](`${url}?${stringify(param)}`); | ||
} | ||
export const $put = (url, param = {}, header = {}) => ($http(url, { method: 'PUT', body: stringify(param) }, header)); | ||
$post(url, param) { | ||
return this[http](url, { method: 'POST', body: stringify(param) }); | ||
} | ||
export const $delete = (url, param = {}, header = {}) => ($http(`${url}?${stringify(param)}`, { method: 'DELETE' }, header)); | ||
$put(url, param) { | ||
return this[http](url, { method: 'PUT', body: stringify(param) }); | ||
} | ||
export const $option = (url, param = {}, header = {}) => ($http(url, { method: 'POST', body: JSON.stringify(param) }, {...header, ...{ "Content-Type": "application/json" }})); | ||
$delete(url, param) { | ||
return this[http](`${url}?${stringify(param)}`, { method: 'DELETE' }); | ||
} | ||
$option(url, param) { | ||
return $http(url, { method: 'POST', body: JSON.stringify(param) }, {"Content-Type": "application/json"}); | ||
} | ||
} | ||
export default new $http(); |
9696
181