Implement link sharing (#213)

* Implement link sharing

* Share default

* Compress and base64 encode data

* Better error messages

* Fix build

* Only change version when it's different
This commit is contained in:
Misode
2022-03-19 19:26:39 +01:00
committed by GitHub
parent 03e9c53d70
commit a5a08fc935
10 changed files with 212 additions and 14 deletions

View File

@@ -0,0 +1,58 @@
import lz from 'lz-string'
import config from '../../config.json'
import type { VersionId } from './Schemas'
const API_PREFIX = 'https://z15g7can.directus.app/items'
export const SHARE_KEY = 'share'
const ShareCache = new Map<string, string>()
export async function shareSnippet(type: string, version: VersionId, jsonData: any) {
try {
const data = lz.compressToBase64(JSON.stringify(jsonData))
const raw = btoa(JSON.stringify(jsonData))
console.log('Compression rate', raw.length / data.length)
const body = JSON.stringify({ data, type, version })
let id = ShareCache.get(body)
if (!id) {
const snippet = await fetchApi('/snippets', body)
ShareCache.set(body, snippet.id)
id = snippet.id as string
}
const gen = config.generators.find(g => g.id === type)!
return `${location.protocol}//${location.host}/${gen.url}/?${SHARE_KEY}=${id}`
} catch (e) {
if (e instanceof Error) {
e.message = `Error creating share link: ${e.message}`
}
throw e
}
}
export async function getSnippet(id: string) {
try {
const snippet = await fetchApi(`/snippets/${id}`)
return {
...snippet,
data: JSON.parse(lz.decompressFromBase64(snippet.data) ?? '{}'),
}
} catch (e) {
if (e instanceof Error) {
e.message = `Error loading shared content: ${e.message}`
}
throw e
}
}
async function fetchApi(url: string, body?: string) {
const res = await fetch(API_PREFIX + url, body ? {
method: 'post',
headers: { 'Content-Type': 'application/json' },
body,
} : undefined)
const data = await res.json()
if (data.data) {
return data.data
}
throw new Error(data.errors?.[0]?.message ?? 'Unknown error')
}

View File

@@ -1,3 +1,4 @@
export * from './Changelogs'
export * from './DataFetcher'
export * from './Schemas'
export * from './Sharing'