Skip to content

Commit

Permalink
fix: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
riderx committed Sep 4, 2024
1 parent f70cde3 commit ede56b5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
44 changes: 28 additions & 16 deletions src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, { controller: AbortController, retries: number }> = new Map();
private uploads: Map<
string,
{ controller: AbortController; retries: number }
> = new Map();

async startUpload(options: uploadOption): Promise<{ id: string }> {
console.log("startUpload", options);
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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);
}
Expand All @@ -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;
Expand Down

0 comments on commit ede56b5

Please sign in to comment.