Skip to content

Commit

Permalink
add title of short link
Browse files Browse the repository at this point in the history
  • Loading branch information
saenyakorn committed Feb 24, 2023
1 parent 647e922 commit 5748dc1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
19 changes: 15 additions & 4 deletions src/lib/ShortURL.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import Input from '$lib/Input.svelte'
import Input from '$lib/Input.svelte'
import type { Campaign } from '../types'
import { getShortLink } from '../utils/shortLink'
import Button from './Button.svelte'
Expand All @@ -11,6 +11,7 @@
}
export let campaign: Campaign
let shortUrl = ''
function validateInput(campaign: Campaign) {
Expand Down Expand Up @@ -59,14 +60,18 @@
return url.toString()
}
let title = ''
let customPath = ''
async function generateShortUrl(): Promise<void> {
try {
const fullUrl = generateFullUrl(campaign)
if (!fullUrl) {
return
}
const shortLink = await getShortLink(fullUrl, customPath)
const shortLink = await getShortLink(fullUrl, {
title: title,
path: customPath,
})
shortUrl = shortLink
} catch (err) {
if (err instanceof AxiosError) {
Expand All @@ -77,8 +82,6 @@
}
$: fullUrl = generateFullUrl(campaign)
</script>

<div class="flex flex-col gap-2 w-full">
Expand All @@ -93,6 +96,14 @@

<Button type="button" onClick={generateShortUrl} label="Generate Short Link" />

<Input
bind:value={title}
id="title"
label="Title"
placeholder="title..."
helperText="Title of created URL to be shown in short.io admin panel"
required
/>
<Input
bind:value={customPath}
id="customPath"
Expand Down
10 changes: 8 additions & 2 deletions src/utils/shortLink.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import axios from 'axios'

export async function getShortLink(url: string, customPath?: string): Promise<string> {
interface Options {
path?: string
title?: string
}

export async function getShortLink(url: string, options?: Options): Promise<string> {
const body = {
domain: import.meta.env.VITE_SHORT_IO_DOMAIN,
originalURL: url,
path: customPath,
path: !options?.path ? undefined : options.path,
title: !options?.title ? undefined : options.title,
}

const { data } = await axios.post('https://api.short.io/links/public', body, {
Expand Down

0 comments on commit 5748dc1

Please sign in to comment.