-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
47 lines (41 loc) · 1.06 KB
/
index.js
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
/* 定期的にNEWS投稿をする */
export const handler = async (event, context) => {
const targetUrl = process.env.TARGET_URL;
if (!targetUrl) {
return {
statusCode: 500,
body: JSON.stringify('Error: TARGET_URL environment variable not set.'),
};
}
try {
const response = await fetch(targetUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
apiKey: process.env.NEWS_API_KEY,
}),
});
const text = await response.text();
if (!response.ok) {
console.error(`HTTP error! status: ${response.status}`);
throw new Error(`HTTP error! status: ${response.status}`);
}
console.log(`Fetched ${targetUrl} successfully.: ${text}`);
return {
statusCode: 200,
body: JSON.stringify({
message: `Fetched ${targetUrl} successfully.`,
status_code: response.status,
response_text: text
}),
};
} catch (error) {
console.error(`Error fetching ${targetUrl}: ${error}`);
return {
statusCode: 500,
body: JSON.stringify(`Error fetching ${targetUrl}: ${error}`),
};
}
};