Skip to content

Commit

Permalink
handle error if use wrong env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
saenyakorn committed Dec 4, 2022
1 parent 8246b5e commit 9588695
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 11 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
},
"dependencies": {
"@iconify/svelte": "^3.0.1",
"axios": "^1.2.0",
"clsx": "^1.2.1",
"crypto-js": "^4.1.1"
}
Expand Down
63 changes: 63 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 13 additions & 5 deletions src/lib/ShortURL.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { getShortLink } from '../utils/shortLink'
import Button from './Button.svelte'
import CopyToClipboard from './CopyToClipboard.svelte'
import { AxiosError } from 'axios'
interface $$Props {
campaign: Campaign
Expand Down Expand Up @@ -58,12 +59,19 @@
}
async function generateShortUrl(): Promise<void> {
const fullUrl = generateFullUrl(campaign)
if (!fullUrl) {
return
try {
const fullUrl = generateFullUrl(campaign)
if (!fullUrl) {
return
}
const shortLink = await getShortLink(fullUrl)
shortUrl = shortLink
} catch (err) {
if (err instanceof AxiosError) {
console.error(err.response?.data.error)
alert(err.response?.data.error)
}
}
const shortLink = await getShortLink(fullUrl)
shortUrl = shortLink
}
$: fullUrl = generateFullUrl(campaign)
Expand Down
11 changes: 5 additions & 6 deletions src/utils/shortLink.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import axios from 'axios'

export async function getShortLink(url: string): Promise<string> {
const data = {
const body = {
domain: import.meta.env.VITE_SHORT_IO_DOMAIN,
originalURL: url,
}

const response = await fetch('https://api.short.io/links/public', {
method: 'post',
const { data } = await axios.post('https://api.short.io/links/public', body, {
headers: {
accept: 'application/json',
'Content-Type': 'application/json',
authorization: import.meta.env.VITE_SHORT_IO_PUBLIC_KEY,
},
body: JSON.stringify(data),
})

const json = await response.json()
return json['shortURL']
return data['shortURL']
}

0 comments on commit 9588695

Please sign in to comment.