txb-cloudinary-image-uploader 1.0.1 → 1.0.3
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.ts +1 -1
- package/dist/index.js +4 -4
- package/package.json +1 -1
- package/readme.md +0 -47
package/dist/index.d.ts
CHANGED
|
@@ -13,5 +13,5 @@ export interface CloudinaryResponse {
|
|
|
13
13
|
* @returns A promise that resolves to a CloudinaryResponse object.
|
|
14
14
|
* @throws Error if the upload fails.
|
|
15
15
|
*/
|
|
16
|
-
declare const uploadToCloudinary: (file: File, uploadPreset: string,
|
|
16
|
+
declare const uploadToCloudinary: (file: File, uploadPreset: string, cloudname: string) => Promise<CloudinaryResponse>;
|
|
17
17
|
export default uploadToCloudinary;
|
package/dist/index.js
CHANGED
|
@@ -6,15 +6,15 @@
|
|
|
6
6
|
* @returns A promise that resolves to a CloudinaryResponse object.
|
|
7
7
|
* @throws Error if the upload fails.
|
|
8
8
|
*/
|
|
9
|
-
const uploadToCloudinary = async (file, uploadPreset,
|
|
10
|
-
if (!file || !uploadPreset || !
|
|
11
|
-
throw new Error("Missing required parameters: file, uploadPreset, or
|
|
9
|
+
const uploadToCloudinary = async (file, uploadPreset, cloudname) => {
|
|
10
|
+
if (!file || !uploadPreset || !cloudname) {
|
|
11
|
+
throw new Error("Missing required parameters: file, uploadPreset, or cloudname");
|
|
12
12
|
}
|
|
13
13
|
const formData = new FormData();
|
|
14
14
|
formData.append("file", file);
|
|
15
15
|
formData.append("upload_preset", uploadPreset);
|
|
16
16
|
try {
|
|
17
|
-
const response = await fetch(
|
|
17
|
+
const response = await fetch(`https://api.cloudinary.com/v1_1/${cloudname}/image/upload`, {
|
|
18
18
|
method: "POST",
|
|
19
19
|
body: formData,
|
|
20
20
|
});
|
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.3",
|
|
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
|
@@ -20,50 +20,3 @@ Before using this function, gather the following details from your [Cloudinary D
|
|
|
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
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
|
-
};
|