From e65a8cfbf13a1ddf1a9aa7bde5438fc2f4b70b51 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Mon, 1 Aug 2022 13:27:45 -0700 Subject: check username exists --- src/lib/utils.js | 8 +++++++- src/routes/[username]/index.js | 13 ++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/lib/utils.js b/src/lib/utils.js index 7831c519..38f2a010 100644 --- a/src/lib/utils.js +++ b/src/lib/utils.js @@ -9,7 +9,13 @@ const base = browser ? '' : import.meta.env.VITE_COINOS_URL; export const get = (url) => fetch(base + url, { headers: { accept: 'application/json' } - }).then((r) => r.json()); + }).then((r) => r.text()).then((body) => { + try { + return JSON.parse(body); + } catch(e) { + throw Error(body); + } + }) export const post = (url, body) => fetch(base + url, { diff --git a/src/routes/[username]/index.js b/src/routes/[username]/index.js index 01613574..5229cc50 100644 --- a/src/routes/[username]/index.js +++ b/src/routes/[username]/index.js @@ -1,3 +1,14 @@ +import { get } from '$lib/utils'; + export async function GET({ params }) { - return { body: params }; + let { username } = params; + let body; + + try { + body = await get(`/users/${username}`); + } catch(e) { + body = {}; + } + + return { body }; } -- cgit v1.2.3