1import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
2
3interface UpdateValArgs {
14 };
15
16 return fetchJSON("https://api.val.town/v1/vals", {
17 headers: {
18 Authorization: `Bearer ${token}`,
99});
100
101export default app.fetch;
1import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON";
2import { querystring } from "https://esm.town/v/stevekrouse/querystring";
3
4export let spotifyRefreshToken = async ({ refresh_token, client_id, client_secret }) =>
5 fetchJSON("https://accounts.spotify.com/api/token", {
6 method: "POST",
7 body: await querystring({
2
3// Immediately invoked function that exports a function generating a URL as a string,
4// which fetches all comments from an article associated with the provided post_id.
5export const constructReadTangleUrl = (() => {
6 return (post_id) => {
20
21async function getNewThreads() {
22 const resp = await fetch(`https://discord.com/api/guilds/${guild}/threads/active`, {
23 headers: {
24 "Authorization": `Bot ${token}`,
119});
120
121export default app.fetch;
46};
47
48async function fetchUser(token: string): Promise<User> {
49 const resp = await fetch("https://api.val.town/v1/me", {
50 headers: {
51 Authorization: `Bearer ${token}`,
54
55 if (resp.status !== 200) {
56 throw new Error("Could not fetch user");
57 }
58
91 const formData = await req.formData();
92 const token = formData.get("token").toString();
93 const user = await fetchUser(token);
94 if (user.username != val.username) {
95 return new Response("Unauthorized", {
1# Fetch Paginated Data
2
3This val exports a function that loops through paginated API responses and returns the combined data as an array. It expects pagination with `next` and there to be a `data` property in the API response. This conforms to the Val Town API, so this function is useful when fetching paginated Val Town API responses for creating custom folders in [pomdtr's vscode extension](https://github.com/pomdtr/valtown-vscode).
4
5Usage:
7```ts
8const id = <vt user id>
9await fetchPaginatedData(`https://api.val.town/v1/users/${id}/vals`, {
10 headers: { Authorization: `Bearer ${Deno.env.get("valtown")}` },
11});
1import { fetch } from "https://esm.town/v/std/fetch";
2
3export const moon_phase = async (city: string): Promise<string> => {
4 let data = await fetch(`https://wttr.in/${city}?format=j1`);
5 let jsonData = await data.json();
6 return `${city}: ${jsonData["weather"][0]["astronomy"][0]["moon_phase"]}: ${
53
54/**
55 * Wrapper for fetch that traces the request. It also passes a propagation header
56 * so if you are calling another val that uses traced_handler their traces will
57 * be tied together.
58 */
59export async function traced_fetch(
60 input: string | URL,
61 init?: RequestInit,
62): Promise<Response> {
63 return await get_tracer().startActiveSpan(`fetch`, async (span) => {
64 const prop_output: { b3: string } = { b3: "" };
65 propagation.inject(context.active(), prop_output);
66 try {
67 const resp: Response = await fetch(input, {
68 ...init,
69 headers: {