-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c814c30
commit 4f93f21
Showing
13 changed files
with
199 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,5 @@ | |
|
||
<script setup lang="ts"> | ||
useBackButton() | ||
useTelegramProfile() | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { initData } from '@telegram-apps/sdk-vue' | ||
import { useFetch } from '@vueuse/core' | ||
|
||
export function useTelegramProfile() { | ||
const user = initData.user() | ||
|
||
const { data } = useFetch(`https://chatgame.space/api/telegram/${user?.id}`, { | ||
async onFetchError(ctx) { | ||
if (ctx?.data?.statusCode === 404 && user?.id) { | ||
await fetch('https://chatgame.space/api/telegram/profile', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Authorization': `Bearer 123`, | ||
}, | ||
body: JSON.stringify({ | ||
telegramId: user.id, | ||
username: user?.username, | ||
}), | ||
}) | ||
} | ||
|
||
return ctx | ||
}, | ||
}).get().json() | ||
|
||
return { profile: data } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
export default defineEventHandler(async (event) => { | ||
try { | ||
const telegramId = getRouterParam(event, 'telegramId') | ||
|
||
const profile = await prisma.telegramProfile.findFirst({ | ||
where: { telegramId }, | ||
include: { | ||
profile: true, | ||
}, | ||
}) | ||
if (!profile) { | ||
throw createError({ | ||
status: 404, | ||
}) | ||
} | ||
|
||
return profile | ||
} catch (error) { | ||
throw errorResolver(error) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export default defineEventHandler(async (event) => { | ||
try { | ||
const id = getRouterParam(event, 'id') | ||
|
||
const profile = await prisma.telegramProfile.findFirst({ | ||
where: { id }, | ||
}) | ||
if (!profile) { | ||
throw createError({ | ||
status: 404, | ||
}) | ||
} | ||
|
||
return profile | ||
} catch (error) { | ||
throw errorResolver(error) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { DBRepository } from '../../../utils/repository' | ||
|
||
export default defineEventHandler(async (event) => { | ||
try { | ||
const body = await readBody(event) | ||
|
||
if (!body.telegramId) { | ||
throw createError({ | ||
statusCode: 400, | ||
message: 'You must provide telegramId', | ||
}) | ||
} | ||
|
||
const repository = new DBRepository() | ||
const profile = await repository.findOrCreateTelegramProfile({ | ||
telegramId: body.telegramId, | ||
username: body.username, | ||
}) | ||
|
||
return { | ||
ok: true, | ||
result: profile, | ||
} | ||
} catch (error) { | ||
throw errorResolver(error) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { H3Error } from 'h3' | ||
|
||
const logger = useLogger('error') | ||
|
||
export function errorResolver(exception: unknown) { | ||
if (exception instanceof H3Error) { | ||
throw exception | ||
} | ||
|
||
// Ok, something intereresting happened | ||
logger.error(exception) | ||
|
||
return createError({ | ||
statusCode: 500, | ||
statusMessage: 'Internal server error', | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters