diff --git a/package.json b/package.json index f3ba830..132dc3c 100644 --- a/package.json +++ b/package.json @@ -88,7 +88,10 @@ }, "prettier": "@ionic/prettier-config", "eslintConfig": { - "extends": "@ionic/eslint-config/recommended" + "extends": "@ionic/eslint-config/recommended", + "rules": { + "@typescript-eslint/prefer-optional-chain": "off" + } }, "capacitor": { "ios": { diff --git a/src/web.ts b/src/web.ts index 1a41add..f15fea6 100644 --- a/src/web.ts +++ b/src/web.ts @@ -3,7 +3,10 @@ import { WebPlugin } from "@capacitor/core"; import type { UploaderPlugin, uploadOption } from "./definitions"; export class UploaderWeb extends WebPlugin implements UploaderPlugin { - private uploads: Map = new Map(); + private uploads: Map< + string, + { controller: AbortController; retries: number } + > = new Map(); async startUpload(options: uploadOption): Promise<{ id: string }> { console.log("startUpload", options); @@ -24,16 +27,22 @@ export class UploaderWeb extends WebPlugin implements UploaderPlugin { if (upload) { upload.controller.abort(); this.uploads.delete(options.id); - this.notifyListeners('events', { - name: 'cancelled', + this.notifyListeners("events", { + name: "cancelled", id: options.id, - payload: {} + payload: {}, }); } } private async doUpload(id: string, options: uploadOption) { - const { filePath, serverUrl, headers = {}, method = 'POST', parameters = {} } = options; + const { + filePath, + serverUrl, + headers = {}, + method = "POST", + parameters = {}, + } = options; const upload = this.uploads.get(id); if (!upload) return; @@ -43,7 +52,7 @@ export class UploaderWeb extends WebPlugin implements UploaderPlugin { if (!file) throw new Error("File not found"); const formData = new FormData(); - formData.append('file', file); + formData.append("file", file); for (const [key, value] of Object.entries(parameters)) { formData.append(key, value); @@ -52,31 +61,32 @@ export class UploaderWeb extends WebPlugin implements UploaderPlugin { const response = await fetch(serverUrl, { method, headers, - body: method === 'PUT' ? file : formData, + body: method === "PUT" ? file : formData, signal: upload.controller.signal, }); - if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`); + if (!response.ok) + throw new Error(`HTTP error! status: ${response.status}`); - this.notifyListeners('events', { - name: 'completed', + this.notifyListeners("events", { + name: "completed", id, - payload: { statusCode: response.status } + payload: { statusCode: response.status }, }); this.uploads.delete(id); } catch (error) { - if ((error as Error).name === 'AbortError') return; + if ((error as Error).name === "AbortError") return; if (upload.retries > 0) { upload.retries--; console.log(`Retrying upload (retries left: ${upload.retries})`); setTimeout(() => this.doUpload(id, options), 1000); } else { - this.notifyListeners('events', { - name: 'failed', + this.notifyListeners("events", { + name: "failed", id, - payload: { error: (error as Error).message } + payload: { error: (error as Error).message }, }); this.uploads.delete(id); } @@ -89,7 +99,9 @@ export class UploaderWeb extends WebPlugin implements UploaderPlugin { try { const response = await fetch(filePath); const blob = await response.blob(); - return new File([blob], filePath.split('/').pop() || 'file', { type: blob.type }); + return new File([blob], filePath.split("/").pop() || "file", { + type: blob.type, + }); } catch (error) { console.error("Error getting file:", error); return null;