From 42860fa15985401825d8d51e73ec497fe5876710 Mon Sep 17 00:00:00 2001 From: Melonai Date: Wed, 24 Mar 2021 09:51:36 +0100 Subject: Update SvelteKit to public beta --- client/src/lib/actions/shorten.ts | 50 ++++++++++++++++++ client/src/lib/components/Form.svelte | 64 ++++++++++++++++++++++++ client/src/lib/components/Response.svelte | 33 ++++++++++++ client/src/lib/components/Responses.svelte | 24 +++++++++ client/src/lib/components/Title.svelte | 22 ++++++++ client/src/lib/components/icons/ArrowIcon.svelte | 10 ++++ client/src/lib/components/icons/CrossIcon.svelte | 10 ++++ client/src/lib/data/links.ts | 18 +++++++ client/src/lib/utils/addProtocol.ts | 6 +++ client/src/lib/utils/checkUrl.ts | 10 ++++ client/src/lib/utils/debounce.ts | 12 +++++ 11 files changed, 259 insertions(+) create mode 100644 client/src/lib/actions/shorten.ts create mode 100644 client/src/lib/components/Form.svelte create mode 100644 client/src/lib/components/Response.svelte create mode 100644 client/src/lib/components/Responses.svelte create mode 100644 client/src/lib/components/Title.svelte create mode 100644 client/src/lib/components/icons/ArrowIcon.svelte create mode 100644 client/src/lib/components/icons/CrossIcon.svelte create mode 100644 client/src/lib/data/links.ts create mode 100644 client/src/lib/utils/addProtocol.ts create mode 100644 client/src/lib/utils/checkUrl.ts create mode 100644 client/src/lib/utils/debounce.ts (limited to 'client/src/lib') diff --git a/client/src/lib/actions/shorten.ts b/client/src/lib/actions/shorten.ts new file mode 100644 index 0000000..ca685c5 --- /dev/null +++ b/client/src/lib/actions/shorten.ts @@ -0,0 +1,50 @@ +interface ShortenResponse { + hash: string; +} + +export interface ShortenRequest { + url: string; + nonce: string; + response: Promise; +} + +async function makeRequest(url: string): Promise { + let body; + + try { + const response = await fetch("/", { + headers: { + Accept: "application/json", + "Content-Type": "application/json", + }, + method: "post", + body: JSON.stringify({ url }), + }); + + body = await response.json(); + } catch (err) { + throw { + error: "Error!", + }; + } + + if (body.hash) { + return { + hash: body.hash, + }; + } else { + throw { + message: body.error || "Error!", + }; + } +} + +export default function shorten(url: string): ShortenRequest { + const nonce = Math.random().toString(36).substr(2, 5); + + return { + url, + nonce, + response: makeRequest(url), + }; +} diff --git a/client/src/lib/components/Form.svelte b/client/src/lib/components/Form.svelte new file mode 100644 index 0000000..a05e868 --- /dev/null +++ b/client/src/lib/components/Form.svelte @@ -0,0 +1,64 @@ + + + + +
+ + +
diff --git a/client/src/lib/components/Response.svelte b/client/src/lib/components/Response.svelte new file mode 100644 index 0000000..0ed8cc9 --- /dev/null +++ b/client/src/lib/components/Response.svelte @@ -0,0 +1,33 @@ + + + + +
+ {info.url} + {#await info.response} + Loading... + {:then { hash }} + sho.rest/{hash} + {:catch { error }} + {error} + {/await} +
\ No newline at end of file diff --git a/client/src/lib/components/Responses.svelte b/client/src/lib/components/Responses.svelte new file mode 100644 index 0000000..6e83658 --- /dev/null +++ b/client/src/lib/components/Responses.svelte @@ -0,0 +1,24 @@ + + + + +
    + {#each $links as info (info.nonce)} +
  • + +
  • + {/each} +
\ No newline at end of file diff --git a/client/src/lib/components/Title.svelte b/client/src/lib/components/Title.svelte new file mode 100644 index 0000000..4266eb1 --- /dev/null +++ b/client/src/lib/components/Title.svelte @@ -0,0 +1,22 @@ + + +
+ +
+

sho.rest

+

Made with ❤ by Mel

+
+
diff --git a/client/src/lib/components/icons/ArrowIcon.svelte b/client/src/lib/components/icons/ArrowIcon.svelte new file mode 100644 index 0000000..52c79ae --- /dev/null +++ b/client/src/lib/components/icons/ArrowIcon.svelte @@ -0,0 +1,10 @@ + + + + + \ No newline at end of file diff --git a/client/src/lib/components/icons/CrossIcon.svelte b/client/src/lib/components/icons/CrossIcon.svelte new file mode 100644 index 0000000..55525d8 --- /dev/null +++ b/client/src/lib/components/icons/CrossIcon.svelte @@ -0,0 +1,10 @@ + + + + + \ No newline at end of file diff --git a/client/src/lib/data/links.ts b/client/src/lib/data/links.ts new file mode 100644 index 0000000..0f0a9ce --- /dev/null +++ b/client/src/lib/data/links.ts @@ -0,0 +1,18 @@ +import type { ShortenRequest } from "$lib/actions/shorten"; +import type { Writable } from "svelte/store"; +import { writable } from "svelte/store"; + +function createLinks() { + const { subscribe, update }: Writable = writable([]); + + function add(request: ShortenRequest) { + update((l) => [request, ...l.slice(0, 2)]); + } + + return { + subscribe, + add, + }; +} + +export const links = createLinks(); diff --git a/client/src/lib/utils/addProtocol.ts b/client/src/lib/utils/addProtocol.ts new file mode 100644 index 0000000..75c6214 --- /dev/null +++ b/client/src/lib/utils/addProtocol.ts @@ -0,0 +1,6 @@ +export default function (url: string) { + if (!/^https?:\/\//.test(url)) { + url = "https://" + url; + } + return url; +} diff --git a/client/src/lib/utils/checkUrl.ts b/client/src/lib/utils/checkUrl.ts new file mode 100644 index 0000000..8ed747f --- /dev/null +++ b/client/src/lib/utils/checkUrl.ts @@ -0,0 +1,10 @@ +import addProtocol from "./addProtocol"; + +export default function (url: string): string | null { + try { + const normalizedUrl = new URL(addProtocol(url)); + return normalizedUrl.toString(); + } catch (e) { + return null; + } +} \ No newline at end of file diff --git a/client/src/lib/utils/debounce.ts b/client/src/lib/utils/debounce.ts new file mode 100644 index 0000000..86ef3db --- /dev/null +++ b/client/src/lib/utils/debounce.ts @@ -0,0 +1,12 @@ +type Procedure = (...args: any[]) => any; + +export default function (f: F, duration: number) { + let timeout: ReturnType | null = null; + return function (...args: Parameters) { + if (timeout !== null) { + clearTimeout(timeout); + timeout = null; + } + timeout = setTimeout(() => f(...args), duration); + }; +} -- cgit 1.4.1