Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
91 lines (85 loc) · 2.42 KB
/
Copy pathindex.js
File metadata and controls
91 lines (85 loc) · 2.42 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
export const FH_BASE = "https://fareharbor.com/api/external/v1";
export const SITES = {
dirtyboat: {
shortname: "dirtyboatislamorada",
displayName: "DirtyBoat",
bookingBase: "https://fareharbor.com/embeds/book/dirtyboatislamorada",
timezone: "America/New_York",
items: {
full_day: 98231,
full_day_private: 71356,
swordfish: 101827,
split: 71364,
three_quarter: 71357,
half_day: 71359,
deep_drop: 101828,
dirtyboat_2: 694728,
},
},
misspenny: {
shortname: "misspenny",
displayName: "Miss Penny",
bookingBase: "https://fareharbor.com/embeds/book/misspenny",
timezone: "America/New_York",
items: {
split: 489246,
swordfish: 489242,
full_day: 489204,
three_quarter: 489240,
half_day: 489241,
},
},
};
export function fhHeaders() {
return {
"X-FareHarbor-API-App":
process.env.FAREHARBOR_APP_KEY || process.env.FH_APP_KEY || "",
"X-FareHarbor-API-User":
process.env.FAREHARBOR_USER_KEY || process.env.FH_USER_KEY || "",
};
}
export async function fhGet(path) {
const res = await fetch(`${FH_BASE}${path}`, { headers: fhHeaders() });
if (!res.ok) {
throw new Error(`FareHarbor ${res.status} ${res.statusText}`);
}
return res.json();
}
export function bookingUrl(siteKey, itemPk, availabilityPk) {
const site = SITES[siteKey];
if (!site) throw new Error(`Unknown site: ${siteKey}`);
return `${site.bookingBase}/items/${itemPk}/availability/${availabilityPk}/book/`;
}
export async function fetchDateAvailability(siteKey, date) {
const site = SITES[siteKey];
if (!site) throw new Error(`Unknown site: ${siteKey}`);
const checks = Object.entries(site.items).map(async ([itemName, itemPk]) => {
try {
const data = await fhGet(
`/companies/${site.shortname}/items/${itemPk}/minimal/availabilities/date/${date}/`
);
return {
site: siteKey,
displayName: site.displayName,
item: itemName,
itemPk,
slots: data.availabilities || [],
};
} catch {
return {
site: siteKey,
displayName: site.displayName,
item: itemName,
itemPk,
slots: [],
};
}
});
return Promise.all(checks);
}
export async function fetchAllSitesAvailability(date) {
const perSite = await Promise.all(
Object.keys(SITES).map((key) => fetchDateAvailability(key, date))
);
return perSite.flat();
}