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
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 trigger
Create an HTTP trigger. 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
Early Return456 words
set up a queue. In your early-returning HTTP file: early-returning.tsRun in Val Town ↗ async function handle(request: Request) { // Send off the relevant data a queue HTTP file. //
Sections
How to set up a queue
early-returning HTTP file: early-returning.tsRun in Val Town ↗ async function handle(request: Request) { // Send off the relevant data a queue HTTP file. // 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
Your first scheduled cron602 words
The default code will look like this: weatherNotifier.ts export default async function (interval: Interval) { // your code… } This function will be run on a schedule. By default, this
Sections
Step 2: Set up a Cron Trigger
The default code will look like this: weatherNotifier.ts 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!
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()
Proxied fetch207 words
contains an alternative version, std/fetch, that wraps the JavaScript Fetch API to provide additional functionality. The fetch function from std/fetch reroutes requests using a proxy vendor so that requests obtain
Sections
Proxied fetch
contains an alternative version, std/fetch, that wraps the JavaScript Fetch API to provide additional functionality. The fetch function from std/fetch reroutes requests using a proxy vendor so that requests obtain
Migrating Deprecated HTTP Vals541 words
return a different random number on each request: const randomValue = Math.random(); export default async function (req: Request): Promise<Response> { return Response.json({ randomValue }); } Run in Val Town. Terminal
Sections
Accidentally re-using values.
return a different random number on each request: const randomValue = Math.random(); export default async function (req: Request): Promise<Response> { return Response.json({ randomValue }); } Run in Val Town. Terminal
Intentionally caching values for performance!
data fetching. const expensiveData = await fetchLargeDataset(); const cache = new Map(); export default async function (req: Request): Promise<Response> { const url = new URL(req.url); const key = url.searchParams.get("key"); if
Migration Checklist
be safely cached. Move variables that need to be unique per request inside the handler function. Consider opportunities to improve performance by intentionally caching expensive computations or initializations. Test your