linkjust
Version:
36 lines (30 loc) • 929 B
text/typescript
import axios from 'axios';
interface ApiResponse {
short_url?: string;
}
class Linkjust {
private apiKey: string;
private apiUrl: string;
constructor(apiKey: string) {
if (!apiKey) {
throw new Error('API Key is required');
}
this.apiKey = apiKey;
this.apiUrl = 'https://linkjust.com/api/shorten';
}
async create(link: string): Promise<string> {
try {
const response = await axios.post<ApiResponse>(this.apiUrl, {
url: link,
api_key: this.apiKey
});
if (response.data && response.data.short_url) {
return response.data.short_url;
}
throw new Error('Failed to shorten URL');
} catch (error) {
throw new Error(`Error shortening URL: ${error}`);
}
}
}
export default Linkjust;