-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
71 lines (64 loc) · 1.98 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import {generateUniqueId} from './helpers/util';
import Database from './helpers/db';
import mongoose,{Connection} from 'mongoose'
export default class MaskUrl {
dbString: string;
urlPrefix: string;
connection!: Connection | Error;
database!: Database;
UrlModel!: typeof mongoose.Model ;
constructor(dbString: string, urlPrefix: string) {
this.dbString = dbString;
this.urlPrefix = urlPrefix;
}
async connectToDatabase(): Promise<void> {
try {
this.database = new Database(this.dbString);
this.connection = await this.database.connect();
const connection = this.connection as mongoose.Connection;
this.UrlModel = connection.model('Url', new mongoose.Schema({
id: String,
prefix: String,
url: String,
}));
} catch (error) {
throw error;
}
}
maskUrl(url: string): string {
if (this.connection) {
const uniqueId = generateUniqueId();
const redirectionUrl = `${this.urlPrefix.trim()}/${uniqueId}`;
const newUrl = new this.UrlModel({
id: uniqueId,
prefix: this.urlPrefix,
url: url,
});
newUrl.save();
return redirectionUrl;
} else {
throw new Error('Database connection is not established');
}
}
async fetchUrl(_url:string){
if (this.connection) {
const id = _url.split("/").pop();
try {
const urlDocument = await this.UrlModel.findOne({ id });
if (urlDocument) {
// Return the URL if found
return urlDocument.url;
} else {
// Return null if no URL found with the provided ID
return null;
}
} catch (error) {
// Handle any errors that occur during the database operation
throw new Error('Error fetching URL from database.');
}
}else{
throw new Error('Database connection is not established');
}
}
}
module.exports = MaskUrl;