-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpHiddenMethod.js
32 lines (32 loc) · 1.02 KB
/
httpHiddenMethod.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const proxiedFetch = window.fetch ;
window.fetch = function(requestedUrl, init) {
let methodParam;
const metaElement=document.querySelector('meta[name="hiddenMethodParam"]');
if(metaElement) {
methodParam=metaElement.content;
}else{
methodParam="_method";
}
let requestedHttpMethod=init && init.method ? init.method : "GET";
let effective={
httpMethod: requestedHttpMethod,
url: requestedUrl
};
if(requestedHttpMethod === "DELETE" || requestedHttpMethod === "PUT") {
let queryParamSeparator;
if(requestedUrl.indexOf("?") === -1){
queryParamSeparator="?";
}else{
queryParamSeparator="&";
}
effective.url=requestedUrl+queryParamSeparator+methodParam+"="+requestedHttpMethod;
effective.httpMethod="POST";
}
if(!init && effective.httpMethod !== "GET") {
init={};
}
if(init) {
init.method=effective.httpMethod;
}
return proxiedFetch.apply(this, [effective.url, init]);
};