Created
April 15, 2024 14:14
-
-
Save cartorjo/957360a8c914b9834f7b45fbb9a984ce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios, { AxiosResponse } from 'axios'; | |
// Define the base URL for the API | |
const API_BASE_URL = process.env.API_BASE_URL || 'https://virtserver.swaggerhub.com/ADAS-A2B-Services/adas-a2b-business-objects/0.9.4'; | |
// Define the data types | |
interface TiposData { | |
// Define the properties of the data here based on the API requirements | |
} | |
interface ErxPreRspData { | |
// Define the properties of the data here based on the API requirements | |
} | |
// Define the response types | |
interface TiposResponse { | |
// Define the properties of the response here based on the API requirements | |
} | |
interface ErxPreRspResponse { | |
// Define the properties of the response here based on the API requirements | |
} | |
// Reusable axios instance with default configurations | |
const axiosInstance = axios.create({ | |
baseURL: API_BASE_URL, | |
// Other common configurations can be added here | |
}); | |
// Custom error handler | |
const handleError = (error: any) => { | |
// Handle different HTTP error statuses here | |
console.error('An error occurred:', error); | |
throw error; | |
}; | |
// Generic function for POST requests | |
async function postRequest<Data, Response>(url: string, data: Data): Promise<Response> { | |
try { | |
const response: AxiosResponse<Response> = await axiosInstance.post(url, data); | |
return response.data; | |
} catch (error) { | |
handleError(error); | |
} | |
} | |
/** | |
* Posts tipos data to the API endpoint. | |
* @param data The data to be posted. | |
* @returns A promise that resolves with the response data. | |
*/ | |
async function postTipos(data: TiposData): Promise<TiposResponse> { | |
return postRequest<TiposData, TiposResponse>('/nnf/pos', data); | |
} | |
/** | |
* Posts erxPreRsp data to the API endpoint. | |
* @param data The data to be posted. | |
* @returns A promise that resolves with the response data. | |
*/ | |
async function postErxPreRsp(data: ErxPreRspData): Promise<ErxPreRspResponse> { | |
return postRequest<ErxPreRspData, ErxPreRspResponse>('/erx/pre/rsp', data); | |
} | |
// Usage | |
const tiposData: TiposData = {}; // Replace with actual data | |
postTipos(tiposData) | |
.then(response => console.log('Tipos response:', response)) | |
.catch(error => console.error(error)); | |
const erxPreRspData: ErxPreRspData = {}; // Replace with actual data | |
postErxPreRsp(erxPreRspData) | |
.then(response => console.log('ErxPreRsp response:', response)) | |
.catch(error => console.error(error)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The provided TypeScript code is a simple API client for interacting with a remote API. It uses the
axios
library to make HTTP requests.The code begins by importing the necessary modules and defining the base URL for the API. The base URL is either taken from an environment variable
API_BASE_URL
or defaults to a hardcoded string.Next, it defines interfaces for the data types and response types. These interfaces are placeholders and should be filled in with the actual properties based on the API requirements.
An instance of
axios
is created with the base URL and other common configurations. This instance is used for all subsequent API requests.A custom error handler function
handleError
is defined. This function logs the error and rethrows it. The error handling could be expanded to handle different types of HTTP errors differently.A generic function
postRequest
is defined for making POST requests. This function takes a URL (relative to the base URL) and the data to be posted, and returns a promise that resolves with the response data.Two specific functions
postTipos
andpostErxPreRsp
are defined for posting data to specific endpoints. These functions use the genericpostRequest
function.