File tree 2 files changed +31
-14
lines changed 2 files changed +31
-14
lines changed Original file line number Diff line number Diff line change @@ -4,20 +4,8 @@ import { postsPerPage } from "@/site";
4
4
import Feed from "@/components/Feed" ;
5
5
import HeroSection from "../components/HeroSection" ;
6
6
import axios , { AxiosResponse } from 'axios' ;
7
+ import { sendSlackMessage } from "@/functions/sendSlackMessage" ;
7
8
8
- interface SlackMessage {
9
- text : string ;
10
- }
11
-
12
- const sendSlackMessage = async ( message : SlackMessage ) : Promise < AxiosResponse > => {
13
- const slackWebhookUrl = process . env . SLACK_WEBHOOK ;
14
-
15
- if ( ! slackWebhookUrl ) {
16
- throw new Error ( 'Slack webhook URL is not defined.' ) ;
17
- }
18
-
19
- return await axios . post ( slackWebhookUrl , message ) ;
20
- } ;
21
9
22
10
const HomePage = async ( {
23
11
searchParams,
@@ -28,7 +16,7 @@ const HomePage = async ({
28
16
const publishedPosts : Article [ ] = await getAllPosts ( ) ;
29
17
30
18
// Send a message to Slack when the page is accessed
31
- const message : SlackMessage = {
19
+ const message = {
32
20
text : `Someone visited the homepage! Page: ${ page } ` ,
33
21
} ;
34
22
Original file line number Diff line number Diff line change
1
+ // slackUtils.ts
2
+
3
+ import axios , { AxiosResponse } from 'axios' ;
4
+
5
+ interface SlackMessage {
6
+ text : string ;
7
+ }
8
+
9
+ export const sendSlackMessage = async ( message : SlackMessage ) : Promise < AxiosResponse | void > => {
10
+ const slackWebhookUrl = process . env . SLACK_WEBHOOK ;
11
+
12
+ if ( ! slackWebhookUrl ) {
13
+ console . error ( 'Slack webhook URL is not defined.' ) ;
14
+ return ;
15
+ }
16
+
17
+ try {
18
+ const response = await axios . post ( slackWebhookUrl , message ) ;
19
+ return response ;
20
+ } catch ( error : unknown ) {
21
+ if ( error instanceof Error ) {
22
+ console . error ( 'Error sending Slack message:' , error . message ) ;
23
+ // Handle the error as needed
24
+ } else {
25
+ console . error ( 'An unexpected error occurred:' , error ) ;
26
+ // Handle other types of errors or log them
27
+ }
28
+ }
29
+ } ;
You can’t perform that action at this time.
0 commit comments