Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] feat: Support put and delete request #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions lib/rexxarFetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rexxar-web",
"version": "0.2.1",
"version": "0.2.2",
"description": "Rexxar Web",
"main": "./lib/index.js",
"scripts": {
Expand Down
17 changes: 9 additions & 8 deletions src/rexxarFetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down