Back to packages list

Vals using js-base64

Description from the NPM package:
Yet another Base64 transcoder in pure-JS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { parseArgs } from "jsr:@std/cli/parse-args";
import { Base64 } from "npm:js-base64";
import open from "npm:open";
function printHelp() {
console.log("help");
}
const { _, language, currency } = parseArgs(Deno.args, {
boolean: ["help"],
string: ["language", "currency"],
});
if (_.length == 0 || !language || !currency) {
printHelp();
Deno.exit(1);
}
const content = Deno.readTextFileSync(_[0]);
const url = new URL(`https://jsoninvoice.pomdtr.me/invoice/${Base64.encode(content)}`);
url.searchParams.set("language", language);
url.searchParams.set("currency", currency);
console.log(url.toString());
await open(url.toString());
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/** @jsxImportSource npm:hono/jsx */
import { Invoice } from "https://esm.town/v/pomdtr/invoice_schema";
import { Base64 } from "npm:js-base64";
const invoice: Invoice = {
id: "20240401",
issued: "2024-01-01",
due: "2024-01-15",
currencies: [{ code: "USD" }, { code: "EUR", rate: 0.94 }],
tax: 10,
from: {
name: "Achille Lacoin",
},
to: {
name: "Val Town",
},
items: [
{
title: {
"en-US": "Creation of JSON Invoice Website",
"fr-FR": "Création du site JSON Invoice",
},
quantity: 7,
price: 70,
},
],
};
const b64Invoice = Base64.encode(JSON.stringify(invoice));
export const HomePage = () => {
return (
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://cdn.tailwindcss.com"></script>
<title>JSON Invoice</title>
<link rel="icon" href="https://fav.farm/📃" />
<script
type="module"
src="https://raw.esm.sh/code-mirror-web-component@0.1.1/dist/json-editor.js"
></script>
</head>
<body>
<nav class="flex justify-between items-center p-4">
<h1 class="text-xl font-bold">JSON Invoice</h1>
</nav>
<div class="relative pt-6 pb-16 sm:pb-24">
<div class="px-4 mx-auto mt-16 max-w-7xl sm:mt-24 sm:px-6">
<div class="text-center">
<h1 class="text-4xl font-extrabold tracking-tight text-gray-900 sm:text-5xl md:text-6xl">
<span class="block text-amber-500">Invoice for Devs</span>
</h1>
<p class="max-w-md mx-auto mt-3 text-base text-gray-500 sm:text-lg md:mt-5 md:text-xl md:max-w-3xl">
An open file format for invoices, targeted toward solo
freelancers.
</p>
</div>
<div class="flex justify-center mt-6 gap-x-5">
<div class="flex flex-col items-center">
<span class="inline-flex rounded-md shadow ">
<a
class="inline-flex items-center px-4 py-2 font-medium text-xl bg-amber-500 hover:bg-amber-400 border border-transparent rounded-lg text-white w-[200px] h-[54px] justify-center"
href="/editor"
>
Open Playground
</a>
</span>
</div>
<div class="flex flex-col items-center">
<span class="inline-flex rounded-md shadow ">
<a
class="inline-flex items-center px-4 py-2 font-medium text-xl bg-gray-100 hover:bg-gray-200 border border-transparent rounded-lg w-[200px] h-[54px] justify-center"
href="/TODO"
>
View Specs
</a>
</span>
</div>
</div>
<div class="flex justify-center mt-24">
<image
class="rounded-lg border w-3/4"
src="https://imagedelivery.net/iHX6Ovru0O7AjmyT5yZRoA/a35e64a5-e0e7-48f1-31b9-f1b77edb4900/public"
/>
</div>
</div>
</div>
</body>
</html>
);
};

HTTP Client

Attach a postman-like http client to your vals, with bookmarks and history support

2fa974c630d393c3b4f419de8a142a2f35a11ceebda42d195b07623889e5604e.png

Usage

Wrap your http handler in an the httpClient middleware.

Create valimport {httpClient} from "https://esm.town/v/pomdtr/http_client" export default httpClient((req) => { return new Response("Hello World!") })

Go to https://<author>-<name>.web.val.run/http-client to view the http client UI.

Adding bookmarks

You might want to bookmark some requests you need often. You can do it by passing a bookmark list as a middleware option:

Create valimport {httpClient} from "https://esm.town/v/pomdtr/http_client" export default httpClient((req) => { return new Response("Hello World!") }, { bookmarks: [ { "label": "Dummy Request", "request": new Request("https://dummyjson.com/products") } ]})

ad7b95611871eb090ddadfa48f73d14692280e5389db1592403053d311aaa623.png

Customizing the client path

