@droz-js/sdk 0.3.17 → 0.3.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@droz-js/sdk",
3
3
  "description": "Droz SDK",
4
- "version": "0.3.17",
4
+ "version": "0.3.18",
5
5
  "private": false,
6
6
  "exports": {
7
7
  ".": "./src/index.js",
@@ -5,6 +5,7 @@ export interface Service {
5
5
  type: string;
6
6
  endpoint: string;
7
7
  }
8
+ type events = 'error';
8
9
  declare class TenantConfig {
9
10
  readonly tenant: string;
10
11
  private readonly services;
@@ -26,9 +27,9 @@ export declare class DrozSdk {
26
27
  static getTenantConfig(customTenant?: string): Promise<TenantConfig>;
27
28
  private static getOrDiscoverTenantConfig;
28
29
  private static discoverTenantConfig;
29
- static on<T>(event: string, listener: (e: T) => void): void;
30
- static off<T>(event: string, listener: (e: T) => void): void;
31
- static emit(event: string, data: any): void;
30
+ static on<T>(event: events, listener: (e: T) => void): void;
31
+ static off<T>(event: events, listener: (e: T) => void): void;
32
+ static emit(event: events, data: any): void;
32
33
  private static addEventListener;
33
34
  private static removeEventListener;
34
35
  }
package/src/nucleus.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { CreatePresignedUploadUrlInput } from './sdks/nucleus';
2
2
  export * from './sdks/nucleus';
3
+ type OnProgress = (progress: number) => void;
3
4
  declare const Nucleus_base: new (options?: import("./client/http").HttpClientOptions) => {
4
5
  readonly http: any;
5
6
  forTenant(tenant: string): any;
@@ -193,7 +194,8 @@ declare const Nucleus_base: new (options?: import("./client/http").HttpClientOpt
193
194
  }>, options?: unknown): Promise<import("./sdks/nucleus").CreatePresignedUploadUrlMutation>;
194
195
  };
195
196
  export declare class Nucleus extends Nucleus_base {
196
- uploadBlob(fileName: string, blob: Blob): Promise<string>;
197
- uploadFile(blob: File): Promise<string>;
197
+ uploadBlob(fileName: string, blob: Blob, onProgress: OnProgress): Promise<string>;
198
+ uploadFile(blob: File, onProgress: OnProgress): Promise<string>;
198
199
  private upload;
200
+ private post;
199
201
  }
package/src/nucleus.js CHANGED
@@ -15,29 +15,38 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  exports.Nucleus = void 0;
18
+ const config_1 = require("./client/config");
18
19
  const helpers_1 = require("./client/helpers");
19
20
  const http_1 = require("./client/http");
20
21
  const nucleus_1 = require("./sdks/nucleus");
21
22
  __exportStar(require("./sdks/nucleus"), exports);
22
23
  class Nucleus extends (0, http_1.HttpClientBuilder)(nucleus_1.serviceName, nucleus_1.getSdk) {
23
- async uploadBlob(fileName, blob) {
24
+ async uploadBlob(fileName, blob, onProgress) {
24
25
  return await this.upload({
25
26
  fileName,
26
27
  blob,
27
28
  contentType: blob.type,
28
- contentLength: blob.size
29
+ contentLength: blob.size,
30
+ onProgress
31
+ }).catch(error => {
32
+ config_1.DrozSdk.emit('error', error);
33
+ throw error;
29
34
  });
30
35
  }
31
- async uploadFile(blob) {
36
+ async uploadFile(blob, onProgress) {
32
37
  return await this.upload({
33
38
  blob,
34
39
  fileName: blob.name,
35
40
  contentType: blob.type,
36
- contentLength: blob.size
41
+ contentLength: blob.size,
42
+ onProgress
43
+ }).catch(error => {
44
+ config_1.DrozSdk.emit('error', error);
45
+ throw error;
37
46
  });
38
47
  }
39
48
  async upload(upload) {
40
- const { blob, ...input } = upload;
49
+ const { blob, onProgress, ...input } = upload;
41
50
  const { createPresignedUploadUrl } = await this.createPresignedUploadUrl({ input });
42
51
  if (createPresignedUploadUrl) {
43
52
  const { method, url, fields, cdnUrl } = createPresignedUploadUrl;
@@ -46,14 +55,38 @@ class Nucleus extends (0, http_1.HttpClientBuilder)(nucleus_1.serviceName, nucle
46
55
  body.append(key, value);
47
56
  });
48
57
  body.append('file', blob);
49
- const response = await fetch(url, { method, body });
58
+ const response = await this.post(method, url, body, onProgress);
50
59
  if (response.ok) {
51
60
  return cdnUrl;
52
61
  }
53
- const text = await response.text();
54
- throw new helpers_1.SdkError('500-upload-failed', text);
62
+ throw new helpers_1.SdkError('500-upload-failed', response.text);
55
63
  }
56
64
  throw new helpers_1.SdkError('500-upload-failed', 'No response from server');
57
65
  }
66
+ async post(method, url, body, onProgress) {
67
+ return new Promise((resolve, reject) => {
68
+ const xhr = new XMLHttpRequest();
69
+ if (onProgress) {
70
+ xhr.upload.addEventListener('progress', evt => {
71
+ onProgress(evt.loaded / evt.total);
72
+ });
73
+ }
74
+ xhr.addEventListener('load', () => {
75
+ const ok = xhr.status >= 200 && xhr.status < 300;
76
+ const text = xhr.responseText;
77
+ if (onProgress)
78
+ onProgress(1); // Ensure progress is 100% after load
79
+ resolve({ ok, text });
80
+ });
81
+ xhr.addEventListener('error', () => {
82
+ reject(new helpers_1.SdkError('500-upload-failed', 'Upload failed'));
83
+ });
84
+ xhr.addEventListener('abort', () => {
85
+ reject(new helpers_1.SdkError('400-upload-aborted', 'Upload aborted by user'));
86
+ });
87
+ xhr.open(method, url, true);
88
+ xhr.send(body);
89
+ });
90
+ }
58
91
  }
59
92
  exports.Nucleus = Nucleus;