@thetechfossil/upfiles 1.0.7 → 1.0.9

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/dist/index.d.mts CHANGED
@@ -56,6 +56,8 @@ type UpfilesClientOptions = {
56
56
  apiKey?: string;
57
57
  apiKeyHeader?: 'authorization' | 'x-api-key' | 'x-up-api-key';
58
58
  thumbnailsPath?: string;
59
+ completionPath?: string;
60
+ callCompletionEndpoint?: boolean;
59
61
  };
60
62
  /**
61
63
  * UpfilesClient - Main client class for interacting with Upfiles Plugin API
@@ -81,6 +83,8 @@ declare class UpfilesClient {
81
83
  private apiKey?;
82
84
  private apiKeyHeader;
83
85
  private thumbnailsPath;
86
+ private completionPath;
87
+ private callCompletionEndpoint;
84
88
  constructor(opts: UpfilesClientOptions);
85
89
  getPresignedUrl(params: {
86
90
  fileName: string;
@@ -94,7 +98,15 @@ declare class UpfilesClient {
94
98
  projectId?: string;
95
99
  folderPath?: string;
96
100
  fetchThumbnails?: boolean;
101
+ callCompletionEndpoint?: boolean;
97
102
  }): Promise<UploadedFileResult>;
103
+ completeUpload(params: {
104
+ fileKey: string;
105
+ originalName: string;
106
+ size: number;
107
+ contentType: string;
108
+ projectId?: string;
109
+ }): Promise<void>;
98
110
  getThumbnails(fileKey: string): Promise<Thumbnail[]>;
99
111
  generateThumbnails(fileKey: string): Promise<{
100
112
  masterWebp?: {
package/dist/index.d.ts CHANGED
@@ -56,6 +56,8 @@ type UpfilesClientOptions = {
56
56
  apiKey?: string;
57
57
  apiKeyHeader?: 'authorization' | 'x-api-key' | 'x-up-api-key';
58
58
  thumbnailsPath?: string;
59
+ completionPath?: string;
60
+ callCompletionEndpoint?: boolean;
59
61
  };
60
62
  /**
61
63
  * UpfilesClient - Main client class for interacting with Upfiles Plugin API
@@ -81,6 +83,8 @@ declare class UpfilesClient {
81
83
  private apiKey?;
82
84
  private apiKeyHeader;
83
85
  private thumbnailsPath;
86
+ private completionPath;
87
+ private callCompletionEndpoint;
84
88
  constructor(opts: UpfilesClientOptions);
85
89
  getPresignedUrl(params: {
86
90
  fileName: string;
@@ -94,7 +98,15 @@ declare class UpfilesClient {
94
98
  projectId?: string;
95
99
  folderPath?: string;
96
100
  fetchThumbnails?: boolean;
101
+ callCompletionEndpoint?: boolean;
97
102
  }): Promise<UploadedFileResult>;
103
+ completeUpload(params: {
104
+ fileKey: string;
105
+ originalName: string;
106
+ size: number;
107
+ contentType: string;
108
+ projectId?: string;
109
+ }): Promise<void>;
98
110
  getThumbnails(fileKey: string): Promise<Thumbnail[]>;
99
111
  generateThumbnails(fileKey: string): Promise<{
100
112
  masterWebp?: {
package/dist/index.js CHANGED
@@ -58,6 +58,8 @@ var UpfilesClient = class {
58
58
  this.apiKey = opts.apiKey;
59
59
  this.apiKeyHeader = opts.apiKeyHeader ?? "authorization";
60
60
  this.thumbnailsPath = opts.thumbnailsPath ?? "/api/plugin/thumbnails";
61
+ this.completionPath = opts.completionPath ?? "/api/plugin/upload/complete";
62
+ this.callCompletionEndpoint = opts.callCompletionEndpoint ?? false;
61
63
  }
62
64
  async getPresignedUrl(params) {
63
65
  if (!params.fileName) throw new Error("fileName is required");
@@ -116,6 +118,20 @@ var UpfilesClient = class {
116
118
  if (!res.ok) {
117
119
  throw new Error(`Upload failed with status ${res.status}`);
118
120
  }
121
+ const shouldCallCompletion = extras?.callCompletionEndpoint ?? this.callCompletionEndpoint;
122
+ if (shouldCallCompletion) {
123
+ try {
124
+ await this.completeUpload({
125
+ fileKey: presign.fileKey,
126
+ originalName: file.name,
127
+ size: file.size,
128
+ contentType: file.type || "application/octet-stream",
129
+ projectId: extras?.projectId
130
+ });
131
+ } catch (error) {
132
+ throw new Error(`Upload verification failed: ${error.message}`);
133
+ }
134
+ }
119
135
  let thumbnails;
120
136
  if (extras?.fetchThumbnails) {
121
137
  try {
@@ -135,6 +151,37 @@ var UpfilesClient = class {
135
151
  thumbnails
136
152
  };
137
153
  }
154
+ async completeUpload(params) {
155
+ const target = this.baseUrl ? `${this.baseUrl}${this.completionPath}` : this.completionPath;
156
+ const headers = {
157
+ "content-type": "application/json",
158
+ ...this.headers || {}
159
+ };
160
+ if (this.apiKey) {
161
+ if (this.apiKeyHeader === "authorization") {
162
+ headers["authorization"] = this.apiKey.startsWith("upk_") ? `Bearer ${this.apiKey}` : this.apiKey;
163
+ } else if (this.apiKeyHeader === "x-api-key") {
164
+ headers["x-api-key"] = this.apiKey;
165
+ } else {
166
+ headers["x-up-api-key"] = this.apiKey;
167
+ }
168
+ }
169
+ const res = await fetch(target, {
170
+ method: "POST",
171
+ headers,
172
+ credentials: this.withCredentials ? "include" : "same-origin",
173
+ body: JSON.stringify(params)
174
+ });
175
+ if (!res.ok) {
176
+ let msg = "Failed to complete upload";
177
+ try {
178
+ const data = await res.json();
179
+ msg = data.error || msg;
180
+ } catch {
181
+ }
182
+ throw new Error(msg);
183
+ }
184
+ }
138
185
  async getThumbnails(fileKey) {
139
186
  const target = this.baseUrl ? `${this.baseUrl}${this.thumbnailsPath}` : this.thumbnailsPath;
140
187
  const url = `${target}?fileKey=${encodeURIComponent(fileKey)}`;
@@ -319,6 +366,20 @@ var Uploader = ({
319
366
  xhr.setRequestHeader("Content-Type", item.type || "application/octet-stream");
320
367
  xhr.send(item.file);
321
368
  });
369
+ if (autoRecordToDb && projectId) {
370
+ try {
371
+ await client.completeUpload({
372
+ fileKey: presign.fileKey,
373
+ originalName: item.name,
374
+ size: item.size,
375
+ contentType: item.type,
376
+ projectId
377
+ });
378
+ } catch (e) {
379
+ console.error("Failed to complete upload:", e);
380
+ throw new Error(`Upload verification failed: ${e.message}`);
381
+ }
382
+ }
322
383
  let thumbs;
323
384
  let masterWebp;
324
385
  try {
@@ -330,38 +391,6 @@ var Uploader = ({
330
391
  } catch (e) {
331
392
  console.warn("Failed to generate thumbnails:", e);
332
393
  }
333
- if (autoRecordToDb && projectId) {
334
- try {
335
- const baseUrl = clientOptions?.baseUrl || "";
336
- const url = baseUrl ? `${baseUrl}${recordUrl}` : recordUrl;
337
- const headers = {
338
- "content-type": "application/json",
339
- ...clientOptions?.headers || {}
340
- };
341
- if (clientOptions?.apiKey) {
342
- const keyHeader = clientOptions.apiKeyHeader || "authorization";
343
- if (keyHeader === "authorization") {
344
- headers["authorization"] = clientOptions.apiKey.startsWith("upk_") ? `Bearer ${clientOptions.apiKey}` : clientOptions.apiKey;
345
- } else {
346
- headers[keyHeader] = clientOptions.apiKey;
347
- }
348
- }
349
- await fetch(url, {
350
- method: "POST",
351
- headers,
352
- credentials: clientOptions?.withCredentials ? "include" : "same-origin",
353
- body: JSON.stringify({
354
- key: presign.fileKey,
355
- originalName: item.name,
356
- size: item.size,
357
- contentType: item.type,
358
- projectId
359
- })
360
- });
361
- } catch (e) {
362
- console.warn("Failed to record file to database:", e);
363
- }
364
- }
365
394
  const result = {
366
395
  ...item,
367
396
  status: "success",
package/dist/index.mjs CHANGED
@@ -9,6 +9,8 @@ var UpfilesClient = class {
9
9
  this.apiKey = opts.apiKey;
10
10
  this.apiKeyHeader = opts.apiKeyHeader ?? "authorization";
11
11
  this.thumbnailsPath = opts.thumbnailsPath ?? "/api/plugin/thumbnails";
12
+ this.completionPath = opts.completionPath ?? "/api/plugin/upload/complete";
13
+ this.callCompletionEndpoint = opts.callCompletionEndpoint ?? false;
12
14
  }
13
15
  async getPresignedUrl(params) {
14
16
  if (!params.fileName) throw new Error("fileName is required");
@@ -67,6 +69,20 @@ var UpfilesClient = class {
67
69
  if (!res.ok) {
68
70
  throw new Error(`Upload failed with status ${res.status}`);
69
71
  }
72
+ const shouldCallCompletion = extras?.callCompletionEndpoint ?? this.callCompletionEndpoint;
73
+ if (shouldCallCompletion) {
74
+ try {
75
+ await this.completeUpload({
76
+ fileKey: presign.fileKey,
77
+ originalName: file.name,
78
+ size: file.size,
79
+ contentType: file.type || "application/octet-stream",
80
+ projectId: extras?.projectId
81
+ });
82
+ } catch (error) {
83
+ throw new Error(`Upload verification failed: ${error.message}`);
84
+ }
85
+ }
70
86
  let thumbnails;
71
87
  if (extras?.fetchThumbnails) {
72
88
  try {
@@ -86,6 +102,37 @@ var UpfilesClient = class {
86
102
  thumbnails
87
103
  };
88
104
  }
105
+ async completeUpload(params) {
106
+ const target = this.baseUrl ? `${this.baseUrl}${this.completionPath}` : this.completionPath;
107
+ const headers = {
108
+ "content-type": "application/json",
109
+ ...this.headers || {}
110
+ };
111
+ if (this.apiKey) {
112
+ if (this.apiKeyHeader === "authorization") {
113
+ headers["authorization"] = this.apiKey.startsWith("upk_") ? `Bearer ${this.apiKey}` : this.apiKey;
114
+ } else if (this.apiKeyHeader === "x-api-key") {
115
+ headers["x-api-key"] = this.apiKey;
116
+ } else {
117
+ headers["x-up-api-key"] = this.apiKey;
118
+ }
119
+ }
120
+ const res = await fetch(target, {
121
+ method: "POST",
122
+ headers,
123
+ credentials: this.withCredentials ? "include" : "same-origin",
124
+ body: JSON.stringify(params)
125
+ });
126
+ if (!res.ok) {
127
+ let msg = "Failed to complete upload";
128
+ try {
129
+ const data = await res.json();
130
+ msg = data.error || msg;
131
+ } catch {
132
+ }
133
+ throw new Error(msg);
134
+ }
135
+ }
89
136
  async getThumbnails(fileKey) {
90
137
  const target = this.baseUrl ? `${this.baseUrl}${this.thumbnailsPath}` : this.thumbnailsPath;
91
138
  const url = `${target}?fileKey=${encodeURIComponent(fileKey)}`;
@@ -270,6 +317,20 @@ var Uploader = ({
270
317
  xhr.setRequestHeader("Content-Type", item.type || "application/octet-stream");
271
318
  xhr.send(item.file);
272
319
  });
320
+ if (autoRecordToDb && projectId) {
321
+ try {
322
+ await client.completeUpload({
323
+ fileKey: presign.fileKey,
324
+ originalName: item.name,
325
+ size: item.size,
326
+ contentType: item.type,
327
+ projectId
328
+ });
329
+ } catch (e) {
330
+ console.error("Failed to complete upload:", e);
331
+ throw new Error(`Upload verification failed: ${e.message}`);
332
+ }
333
+ }
273
334
  let thumbs;
274
335
  let masterWebp;
275
336
  try {
@@ -281,38 +342,6 @@ var Uploader = ({
281
342
  } catch (e) {
282
343
  console.warn("Failed to generate thumbnails:", e);
283
344
  }
284
- if (autoRecordToDb && projectId) {
285
- try {
286
- const baseUrl = clientOptions?.baseUrl || "";
287
- const url = baseUrl ? `${baseUrl}${recordUrl}` : recordUrl;
288
- const headers = {
289
- "content-type": "application/json",
290
- ...clientOptions?.headers || {}
291
- };
292
- if (clientOptions?.apiKey) {
293
- const keyHeader = clientOptions.apiKeyHeader || "authorization";
294
- if (keyHeader === "authorization") {
295
- headers["authorization"] = clientOptions.apiKey.startsWith("upk_") ? `Bearer ${clientOptions.apiKey}` : clientOptions.apiKey;
296
- } else {
297
- headers[keyHeader] = clientOptions.apiKey;
298
- }
299
- }
300
- await fetch(url, {
301
- method: "POST",
302
- headers,
303
- credentials: clientOptions?.withCredentials ? "include" : "same-origin",
304
- body: JSON.stringify({
305
- key: presign.fileKey,
306
- originalName: item.name,
307
- size: item.size,
308
- contentType: item.type,
309
- projectId
310
- })
311
- });
312
- } catch (e) {
313
- console.warn("Failed to record file to database:", e);
314
- }
315
- }
316
345
  const result = {
317
346
  ...item,
318
347
  status: "success",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thetechfossil/upfiles",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "description": "Lightweight client and React components for Upfiles Plugin API (presigned S3)",
5
5
  "license": "MIT",
6
6
  "author": "UpFiles",