-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
49 lines (40 loc) 路 1.35 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
48
49
const url = require('url')
const { createError } = require('micro')
const fetchEvents = require('./lib/fetch-events')
const sanitizeEvent = require('./lib/sanitize-event')
const filterEvent = require('./lib/filter-event')
module.exports = async function(req, res) {
// allow public access to our api from anywhere
res.setHeader('Access-Control-Allow-Origin', '*')
// get the pathname "/year/month" from the url as ['', year, month]
const pathname = url.parse(req.url).pathname.split('/')
// set the year and month that we'll use to get events
let year, month
if (pathname.length !== 3) {
// if the request didn't include a pathname,
// then get the current year/month
const now = new Date()
year = now.getUTCFullYear()
month = now.getUTCMonth() + 1
} else {
// otherwise get the provided year/month
year = pathname[1]
month = pathname[2]
}
// create a date from year/month
const date = new Date(Date.UTC(year, month - 1))
// throw an error if year/month does not make a date
if (isNaN(date.getTime())) {
throw createError(400, 'Not a date')
}
// fetch the events from google calendar
let events
try {
events = await fetchEvents(date)
} catch (err) {
err.statusCode = 400
throw err
}
// return the events as a json 200 OK response
return events.map(sanitizeEvent).filter(filterEvent)
}