Skip to content

Commit

Permalink
feat: getting data by wrapper, not direct fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
hmbanan666 committed Jun 23, 2024
1 parent 6a725a0 commit 332fead
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 22 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ PUBLIC_COOKIE_KEY=""

# Our secret...
PRIVATE_JWT_SECRET_KEY=""
PRIVATE_WEBSITE_BEARER=""

# WebSocket server with event messages
PUBLIC_WEBSOCKET_URL=""
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions src/lib/server/api.ts
Original file line number Diff line number Diff line change
@@ -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 ?? '')
11 changes: 8 additions & 3 deletions src/routes/[lang]/(website)/+page.server.ts
Original file line number Diff line number Diff line change
@@ -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
20 changes: 12 additions & 8 deletions src/routes/[lang]/(website)/auth/sign-in/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Profile, 'twitchId' | 'userName'>): Promise<Profile> {
const res = await fetch(`https://chatgame.space/api/profile/twitchId/${twitchId}`)
const profile = await res.json()
async function findOrCreateProfile({ twitchId, userName }: Pick<Profile, 'twitchId' | 'userName'>) {
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
Expand Down
7 changes: 5 additions & 2 deletions src/routes/[lang]/(website)/character/+page.server.ts
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
7 changes: 5 additions & 2 deletions src/routes/[lang]/(website)/character/[id]/+page.server.ts
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
7 changes: 3 additions & 4 deletions src/routes/[lang]/(website)/p/[userName]/+page.server.ts
Original file line number Diff line number Diff line change
@@ -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')
}

Expand Down
36 changes: 33 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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==
Expand Down Expand Up @@ -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==
Expand Down Expand Up @@ -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==
Expand Down

0 comments on commit 332fead

Please sign in to comment.