str-react-api 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.
- package/app.js +50 -0
- package/intregare.js +78 -0
- package/package.json +11 -0
package/app.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// api.js
|
|
2
|
+
const BASE_URL = "https://yourapi.com"; // Aap apni API URL yahan dalenge
|
|
3
|
+
|
|
4
|
+
const headers = {
|
|
5
|
+
"Content-Type": "application/json",
|
|
6
|
+
// Agar token ya koi aur headers ki zarurat ho toh, wo bhi yahan add kar sakte hain
|
|
7
|
+
// "Authorization": `Bearer ${token}`,
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const handleResponse = async (response) => {
|
|
11
|
+
if (!response.ok) {
|
|
12
|
+
const error = await response.json();
|
|
13
|
+
throw new Error(error.message || "Something went wrong");
|
|
14
|
+
}
|
|
15
|
+
return await response.json();
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const get = async (endpoint) => {
|
|
19
|
+
const response = await fetch(`${BASE_URL}/${endpoint}`, {
|
|
20
|
+
method: "GET",
|
|
21
|
+
headers,
|
|
22
|
+
});
|
|
23
|
+
return handleResponse(response);
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export const post = async (endpoint, data) => {
|
|
27
|
+
const response = await fetch(`${BASE_URL}/${endpoint}`, {
|
|
28
|
+
method: "POST",
|
|
29
|
+
headers,
|
|
30
|
+
body: JSON.stringify(data),
|
|
31
|
+
});
|
|
32
|
+
return handleResponse(response);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export const put = async (endpoint, data) => {
|
|
36
|
+
const response = await fetch(`${BASE_URL}/${endpoint}`, {
|
|
37
|
+
method: "PUT",
|
|
38
|
+
headers,
|
|
39
|
+
body: JSON.stringify(data),
|
|
40
|
+
});
|
|
41
|
+
return handleResponse(response);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export const remove = async (endpoint) => {
|
|
45
|
+
const response = await fetch(`${BASE_URL}/${endpoint}`, {
|
|
46
|
+
method: "DELETE",
|
|
47
|
+
headers,
|
|
48
|
+
});
|
|
49
|
+
return handleResponse(response);
|
|
50
|
+
};
|
package/intregare.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import React, { useState, useEffect } from "react";
|
|
2
|
+
import { get, post, put, remove } from "./api";
|
|
3
|
+
|
|
4
|
+
const App = () => {
|
|
5
|
+
const [data, setData] = useState([]);
|
|
6
|
+
const [newItem, setNewItem] = useState("");
|
|
7
|
+
|
|
8
|
+
useEffect(() => {
|
|
9
|
+
// Data ko load karte hain (GET request)
|
|
10
|
+
fetchData();
|
|
11
|
+
}, []);
|
|
12
|
+
|
|
13
|
+
const fetchData = async () => {
|
|
14
|
+
try {
|
|
15
|
+
const result = await get("items"); // "items" ko apne endpoint se replace karein
|
|
16
|
+
setData(result);
|
|
17
|
+
} catch (error) {
|
|
18
|
+
console.error("Error fetching data:", error.message);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const handleAdd = async () => {
|
|
23
|
+
try {
|
|
24
|
+
await post("items", { name: newItem }); // POST request with new item
|
|
25
|
+
setNewItem(""); // Clear input
|
|
26
|
+
fetchData(); // Fetch updated data
|
|
27
|
+
} catch (error) {
|
|
28
|
+
console.error("Error adding item:", error.message);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const handleUpdate = async (id, updatedName) => {
|
|
33
|
+
try {
|
|
34
|
+
await put(`items/${id}`, { name: updatedName }); // PUT request to update item
|
|
35
|
+
fetchData();
|
|
36
|
+
} catch (error) {
|
|
37
|
+
console.error("Error updating item:", error.message);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const handleDelete = async (id) => {
|
|
42
|
+
try {
|
|
43
|
+
await remove(`items/${id}`); // DELETE request
|
|
44
|
+
fetchData();
|
|
45
|
+
} catch (error) {
|
|
46
|
+
console.error("Error deleting item:", error.message);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
return (
|
|
51
|
+
<div>
|
|
52
|
+
<h1>CRUD Operations in React</h1>
|
|
53
|
+
|
|
54
|
+
<div>
|
|
55
|
+
<input
|
|
56
|
+
type="text"
|
|
57
|
+
value={newItem}
|
|
58
|
+
onChange={(e) => setNewItem(e.target.value)}
|
|
59
|
+
/>
|
|
60
|
+
<button onClick={handleAdd}>Add Item</button>
|
|
61
|
+
</div>
|
|
62
|
+
|
|
63
|
+
<ul>
|
|
64
|
+
{data.map((item) => (
|
|
65
|
+
<li key={item.id}>
|
|
66
|
+
{item.name}
|
|
67
|
+
<button onClick={() => handleUpdate(item.id, "Updated Name")}>
|
|
68
|
+
Update
|
|
69
|
+
</button>
|
|
70
|
+
<button onClick={() => handleDelete(item.id)}>Delete</button>
|
|
71
|
+
</li>
|
|
72
|
+
))}
|
|
73
|
+
</ul>
|
|
74
|
+
</div>
|
|
75
|
+
);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
export default App;
|