4import { currency } from "https://esm.town/v/stevekrouse/currency";
5
6export async function btcPriceAlert() {
7 const lastBtcPrice: number = await blob.getJSON("lastBtcPrice");
8 let btcPrice = await currency("usd", "btc");
1export default async function (req: Request): Promise<Response> {
2 return Response.json({ ok: true })
3}
31
32// ------------
33// Functions
34// ------------
35
36async function execute(statement: InStatement, args?: InArgs): Promise<ResultSet> {
37 const res = await fetch(`${API_URL}/v1/sqlite/execute`, {
38 method: "POST",
49}
50
51async function batch(statements: InStatement[], mode?: TransactionMode): Promise<ResultSet[]> {
52 const res = await fetch(`${API_URL}/v1/sqlite/batch`, {
53 method: "POST",
64}
65
66function createResError(body: string) {
67 try {
68 const e = zLibsqlError.parse(JSON.parse(body));
85}
86
87function normalizeStatement(statement: InStatement, args?: InArgs) {
88 if (Array.isArray(statement)) {
89 // for the case of an array of arrays
107}
108
109function upgradeResultSet(results: ImpoverishedResultSet): ResultSet {
110 return {
111 ...results,
116// adapted from
117// https://github.com/tursodatabase/libsql-client-ts/blob/17dd996b840c950dd22b871adfe4ba0eb4a5ead3/packages/libsql-client/src/sqlite3.ts#L314C1-L337C2
118function rowFromSql(
119 sqlRow: Array<unknown>,
120 columns: Array<string>,
4import { blob } from "https://esm.town/v/std/blob";
5
6function App() {
7 const [imageData, setImageData] = useState<string | null>(null);
8 const [error, setError] = useState<string | null>(null);
9
10 useEffect(() => {
11 async function fetchImage() {
12 try {
13 const response = await fetch("/image");
47}
48
49function client() {
50 createRoot(document.getElementById("root")!).render(<App />);
51}
55}
56
57export default async function server(request: Request): Promise<Response> {
58 if (request.url.endsWith("/image")) {
59 try {
27}
28
29function App() {
30 console.log("Rendering App component");
31 const [categories, setCategories] = useState([]);
229}
230
231function client() {
232 try {
233 console.log("Starting client-side rendering");
251if (typeof document !== "undefined") { setTimeout(client, 0); }
252
253export default async function server(request: Request): Promise<Response> {
254 const { sqlite } = await import("https://esm.town/v/stevekrouse/sqlite");
255 const SCHEMA_VERSION = 1
284 <body>
285 <div id="root"></div>
286 <script>window.onerror = function(message, source, lineno, colno, error) { console.error("Global error:", message, source, lineno, colno, error); };</script>
287 <script src="https://esm.town/v/std/catch"></script>
288 <script type="module" src="${import.meta.url}"></script>
4import { blob } from "https://esm.town/v/std/blob";
5
6function App() {
7 const [imageData, setImageData] = useState<string | null>(null);
8 const [error, setError] = useState<string | null>(null);
9
10 useEffect(() => {
11 async function fetchImage() {
12 try {
13 const response = await fetch("/image");
49}
50
51function client() {
52 createRoot(document.getElementById("root")!).render(<App />);
53}
57}
58
59export default async function server(request: Request): Promise<Response> {
60 if (request.url.endsWith("/image")) {
61 try {
23
24 - The client-side React component makes a fetch request to "/image".
25 - This request is handled by our server function.
26
27
282. Server-side handling:
29
30 - The server function calls `blob.getJSON("image-test")`.
31 - This `blob.getJSON()` method makes an HTTP request to the Val Town API.
32 - The API returns a Response object containing the JSON data.
3import { email } from "https://esm.town/v/std/email";
4
5async function fetchStories(type: string, count: number) {
6 const response = await fetch(`https://hacker-news.firebaseio.com/v0/${type}stories.json`);
7 const storyIds = await response.json();
15}
16
17function createStoryHTML(story: any) {
18 return `
19 <li>
28}
29
30function createEmailContent(
31 topStories: any[],
32 newStories: any[],
118}
119
120export default async function server(req: Request) {
121 try {
122 const topStories = await fetchStories("top", 10);
1const cache = await caches.open("valtown")
2
3export default async function(req: Request) {
4 const cached = await cache.match(req)
5 if (cached) {
12 * @param {number} [opts.timeout=10 minutes] - The maximum amount of time (in milliseconds) the client will wait for a response before timing out.
13 * @param {number} [opts.httpAgent] - An HTTP agent used to manage HTTP(s) connections.
14 * @param {Core.Fetch} [opts.fetch] - Specify a custom `fetch` function implementation.
15 * @param {number} [opts.maxRetries=2] - The maximum number of times the client will retry a request.
16 * @param {Core.Headers} opts.defaultHeaders - Default headers to include with every request to the API.