From a7423765135af7ee6e8643f50ad7f37d244858df Mon Sep 17 00:00:00 2001 From: zhangyinghui Date: Sun, 2 Dec 2018 22:32:02 +0800 Subject: [PATCH] support put and delete request --- lib/rexxarFetch.js | 17 +++++++++-------- package.json | 2 +- src/rexxarFetch.js | 17 +++++++++-------- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/lib/rexxarFetch.js b/lib/rexxarFetch.js index 87991b0..0dc7d06 100644 --- a/lib/rexxarFetch.js +++ b/lib/rexxarFetch.js @@ -20,7 +20,7 @@ var isAndroid = navigator ? /android/i.test(navigator.userAgent.toLowerCase()) : /** * `rexxarFetch` wraps whatwg-fetch function. Use rexxarFetch like using the normal fetch API. * However, there are some limitation, rexxarFetch does not support Request object as - * argument when you are using for HTTP POST, and `application/x-www-form-urlencoded` + * argument when you are using for HTTP POST, PUT and DELETE, and `application/x-www-form-urlencoded` * must be specified as content-type. * * @param {string|object} input Url string or a Request object @@ -34,32 +34,33 @@ function rexxarFetch(input, init) { if (Request.prototype.isPrototypeOf(input) && !init) { request = input; - if (request.method === 'POST') { - throw new Error('rexxarFetch POST error: please use `rexxarFetch(input, init)` for HTTP POST'); + if (request.method === 'POST' || request.method === 'PUT' || request.method === 'DELETE') { + throw new Error('rexxarFetch ' + request.method + ' error: please use `rexxarFetch(input, init)` for HTTP ' + request.method); } } else { request = new Request(input, init); } - if (request.method === 'POST') { + if (request.method === 'POST' || request.method === 'PUT' || request.method === 'DELETE') { var contentType = request.headers.get('content-type'); var body = init.body; + var method = request.method; if (!contentType && !body) { - input = (input + '&_rexxar_method=POST').replace(/[&?]/, '?'); + input = (input + '&_rexxar_method=' + method).replace(/[&?]/, '?'); promise = (0, _isomorphicFetch2.default)(input); } else if (contentType && contentType.indexOf('application/x-www-form-urlencoded') > -1) { if (window && 'URLSearchParams' in window && window.URLSearchParams.prototype.isPrototypeOf(body)) { body = body.toString(); } if ((0, _utils.getType)(body) === 'String') { - input = (input + '&' + body + '&_rexxar_method=POST').replace(/[&?]/, '?'); + input = (input + '&' + body + '&_rexxar_method=' + method).replace(/[&?]/, '?'); promise = (0, _isomorphicFetch2.default)(input); } else { - throw new Error('rexxarFetch POST error: cannot handle this body type'); + throw new Error('rexxarFetch ' + method + ' error: cannot handle this body type'); } } else { - throw new Error('rexxarFetch POST error: only supports `application/x-www-form-urlencoded` as content-type'); + throw new Error('rexxarFetch ' + method + ' error: only supports `application/x-www-form-urlencoded` as content-type'); } } else { promise = (0, _isomorphicFetch2.default)(request); diff --git a/package.json b/package.json index 00ae1f0..e6cfe39 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rexxar-web", - "version": "0.2.1", + "version": "0.2.2", "description": "Rexxar Web", "main": "./lib/index.js", "scripts": { diff --git a/src/rexxarFetch.js b/src/rexxarFetch.js index 6860330..d770a40 100644 --- a/src/rexxarFetch.js +++ b/src/rexxarFetch.js @@ -9,7 +9,7 @@ const isAndroid = navigator ? /android/i.test(navigator.userAgent.toLowerCase()) /** * `rexxarFetch` wraps whatwg-fetch function. Use rexxarFetch like using the normal fetch API. * However, there are some limitation, rexxarFetch does not support Request object as - * argument when you are using for HTTP POST, and `application/x-www-form-urlencoded` + * argument when you are using for HTTP POST, PUT and DELETE, and `application/x-www-form-urlencoded` * must be specified as content-type. * * @param {string|object} input Url string or a Request object @@ -23,32 +23,33 @@ export default function rexxarFetch(input, init) { if (Request.prototype.isPrototypeOf(input) && !init) { request = input; - if (request.method === 'POST') { - throw new Error('rexxarFetch POST error: please use `rexxarFetch(input, init)` for HTTP POST'); + if (request.method === 'POST' || request.method === 'PUT' || request.method === 'DELETE') { + throw new Error(`rexxarFetch ${request.method} error: please use \`rexxarFetch(input, init)\` for HTTP ${request.method}`); } } else { request = new Request(input, init); } - if (request.method === 'POST') { + if (request.method === 'POST' || request.method === 'PUT' || request.method === 'DELETE') { let contentType = request.headers.get('content-type'); let body = init.body; + let method = request.method; if (!contentType && !body ) { - input = `${input}&_rexxar_method=POST`.replace(/[&?]/, '?'); + input = `${input}&_rexxar_method=${method}`.replace(/[&?]/, '?'); promise = fetch(input); } else if (contentType && contentType.indexOf('application/x-www-form-urlencoded') > -1) { if (window && 'URLSearchParams' in window && window.URLSearchParams.prototype.isPrototypeOf(body)) { body = body.toString(); } if (getType(body) === 'String') { - input = `${input}&${body}&_rexxar_method=POST`.replace(/[&?]/, '?'); + input = `${input}&${body}&_rexxar_method=${method}`.replace(/[&?]/, '?'); promise = fetch(input); } else { - throw new Error('rexxarFetch POST error: cannot handle this body type'); + throw new Error(`rexxarFetch ${method} error: cannot handle this body type`); } } else { - throw new Error('rexxarFetch POST error: only supports `application/x-www-form-urlencoded` as content-type'); + throw new Error(`rexxarFetch ${method} error: only supports \`application/x-www-form-urlencoded\` as content-type`); } } else { promise = fetch(request);