Skip to main content

PUT Request

Add phantomPut with an ID, headers, token, and refetching the latest data after a successful patch

Example

import { phantomPut } from "phantom-request"; // Import the hook

function App() {
const { put, latestData, loading, error, response } = phantomPut({
baseURL: "http://localhost:3000/", // Base URL for your API
route: "user/update", // API route for the PUT request
id: "12345", // ID of the resource to update (optional)
getLatestData: "user", // Refetch this route after the PUT request
token: "your-auth-token", // Authorization token
onUnauthorized: () => {
alert("Session expired. Please log in again.");
}, // Handle 401 errors
headers: {
"X-Custom-Header": "MyValue",
}, // Custom headers
contentType: "application/json", // Content-Type of the request body
});

const handleUpdate = () => {
put({
username: "newUsername", // Updated data
email: "newEmail@example.com",
});
};

return (
<div>
<button onClick={handleUpdate}>Update User</button>
{loading && <p>Loading...</p>} {/* Show a loading indicator */}
{error && <p>Error: {error.message}</p>} {/* Display errors */}
{latestData && <pre>{JSON.stringify(latestData, null, 2)}</pre>} {/* Display the latest fetched data */}
{response && <pre>{JSON.stringify(response, null, 2)}</pre>} {/* Display the API response */}
</div>
);
}

export default App;

Example with File Upload (e.g., Profile Picture)

const App = () => {
const { put, latestData, loading, error } = phantomPut({
baseURL: "http://localhost:3000/",
route: "profile/update",
id: "12345",
cloudinaryUpload: {
cloud_base_url: "https://api.cloudinary.com/v1_1/your-cloud-name",

upload_preset: "preset-name",
},
getLatestData: "profile",
});

const handleUpdate = () => {
put({
avatar: { value: "path/to/profile.jpg", CloudinaryImage: true },
});
};

return (
<div>
<button onClick={handleUpdate}>Update Profile</button>
{loading && <p>Loading...</p>}
{error && <p>Error: {error.message}</p>}
{latestData && <pre>{JSON.stringify(latestData, null, 2)}</pre>}
</div>
);
};

Features and Parameters

ParameterTypeDescription
baseURLstringBase URL for your API, e.g., http://localhost:3000/.
routestringAPI endpoint route for the PUT request, e.g., user/update.
idstring (optional)Resource ID to append to the route, e.g., user/update/:id.
tokenstring (optional)Authorization token sent in Authorization: Bearer <token>.
onUnauthorized() => void (optional)Callback for 401 Unauthorized responses, e.g., redirecting to login.
headersRecord<string, string> (optional)Additional headers for the request.
contentType"application/json" | "multipart/form-data" | "application/x-www-form-urlencoded"Content-Type of the request. Default is application/json.
getLatestDatastring (optional)Refetch the latest data from this route after a successful PUT.

Return Parameters

ParameterTypeDescription
responseR | nullResponse from the PUT request, e.g., updated resource data.
erroranyError object if the PUT request fails.
loadingbooleanIndicates whether the PUT request is in progress.
put(data: any) => voidFunction to trigger the PUT request with the provided data.
latestDataR | nullRefreshed data fetched after a successful PUT request (if getLatestData).