about summary refs log tree commit diff
path: root/src/state/queries/shorten-link.ts
blob: 76c63c3569e49862702963ae5605ff16a8e708d8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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()
  }
}