Skip to content

Instantly share code, notes, and snippets.

@cartorjo
Created April 15, 2024 14:14
Show Gist options
  • Save cartorjo/957360a8c914b9834f7b45fbb9a984ce to your computer and use it in GitHub Desktop.
Save cartorjo/957360a8c914b9834f7b45fbb9a984ce to your computer and use it in GitHub Desktop.
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));
@cartorjo
Copy link
Author

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.

import axios, { AxiosResponse } from 'axios';
const API_BASE_URL = process.env.API_BASE_URL || 'https://virtserver.swaggerhub.com/ADAS-A2B-Services/adas-a2b-business-objects/0.9.4';

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.

interface TiposData { /* ... */ }
interface ErxPreRspData { /* ... */ }
interface TiposResponse { /* ... */ }
interface ErxPreRspResponse { /* ... */ }

An instance of axios is created with the base URL and other common configurations. This instance is used for all subsequent API requests.

const axiosInstance = axios.create({ baseURL: API_BASE_URL });

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.

const handleError = (error: any) => { /* ... */ };

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.

async function postRequest<Data, Response>(url: string, data: Data): Promise<Response> { /* ... */ }

Two specific functions postTipos and postErxPreRsp are defined for posting data to specific endpoints. These functions use the generic postRequest function.

async function postTipos(data: TiposData): Promise<TiposResponse> { /* ... */ }
async function postErxPreRsp(data: ErxPreRspData): Promise<ErxPreRspResponse> { /* ... */ }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment