Script118 words
standalone functions that can be imported by your HTTP, Cron, and Email vals. A script val can be as simple as a function that just adds two numbers: export function
Sections
Script
standalone functions that can be imported by your HTTP, Cron, and Email vals. A script val can be as simple as a function that just adds two numbers: export function
Upgrade to Deno633 words
User-defined functions can be synchronous 🥳 In the old runtime, you would need to await the call to any @user.function. That is no longer the case! Now only async functions
Sections
User-defined functions can be synchronous 🥳
User-defined functions can be synchronous 🥳 In the old runtime, you would need to await the call to any @user.function. That is no longer the case! Now only async functions
setTimeout has been removed
setTimeout has been removed. Contact us if you need this functionality.
Promises are not recursively resolved between functions
recursively resolved between functions. In the prior runtime, all values were recursively awaited. This means that if you returned an array or object with a Promise nested somewhere inside it,
Cron evaluations has 0 arguments where it used to have 1
purple). This does not affect that fact that crons pass the Interval object to the function that has been scheduled, which allows you to get the lastRunAt value of the
Express to HTTP migration325 words
for the express versus HTTP types: // Express handler. export function handler(req, res) { res.send("Hello world"); } // HTTP handler. export function handler(req) { return new Response("Hello world"); } The
Sections
Parameters
for the express versus HTTP types: // Express handler. export function handler(req, res) { res.send("Hello world"); } // HTTP handler. export function handler(req) { return new Response("Hello world"); }
The response object
other details by chaining functions off of the response object, with the HTTP type, these are options you set for the Response object. // Express handler. export function handler(req, res)
The request object
string parameters, will require different code: // Express handler. export function handler(req, res) { res.send(req.query.name); } // HTTP handler. export function handler(req) { return new Response(new URL(req.url).searchParams.get("name")); }
Exports121 words
the function to run when that val is triggered. If your val has multiple exports, then we require one of them to the default export, which will be the function
Sections
Exports
the function to run when that val is triggered. If your val has multiple exports, then we require one of them to the default export, which will be the function
Cron (Scheduled)261 words
updates. Sending a reminder email every morning. Cron functions can be configured with cron syntax, or simple intervals like “once an hour” Functions can run up to once every 15
Sections
Cron (Scheduled)
updates. Sending a reminder email every morning. Cron functions can be configured with cron syntax, or simple intervals like “once an hour” Functions can run up to once every 15
Type Signature
Signature. Cron functions should take an Interval object as a parameter, and can return anything as a result. The return value is ignored. ExampleRun in Val Town ↗ export function
HTTP99 words
Peko. These handlers need to export a function that takes a Request object as the first parameter and returns a Response object. The function can be asynchronous. Basic examples Routing
Sections
HTTP
Peko. These handlers need to export a function that takes a Request object as the first parameter and returns a Response object. The function can be asynchronous. Basic examples Routing
Save HTML form data356 words
web browser, the server (your val function) gets sent a GET request. You can check the HTTP method using req.method and change how your val function responds. See Web forms
Sections
Create an HTTP val
Create an HTTP val. Write a val function that accepts a Request and returns a Response.
Host your form on Val Town
web browser, the server (your val function) gets sent a GET request. You can check the HTTP method using req.method and change how your val function responds. See Web forms
Your first cron val583 words
like this: Step 2Run in Val Town ↗ export default async function (interval: Interval) { // your code… } This function will be run on a schedule. By default, this
Sections
Step 2: Create a Cron val
like this: Step 2Run in Val Town ↗ export default async function (interval: Interval) { // your code… } This function will be run on a schedule. By default, this
Step 3: Get the weather
Step 3Run in Val Town ↗ import { getWeather } from "https://esm.town/v/stevekrouse/getWeather"; export default async function (interval: Interval) { let weather = await getWeather("Brooklyn, NY"); console.log(weather.current_condition[0].FeelsLikeF); } Replace Brooklyn, NY
Step 4: Send yourself an email
import { email } from "https://esm.town/v/std/email"; import { getWeather } from "https://esm.town/v/stevekrouse/getWeather"; export default async function (interval: Interval) { let weather = await getWeather("Brooklyn, NY"); let feelsLike = weather.current_condition[0].FeelsLikeF; let
Next steps
updates to Discord. weather_forecast_in_the_morning - weather forecast on Telegram. weatherBot - OpenAI Weather Bot via function calling. aqi - email alerts when AQI is unhealthy near you. …add yours here!
Early Return453 words
set up a queue. In your early-returning HTTP val: early-returning.tsRun in Val Town ↗ async function handle(request: Request) { // Send off the relevant data a queue HTTP val. //
Sections
How to set up a queue
early-returning HTTP val: early-returning.tsRun in Val Town ↗ async function handle(request: Request) { // Send off the relevant data a queue HTTP val. // This `fetch` is not awaited. fetch("https://my-queue.web.val.run",
Promises should otherwise be awaited
- besides this narrow use-case, errors that occur in promises won’t be properly handled and functions may run out-of-order. For example, if you use fetch to request some resource, but
CORS356 words
any default headers. Example: Custom CORS Configuration. Custom CorsRun in Val Town ↗ export async function myEndpoint() { return new Response("Hello", { headers: { "Access-Control-Allow-Origin": "https://specific-domain.com", "Access-Control-Allow-Methods": "GET,POST", }, });
Sections
Example: Custom CORS Configuration
Example: Custom CORS Configuration. Custom CorsRun in Val Town ↗ export async function myEndpoint() { return new Response("Hello", { headers: { "Access-Control-Allow-Origin": "https://specific-domain.com", "Access-Control-Allow-Methods": "GET,POST", }, }); }
Example: Handling Preflight Requests
Example: Handling Preflight Requests. For complete control over CORS behavior, you can handle OPTIONS requests explicitly: Handle Preflight Request export async function myEndpoint(req) { if (req.method === "OPTIONS") { return
Removing CORS Headers
Removing CORS Headers. If we detect that you’ve set your own "Access-Control-Allow-Origin" header we won’t add any custom CORS headers to your request. Remove CORS Headers export async function myEndpoint()