txb-cloudinary-image-uploader 1.0.0 → 1.0.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/package.json +1 -1
- package/readme.md +56 -50
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "txb-cloudinary-image-uploader",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "A lightweight, promise-based utility to handle seamless image uploads to Cloudinary directly from the browser.",
|
|
5
5
|
"license": "ISC",
|
|
6
6
|
"author": "Saleh Ahmed Mahin",
|
package/readme.md
CHANGED
|
@@ -1,63 +1,69 @@
|
|
|
1
1
|
# Cloudinary Image Upload Utility
|
|
2
2
|
|
|
3
|
-
A lightweight TypeScript utility
|
|
3
|
+
A lightweight, type-safe TypeScript utility for handling direct client-side image uploads to Cloudinary using **Unsigned Uploads**. This implementation leverages the native `fetch` API, keeping your bundle size small by avoiding external dependencies like Axios.
|
|
4
4
|
|
|
5
5
|
## Table of Contents
|
|
6
6
|
* [Features](#features)
|
|
7
7
|
* [Prerequisites](#prerequisites)
|
|
8
|
-
* [
|
|
9
|
-
* [Usage](#usage)
|
|
8
|
+
* [The Utility Function](#the-utility-function)
|
|
9
|
+
* [Usage Example (React)](#usage-example-react)
|
|
10
10
|
* [Response Structure](#response-structure)
|
|
11
11
|
|
|
12
12
|
## Features
|
|
13
|
-
- **
|
|
14
|
-
- **
|
|
15
|
-
- **
|
|
16
|
-
- **Lightweight**:
|
|
13
|
+
- **Zero Dependencies**: Uses the native Fetch API.
|
|
14
|
+
- **Type Safety**: Fully typed interface for predictable data structures and better DX.
|
|
15
|
+
- **Error Handling**: Captures and logs detailed Cloudinary API responses.
|
|
16
|
+
- **Lightweight**: Optimized for modern browser environments.
|
|
17
17
|
|
|
18
18
|
## Prerequisites
|
|
19
|
-
Before using this function, gather the following details from your Cloudinary Dashboard:
|
|
19
|
+
Before using this function, gather the following details from your [Cloudinary Dashboard](https://cloudinary.com/console):
|
|
20
20
|
1. **Cloud Name**: Your unique Cloudinary identifier.
|
|
21
21
|
2. **Upload Preset**: Create an **'Unsigned'** upload preset in Cloudinary Settings (Settings > Upload).
|
|
22
|
-
3. **
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
);
|
|
62
|
-
|
|
63
|
-
|
|
22
|
+
3. **API URL**: Typically formatted as: `https://api.cloudinary.com/v1_1/YOUR_CLOUD_NAME/image/upload`
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## The Utility Function
|
|
27
|
+
|
|
28
|
+
Create a file named `cloudinary.ts` inside your project's `utils` or `lib` folder.
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
export interface CloudinaryResponse {
|
|
32
|
+
public_id: string;
|
|
33
|
+
version: number;
|
|
34
|
+
width: number;
|
|
35
|
+
height: number;
|
|
36
|
+
format: string;
|
|
37
|
+
resource_type: string;
|
|
38
|
+
created_at: string;
|
|
39
|
+
bytes: number;
|
|
40
|
+
type: string;
|
|
41
|
+
url: string;
|
|
42
|
+
secure_url: string;
|
|
43
|
+
[key: string]: any; // Catch-all for additional metadata
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Uploads a file to Cloudinary using Unsigned Uploads.
|
|
48
|
+
*/
|
|
49
|
+
export const uploadToCloudinary = async (
|
|
50
|
+
file: File,
|
|
51
|
+
uploadPreset: string,
|
|
52
|
+
cloudinaryUrl: string
|
|
53
|
+
): Promise<CloudinaryResponse> => {
|
|
54
|
+
const formData = new FormData();
|
|
55
|
+
formData.append("file", file);
|
|
56
|
+
formData.append("upload_preset", uploadPreset);
|
|
57
|
+
|
|
58
|
+
const response = await fetch(cloudinaryUrl, {
|
|
59
|
+
method: "POST",
|
|
60
|
+
body: formData,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
if (!response.ok) {
|
|
64
|
+
const errorData = await response.json();
|
|
65
|
+
throw new Error(errorData.error?.message || "Failed to upload image");
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return response.json();
|
|
69
|
+
};
|