Create valimport {httpClient} from "https://esm.town/v/pomdtr/http_client" export default httpClient((req) => { return new Response("Hello World!") }, { path: "/" // show the http client on the website root })

TODO

  • fix syntax highlighting on successive request
  • allow to prefill the initial request
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/** @jsxImportSource npm:preact **/
import { html } from "https://esm.town/v/stevekrouse/html?v=5";
import { Base64 } from "npm:js-base64";
import { render } from "npm:preact-render-to-string";
type SerializedRequest = {
url: string;
method: string;
headers: [string, string][];
body: string | undefined;
};
type Bookmark = {
label: string;
request: Request;
};
async function serializeRequest(request: Request): Promise<SerializedRequest> {
return {
url: request.url,
method: request.method,
headers: Array.from(request.headers.entries()),
body: request.body ? Base64.encode(await request.text()) : undefined,
};
}
const getBody = async (bookmarks: Bookmark[]) => {
const serializedBookmarks = JSON.stringify(
await Promise.all(bookmarks.map(async (bookmark) => ({
label: bookmark.label,
request: await serializeRequest(bookmark.request),
}))),
);
return render(
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>REST Client</title>
<link rel="icon" href="https://fav.farm/🌐" />
<script src="https://cdn.tailwindcss.com"></script>
<script
type="module"
src="https://cdn.jsdelivr.net/npm/code-mirror-web-component@0.0.20/dist/code-mirror.js"
>
</script>
</head>
<body class="bg-orange-100">
<div id="app" data-bookmarks={serializedBookmarks}></div>
<script type="module" src="https://esm.town/v/pomdtr/http_client_component"></script>
</body>
</html>,
);
};
export function httpClient(next: (req: Request) => Response | Promise<Response>, options: {
path: string;
bookmarks: Bookmark[];
} = { path: "/http-client", bookmarks: [] }) {
return async (req: Request) => {
const url = new URL(req.url);
if (url.pathname == options.path) {
return html(await getBody(options.bookmarks));
}
return next(req);
};
}
export default async function(req: Request) {
const bookmarks = [{ label: "Dummy Request", request: new Request("https://dummyjson.com/products") }];
return html(await getBody(bookmarks));
}

HTTP Client

Attach a postman-like http client to your vals, with bookmarks and history support

2fa974c630d393c3b4f419de8a142a2f35a11ceebda42d195b07623889e5604e.png

Usage

Wrap your http handler in an the httpClient middleware.

Create valimport {httpClient} from "https://esm.town/v/pomdtr/http_client" export default httpClient((req) => { return new Response("Hello World!") })

The http client will be shown on the root.

Adding bookmarks

You might want to bookmark some requests you need often. You can do it by passing a bookmark list as a middleware option:

Create valimport {httpClient} from "https://esm.town/v/pomdtr/http_client" export default httpClient((req) => { return new Response("Hello World!") }, { bookmarks: [ { "label": "Dummy Request", "request": new Request("https://dummyjson.com/products") } ]})

ad7b95611871eb090ddadfa48f73d14692280e5389db1592403053d311aaa623.png

Customizing the client path

Create valimport {httpClient} from "https://esm.town/v/pomdtr/http_client" export default httpClient((req) => { return new Response("Hello World!") }, { path: "/http-client" })

TODO

  • fix syntax highlighting on successive request
  • allow to prefill the initial request
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/** @jsxImportSource npm:preact **/
import { html } from "https://esm.town/v/stevekrouse/html?v=5";
import { Base64 } from "npm:js-base64";
import { render } from "npm:preact-render-to-string";
type SerializedRequest = {
url: string;
method: string;
headers: [string, string][];
body: string | undefined;
};
type Bookmark = {
label: string;
request: Request;
};
async function serializeRequest(request: Request): Promise<SerializedRequest> {
return {
url: request.url,
method: request.method,
headers: Array.from(request.headers.entries()),
body: request.body ? Base64.encode(await request.text()) : undefined,
};
}
const getBody = async (bookmarks: Bookmark[]) => {
const serializedBookmarks = JSON.stringify(
await Promise.all(bookmarks.map(async (bookmark) => ({
label: bookmark.label,
request: await serializeRequest(bookmark.request),
}))),
);
return render(
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>REST Client</title>
<link rel="icon" href="https://fav.farm/🌐" />
<script src="https://cdn.tailwindcss.com"></script>
<script
type="module"
src="https://cdn.jsdelivr.net/npm/code-mirror-web-component@0.0.20/dist/code-mirror.js"
>
</script>
</head>
<body class="bg-orange-100">
<div id="app" data-bookmarks={serializedBookmarks}></div>
<script type="module" src="https://esm.town/v/pomdtr/http_client_component"></script>
</body>
</html>,
);
};
export function httpClient(next: (req: Request) => Response | Promise<Response>, options: {
path: string;
bookmarks: Bookmark[];
} = { path: "/", bookmarks: [] }) {
return async (req: Request) => {
const url = new URL(req.url);
if (url.pathname == options.path) {
return html(await getBody(options.bookmarks));
}
return next(req);
};
}
export default async function(req: Request) {
const bookmarks = [{ label: "Dummy Request", request: new Request("https://dummyjson.com/products") }];
return html(await getBody(bookmarks));
}

This val is supposed to be used with the val.town extension. See the extension readme for installation instructions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { Base64 } from "npm:js-base64@3.7.5";
import { BrowserContext } from "https://esm.town/v/pomdtr/browser";
import { extractValInfo } from "https://esm.town/v/pomdtr/extractValInfo";
export default async function(ctx: BrowserContext<{ author: string; name: string }>) {
const { author, name } = ctx.params;
const title = `@${author}/${name}`;
const resp = await fetch(`https://api.val.town/v1/alias/${author}/${name}`, {
headers: {
Authorization: `Bearer ${Deno.env.get("valtown")}`,
},
});
if (!resp.ok) {
throw new Error(await resp.text());
}
const { code } = await resp.json();
const hash = `#language=tsx&title=${title}&code=${Base64.encodeURI(code)}`;
return { type: "open", url: `https://ray.so${hash}` };
}
// #web
1
Next