txb-cloudinary-image-uploader 1.0.0

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.
@@ -0,0 +1,17 @@
1
+ /**
2
+ * CloudinaryResponse interface defines the structure of the successful upload response.
3
+ */
4
+ export interface CloudinaryResponse {
5
+ url: string;
6
+ public_id: string;
7
+ }
8
+ /**
9
+ * uploadToCloudinary uploads a file to Cloudinary using the Fetch API.
10
+ * * @param file - The File object to be uploaded.
11
+ * @param uploadPreset - Your Cloudinary Unsigned Upload Preset.
12
+ * @param cloudinaryUrl - Your Cloudinary API URL (e.g., https://api.cloudinary.com/v1_1/your_cloud_name).
13
+ * @returns A promise that resolves to a CloudinaryResponse object.
14
+ * @throws Error if the upload fails.
15
+ */
16
+ declare const uploadToCloudinary: (file: File, uploadPreset: string, cloudinaryUrl: string) => Promise<CloudinaryResponse>;
17
+ export default uploadToCloudinary;
package/dist/index.js ADDED
@@ -0,0 +1,38 @@
1
+ /**
2
+ * uploadToCloudinary uploads a file to Cloudinary using the Fetch API.
3
+ * * @param file - The File object to be uploaded.
4
+ * @param uploadPreset - Your Cloudinary Unsigned Upload Preset.
5
+ * @param cloudinaryUrl - Your Cloudinary API URL (e.g., https://api.cloudinary.com/v1_1/your_cloud_name).
6
+ * @returns A promise that resolves to a CloudinaryResponse object.
7
+ * @throws Error if the upload fails.
8
+ */
9
+ const uploadToCloudinary = async (file, uploadPreset, cloudinaryUrl) => {
10
+ if (!file || !uploadPreset || !cloudinaryUrl) {
11
+ throw new Error("Missing required parameters: file, uploadPreset, or cloudinaryUrl");
12
+ }
13
+ const formData = new FormData();
14
+ formData.append("file", file);
15
+ formData.append("upload_preset", uploadPreset);
16
+ try {
17
+ const response = await fetch(`${cloudinaryUrl}/image/upload`, {
18
+ method: "POST",
19
+ body: formData,
20
+ });
21
+ if (!response.ok) {
22
+ const errorData = await response.json();
23
+ console.error("Cloudinary Error Detail:", errorData);
24
+ throw new Error(errorData.error?.message || "Image upload failed");
25
+ }
26
+ const data = await response.json();
27
+ // Returning secure_url as 'url' and public_id as requested
28
+ return {
29
+ url: data.secure_url,
30
+ public_id: data.public_id,
31
+ };
32
+ }
33
+ catch (error) {
34
+ console.error("Cloudinary Upload Process Error:", error);
35
+ throw error;
36
+ }
37
+ };
38
+ export default uploadToCloudinary;
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "txb-cloudinary-image-uploader",
3
+ "version": "1.0.0",
4
+ "description": "A lightweight, promise-based utility to handle seamless image uploads to Cloudinary directly from the browser.",
5
+ "license": "ISC",
6
+ "author": "Saleh Ahmed Mahin",
7
+ "type": "module",
8
+ "main": "./dist/index.js",
9
+ "module": "./dist/index.js",
10
+ "types": "./dist/index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "import": "./dist/index.js",
14
+ "types": "./dist/index.d.ts"
15
+ }
16
+ },
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "keywords": [
21
+ "cloudinary",
22
+ "upload",
23
+ "image-upload",
24
+ "react",
25
+ "javascript",
26
+ "techxbureau"
27
+ ],
28
+ "scripts": {
29
+ "build": "tsc",
30
+ "prepare": "npm run build",
31
+ "prepublishOnly": "npm run build"
32
+ },
33
+ "devDependencies": {
34
+ "typescript": "^5.x.x"
35
+ }
36
+ }
package/readme.md ADDED
@@ -0,0 +1,63 @@
1
+ # Cloudinary Image Upload Utility
2
+
3
+ A lightweight TypeScript utility function to handle image uploads directly from the client-side (browser) to Cloudinary using **Unsigned Uploads**. This utility uses the native `fetch` API, eliminating the need for heavy external libraries like Axios.
4
+
5
+ ## Table of Contents
6
+ * [Features](#features)
7
+ * [Prerequisites](#prerequisites)
8
+ * [Installation](#installation)
9
+ * [Usage](#usage)
10
+ * [Response Structure](#response-structure)
11
+
12
+ ## Features
13
+ - **Simple Integration**: Upload images with a single function call.
14
+ - **TypeScript Support**: Fully typed interface for predictable data structures and better DX.
15
+ - **Detailed Error Handling**: Log detailed error data to the console if the upload fails.
16
+ - **Lightweight**: Built with the native Fetch API—no extra dependencies required.
17
+
18
+ ## Prerequisites
19
+ Before using this function, gather the following details from your Cloudinary Dashboard:
20
+ 1. **Cloud Name**: Your unique Cloudinary identifier.
21
+ 2. **Upload Preset**: Create an **'Unsigned'** upload preset in Cloudinary Settings (Settings > Upload).
22
+ 3. **Cloudinary URL**: Typically formatted as: `https://api.cloudinary.com/v1_1/YOUR_CLOUD_NAME`
23
+
24
+ ## Installation
25
+ Create a file named `cloudinary.ts` inside your project's `utils` or `lib` folder and paste the function code there.
26
+
27
+ ```bash
28
+ # No additional packages are required.
29
+
30
+ ### React Example
31
+ ```
32
+ import { uploadToCloudinary } from './utils/cloudinary';
33
+
34
+ const ImageUploadComponent = () => {
35
+ const handleFileChange = async (event: React.ChangeEvent<HTMLInputElement>) => {
36
+ const file = event.target.files?.[0];
37
+ if (!file) return;
38
+
39
+ // Use environment variables for production security
40
+ const CLOUDINARY_URL = "[https://api.cloudinary.com/v1_1/your_cloud_name](https://api.cloudinary.com/v1_1/your_cloud_name)";
41
+ const UPLOAD_PRESET = "your_unsigned_preset";
42
+
43
+ try {
44
+ const response = await uploadToCloudinary(file, UPLOAD_PRESET, CLOUDINARY_URL);
45
+
46
+ console.log("Uploaded URL:", response.url);
47
+ console.log("Public ID:", response.public_id);
48
+
49
+ alert("Upload Successful!");
50
+ } catch (error) {
51
+ console.error("Upload failed:", error);
52
+ alert("Failed to upload image. Please check the console for details.");
53
+ }
54
+ };
55
+
56
+ return (
57
+ <div>
58
+ <label>Choose an image:</label>
59
+ <input type="file" onChange={handleFileChange} />
60
+ </div>
61
+ );
62
+ };
63
+ ```