3export let fetchJSON = async (url: string, options?: any) => {
4 //url must return a JSON in string form
5 //this function parses that string into a javascript object
6 let f = await fetch(url, {
7 ...options,
1export function myApi(name) {
2 return "hi " + name;
3}
3// not working yet, didn't manage to affect dynamic value to @me.__state
4
5export const state = function (
6 id: string,
7 defaultValue: { [key: string]: any }
18 return [
19 __state[id],
20 function (newState) {
21 const newStore = {
22 ...__state,
22
23```js
24function updateFeedback(ref) {
25 let feedback = [...document.getElementsByTagName('a')].find(e => e.innerText == 'Feedback')
26 feedback.setAttribute('href', "https://stevekrouse-docfeedbackform.web.val.run/?ref=" + ref)
30```
31
32Finally, you may be wondering why I queue up feedback in `@stevekrouse.docsFeedback`, a private JSON val, and then process it via [`@stevekrouse.formFeedbackAlert`](https://www.val.town/v/stevekrouse.formFeedbackAlert) instead of sending it along to Discord directly in this val. I [tried that originally](https://www.val.town/v/stevekrouse.docFeedbackForm?v=61) but it felt too slow to wait for the API call to Discord before returning the "Thanks for your feedback" message. This is where the `context.waitUntil` method (that Cloudflare workers and Vercel Edge Functions support) would really come in handy – those allow you to return a Response, and then continue to compute. Currently Val Town requires you to stop all compute with the returning of your Response, so the only way to compute afterwards is to queue it up for another val to take over, and that's what I'm doing here.
33
34
3The Javascript [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) is directly available within a Val. However sometimes fetch calls are blocked by the receiving server for using particular IP addresses. Additionally, network blips or unreliable web services may lead to failures if not handled properly.
4
5The Val Town standard library contains an alternative version, [`std/fetch`](https://www.val.town/v/std/fetch), that wraps the JavaScript Fetch API to provide additional functionality. The fetch function from [`std/fetch`](https://www.val.town/v/std/fetch) reroutes requests using a proxy vendor so that requests obtain different IP addresses. It also automatically retries failed requests several times. Note that using [`std/fetch`](https://www.val.town/v/std/fetch) will be significantly slower than directly calling the Javascript Fetch API due to extra network hops.
6
7## Usage
22
23```js
24function updateFeedback(ref) {
25 let feedback = [...document.getElementsByTagName('a')].find(e => e.innerText == 'Feedback')
26 feedback.setAttribute('href', "https://stevekrouse-docfeedbackform.web.val.run/?ref=" + ref)
30```
31
32Finally, you may be wondering why I queue up feedback in `@stevekrouse.docsFeedback`, a private JSON val, and then process it via [`@stevekrouse.formFeedbackAlert`](https://www.val.town/v/stevekrouse.formFeedbackAlert) instead of sending it along to Discord directly in this val. I [tried that originally](https://www.val.town/v/stevekrouse.docFeedbackForm?v=61) but it felt too slow to wait for the API call to Discord before returning the "Thanks for your feedback" message. This is where the `context.waitUntil` method (that Cloudflare workers and Vercel Edge Functions support) would really come in handy – those allow you to return a Response, and then continue to compute. Currently Val Town requires you to stop all compute with the returning of your Response, so the only way to compute afterwards is to queue it up for another val to take over, and that's what I'm doing here.
33
34
15There will be one migration per val, per version.
16
17You can do anything you want in the function passed to `migrate`; create or alter tables, populate data... whatever!
18
19## Migrating backwards
171. [Generate your keypair](https://www.val.town/embed/new?code=import+%7B+set+%7D+from+%22https%3A%2F%2Fesm.town%2Fv%2Fstd%2Fset%22%3B%0Aimport+%7B+generateKeys+%7D+from+%22https%3A%2F%2Fesm.town%2Fv%2Fstevekrouse%2FgenerateKeys%22%3B%0A%0Alet+keys+%3D+await+generateKeys%28%29%0A%0Aexport+let+exportedKeySetter+%3D+Promise.all%28%5B%0A++set%28%22exportedKeys%22%2C+keys%29%2C%0A++set%28%22publicKey%22%2C+keys.publicKey%29%0A%5D%29)
182. On [your publicKey](https://www.val.town/v/me/publicKey) click the lock icon🔒 to change the permissions to `Unlisted`.
193. [Fork this helper function](https://www.val.town/v/stevekrouse.turso/fork) replacing `stevekrouse` with your own username
204. [Try out some queries!](https://www.val.town/v/stevekrouse.tursoExamples/fork)
21
35## Architecture
36
37This @std.turso function is the client or SDK to @std.tursoAPI, which acts as a "proxy" to Turso. It handles authentication, creates databases, and forwards on your SQL queries. You can get lower latency (~200ms vs ~800ms), more storage, databases, CLI & API access by having your own Turso account.
38
39Migrated from folder: turso/turso
12
131. This val requires you to setup public key auth, which is really just three clicks here: https://blog.val.town/public-key-auth-val-town-users-can-be-your-users
142. [Optional] You can setup a helper function so you don't have to pass your keys every time:
15
16```js
18```
19
20Warning: Do **not** make your blob function public or unlisted. That would allow anyone to use it.
21
22## Usage
18```
19
20⚠️ For the notification to be sent it needs to be published (`publish` function).
21
22## Use helper
23
24Executes specified functions that can modify the notification. Can be used to streamline authentication, apply common operations, etc.
25
26```ts
40```
41
42You can pass it multiple functions.
43
44## Functions
45
46### `toServer(url)` - optional
120```
121
122Alternatively you can use dedicated functions: `.withMinPriority()`, `.withLowPriority()`, `.withDefaultPriority()`, `.withHighPriority()`, `.withMaxPriority()`
123
124```ts