From 332fead7c7ff3387e7cf260dc3d4e27aac570508 Mon Sep 17 00:00:00 2001 From: Nick Kosarev Date: Sun, 23 Jun 2024 14:23:10 +0000 Subject: [PATCH] feat: getting data by wrapper, not direct fetch --- .env.example | 1 + package.json | 1 + src/lib/server/api.ts | 4 +++ src/routes/[lang]/(website)/+page.server.ts | 11 ++++-- .../[lang]/(website)/auth/sign-in/+server.ts | 20 ++++++----- .../(website)/character/+page.server.ts | 7 ++-- .../(website)/character/[id]/+page.server.ts | 7 ++-- .../(website)/p/[userName]/+page.server.ts | 7 ++-- yarn.lock | 36 +++++++++++++++++-- 9 files changed, 72 insertions(+), 22 deletions(-) create mode 100644 src/lib/server/api.ts diff --git a/.env.example b/.env.example index 12792878..a8d41a44 100644 --- a/.env.example +++ b/.env.example @@ -9,6 +9,7 @@ PUBLIC_COOKIE_KEY="" # Our secret... PRIVATE_JWT_SECRET_KEY="" +PRIVATE_WEBSITE_BEARER="" # WebSocket server with event messages PUBLIC_WEBSOCKET_URL="" diff --git a/package.json b/package.json index d87da444..24329de3 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "prepare": "husky" }, "dependencies": { + "@hmbanan666/chat-game-api": "^0.1.5", "@paralleldrive/cuid2": "^2.2.2", "@radix-ui/colors": "^3.0.0", "@twurple/api": "^7.1.0", diff --git a/src/lib/server/api.ts b/src/lib/server/api.ts new file mode 100644 index 00000000..318db17b --- /dev/null +++ b/src/lib/server/api.ts @@ -0,0 +1,4 @@ +import { ChatGameAPI } from '@hmbanan666/chat-game-api' +import { env } from '$env/dynamic/private' + +export const api = new ChatGameAPI(env.PRIVATE_WEBSITE_BEARER ?? '') diff --git a/src/routes/[lang]/(website)/+page.server.ts b/src/routes/[lang]/(website)/+page.server.ts index a4bb6082..f6d57609 100644 --- a/src/routes/[lang]/(website)/+page.server.ts +++ b/src/routes/[lang]/(website)/+page.server.ts @@ -1,13 +1,18 @@ import type { PageServerLoad } from './$types' +import { api } from '$lib/server/api' export const prerender = false export const ssr = true export const load = (async () => { - const res = await fetch('https://chatgame.space/api/profile') - const profile = await res.json() + const profileInfo = await api.profile.getInfo() + if (profileInfo instanceof Error) { + return { + count: 0, + } + } return { - count: profile.count, + count: profileInfo.count, } }) satisfies PageServerLoad diff --git a/src/routes/[lang]/(website)/auth/sign-in/+server.ts b/src/routes/[lang]/(website)/auth/sign-in/+server.ts index 5d3e401c..9d09e53a 100644 --- a/src/routes/[lang]/(website)/auth/sign-in/+server.ts +++ b/src/routes/[lang]/(website)/auth/sign-in/+server.ts @@ -6,17 +6,21 @@ import type { RequestHandler } from './$types' import type { Profile } from '$lib/types' import { env as publicEnv } from '$env/dynamic/public' import { env as privateEnv } from '$env/dynamic/private' +import { api } from '$lib/server/api' -async function findOrCreateProfile({ twitchId, userName }: Pick): Promise { - const res = await fetch(`https://chatgame.space/api/profile/twitchId/${twitchId}`) - const profile = await res.json() +async function findOrCreateProfile({ twitchId, userName }: Pick) { + const profile = await api.profile.getByTwitchId(twitchId) + if (profile instanceof Error) { + throw profile + } if (!profile) { - const res = await fetch('https://chatgame.space/api/profile', { - method: 'POST', - body: JSON.stringify({ twitchId, userName }), - }) - return res.json() + const newProfile = await api.profile.create({ data: { twitchId, userName } }) + if (newProfile instanceof Error) { + throw newProfile + } + + return newProfile.result } return profile diff --git a/src/routes/[lang]/(website)/character/+page.server.ts b/src/routes/[lang]/(website)/character/+page.server.ts index 2c0b0641..788ba866 100644 --- a/src/routes/[lang]/(website)/character/+page.server.ts +++ b/src/routes/[lang]/(website)/character/+page.server.ts @@ -1,9 +1,12 @@ import { error } from '@sveltejs/kit' import type { PageServerLoad } from './$types' +import { api } from '$lib/server/api' export const load: PageServerLoad = async () => { - const res = await fetch('https://chatgame.space/api/character') - const characters = await res.json() + const characters = await api.character.getList() + if (characters instanceof Error) { + throw characters + } if (!characters.length) { error(404, 'Not found') diff --git a/src/routes/[lang]/(website)/character/[id]/+page.server.ts b/src/routes/[lang]/(website)/character/[id]/+page.server.ts index 9615d561..0b4d435c 100644 --- a/src/routes/[lang]/(website)/character/[id]/+page.server.ts +++ b/src/routes/[lang]/(website)/character/[id]/+page.server.ts @@ -1,9 +1,12 @@ import { error } from '@sveltejs/kit' import type { PageServerLoad } from './$types' +import { api } from '$lib/server/api' export const load: PageServerLoad = async ({ params }) => { - const res = await fetch(`https://chatgame.space/api/character/${params.id}`) - const character = await res.json() + const character = await api.character.getById(params.id) + if (character instanceof Error) { + throw character + } if (!character) { error(404, 'Not found') diff --git a/src/routes/[lang]/(website)/p/[userName]/+page.server.ts b/src/routes/[lang]/(website)/p/[userName]/+page.server.ts index c6a528ab..4bd56441 100644 --- a/src/routes/[lang]/(website)/p/[userName]/+page.server.ts +++ b/src/routes/[lang]/(website)/p/[userName]/+page.server.ts @@ -1,11 +1,10 @@ import { error } from '@sveltejs/kit' import type { PageServerLoad } from './$types' +import { api } from '$lib/server/api' export const load = (async ({ params }) => { - const res = await fetch(`https://chatgame.space/api/profile/userName/${params.userName}`) - const profile = await res.json() - - if (!profile) { + const profile = await api.profile.getByUserName(params.userName) + if (!profile || profile instanceof Error) { error(404, 'Not found') } diff --git a/yarn.lock b/yarn.lock index 8c1db31c..cf1ef1d1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -528,6 +528,11 @@ resolved "https://registry.yarnpkg.com/@eslint/object-schema/-/object-schema-2.1.4.tgz#9e69f8bb4031e11df79e03db09f9dbbae1740843" integrity sha512-BsWiH1yFGjXXS2yvrf5LyuoSIIbPrGUWob917o+BTKuZ7qJdxX8aJLRxs1fS9n6r7vESrq1OUqb68dANcFXuQQ== +"@hmbanan666/chat-game-api@^0.1.5": + version "0.1.5" + resolved "https://registry.yarnpkg.com/@hmbanan666/chat-game-api/-/chat-game-api-0.1.5.tgz#1a5747088f8f5a9bb7a729c87cd80d7e440c2287" + integrity sha512-2hkKw8E3MtOYaWf2/dvkx2Lba1+P+NvbkT1aSpwcRjBNVS/dmqjS4JKy5wGq7ThSXuP2kjmk/BhbumMJ5eJ0ww== + "@humanwhocodes/module-importer@^1.0.1": version "1.0.1" resolved "https://registry.yarnpkg.com/@humanwhocodes/module-importer/-/module-importer-1.0.1.tgz#af5b2691a22b44be847b0ca81641c5fb6ad0172c" @@ -5058,7 +5063,16 @@ string-argv@^0.3.1, string-argv@~0.3.2: resolved "https://registry.yarnpkg.com/string-argv/-/string-argv-0.3.2.tgz#2b6d0ef24b656274d957d54e0a4bbf6153dc02b6" integrity sha512-aqD2Q0144Z+/RqG52NeHEkZauTAUWJO8c6yTftGJKO3Tja5tUgIfmIl6kExvhtxSDP7fXB6DvzkfMpCd/F3G+Q== -"string-width-cjs@npm:string-width@^4.2.0", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3: +"string-width-cjs@npm:string-width@^4.2.0": + version "4.2.3" + resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010" + integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g== + dependencies: + emoji-regex "^8.0.0" + is-fullwidth-code-point "^3.0.0" + strip-ansi "^6.0.1" + +string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3: version "4.2.3" resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010" integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g== @@ -5101,7 +5115,14 @@ stringify-object@^3.2.1: is-obj "^1.0.1" is-regexp "^1.0.0" -"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1: +"strip-ansi-cjs@npm:strip-ansi@^6.0.1": + version "6.0.1" + resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9" + integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A== + dependencies: + ansi-regex "^5.0.1" + +strip-ansi@^6.0.0, strip-ansi@^6.0.1: version "6.0.1" resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9" integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A== @@ -5621,7 +5642,16 @@ word-wrap@^1.2.5: resolved "https://registry.yarnpkg.com/word-wrap/-/word-wrap-1.2.5.tgz#d2c45c6dd4fbce621a66f136cbe328afd0410b34" integrity sha512-BN22B5eaMMI9UMtjrGd5g5eCYPpCPDUy0FJXbYsaT5zYxjFOckS53SQDE3pWkVoWpHXVb3BrYcEN4Twa55B5cA== -"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0: +"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0": + version "7.0.0" + resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43" + integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q== + dependencies: + ansi-styles "^4.0.0" + string-width "^4.1.0" + strip-ansi "^6.0.0" + +wrap-ansi@^7.0.0: version "7.0.0" resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43" integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==