|
| 1 | +import axios from "axios" |
1 | 2 | import type { GatsbyFunctionRequest, GatsbyFunctionResponse } from "gatsby" |
2 | | -import { lambda } from "../lambda/calendarEvents" |
3 | 3 |
|
4 | 4 | async function handler( |
5 | 5 | __req: GatsbyFunctionRequest, |
6 | 6 | res: GatsbyFunctionResponse |
7 | 7 | ): Promise<void> { |
8 | | - // passing env vars as arguments due to a bug on GC functions where env vars |
9 | | - // can not be accessed by imported functions |
10 | | - const { statusCode, body } = await lambda( |
11 | | - process.env.GOOGLE_API_KEY!, |
12 | | - process.env.GOOGLE_CALENDAR_ID! |
13 | | - ) |
14 | | - res.status(statusCode).send(body) |
| 8 | + const apiKey = process.env.GOOGLE_API_KEY |
| 9 | + const calendarId = process.env.GOOGLE_CALENDAR_ID |
| 10 | + |
| 11 | + try { |
| 12 | + const futureEventsReq = await axios.get( |
| 13 | + `https://content.googleapis.com/calendar/v3/calendars/${calendarId}/events`, |
| 14 | + { |
| 15 | + params: { |
| 16 | + key: apiKey, |
| 17 | + timeMin: new Date().toISOString(), |
| 18 | + maxResults: 3, |
| 19 | + singleEvents: true, |
| 20 | + orderBy: "startTime", |
| 21 | + }, |
| 22 | + } |
| 23 | + ) |
| 24 | + |
| 25 | + const pastEventsReq = await axios.get( |
| 26 | + `https://content.googleapis.com/calendar/v3/calendars/${calendarId}/events`, |
| 27 | + { |
| 28 | + params: { |
| 29 | + key: apiKey, |
| 30 | + timeMax: new Date().toISOString(), |
| 31 | + maxResults: 4, |
| 32 | + singleEvents: true, |
| 33 | + orderBy: "startTime", |
| 34 | + }, |
| 35 | + } |
| 36 | + ) |
| 37 | + |
| 38 | + const response = { |
| 39 | + pastEvents: pastEventsReq.data.items, |
| 40 | + futureEvents: futureEventsReq.data.items, |
| 41 | + } |
| 42 | + |
| 43 | + res.status(200).send(JSON.stringify(response)) |
| 44 | + } catch (error) { |
| 45 | + console.error(error) |
| 46 | + res.status(500).send( |
| 47 | + JSON.stringify({ |
| 48 | + msg: "Something went wrong with requesting the calendar events data.", |
| 49 | + }) |
| 50 | + ) |
| 51 | + } |
15 | 52 | } |
16 | 53 |
|
17 | 54 | export default handler |
0 commit comments