ERP-node/frontend/lib/api/openApi.ts

112 lines
2.2 KiB
TypeScript
Raw Normal View History

/**
* OpenAPI
* - API(, )
*/
import { apiClient } from './client';
// ============================================================
// 타입 정의
// ============================================================
/**
*
*/
export interface WeatherData {
city: string;
country: string;
temperature: number;
feelsLike: number;
humidity: number;
pressure: number;
weatherMain: string;
weatherDescription: string;
weatherIcon: string;
windSpeed: number;
clouds: number;
timestamp: string;
}
/**
*
*/
export interface ExchangeRateData {
base: string;
target: string;
rate: number;
timestamp: string;
}
/**
* Geocoding
*/
export interface GeocodeData {
address: string;
lat: number;
lng: number;
}
/**
* API
*/
interface ApiResponse<T> {
success: boolean;
data: T;
message?: string;
}
// ============================================================
// API 함수
// ============================================================
/**
*
* @param city (기본값: Seoul)
* @param units (metric: 섭씨, imperial: 화씨)
* @param lang (kr: 한국어, en: 영어)
*/
export async function getWeather(
city: string = '서울',
units: string = 'metric',
lang: string = 'kr'
): Promise<WeatherData> {
const response = await apiClient.get<ApiResponse<WeatherData>>(
`/open-api/weather`,
{
params: { city, units, lang },
}
);
return response.data.data;
}
/**
*
* @param base (기본값: KRW)
* @param target (기본값: USD)
*/
export async function getExchangeRate(
base: string = 'KRW',
target: string = 'USD'
): Promise<ExchangeRateData> {
const response = await apiClient.get<ApiResponse<ExchangeRateData>>(
`/open-api/exchange-rate`,
{
params: { base, target },
}
);
return response.data.data;
}
/**
* (Geocoding)
* @param address
*/
export async function geocode(address: string): Promise<GeocodeData> {
const response = await apiClient.post<ApiResponse<GeocodeData>>(
`/open-api/geocode`,
{ address }
);
return response.data.data;
}