@vicaniddouglas/js_aide 1.14.0 → 1.15.1
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 +52 -0
- package/declarations.d.ts +55 -0
- package/dist/js_aide.cjs.js +218 -218
- package/dist/js_aide.cjs.js.map +3 -3
- package/dist/js_aide.esm.js +218 -218
- package/dist/js_aide.esm.js.map +3 -3
- package/dist/js_aide.min.js +218 -218
- package/dist/js_aide.min.js.map +3 -3
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -281,6 +281,58 @@ sendRequest({
|
|
|
281
281
|
});
|
|
282
282
|
```
|
|
283
283
|
|
|
284
|
+
### Example: Using the FileUploader
|
|
285
|
+
|
|
286
|
+
The `FileUploader` module provides standardized utilities for validating and uploading files. It supports both traditional binary (`Multipart/Form-Data`) and JSON-based Base64 uploads.
|
|
287
|
+
|
|
288
|
+
#### Base64 File Conversions (For Previews)
|
|
289
|
+
The `toBase64` utility converts a raw `File` object into a Base64 string. It follows the "Always Resolve" pattern.
|
|
290
|
+
|
|
291
|
+
```javascript
|
|
292
|
+
import { toBase64 } from "@vicaniddouglas/js_aide";
|
|
293
|
+
|
|
294
|
+
const res = await toBase64(myFile);
|
|
295
|
+
|
|
296
|
+
if (res.status) {
|
|
297
|
+
// res.data is the full data URL (e.g. data:image/png;base64,...)
|
|
298
|
+
myImagePreview.src = res.data;
|
|
299
|
+
} else {
|
|
300
|
+
console.error("Conversion failed:", res.log);
|
|
301
|
+
}
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
#### Standard Binary Upload
|
|
305
|
+
By default, `uploadFile` sends files as a binary `Multipart/Form-Data` stream.
|
|
306
|
+
|
|
307
|
+
```javascript
|
|
308
|
+
import { uploadFile, popup } from "@vicaniddouglas/js_aide";
|
|
309
|
+
|
|
310
|
+
await uploadFile({
|
|
311
|
+
files: document.getElementById('myFileInput').files[0],
|
|
312
|
+
endpoint: '/api/upload-avatar',
|
|
313
|
+
fieldName: 'avatar',
|
|
314
|
+
onSuccess: () => popup.success("Avatar uploaded!")
|
|
315
|
+
});
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
#### Base64 JSON Upload
|
|
319
|
+
For APIs that require images to be embedded in a JSON payload, simply toggle the `asBase64` flag.
|
|
320
|
+
|
|
321
|
+
```javascript
|
|
322
|
+
import { uploadFile } from "@vicaniddouglas/js_aide";
|
|
323
|
+
|
|
324
|
+
await uploadFile({
|
|
325
|
+
files: selectedFiles,
|
|
326
|
+
endpoint: '/api/upload-as-json',
|
|
327
|
+
asBase64: true, // Automically converts to Base64 strings
|
|
328
|
+
stripPrefix: true, // Only send the raw Base64 data (no "data:image/..." prefix)
|
|
329
|
+
payload: {
|
|
330
|
+
userId: 123,
|
|
331
|
+
description: "Holiday photo"
|
|
332
|
+
}
|
|
333
|
+
});
|
|
334
|
+
```
|
|
335
|
+
|
|
284
336
|
### Example: Using the Popup System
|
|
285
337
|
|
|
286
338
|
The `popup` module provides a Promise-based modal system for showing professional alerts. It is fully accessible (supports the `Escape` key) and highly customizable.
|
package/declarations.d.ts
CHANGED
|
@@ -499,6 +499,61 @@ declare module "@vicaniddouglas/js_aide" {
|
|
|
499
499
|
escapeCsvValue(value: any, delimiter: string): string;
|
|
500
500
|
}
|
|
501
501
|
|
|
502
|
+
// =========================================================
|
|
503
|
+
// fileUploader (from fileUploader.js)
|
|
504
|
+
// =========================================================
|
|
505
|
+
interface FileUploaderValidationResult {
|
|
506
|
+
isValid: boolean;
|
|
507
|
+
error: string | null;
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
interface ValidateFileOptions {
|
|
511
|
+
allowedTypes?: string[];
|
|
512
|
+
maxSize?: number;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
interface toBase64Options {
|
|
516
|
+
stripPrefix?: boolean;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
interface Base64Result {
|
|
520
|
+
status: boolean;
|
|
521
|
+
data: string | null;
|
|
522
|
+
log: string | null;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
interface UploadOptions {
|
|
526
|
+
files: File | File[];
|
|
527
|
+
endpoint: string;
|
|
528
|
+
fieldName?: string;
|
|
529
|
+
payload?: object;
|
|
530
|
+
maxSize?: number;
|
|
531
|
+
allowedTypes?: string[];
|
|
532
|
+
onProgress?: (percent: number) => void;
|
|
533
|
+
showLoading?: (isLoading: boolean) => void | Promise<void>;
|
|
534
|
+
silent?: boolean;
|
|
535
|
+
asBase64?: boolean;
|
|
536
|
+
stripPrefix?: boolean;
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
export function validateFile(
|
|
540
|
+
file: File,
|
|
541
|
+
options?: ValidateFileOptions,
|
|
542
|
+
): FileUploaderValidationResult;
|
|
543
|
+
|
|
544
|
+
export function toBase64(
|
|
545
|
+
file: File,
|
|
546
|
+
options?: toBase64Options,
|
|
547
|
+
): Promise<Base64Result>;
|
|
548
|
+
|
|
549
|
+
export function uploadFile(options: UploadOptions): Promise<StandardizedResponse>;
|
|
550
|
+
|
|
551
|
+
export function uploadDataUrl(
|
|
552
|
+
dataUrl: string,
|
|
553
|
+
fileName: string,
|
|
554
|
+
options?: UploadOptions,
|
|
555
|
+
): Promise<StandardizedResponse>;
|
|
556
|
+
|
|
502
557
|
// =========================================================
|
|
503
558
|
// camera (from camera.js)
|
|
504
559
|
// =========================================================
|