159<h3>External Code</h3>
160161<h4>Fetch</h4>
162<p>Fetch is a way for one server to request data from another server. A fetch requires
163two const variables. The first is const response = await fetch(url). The second is
164const data = await response.json();. If you want to do something besides just retrieve data,
165you need to add the method. Change the first line from const response = await fetch(url) to
166await fetch(url, {
167method: 'any method except GET',
168headers: { 'Content-Type': 'application/json' },
171172<p>
173To make a fetch request with authorization, you would write const response = await fetch('API endpoint')
174followed by const data = await response.json() and include headers: { 'Authorization': 'token' } in
175your request.
195const app = new Hono();
196app.get("/", HTTP_Guide);
197export default app.fetch;
105});
106107export default app.fetch;
63/**
64* Wrap the incoming request, inject the Deno env vars into the Hono app,
65* and then call the Hono api entrypoint (`app.fetch`)
66*/
67export default async function(req: Request): Promise<Response> {
74//
75// If you don't want those values, remove them from the env object
76return app.fetch(req, env);
77}
GitHubSyncREADME.md4 matches
1213- `/push` will copy the contents from a list of vals specified in `config.json` and push them to a GitHub repo
14- `/deploy` is a GitHub webhook URL that will fetch contents from GitHub and update the code on Val Town
15161. Fork this val
271. Add a `VAL_SECRET` env var to the val. Use this secret to sign the webhook POST request to the `/push` endpoint. Use this endpoint to commit vals from Val Town to your GitHub repo.
2829### Example push to GitHub fetch
3031You can use this example to POST to the `/push` endpoint to copy vals to GitHub.
46const signature = await sign(body, secret);
4748const res = await fetch(url, {
49method: "POST",
50body,
89- [x] Monkey test
90- [x] Add setup instructions to readme
91- [x] Add example code for private webhook fetch
92- [x] Make val and repo public
93- [ ] Check modified date before export to GitHub??
GitHubSyncindex1 match
22app.post("/deploy", verifyGitHubSignature(GITHUB_WEBHOOK_SECRET), deploy);
2324export default app.fetch;
63/**
64* Wrap the incoming request, inject the Deno env vars into the Hono app,
65* and then call the Hono api entrypoint (`app.fetch`)
66*/
67export default async function(req: Request): Promise<Response> {
74//
75// If you don't want those values, remove them from the env object
76return app.fetch(req, env);
77}
gracefulVioletTunavt1 match
2const apiToken = Deno.env.get("VAL_TOWN_RW_PROJECTS");
34const response = await fetch("https://api.val.town/v1/projects", {
5method: "POST",
6headers: {
projectConverterDraftconverter.ts17 matches
89// Create a new project
10const projectResponse = await fetch("https://api.val.town/v1/projects", {
11method: "POST",
12headers: {
140async function createDirectory(projectId: string, directoryPath: string, apiToken: string): Promise<void> {
141const encodedPath = encodeURIComponent(directoryPath);
142const response = await fetch(`https://api.val.town/v1/projects/${projectId}/files/${encodedPath}`, {
143method: "POST",
144headers: {
167168const encodedPath = encodeURIComponent(file.path);
169const fileResponse = await fetch(`https://api.val.town/v1/projects/${projectId}/files/${encodedPath}`, {
170method: "POST",
171headers: {
204const appContent = appMatch ? appMatch[0].trim() : "";
205206// Extract route paths from fetch calls
207const fetchCalls = input.match(/fetch\(['"`]([^'"`]+)['"`]|fetch\(\s*['"`]([^'"`]+)['"`]/g) || [];
208const fetchRoutes = [];
209for (const fetchCall of fetchCalls) {
210const urlMatch = fetchCall.match(/['"`](\/[^'"`]+)['"`]/);
211if (urlMatch && urlMatch[1] && !fetchRoutes.includes(urlMatch[1])) {
212fetchRoutes.push(urlMatch[1]);
213}
214}
289290useEffect(() => {
291async function fetchData() {
292try {
293// Example data - replace with your own data source
304}
305
306fetchData();
307}, []);
308410}
411412// Create Backend index with any detected fetch routes
413const backendIndexContent =
414`import { parseProject, serveFile } from "https://esm.town/v/std/utils@64-main/index.ts";
438});
439${
440fetchRoutes.includes("/convert")
441? `
442// Handle /convert endpoint for the converter
465});
466467// Export app.fetch (not the app object) - CRITICAL for Val Town HTTP files
468export default app.fetch;`;
469470files.push({
514});
515516export default app.fetch;`,
517type: "http",
518},
projectConverterDraftindex.ts3 matches
28});
2930// Add the /convert endpoint to handle the React app's fetch calls
31app.post("/convert", async (c) => {
32try {
55});
5657// Export app.fetch (not the app object) - CRITICAL for Val Town HTTP files
58export default app.fetch;
1011* This file must be set as an HTTP type in Val Town, not a Script
12* It exports `app.fetch` (not the app object) which is required for Val Town HTTP handlers