Changing fetch package for better web support

This commit is contained in:
Fabian Stamm
2019-01-20 22:54:01 +01:00
parent b76808022f
commit a8d5382ac3
3 changed files with 27 additions and 65 deletions

View File

@ -1,5 +1,6 @@
import Observable from "./observable";
import Lock from "./lock";
import fetch from "cross-fetch"
export interface IFileVersion {
version: string;
@ -66,8 +67,6 @@ export class BadRequest extends Error {
}
}
import * as fetch from "isomorphic-fetch";
function statusParser(res: Response) {
if (res.status !== 200) {
switch (res.status) {
@ -131,13 +130,11 @@ export default class SecureFileWrapper {
"cache-control": "no-cache"
};
let body_n;
if (body) {
headers["Content-Type"] = "application/octet-stream"
body_n = Buffer ? Buffer.from(body instanceof ArrayBuffer ? body : body.buffer) : body;
}
try {
let res = await fetch(this.server + endpoint + query_str, { method, body: body_n, headers });
let res = await fetch(this.server + endpoint + query_str, { method, body, headers });
if (res.status === 401 && !second) {
await this.getJWT();
return this.makeRequest(endpoint, method, query, body, true);
@ -206,13 +203,7 @@ export default class SecureFileWrapper {
res = await this.makeRequest("/files/" + id, "GET", {});
}
if (res.arrayBuffer) {
return res.arrayBuffer()
} else {
let blob: Buffer = await (<any>res).buffer()
// console.log(blob.length);
return Uint8Array.from(blob).buffer;
}
return res.arrayBuffer()
}
async update(id: string, data: ArrayBuffer | ArrayBufferView, preview?: string, date?: Date, old = false): Promise<IFile> {