From 1bef54163cd8c20bfd00d9beac1c7bf7b7b055b9 Mon Sep 17 00:00:00 2001 From: infra <77526490+i-infra@users.noreply.github.com> Date: Sat, 29 Apr 2023 20:43:20 -0400 Subject: [PATCH] Simple web UI for shortlink creation (#15) * add HTML interface (inlined into typescript) - with CSS and autocomplete hand-holding * Some changes (listed below) - Make UI optional and disabled by default - Lint it in line with other code - Import UI from a bundled HTML file - Add UI to README - Add support for random URL generation - Show the URL in the output --------- Co-authored-by: Erisa A --- .prettierrc | 10 ++++- README.md | 6 +++ package.json | 2 +- src/form.html | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/html.d.ts | 4 ++ src/index.ts | 8 ++++ wrangler.toml | 3 ++ 7 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 src/form.html create mode 100644 src/html.d.ts diff --git a/.prettierrc b/.prettierrc index 233afa3..12e87cd 100644 --- a/.prettierrc +++ b/.prettierrc @@ -3,5 +3,13 @@ "semi": false, "trailingComma": "all", "useTabs": true, - "printWidth": 80 + "printWidth": 80, + "overrides": [ + { + "files": "src/*.html", + "options": { + "printWidth": 140 + } + } + ] } diff --git a/README.md b/README.md index 7a70e8e..7ef7cfc 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,12 @@ For debuggging, you can use `yarn dev` (Which runs `wrangler dev --local`). To c You can also debug on the edge with `wrangler dev`, though you will need to first configure a prepview namespace in `wrangler.toml` and add the `WORKERLINKS_SECRET` secret to the Worker. +## (Optional) User Interface + +If `ENABLE_INDEX_FORM` is enabled in `wrangler.toml`, an optional UI form is available when visiting the Worker in a browser, allowing easy creation of links: + +![](https://up.erisa.uk/firefox_qFWwv7NIqf.png) + ## Usage Once deployed, interacting with the API should be rather simple. It's based on headers, specifically with the `Authorization` and `URL` headers. diff --git a/package.json b/package.json index 7f45a5c..1891a4c 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "author": "Erisa A", "license": "MIT", "scripts": { - "format": "prettier --write '**/*.{ts,js,css,json,md}'", + "format": "prettier --write '**/*.{ts,js,css,json,md,html}'", "deploy": "wrangler publish", "dev": "wrangler dev", "addsecret": "wrangler secret put WORKERLINKS_SECRET", diff --git a/src/form.html b/src/form.html new file mode 100644 index 0000000..cd72886 --- /dev/null +++ b/src/form.html @@ -0,0 +1,106 @@ + + + + Shorten a URL + + + + + +
+ +

+ +

+ +

+

+
+
+ +
+ + diff --git a/src/html.d.ts b/src/html.d.ts new file mode 100644 index 0000000..c25ff28 --- /dev/null +++ b/src/html.d.ts @@ -0,0 +1,4 @@ +declare module '*.html' { + const value: string + export default value +} diff --git a/src/index.ts b/src/index.ts index 02baf19..d8adb8a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,9 @@ import { Context as HonoContext, Env as HonoEnv, Hono } from 'hono' import * as st from 'simple-runtypes' +// html.d.ts tells typescript that this is a normal thing to do +import creationPageHtml from './form.html' + type Variables = { path: string key: string @@ -12,6 +15,7 @@ type Bindings = { PLAUSIBLE_HOST?: string KV: KVNamespace kv: KVNamespace + ENABLE_INDEX_FORM: boolean } const url = st.runtype((v) => { @@ -107,6 +111,10 @@ app.get('/', async (c) => { })), }) } else { + const urlResult = await c.env.KV.get(c.get('key')) + if (urlResult == null && c.env.ENABLE_INDEX_FORM) { + return c.html(creationPageHtml) + } return handleGetHead(c) } }) diff --git a/wrangler.toml b/wrangler.toml index f356aaf..606c3f0 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -14,3 +14,6 @@ compatibility_date = "2023-03-05" # Remove or comment out the route line if using workers_dev workers_dev = false route = { pattern = "erisa.link/*", zone_name = "erisa.link" } + +[vars] +ENABLE_INDEX_FORM = true