diff --git a/src/url-shortener/response.interface.ts b/src/url-shortener/response.interface.ts new file mode 100644 index 0000000..73c52ad --- /dev/null +++ b/src/url-shortener/response.interface.ts @@ -0,0 +1,5 @@ +export interface ApiResponse { + data?: string; + error?: string; + status: number; +} \ No newline at end of file diff --git a/src/url-shortener/url-shortener.controller.ts b/src/url-shortener/url-shortener.controller.ts index 3505821..bb8506c 100644 --- a/src/url-shortener/url-shortener.controller.ts +++ b/src/url-shortener/url-shortener.controller.ts @@ -1,26 +1,35 @@ -import { Controller, Post, Body, Get, Param, Redirect, HttpException, HttpStatus } from '@nestjs/common'; +import { Controller, Post, Body, Get, Param, HttpException, HttpStatus, Res } from '@nestjs/common'; import { UrlShortenerService } from './url-shortener.service'; +import { ApiResponse } from './response.interface'; +import { Response } from 'express'; @Controller('url-shortener') export class UrlShortenerController { constructor(private readonly urlShortenerService: UrlShortenerService) {} @Post('shorten') - async shortenUrl(@Body('url') url: string): Promise<{ shortUrl: string }> { + async shortenUrl(@Body('url') url: string): Promise { if (!url) { throw new HttpException('URL is required', HttpStatus.BAD_REQUEST); } - const shortUrl = await this.urlShortenerService.generateShortUrl(url); - return { shortUrl }; + try { + const shortUrl = await this.urlShortenerService.generateShortUrl(url); + return { data: shortUrl, status: HttpStatus.CREATED }; + } catch (error) { + return { error: error.message, status: HttpStatus.BAD_REQUEST }; + } } @Get(':shortUrl') - @Redirect() - async resolveShortUrl(@Param('shortUrl') shortUrl: string) { - const originalUrl = this.urlShortenerService.getOriginalUrl(shortUrl); - if (!originalUrl) { - throw new HttpException('URL not found!', HttpStatus.NOT_FOUND); + async resolveShortUrl(@Param('shortUrl') shortUrl: string, @Res() res: Response) { + try { + const originalUrl = this.urlShortenerService.getOriginalUrl(shortUrl); + if (!originalUrl) { + return res.redirect('http://localhost:3000/404'); + } + return res.redirect(originalUrl); + } catch (error) { + throw new HttpException('An error occurred', HttpStatus.INTERNAL_SERVER_ERROR); } - return { url: originalUrl }; } } \ No newline at end of file diff --git a/src/url-shortener/url-shortener.service.ts b/src/url-shortener/url-shortener.service.ts index df47c19..c11100b 100644 --- a/src/url-shortener/url-shortener.service.ts +++ b/src/url-shortener/url-shortener.service.ts @@ -1,6 +1,7 @@ import { Injectable } from '@nestjs/common'; import * as shortid from 'shortid'; import { DnsService } from '../dns/dns.service'; +import { HttpException, HttpStatus } from '@nestjs/common'; @Injectable() @@ -11,19 +12,21 @@ export class UrlShortenerService { constructor(private readonly dnsService: DnsService) {} async generateShortUrl(originalUrl: string): Promise { - if(!originalUrl) { - throw new Error('URL is required'); + if (!originalUrl) { + throw new HttpException('URL is required', HttpStatus.BAD_REQUEST); } - let isValid = await this.dnsService.isValidUrl(originalUrl); - if(!isValid) { - throw new Error('URL is invalid'); + const isValid = await this.dnsService.isValidUrl(originalUrl); + if (!isValid) { + throw new HttpException('URL is invalid', HttpStatus.BAD_REQUEST); } - let shortUrl = "tiny." + shortid.generate() + ".com"; + const shortUrl = "tiny." + shortid.generate() + ".com"; this.urlMap.set(shortUrl, originalUrl); - return shortUrl + return shortUrl; } getOriginalUrl(shortUrl: string): string | undefined { - return this.urlMap.get(shortUrl); + let url: string | undefined = this.urlMap.get(shortUrl); + if (url) return url; + return undefined; } } \ No newline at end of file