helixzypher-api-client
Version:
Orval generated API client for heliZypher
52 lines (51 loc) • 2.51 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.customInstance = exports.AXIOS_INSTANCE = void 0;
// src/api/mutator/custom-instance.ts
const axios_1 = require("axios"); // Added 'type' keyword for AxiosRequestConfig
// Create a default Axios instance.
// You might want to get this from environment variables or a configuration file.
exports.AXIOS_INSTANCE = axios_1.default.create({
// Your Spring Boot backend URL
baseURL: 'http://localhost:9000', // Replace with your actual backend URL
});
/**
* Custom Axios instance function for Orval.
* Orval will pass the request configuration and optionally an options object.
* This function should return a Promise that resolves with the response data.
* @template T - The expected type of the response data.
* @param {AxiosRequestConfig} config - The Axios request configuration generated by Orval.
* @param {AxiosRequestConfig} [options] - Additional Axios request options.
* @returns {Promise<T>} A promise that resolves with the response data.
*/
const customInstance = async (config, options) => {
try {
// Example: Adding an authorization header (e.g., from localStorage or a global state)
const token = localStorage.getItem('authToken'); // Replace with your actual token retrieval method
if (token) {
config.headers = {
...config.headers,
Authorization: `Bearer ${token}`,
};
}
// Make the actual API call using the configured Axios instance
const promise = (0, exports.AXIOS_INSTANCE)({ ...config, ...options }).then(({ data }) => data);
// If you're using React Query's cancellation features, you might want to expose a cancel function
// For basic usage, this isn't strictly necessary.
// (promise as any).cancel = () => {
// // Implement cancellation logic if needed
// };
return promise;
}
catch (error) {
// Global error handling: You can log errors, show notifications, or redirect users.
console.error('API call failed:', error);
// Re-throw the error so React Query can catch it
throw error;
}
};
exports.customInstance = customInstance;
// If you want to use the default Axios instance instead of a custom one,
// you can simply export `Axios` or `Axios.create()` directly.
// For example:
// export { default as customInstance } from 'axios'; // This would use the default axios instance