aboutsummaryrefslogtreecommitdiff
path: root/client/src/actions
diff options
context:
space:
mode:
authorMelonai <einebeere@gmail.com>2021-01-20 23:18:09 +0100
committerMelonai <einebeere@gmail.com>2021-01-20 23:18:09 +0100
commit826c7c47785ee01d2b9267919132ada696425344 (patch)
tree901cc90be9a953a6c3f968b6c1abe33cc13774b4 /client/src/actions
parent2953dec527cedaabaa5f0eb48637c5ddd4a4103b (diff)
downloadshorest-826c7c47785ee01d2b9267919132ada696425344.tar.zst
shorest-826c7c47785ee01d2b9267919132ada696425344.zip
Remade the client in SvelteKit
Diffstat (limited to 'client/src/actions')
-rw-r--r--client/src/actions/shorten.ts50
1 files changed, 50 insertions, 0 deletions
diff --git a/client/src/actions/shorten.ts b/client/src/actions/shorten.ts
new file mode 100644
index 0000000..58dd4f0
--- /dev/null
+++ b/client/src/actions/shorten.ts
@@ -0,0 +1,50 @@
+interface ShortenResponse {
+ hash: string;
+}
+
+export interface ShortenRequest {
+ url: string;
+ nonce: string;
+ response: Promise<ShortenResponse>;
+}
+
+async function makeRequest(url: string): Promise<ShortenResponse> {
+ let body;
+
+ try {
+ const response = await fetch("http://localhost:4000/", {
+ 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),
+ };
+}