about summary refs log tree commit diff
path: root/src/state/queries/shorten-link.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/state/queries/shorten-link.ts')
-rw-r--r--src/state/queries/shorten-link.ts23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/state/queries/shorten-link.ts b/src/state/queries/shorten-link.ts
new file mode 100644
index 000000000..76c63c356
--- /dev/null
+++ b/src/state/queries/shorten-link.ts
@@ -0,0 +1,23 @@
+import {logger} from '#/logger'
+
+export function useShortenLink() {
+  return async (inputUrl: string): Promise<{url: string}> => {
+    const url = new URL(inputUrl)
+    const res = await fetch('https://go.bsky.app/link', {
+      method: 'POST',
+      body: JSON.stringify({
+        path: url.pathname,
+      }),
+      headers: {
+        'Content-Type': 'application/json',
+      },
+    })
+
+    if (!res.ok) {
+      logger.error('Failed to shorten link', {safeMessage: res.status})
+      return {url: inputUrl}
+    }
+
+    return res.json()
+  }
+}