24 lines
611 B
TypeScript
24 lines
611 B
TypeScript
import { ShortenUrlRequest, ShortenUrlResponse } from "./types";
|
|
|
|
const baseUrl = process.env.REACT_APP_API_BASE_URL
|
|
|
|
export async function shortenUrlApi(
|
|
payload: ShortenUrlRequest
|
|
): Promise<ShortenUrlResponse> {
|
|
const response = await fetch(`${baseUrl}/api/shorten`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(payload),
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error('Failed to shorten URL');
|
|
}
|
|
|
|
const data = await response.json()
|
|
|
|
return {
|
|
url: data.url,
|
|
shortcode: data.shortcode
|
|
}
|
|
} |