Avatar

@alp

14 likes20 public vals
Joined February 28, 2023
1
2
3
4
5
6
7
8
9
10
11
12
13
import { fetch } from "https://esm.town/v/std/fetch";
export const proxyFetch2 = async (req, res) => {
const { url, options } = req.body;
try {
const response = await fetch(url, options);
return res.status(response.status).send(await response.text());
} catch (e) {
const errorMessage = e instanceof Error ? e.message : "Unknown error";
console.error("Failed to initiate fetch", e);
return res.status(500).send(`Failed to initiate fetch: ${errorMessage}`);
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
import { fetch } from "https://esm.town/v/std/fetch";
export const proxyFetch1 = async (req, res) => {
const { url, options } = req.body;
try {
const response = await fetch(url, options);
return res.status(response.status).send(await response.text());
} catch (e) {
const errorMessage = e instanceof Error ? e.message : "Unknown error";
console.error("Failed to initiate fetch", e);
return res.status(500).send(`Failed to initiate fetch: ${errorMessage}`);
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import { fetch } from "https://esm.town/v/std/fetch";
export const proxyFetch = async (req, res) => {
const { url, options } = req.body;
try {
const response = await fetch(url, options);
return res.status(response.status).send(await response.text());
}
catch (e) {
const errorMessage = e instanceof Error ? e.message : "Unknown error";
console.error("Failed to initiate fetch", e);
return res.status(500).send(`Failed to initiate fetch: ${errorMessage}`);
}
};
1
2
3
4
5
6
7
8
9
10
import { fulfillReservationRequests } from "https://esm.town/v/alp/fulfillReservationRequests";
export const fulfillReservationRequestsCRON = async () => {
// Try to fulfill 3 times to prevent against random failures. If a request is
// successful, it won't be fulfilled again (and even if it does, Resy rejects the request
// most of the time as it won't let you book over an existing reservation).
await fulfillReservationRequests();
await fulfillReservationRequests();
await fulfillReservationRequests();
};
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
import { set } from "https://esm.town/v/std/set?v=11";
import { reservationRequests } from "https://esm.town/v/alp/reservationRequests";
export const addResyReservationRequest = async (request: {
reservation: {
date: string;
seats: number;
timeRange: [
string,
string,
];
};
auth: {
email: string;
password: string;
};
// https://resy.com/cities/ny/venues/rule-of-thirds
// ^ venue city ^ venue slug
venue: {
// e.g. rule-of-thirds
slug: string;
// e.g. ny
city: string;
};
}) => {
reservationRequests.push(request);
await set(
"reservationRequests",
reservationRequests,
);
};
1
2
3
4
5
6
7
8
9
10
11
export const kyDoesNotWorkDemo = (async () => {
const ky = await import("npm:ky");
console.log("has get method:", ky.get === undefined ? "❌" : "✅");
console.log("has post method:", ky.get === undefined ? "❌" : "✅");
console.log(`available methods: [${Object.keys(ky).join(",")}]`);
const { post } = await import("npm:ky");
console.log(
"has post method if imported directly:",
post === undefined ? "❌" : "✅"
);
})();
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
92
93
94
95
96
97
98
99
100
import { fetch } from "https://esm.town/v/std/fetch";
export const bookReservationOnResy = async ({
reservation,
venue,
auth,
}: {
reservation: {
// How many seats to book
seats: number;
// e.g. 2023-05-01 (interpreted as May 05, 2023)
date: string;
// e.g. [17:00, 22:00]
timeRange: [string, string];
};
venue:
| {
type: "id";
id: string;
}
| {
type: "slug";
slug: string;
city: string;
};
auth: {
email: string;
password: string;
};
}) => {
const { z } = await import("npm:zod");
const RESY_API_URL = "https://api.resy.com";
const RESY_DEFAULT_HEADERS = {
accept: "application/json, text/plain, */*",
"accept-encoding": "gzip, deflate, br",
"accept-language": "en-US,en;q=0.9",
authorization: 'ResyAPI api_key="VbWk7s3L4KiK5fzlO7JD3Q5EYolJI7n5"',
"x-origin": "https://resy.com",
origin: "https://resy.com/",
dnt: "1",
referer: "https://resy.com/",
"content-type": "application/json",
};
const BookedReservationResponseSchema = z.object({
resy_token: z.string(),
reservation_id: z.number(),
});
const SlotDetailsResponseSchema = z.object({
book_token: z.object({
value: z.string(),
date_expires: z.string(),
}),
});
const FindResponseSchema = z.object({
results: z.object({
venues: z
.object({
venue: z.object({
id: z.object({
resy: z.number(),
}),
name: z.string(),
location: z.object({
time_zone: z.string(),
}),
}),
slots: z
.object({
availability: z.object({
id: z.number(),
}),
config: z.object({
id: z.number(),
// Reservations are booked with the token
token: z.string(),
type: z.string(),
}),
date: z.object({
start: z.string(),
end: z.string(),
}),
payment: z.object({
cancellation_fee: z.number().nullish(),
deposit_fee: z.number().nullish(),
is_paid: z.boolean(),
}),
})
.array(),
})
.array()
.default([]),
}),
});
const PaymentMethodSchema = z.object({
id: z.number(),
type: z.string(),
});
const AuthenticationResponseSchema = z.object({
id: z.number(),
token: z.string(),
1
2
3
4
5
6
7
8
import { email } from "https://esm.town/v/std/email?v=9";
export const ping = async () => {
await email({
subject: `Got a ping! ${new Date().toISOString()}`,
});
return "pong";
};
1
2
3
4
5
6
7
8
9
10
import { ping } from "https://esm.town/v/alp/ping";
export const log = async (req, res) => {
await ping();
res.json({
forwardedFor: req.headers["x-forwarded-for"],
remoteAddress: req.connection.remoteAddress,
ip: req.ip,
});
};
1
2
3
export const testEnvironment = () => {
console.log("globalThis", globalThis);
};
Next