@signskart/uploader 2.0.12 → 2.0.14

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/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Production-grade Upload Manager SDK by **Signskart**.
4
4
 
5
- A powerful, fully-typed file upload SDK with queue management, retry logic, cancellation support, concurrency control, and multi-provider architecture (S3 + Cloudinary).
5
+ A powerful, fully-typed file upload SDK with queue management, retry logic, cancellation support, concurrency control, and multi-provider architecture (S3 + Cloudinary + Firebase Storage).
6
6
 
7
7
  ---
8
8
 
@@ -13,7 +13,7 @@ A powerful, fully-typed file upload SDK with queue management, retry logic, canc
13
13
  * 📊 Real-time progress tracking
14
14
  * ❌ Cancel uploads (AbortController support)
15
15
  * ⚡ Concurrency control
16
- * ☁ Multi-provider support (Amazon S3, Cloudinary)
16
+ * ☁ Multi-provider support (Amazon S3, Cloudinary, Firebase Storage)
17
17
  * 🧠 Fully typed (TypeScript)
18
18
  * 🌍 Works with React, Vue, Next.js, Vite, vanilla JS
19
19
  * 🔐 Secure S3 presigned upload support
@@ -51,25 +51,22 @@ Cloudinary does NOT require backend if using unsigned preset.
51
51
 
52
52
  # 🚀 Quick Start
53
53
 
54
- ---
55
-
56
- # ☁ Using Amazon S3
57
-
58
- ## 1️⃣ Frontend Setup
54
+ ## 1️⃣ Client (S3)
59
55
 
60
56
  ```ts
61
57
  import { UploadManager, S3Uploader } from '@signskart/uploader';
62
58
 
63
- const uploader = new S3Uploader({
64
- presignEndpoint: '/api/s3/presign-upload',
65
- publicUrl: 'https://cdn.yoursite.com'
66
- });
67
-
68
- const manager = new UploadManager(uploader, 2);
59
+ const manager = new UploadManager(
60
+ new S3Uploader({
61
+ apiBaseUrl: import.meta.env.VITE_APP_API_BASE_URL,
62
+ publicUrl: import.meta.env.VITE_AWS_S3_BASE_URL,
63
+ }),
64
+ 3
65
+ );
69
66
 
70
67
  const task = manager.add({
71
68
  file,
72
- folder: 'designs'
69
+ folder: 'uploads',
73
70
  });
74
71
 
75
72
  task.events.subscribe((state) => {
@@ -77,6 +74,76 @@ task.events.subscribe((state) => {
77
74
  });
78
75
  ```
79
76
 
77
+ ## 2️⃣ Server Presign Endpoint
78
+
79
+ Use `createS3PresignHandler` in your backend and expose:
80
+
81
+ `POST /api/s3/presign-upload`
82
+
83
+ Expected request body:
84
+
85
+ ```json
86
+ {
87
+ "fileName": "logo.png",
88
+ "folder": "uploads",
89
+ "contentType": "image/png"
90
+ }
91
+ ```
92
+
93
+ Expected response body:
94
+
95
+ ```json
96
+ {
97
+ "signedUrl": "https://...",
98
+ "key": "uploads/1710000000000-logo.png",
99
+ "url": "https://cdn.example.com/uploads/1710000000000-logo.png"
100
+ }
101
+ ```
102
+
103
+ ---
104
+
105
+ # ✅ Signskart Admin Pattern (Recommended)
106
+
107
+ Goal: keep upload implementation inside `@signskart/uploader`; in admin only pass env and selected file.
108
+
109
+ ## 1️⃣ Admin wrapper (env only)
110
+
111
+ ```ts
112
+ // apps/signskart-admin/src/lib/uploads/createUploadManager.ts
113
+ import { S3Uploader, UploadManager } from '@signskart/uploader';
114
+
115
+ export const createUploadManager = (concurrency = 3) => {
116
+ return new UploadManager(
117
+ new S3Uploader({
118
+ apiBaseUrl: import.meta.env.VITE_APP_API_BASE_URL,
119
+ publicUrl: import.meta.env.VITE_AWS_S3_BASE_URL,
120
+ }),
121
+ concurrency
122
+ );
123
+ };
124
+ ```
125
+
126
+ ## 2️⃣ Admin component (select file + call manager)
127
+
128
+ ```tsx
129
+ const uploadManager = useMemo(() => createUploadManager(3), []);
130
+
131
+ const handleUpload = (file: File) => {
132
+ const task = uploadManager.add({
133
+ file,
134
+ folder: 'assets/store',
135
+ });
136
+
137
+ task.events.subscribe((state) => {
138
+ if (state.status === 'success') {
139
+ console.log('Uploaded URL:', state.response?.url);
140
+ }
141
+ });
142
+ };
143
+ ```
144
+
145
+ This keeps admin thin: env + selected file only.
146
+
80
147
  ---
