forked from mystiq/hydrogen-web
extract map -> formdata conversion and also suppor this for xhr
This commit is contained in:
parent
a644621889
commit
4caabae895
3 changed files with 21 additions and 14 deletions
|
@ -27,6 +27,21 @@ export function addCacheBuster(urlStr, random = Math.random) {
|
||||||
return urlStr + `_cacheBuster=${Math.ceil(random() * Number.MAX_SAFE_INTEGER)}`;
|
return urlStr + `_cacheBuster=${Math.ceil(random() * Number.MAX_SAFE_INTEGER)}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function mapAsFormData(map) {
|
||||||
|
const formData = new FormData();
|
||||||
|
for (const [name, value] of map) {
|
||||||
|
let filename;
|
||||||
|
// Special case {name: string, blob: BlobHandle} to set a filename.
|
||||||
|
// This is the format returned by platform.openFile
|
||||||
|
if (value.blob?.nativeBlob && value.name) {
|
||||||
|
formData.set(name, value.blob.nativeBlob, value.name);
|
||||||
|
} else {
|
||||||
|
formData.set(name, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return formData;
|
||||||
|
}
|
||||||
|
|
||||||
export function tests() {
|
export function tests() {
|
||||||
return {
|
return {
|
||||||
"add cache buster": assert => {
|
"add cache buster": assert => {
|
||||||
|
|
|
@ -20,7 +20,7 @@ import {
|
||||||
ConnectionError
|
ConnectionError
|
||||||
} from "../../../../matrix/error.js";
|
} from "../../../../matrix/error.js";
|
||||||
import {abortOnTimeout} from "../../../../utils/timeout";
|
import {abortOnTimeout} from "../../../../utils/timeout";
|
||||||
import {addCacheBuster} from "./common.js";
|
import {addCacheBuster, mapAsFormData} from "./common.js";
|
||||||
import {xhrRequest} from "./xhr.js";
|
import {xhrRequest} from "./xhr.js";
|
||||||
|
|
||||||
class RequestResult {
|
class RequestResult {
|
||||||
|
@ -71,18 +71,7 @@ export function createFetchRequest(createTimeout, serviceWorkerHandler) {
|
||||||
body = body.nativeBlob;
|
body = body.nativeBlob;
|
||||||
}
|
}
|
||||||
if (body instanceof Map) {
|
if (body instanceof Map) {
|
||||||
const formData = new FormData();
|
body = mapAsFormData(body);
|
||||||
for (const [name, value] of body) {
|
|
||||||
let filename;
|
|
||||||
// Special case {name: string, blob: BlobHandle} to set a filename.
|
|
||||||
// This is the format returned by platform.openFile
|
|
||||||
if (value.blob?.nativeBlob && value.name) {
|
|
||||||
formData.set(name, value.blob.nativeBlob, value.name);
|
|
||||||
} else {
|
|
||||||
formData.set(name, value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
body = formData;
|
|
||||||
}
|
}
|
||||||
let options = {method, body};
|
let options = {method, body};
|
||||||
if (controller) {
|
if (controller) {
|
||||||
|
|
|
@ -18,7 +18,7 @@ import {
|
||||||
AbortError,
|
AbortError,
|
||||||
ConnectionError
|
ConnectionError
|
||||||
} from "../../../../matrix/error.js";
|
} from "../../../../matrix/error.js";
|
||||||
import {addCacheBuster} from "./common.js";
|
import {addCacheBuster, mapAsFormData} from "./common.js";
|
||||||
|
|
||||||
class RequestResult {
|
class RequestResult {
|
||||||
constructor(promise, xhr) {
|
constructor(promise, xhr) {
|
||||||
|
@ -94,6 +94,9 @@ export function xhrRequest(url, options) {
|
||||||
if (body?.nativeBlob) {
|
if (body?.nativeBlob) {
|
||||||
body = body.nativeBlob;
|
body = body.nativeBlob;
|
||||||
}
|
}
|
||||||
|
if (body instanceof Map) {
|
||||||
|
body = mapAsFormData(body);
|
||||||
|
}
|
||||||
xhr.send(body || null);
|
xhr.send(body || null);
|
||||||
|
|
||||||
return new RequestResult(promise, xhr);
|
return new RequestResult(promise, xhr);
|
||||||
|
|
Loading…
Reference in a new issue