-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented code reuse and smooth error handling
- Loading branch information
Showing
5 changed files
with
72 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,11 @@ | ||
import Log from "../constants/Log"; | ||
import { Notifier } from "./Notifier"; | ||
|
||
export class CustomNotifier extends Notifier { | ||
private message: string = ""; | ||
|
||
constructor(private readonly webhookUrl: string) { | ||
super(); | ||
this.webhookUrl = webhookUrl; | ||
} | ||
|
||
validate() { | ||
if (!this.webhookUrl) { | ||
Log.error("Custom webhook url is not set"); | ||
throw new Error("[-] Custom webhook url is not set"); | ||
} | ||
|
||
const newUrl = new URL(this.webhookUrl); | ||
if (newUrl.protocol !== "http:" && newUrl.protocol !== "https:") { | ||
throw new Error("[-] Custom webhook url is invalid. "); | ||
} | ||
constructor(webhookUrl: string, message: string) { | ||
super(webhookUrl, message); | ||
} | ||
|
||
async notify(message?: string) { | ||
if (message) { | ||
this.message = message; | ||
} | ||
this.validate(); | ||
const webhookMessage = { | ||
content: this.message, | ||
}; | ||
try { | ||
await fetch(this.webhookUrl, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify(webhookMessage), | ||
}); | ||
console.log("[+] Notification successfully sent to slack"); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
sendNotification(): void { | ||
this.notify(); | ||
} | ||
|
||
withMessage(message: string) { | ||
this.message = message; | ||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,11 @@ | ||
import Log from "../constants/Log"; | ||
import { Notifier } from "./Notifier"; | ||
|
||
export class DiscordNotifier extends Notifier { | ||
private message: string = ""; | ||
|
||
constructor(private readonly webhookUrl: string) { | ||
super(); | ||
this.webhookUrl = webhookUrl; | ||
} | ||
|
||
validate() { | ||
if (!this.webhookUrl) { | ||
Log.error("Discord_URL is not set"); | ||
throw new Error("[-] Discord_URL is not set"); | ||
} | ||
|
||
const newUrl = new URL(this.webhookUrl); | ||
if (newUrl.protocol !== "http:" && newUrl.protocol !== "https:") { | ||
throw new Error("[-] Discord webhook url is invalid. "); | ||
} | ||
constructor(webhookUrl: string, message: string) { | ||
super(webhookUrl, message); | ||
} | ||
|
||
async notify(message?: string) { | ||
if (message) { | ||
this.message = message; | ||
} | ||
this.validate(); | ||
const webhookMessage = { | ||
content: this.message, | ||
}; | ||
try { | ||
await fetch(this.webhookUrl, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify(webhookMessage), | ||
}); | ||
console.log("[+] Notification successfully sent to discord."); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
sendNotification(): void { | ||
this.notify(); | ||
} | ||
|
||
withMessage(message: string) { | ||
this.message = message; | ||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,11 @@ | ||
import Log from "../constants/Log"; | ||
import { Notifier } from "./Notifier"; | ||
|
||
export class SlackNotifier extends Notifier { | ||
private message: string = ""; | ||
|
||
constructor(private readonly webhookUrl: string) { | ||
super(); | ||
this.webhookUrl = webhookUrl; | ||
} | ||
|
||
validate() { | ||
if (!this.webhookUrl) { | ||
Log.error("WEBHOOK_URL is not set"); | ||
throw new Error("[-] WEBHOOK_URL is not set"); | ||
} | ||
|
||
const newUrl = new URL(this.webhookUrl); | ||
if (newUrl.protocol !== "http:" && newUrl.protocol !== "https:") { | ||
throw new Error("[-] Webhook url is invalid. "); | ||
} | ||
constructor(webhookUrl: string, message: string) { | ||
super(webhookUrl, message); | ||
} | ||
|
||
async notify(message?: string) { | ||
if (message) { | ||
this.message = message; | ||
} | ||
this.validate(); | ||
const webhookMessage = { | ||
content: this.message, | ||
}; | ||
try { | ||
await fetch(this.webhookUrl, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify(webhookMessage), | ||
}); | ||
console.log("[+] Notification successfully sent"); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
sendNotification(): void { | ||
this.notify(); | ||
} | ||
|
||
withMessage(message: string) { | ||
this.message = message; | ||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters