Skip to content

Commit

Permalink
Add JSON requests
Browse files Browse the repository at this point in the history
  • Loading branch information
nyanja committed Apr 23, 2024
1 parent fc68fb3 commit d897dfd
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions twinspark.js
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@

/**
* Merge two sets of parameters into one
* @param {FormData|URLSearchParams} p1 Collection of parameters to be updated.
* @param {FormData} p1 Collection of parameters to be updated.
* @param {FormData|URLSearchParams|Iterable} p2 Collection of parameters to be merged in.
* @param {boolean=} removeEmpty Indicate if empty ('' or null) parameters from p2 should be removed.
* @return FormData|URLSearchParams
Expand Down Expand Up @@ -1363,9 +1363,8 @@
return opts;
}

var issimple = (Array.from(body.entries())
.every((x) => typeof x[1] === "string"));
if (!issimple) {
if (typeof body === "string" ||
!Array.from(body.entries()).every((x) => typeof x[1] === "string")) {
opts.body = body;
return opts;
}
Expand All @@ -1382,9 +1381,21 @@

var url = batch[0].url;
var method = batch[0].method;
var data = batch.reduce(function(acc, req) {
return mergeParams(acc, req.opts.data);
}, new FormData());
var data;

let json = getattr(batch[0].el, 'ts-json');

if (json) {
if (batch.length > 1) {
throw extraerr('Cannot batch json requests', {batch});
}
data = json;
batch[0].opts.headers['Content-Type'] = "application/json";
} else {
data = batch.reduce(function(acc, req) {
return mergeParams(acc, req.opts.data);
}, new FormData());
}

var qs = method == 'GET' ? new URLSearchParams(data).toString() : null;
var body = method != 'GET' ? data : null;
Expand Down

0 comments on commit d897dfd

Please sign in to comment.