81
148
 
82
149
  ## 2️⃣ Backend Setup (Next.js Example)
@@ -162,6 +229,231 @@ const task = manager.add({
162
229
 
163
230
  ---
164
231
 
232
+ # 🔥 Firebase Storage Full Examples (Next.js + Node.js)
233
+
234
+ Firebase mode is different from S3 mode:
235
+
236
+ * S3: frontend uploads via backend presign endpoint.
237
+ * Firebase: frontend can upload directly with Firebase client SDK.
238
+
239
+ ## 1️⃣ Next.js Client Example (using uploader package)
240
+
241
+ Install:
242
+
243
+ ```bash
244
+ npm install @signskart/uploader firebase
245
+ ```
246
+
247
+ Create env file:
248
+
249
+ ```bash
250
+ # .env.local
251
+ NEXT_PUBLIC_FIREBASE_API_KEY=...
252
+ NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN=...
253
+ NEXT_PUBLIC_FIREBASE_PROJECT_ID=...
254
+ NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET=...
255
+ NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID=...
256
+ NEXT_PUBLIC_FIREBASE_APP_ID=...
257
+ NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID=...
258
+ ```
259
+
260
+ Create uploader helper:
261
+
262
+ ```ts
263
+ // lib/firebaseUploader.ts
264
+ import { FirebaseStorageUploader, UploadManager } from '@signskart/uploader';
265
+
266
+ export const createFirebaseUploadManager = (concurrency = 3) => {
267
+ return new UploadManager(
268
+ new FirebaseStorageUploader({
269
+ firebaseConfig: {
270
+ apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
271
+ authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
272
+ projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
273
+ storageBucket: process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET,
274
+ messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID,
275
+ appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID,
276
+ measurementId: process.env.NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID,
277
+ },
278
+ appName: 'signskart-nextjs-uploader',
279
+ defaultFolder: 'uploads/nextjs',
280
+ }),
281
+ concurrency
282
+ );
283
+ };
284
+ ```
285
+
286
+ Use in a client component:
287
+
288
+ ```tsx
289
+ // app/upload/page.tsx
290
+ 'use client';
291
+
292
+ import { useMemo, useState } from 'react';
293
+ import { createFirebaseUploadManager } from '@/lib/firebaseUploader';
294
+
295
+ export default function UploadPage() {
296
+ const [progress, setProgress] = useState(0);
297
+ const [uploadedUrl, setUploadedUrl] = useState('');
298
+ const [status, setStatus] = useState('idle');
299
+
300
+ const manager = useMemo(() => createFirebaseUploadManager(3), []);
301
+
302
+ const onSelectFile = async (event: React.ChangeEvent<HTMLInputElement>) => {
303
+ const file = event.target.files?.[0];
304
+ if (!file) return;
305
+
306
+ const task = manager.add({
307
+ file,
308
+ metadata: { source: 'nextjs-client' },
309
+ });
310
+
311
+ task.events.subscribe((state) => {
312
+ setProgress(state.progress);
313
+ setStatus(state.status);
314
+
315
+ if (state.status === 'success') {
316
+ setUploadedUrl(state.response?.url || '');
317
+ }
318
+ });
319
+
320
+ // manager.add() starts automatically. Use task.cancel() to stop it.
321
+ };
322
+
323
+ return (
324
+ <main style={{ padding: 24 }}>
325
+ <h1>Firebase Upload</h1>
326
+ <input type="file" onChange={onSelectFile} />
327
+ <p>Status: {status}</p>
328
+ <p>Progress: {progress}%</p>
329
+ {uploadedUrl ? (
330
+ <p>
331
+ File URL: <a href={uploadedUrl} target="_blank" rel="noreferrer">{uploadedUrl}</a>
332
+ </p>
333
+ ) : null}
334
+ </main>
335
+ );
336
+ }
337
+ ```
338
+
339
+ ## 2️⃣ Node.js Example (server upload flow)
340
+
341
+ Use `createFirebaseAdminUploadHandler` with Firebase Admin SDK on the server.
342
+
343
+ Install:
344
+
345
+ ```bash
346
+ npm install @signskart/uploader firebase-admin
347
+ ```
348
+
349
+ Next.js route handler example:
350
+
351
+ ```ts
352
+ // app/api/firebase/upload/route.ts
353
+ import { NextRequest, NextResponse } from 'next/server';
354
+ import admin from 'firebase-admin';
355
+ import { createFirebaseAdminUploadHandler } from '@signskart/uploader/server';
356
+
357
+ export const runtime = 'nodejs';
358
+
359
+ if (!admin.apps.length) {
360
+ admin.initializeApp({
361
+ credential: admin.credential.cert({
362
+ projectId: process.env.FIREBASE_PROJECT_ID,
363
+ clientEmail: process.env.FIREBASE_CLIENT_EMAIL,
364
+ privateKey: process.env.FIREBASE_PRIVATE_KEY?.replace(/\\n/g, '\n'),
365
+ }),
366
+ storageBucket: process.env.FIREBASE_STORAGE_BUCKET,
367
+ });
368
+ }
369
+
370
+ const uploadToFirebase = createFirebaseAdminUploadHandler({
371
+ bucket: admin.storage().bucket(),
372
+ defaultFolder: 'uploads/server',
373
+ makePublic: true,
374
+ });
375
+
376
+ export async function POST(req: NextRequest) {
377
+ try {
378
+ const formData = await req.formData();
379
+ const file = formData.get('file');
380
+
381
+ if (!(file instanceof File)) {
382
+ return NextResponse.json({ error: 'File is required' }, { status: 400 });
383
+ }
384
+
385
+ const result = await uploadToFirebase({
386
+ file,
387
+ fileName: file.name,
388
+ contentType: file.type,
389
+ folder: String(formData.get('folder') || 'uploads/server'),
390
+ metadata: { source: 'next-api' },
391
+ });
392
+
393
+ return NextResponse.json(result);
394
+ } catch (error) {
395
+ console.error(error);
396
+ return NextResponse.json({ error: 'Upload failed' }, { status: 500 });
397
+ }
398
+ }
399
+ ```
400
+
401
+ Express example:
402
+
403
+ ```ts
404
+ import express from 'express';
405
+ import multer from 'multer';
406
+ import admin from 'firebase-admin';
407
+ import { createFirebaseAdminUploadHandler } from '@signskart/uploader/server';
408
+
409
+ const app = express();
410
+ const upload = multer({ storage: multer.memoryStorage() });
411
+
412
+ admin.initializeApp({
413
+ credential: admin.credential.cert({
414
+ projectId: process.env.FIREBASE_PROJECT_ID,
415
+ clientEmail: process.env.FIREBASE_CLIENT_EMAIL,
416
+ privateKey: process.env.FIREBASE_PRIVATE_KEY?.replace(/\\n/g, '\n'),
417
+ }),
418
+ storageBucket: process.env.FIREBASE_STORAGE_BUCKET,
419
+ });
420
+
421
+ const uploadToFirebase = createFirebaseAdminUploadHandler({
422
+ bucket: admin.storage().bucket(),
423
+ defaultFolder: 'uploads/node',
424
+ makePublic: true,
425
+ });
426
+
427
+ app.post('/api/firebase/upload', upload.single('file'), async (req, res) => {
428
+ if (!req.file) return res.status(400).json({ error: 'File is required' });
429
+
430
+ const result = await uploadToFirebase({
431
+ file: req.file.buffer,
432
+ fileName: req.file.originalname,
433
+ contentType: req.file.mimetype,
434
+ folder: req.body.folder,
435
+ metadata: { source: 'express-api' },
436
+ });
437
+
438
+ res.json(result);
439
+ });
440
+
441
+ app.listen(3000, () => {
442
+ console.log('Server running on http://localhost:3000');
443
+ });
444
+ ```
445
+
446
+ Node.js env example:
447
+
448
+ ```bash
449
+ FIREBASE_PROJECT_ID=...
450
+ FIREBASE_CLIENT_EMAIL=...
451
+ FIREBASE_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n"
452
+ FIREBASE_STORAGE_BUCKET=your-project-id.appspot.com
453
+ ```
454
+
455
+ ---
456
+
165
457
  # 🧠 API Reference
166
458
 
167
459
  ---
@@ -207,15 +499,218 @@ task.cancel()
207
499
 
208
500
  ```ts
209
501
  {
210
- file: File;
211
- folder: string;
502
+ file: File | Blob;
503
+ folder?: string;
212
504
  fileName?: string;
505
+ contentType?: string;
213
506
  metadata?: Record<string, any>;
214
507
  }
215
508
  ```
216
509
 
217
510
  ---
218
511
 
512
+ ## Upload Response
513
+
514
+ Every provider returns the same top-level shape so app code can read common fields without checking the provider first.
515
+
516
+ ```ts
517
+ {
518
+ url: string;
519
+ provider: 's3' | 'cloudinary' | 'firebase' | string;
520
+ key?: string;
521
+ fileName?: string;
522
+ originalFileName?: string;
523
+ folder?: string;
524
+ contentType?: string;
525
+ size?: number;
526
+ providerData?: Record<string, unknown>;
527
+ }
528
+ ```
529
+
530
+ ### Common response fields
531
+
532
+ | Field | Description |
533
+ | ----- | ----------- |
534
+ | `url` | Final URL returned by the provider. Use this in your app/database. |
535
+ | `provider` | Provider that handled the upload: `s3`, `cloudinary`, or `firebase`. |
536
+ | `key` | Provider object key/path/public id when available. |
537
+ | `fileName` | File name used for upload. |
538
+ | `originalFileName` | Original browser/server file name when available. |
539
+ | `folder` | Folder passed in upload options or configured default folder. |
540
+ | `contentType` | MIME type used for the uploaded object. |
541
+ | `size` | Uploaded file size in bytes when available. |
542
+ | `providerData` | Provider-specific details and raw response data. |
543
+
544
+ ### S3 response example
545
+
546
+ ```ts
547
+ {
548
+ url: 'https://cdn.example.com/uploads/logo.png',
549
+ provider: 's3',
550
+ key: 'uploads/1710000000000-logo.png',
551
+ fileName: 'logo.png',
552
+ originalFileName: 'logo.png',
553
+ folder: 'uploads',
554
+ contentType: 'image/png',
555
+ size: 48231,
556
+ providerData: {
557
+ bucket: 'my-bucket',
558
+ region: 'us-east-1',
559
+ signedUrl: 'https://my-bucket.s3.amazonaws.com/...',
560
+ publicUrl: 'https://cdn.example.com/uploads/logo.png',
561
+ presign: {
562
+ signedUrl: 'https://my-bucket.s3.amazonaws.com/...',
563
+ key: 'uploads/1710000000000-logo.png',
564
+ publicUrl: 'https://cdn.example.com/uploads/logo.png'
565
+ },
566
+ upload: {
567
+ status: 200,
568
+ statusText: 'OK',
569
+ etag: '"abc123"',
570
+ headers: {
571
+ etag: '"abc123"'
572
+ }
573
+ }
574
+ }
575
+ }
576
+ ```
577
+
578
+ ### Cloudinary response example
579
+
580
+ Cloudinary returns the richest media metadata for images and videos, including dimensions, format, resource type, bytes, ids, and the raw Cloudinary response.
581
+
582
+ ```ts
583
+ {
584
+ url: 'https://res.cloudinary.com/demo/image/upload/v1710000000/designs/logo.png',
585
+ provider: 'cloudinary',
586
+ key: 'designs/logo',
587
+ fileName: 'logo',
588
+ originalFileName: 'logo.png',
589
+ folder: 'designs',
590
+ contentType: 'image/png',
591
+ size: 48231,
592
+ providerData: {
593
+ assetId: 'a1b2c3',
594
+ publicId: 'designs/logo',
595
+ version: 1710000000,
596
+ versionId: 'version-id',
597
+ signature: 'signature',
598
+ width: 1200,
599
+ height: 800,
600
+ format: 'png',
601
+ resourceType: 'image',
602
+ type: 'upload',
603
+ createdAt: '2026-05-24T12:00:00Z',
604
+ bytes: 48231,
605
+ etag: 'abc123',
606
+ url: 'http://res.cloudinary.com/demo/image/upload/...',
607
+ secureUrl: 'https://res.cloudinary.com/demo/image/upload/...',
608
+ originalFilename: 'logo',
609
+ response: {
610
+ // Full Cloudinary upload response
611
+ }
612
+ }
613
+ }
614
+ ```
615
+
616
+ ### Firebase client response example
617
+
618
+ `FirebaseStorageUploader` uses the browser Firebase SDK and returns Storage metadata from the upload snapshot.
619
+
620
+ ```ts
621
+ {
622
+ url: 'https://firebasestorage.googleapis.com/v0/b/app.appspot.com/o/uploads%2Flogo.png?...',
623
+ provider: 'firebase',
624
+ key: 'uploads/logo.png',
625
+ fileName: 'logo.png',
626
+ originalFileName: 'logo.png',
627
+ folder: 'uploads',
628
+ contentType: 'image/png',
629
+ size: 48231,
630
+ providerData: {
631
+ bucket: 'app.appspot.com',
632
+ fullPath: 'uploads/logo.png',
633
+ name: 'logo.png',
634
+ contentType: 'image/png',
635
+ size: 48231,
636
+ generation: '1710000000000000',
637
+ metageneration: '1',
638
+ timeCreated: '2026-05-24T12:00:00.000Z',
639
+ updated: '2026-05-24T12:00:00.000Z',
640
+ md5Hash: '...',
641
+ cacheControl: undefined,
642
+ contentDisposition: undefined,
643
+ contentEncoding: undefined,
644
+ contentLanguage: undefined,
645
+ customMetadata: {
646
+ source: 'nextjs-client'
647
+ },
648
+ downloadUrl: 'https://firebasestorage.googleapis.com/...',
649
+ metadata: {
650
+ // Full Firebase Storage metadata object
651
+ }
652
+ }
653
+ }
654
+ ```
655
+
656
+ ### Firebase Admin response example
657
+
658
+ `createFirebaseAdminUploadHandler` returns the same shared shape on Node.js, plus Admin Storage metadata when `getMetadata()` is available.
659
+
660
+ ```ts
661
+ {
662
+ url: 'https://storage.googleapis.com/app.appspot.com/uploads/server/logo.png',
663
+ provider: 'firebase',
664
+ key: 'uploads/server/1710000000000-logo.png',
665
+ fileName: 'logo.png',
666
+ originalFileName: 'logo.png',
667
+ folder: 'uploads/server',
668
+ contentType: 'image/png',
669
+ size: 48231,
670
+ providerData: {
671
+ bucket: 'app.appspot.com',
672
+ fullPath: 'uploads/server/1710000000000-logo.png',
673
+ name: '1710000000000-logo.png',
674
+ contentType: 'image/png',
675
+ size: 48231,
676
+ md5Hash: '...',
677
+ crc32c: '...',
678
+ generation: '1710000000000000',
679
+ metageneration: '1',
680
+ timeCreated: '2026-05-24T12:00:00.000Z',
681
+ updated: '2026-05-24T12:00:00.000Z',
682
+ customMetadata: {
683
+ source: 'next-api'
684
+ },
685
+ downloadUrl: 'https://storage.googleapis.com/app.appspot.com/uploads/server/logo.png',
686
+ saveResponse: undefined,
687
+ makePublicResponse: undefined,
688
+ metadata: {
689
+ // Full Firebase Admin Storage metadata object
690
+ }
691
+ }
692
+ }
693
+ ```
694
+
695
+ ### Reading response data in your app
696
+
697
+ ```ts
698
+ task.events.subscribe((state) => {
699
+ if (state.status !== 'success') return;
700
+
701
+ const uploaded = state.response;
702
+
703
+ console.log(uploaded?.url);
704
+ console.log(uploaded?.provider);
705
+ console.log(uploaded?.key);
706
+ console.log(uploaded?.contentType);
707
+ console.log(uploaded?.size);
708
+ console.log(uploaded?.providerData);
709
+ });
710
+ ```
711
+
712
+ ---
713
+
219
714
  # 🔁 Retry Logic
220
715
 
221
716
  * Automatic retry
@@ -314,4 +809,4 @@ import { createS3PresignHandler } from '@signskart/uploader/server';
314
809
 
315
810
  # 📜 License
316
811
 
317
- MIT © Signskart
812
+ MIT © Signskart
@@ -0,0 +1,20 @@
1
+ interface PresignConfig {
2
+ region: string;
3
+ accessKeyId: string;
4
+ secretAccessKey: string;
5
+ bucket: string;
6
+ publicUrl: string;
7
+ expiresIn?: number;
8
+ }
9
+ interface PresignRequest {
10
+ fileName: string;
11
+ contentType: string;
12
+ folder?: string;
13
+ }
14
+ declare function createS3PresignHandler(config: PresignConfig): ({ fileName, contentType, folder, }: PresignRequest) => Promise<{
15
+ signedUrl: string;
16
+ key: string;
17
+ publicUrl: string;
18
+ }>;
19
+
20
+ export { type PresignConfig as P, type PresignRequest as a, createS3PresignHandler as c };
@@ -0,0 +1,20 @@
1
+ interface PresignConfig {
2
+ region: string;
3
+ accessKeyId: string;
4
+ secretAccessKey: string;
5
+ bucket: string;
6
+ publicUrl: string;
7
+ expiresIn?: number;
8
+ }
9
+ interface PresignRequest {
10
+ fileName: string;
11
+ contentType: string;
12
+ folder?: string;
13
+ }
14
+ declare function createS3PresignHandler(config: PresignConfig): ({ fileName, contentType, folder, }: PresignRequest) => Promise<{
15
+ signedUrl: string;
16
+ key: string;
17
+ publicUrl: string;
18
+ }>;
19
+
20
+ export { type PresignConfig as P, type PresignRequest as a, createS3PresignHandler as c };