1/**
2 * This API integrates with Calendly to fetch personal events for the current week.
3 * It returns the events as raw JSON.
4 *
18 try {
19 // First, get the user's URI
20 const userResponse = await fetch('https://api.calendly.com/users/me', {
21 headers: {
22 'Content-Type': 'application/json',
38
39 // Now fetch the events using the user's URI
40 const eventsUrl = `https://api.calendly.com/scheduled_events?user=${userUri}&min_start_time=${startOfWeek.toISOString()}&max_start_time=${endOfWeek.toISOString()}`;
41
42 const eventsResponse = await fetch(eventsUrl, {
1import { API_URL } from "https://esm.town/v/std/API_URL";
2import { LibsqlError, type ResultSet, type Row, type TransactionMode } from "npm:@libsql/client";
3import { z } from "npm:zod";
35
36async function execute(statement: InStatement, args?: InArgs): Promise<ResultSet> {
37 const res = await fetch(`${API_URL}/v1/sqlite/execute`, {
38 method: "POST",
39 headers: {
50
51async function batch(statements: InStatement[], mode?: TransactionMode): Promise<ResultSet[]> {
52 const res = await fetch(`${API_URL}/v1/sqlite/batch`, {
53 method: "POST",
54 headers: {
78
79/* Val Town's Turso Proxy returns rows as an array of values
80 * Yet the LibSQL API has a Row type which behave as an array or object,
81 * ie you can access it via numerical index or string
82 */
8const REDIRECT_URI = Deno.env.get("REDIRECT_URI"); // Should be set to your Val Town endpoint URL + "/callback"
9
10const SCOPES = ["https://www.googleapis.com/auth/calendar.readonly"];
11
12export default async function server(request: Request): Promise<Response> {
46
47 // Exchange code for tokens
48 const tokenResponse = await fetch("https://oauth2.googleapis.com/token", {
49 method: "POST",
50 headers: {
68
69 const calendarResponse = await fetch(
70 `https://www.googleapis.com/calendar/v3/calendars/primary/events?` +
71 `timeMin=${now.toISOString()}` +
72 `&timeMax=${oneWeekLater.toISOString()}` +
6* Fork this val to your own profile.
7* Make a folder for the temporary vals that get generated, take the ID from the URL, and put it in `tempValsParentFolderId`.
8* If you want to use OpenAI models you need to set the `OPENAI_API_KEY` [env var](https://www.val.town/settings/environment-variables).
9* If you want to use Anthropic models you need to set the `ANTHROPIC_API_KEY` [env var](https://www.val.town/settings/environment-variables).
10* Create a [Val Town API token](https://www.val.town/settings/api), open the browser preview of this val, and use the API token as the password to log in.
11
12<img width=500 src="https://imagedelivery.net/iHX6Ovru0O7AjmyT5yZRoA/7077d1b5-1fa7-4a9b-4b93-f8d01d3e4f00/public"/>
1* Fork this val to your own profile.
2* Create a [Val Town API token](https://www.val.town/settings/api), open the browser preview of this val, and use the API token as the password to log in.
3
38 model: openai("gpt-4o", {
39 baseURL: "https://std-openaiproxy.web.val.run/v1",
40 apiKey: Deno.env.get("valtown"),
41 } as any),
42 messages: [
36
37Custom middleware can be added in an array as the third argument.
38Middleware can add data to the `req.data` object or return a response for things like API endpoints.
39
40```tsx
62```tsx
63// example middleware
64async function api (req: Request, res: Response, next): Promise<Response> {
65 if (req.pathname !== "/api") return next();
66 if (req.method === "POST") {
67 return Repsonse.json({ message: "Hello POST request" });
70}
71
72export default render(App, import.meta.url, [ api ]);
73```
74
34 const useMiddleware = Array.isArray(opts); // for backwards compat
35 const options: ReactStreamOptions = !Array.isArray(opts) ? opts : {};
36 const { api, getInitialProps } = options;
37
38 if (typeof document !== "undefined" && module) {
48 // DEPRECATED (for backwards compat)
49 options.robots && robots(options.robots),
50 options.api && deprecatedCustomAPI(options.api),
51 options.getInitialProps && deprecatedGetInitiaProps(options.getInitialProps),
52 // New custom middleware
127// DEPRECATED
128// DEPRECATE (for backwards compat)
129const deprecatedCustomAPI = (api?: RequestHandler): Middleware => async (req, res, next) => {
130 if (!api) return next();
131 if (req.method === "GET") return next();
132 return api(req);
133};
134const deprecatedGetInitiaProps = (getProps: DataFetcher<any>): Middleware => async (req, res, next) => {
142 /** DEPRECATED: Optional text response for robots.txt */
143 robots?: string;
144 /** DEPRECATED: Optional API request handler for all non-GET methods */
145 api?: RequestHandler;
146 /** DEPRECATED: data fetcher to set initial props based on request */
147 getInitialProps?: DataFetcher<any>;
1/**
2 * This program creates a basic calculator endpoint that accepts and returns data in the specified format.
3 * It uses a RESTful API approach where the operation is specified in the URL path.
4 * The numbers to operate on are passed as query parameters.
5 * The result is returned as JSON.
4async function fetchRandomJoke() {
5 const response = await fetch(
6 "https://official-joke-api.appspot.com/random_joke",
7 );
8 return response.json();