12 * The email address(es) to send the email to. Only available to Val Town Pro subscribers.
13 * Can be a single string, IAddress object, or an array of strings/IAddress objects.
14 * @default the email of the logged user calling this function.
15 */
16 to?: (IAddress | string)[] | IAddress | string;
31
32// ------------
33// Functions
34// ------------
35
36async function execute(statement: InStatement): Promise<ResultSet> {
37 const res = await fetch(`${API_URL}/v1/sqlite/execute`, {
38 method: "POST",
48}
49
50async function batch(statements: InStatement[], mode?: TransactionMode): Promise<ResultSet[]> {
51 const res = await fetch(`${API_URL}/v1/sqlite/batch`, {
52 method: "POST",
62}
63
64function createResError(body: string) {
65 try {
66 const e = zLibsqlError.parse(JSON.parse(body));
113 *
114 * The types are currently shown for types declared in a SQL table. For
115 * column types of function calls, for example, an empty string is
116 * returned.
117 */
4
5/**
6 * Provides functions for interacting with your account's blob storage.
7 * Blobs can store any data type (text, JSON, images, etc.) and allow
8 * retrieval across different vals using the same key.
80};
81
82async function list(prefix?: string): Promise<{ key: string; size: number; lastModified: string }[]> {
83 let querystring = prefix ? `?prefix=${encodeURIComponent(prefix)}` : "";
84 const res = await fetch(`${API_URL}/v1/blob${querystring}`, {
94}
95
96async function delete_(key: string) {
97 const res = await fetch(`${API_URL}/v1/blob/${encodeURIComponent(key)}`, {
98 method: "DELETE",
107}
108
109async function get(key: string) {
110 const res = await fetch(`${API_URL}/v1/blob/${encodeURIComponent(key)}`, {
111 headers: {
123}
124
125async function set(key: string, value: BodyInit) {
126 const res = await fetch(`${API_URL}/v1/blob/${encodeURIComponent(key)}`, {
127 method: "POST",
137}
138
139async function copy(previous: string, next: string) {
140 const res = await get(previous);
141 await set(next, res.body);
142}
143
144async function move(previous: string, next: string) {
145 await copy(previous, next);
146 await delete_(previous);
147}
148
149async function getJSON(key: string) {
150 try {
151 const res = await get(key);
159}
160
161async function setJSON(key: string, value: any) {
162 return set(key, JSON.stringify(value));
163}
1export default async function(req: Request): Promise<Response> {
2 return Response.json({
3 idea: `imagine that https://www.${crypto.randomUUID()}.com was a cool web site about ${crypto.randomUUID()}`,
21export default router.fetch;
22
23export async function createScreenshot(code: string, theme: string = "dark-plus"): Promise<URL> {
24 const apiUrl = "https://sourcecodeshots.com/api/image/permalink";
25 const { url } = await ky.post(apiUrl, {
2import { easyAQI } from "https://esm.town/v/stevekrouse/easyAQI";
3
4export async function aqi(interval: Interval) {
5 const location = "Brooklyn Navy Yard, Brooklyn"; // <-- change to place, city, or zip code
6 const data = await easyAQI({ location });
2
3const openai = new OpenAI();
4const functionExpression = await openai.chat.completions.create({
5 "messages": [
6 { "role": "user", "content": "whats the weather in sf" },
8 tools: [
9 {
10 function: {
11 name: "weather",
12 parameters: {
19 },
20 },
21 type: "function",
22 },
23 ],
26});
27
28console.log(functionExpression.choices);
1export function currentDate() {
2 return new Date();
3}
7The idea is simple: you write down a javascript snippet (named vals) in your browser, and it's instantly executed on a server. You can use it to:
8
9- execute a function on a cron schedule
10- host a small websites (this article [hosted on Val Town](https://www.val.town/v/pomdtr/love_letter))
11- send yourself emails
32
33// run every hour
34export default function(interval: Interval) {
35 // create the val table
36 await options.sqlite.execute(createQuery);
78
79// triggered each time an email is sent to pomdtr.sqlite_email@valtown.email
80export default async function(email: Email) {
81 // forward the email to my own email address
82 await sendEmail({
96## Publishing the Table
97
98The val.town stdlib provides a neat `rpc` function that provides a simple way to expose a function as an API. So I decided to write a simple val that would run a query on the table, and return the result.
99
100```ts
3// Send a pushover message.
4// token, user, and other opts are as specified at https://pushover.net/api
5export default async function pushover({
6 token,
7 user,