Add swagger docs + icon marketplace get icons endpoint
This commit is contained in:
249
src/index.ts
249
src/index.ts
@@ -1,63 +1,250 @@
|
||||
import { Elysia } from 'elysia'
|
||||
import { Elysia, t } from 'elysia'
|
||||
import { cors } from '@elysiajs/cors'
|
||||
import { jsonResponse } from './lib/util'
|
||||
import dotenv from 'dotenv'
|
||||
import swagger from '@elysiajs/swagger'
|
||||
|
||||
import { handler as canLoadClientHandler } from './routes/can-load-client'
|
||||
|
||||
import { handler as launcherVersionsHandler } from './routes/launcher/versions'
|
||||
import { handler as launcherLatestHandler } from './routes/launcher/latest'
|
||||
import { handler as launcherLoaderLatestHandler } from './routes/launcher/loader/latest'
|
||||
import { handler as launcherLoaderUpdateDataHandler } from './routes/launcher/loader/update-data'
|
||||
|
||||
import { handler as berrydashLeaderboardsGetHandler } from './routes/berrydash/leaderboards/get'
|
||||
|
||||
import { handler as berrydashProfileGetHandler } from './routes/berrydash/profile/get'
|
||||
import { handler as berrydashProfilePostsDeleteHandler } from './routes/berrydash/profile/posts/delete'
|
||||
import { handler as berrydashProfilePostsGetHandler } from './routes/berrydash/profile/posts/get'
|
||||
import { handler as berrydashProfilePostsPostHandler } from './routes/berrydash/profile/posts/post'
|
||||
import { handler as berrydashProfilePostsPutHandler } from './routes/berrydash/profile/posts/put'
|
||||
|
||||
import { handler as berryDashIconMarketplacePostHandler } from './routes/berrydash/icon-marketplace/post'
|
||||
|
||||
dotenv.config()
|
||||
|
||||
const app = new Elysia().use(
|
||||
cors({
|
||||
origin: '*',
|
||||
methods: ['POST', 'GET']
|
||||
})
|
||||
)
|
||||
const intNotStr = (name: string) => {
|
||||
return (
|
||||
'\n\n**The type for parameter `' +
|
||||
name +
|
||||
'` is actually a `number`, but it shows as a `string` here.**'
|
||||
)
|
||||
}
|
||||
|
||||
const boolNotStr = (name: string) => {
|
||||
return (
|
||||
'\n\n**The type for parameter `' +
|
||||
name +
|
||||
'` is actually a `boolean`, but it shows as a `string` here.**'
|
||||
)
|
||||
}
|
||||
|
||||
const app = new Elysia()
|
||||
.use(
|
||||
cors({
|
||||
origin: '*',
|
||||
methods: ['POST', 'GET']
|
||||
})
|
||||
)
|
||||
.use(
|
||||
swagger({
|
||||
path: '/docs',
|
||||
documentation: {
|
||||
info: {
|
||||
title: 'Lncvrt Games API',
|
||||
description:
|
||||
'This is the official documentation for the Lncvrt Games API!',
|
||||
version: '1.0.0'
|
||||
}
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
app.get('/can-load-client', context => canLoadClientHandler(context))
|
||||
app.get('/launcher/versions', context => launcherVersionsHandler(context))
|
||||
app.get('/launcher/latest', launcherLatestHandler)
|
||||
app.get('/launcher/loader/latest', launcherLoaderLatestHandler)
|
||||
app.get('/launcher/loader/update-data', context =>
|
||||
launcherLoaderUpdateDataHandler(context)
|
||||
app.get('/launcher/versions', context => launcherVersionsHandler(context), {
|
||||
detail: {
|
||||
description:
|
||||
'The endpoint for getting the launcher manifest.\n\nNote: if going to use the params, both must be provided at the same time.',
|
||||
tags: ['Launcher']
|
||||
},
|
||||
params: t.Object({
|
||||
platform: t.Optional(t.String()),
|
||||
arch: t.Optional(t.String())
|
||||
})
|
||||
})
|
||||
app.get('/launcher/latest', launcherLatestHandler, {
|
||||
detail: {
|
||||
description: 'The endpoint for getting the latest launcher version.',
|
||||
tags: ['Launcher']
|
||||
}
|
||||
})
|
||||
app.get('/launcher/loader/latest', launcherLoaderLatestHandler, {
|
||||
detail: {
|
||||
description:
|
||||
'The endpoint for getting the latest loader/auto updater version.',
|
||||
tags: ['Launcher']
|
||||
}
|
||||
})
|
||||
app.get(
|
||||
'/launcher/loader/update-data',
|
||||
context => launcherLoaderUpdateDataHandler(context),
|
||||
{
|
||||
detail: {
|
||||
description:
|
||||
'The endpoint for getting Launcher Update data for when a new Update is released. It will be send & read by the updater.\n\nNote: if going to use the params, both must be provided at the same time.',
|
||||
tags: ['Launcher']
|
||||
},
|
||||
params: t.Object({
|
||||
platform: t.Optional(t.String()),
|
||||
arch: t.Optional(t.String())
|
||||
})
|
||||
}
|
||||
)
|
||||
app.get('/berrydash/leaderboards/score', context =>
|
||||
berrydashLeaderboardsGetHandler(context, 0)
|
||||
app.get(
|
||||
'/berrydash/leaderboards/score',
|
||||
context => berrydashLeaderboardsGetHandler(context, 0),
|
||||
{
|
||||
detail: {
|
||||
description: 'The endpoint for getting the score leaderboards.',
|
||||
tags: ['Berry Dash', 'Leaderboards']
|
||||
}
|
||||
}
|
||||
)
|
||||
app.get('/berrydash/leaderboards/berry', context =>
|
||||
berrydashLeaderboardsGetHandler(context, 1)
|
||||
app.get(
|
||||
'/berrydash/leaderboards/berry',
|
||||
context => berrydashLeaderboardsGetHandler(context, 1),
|
||||
{
|
||||
detail: {
|
||||
description:
|
||||
'The endpoint for getting the berry leaderboards.' + intNotStr('berry'),
|
||||
tags: ['Berry Dash', 'Leaderboards']
|
||||
},
|
||||
params: t.Object({
|
||||
berry: t.String()
|
||||
})
|
||||
}
|
||||
)
|
||||
app.get('/berrydash/leaderboards/coin', context =>
|
||||
berrydashLeaderboardsGetHandler(context, 2)
|
||||
app.get(
|
||||
'/berrydash/leaderboards/coin',
|
||||
context => berrydashLeaderboardsGetHandler(context, 2),
|
||||
{
|
||||
detail: {
|
||||
description: 'The endpoint for getting the coin leaderboards.',
|
||||
tags: ['Berry Dash', 'Leaderboards']
|
||||
}
|
||||
}
|
||||
)
|
||||
app.get('/berrydash/leaderboards/legacy', context =>
|
||||
berrydashLeaderboardsGetHandler(context, 3)
|
||||
app.get(
|
||||
'/berrydash/leaderboards/legacy',
|
||||
context => berrydashLeaderboardsGetHandler(context, 3),
|
||||
{
|
||||
detail: {
|
||||
description: 'The endpoint for getting the legacy leaderboards.',
|
||||
tags: ['Berry Dash', 'Leaderboards']
|
||||
}
|
||||
}
|
||||
)
|
||||
app.get('/berrydash/leaderboards/total', context =>
|
||||
berrydashLeaderboardsGetHandler(context, 4)
|
||||
app.get(
|
||||
'/berrydash/leaderboards/total',
|
||||
context => berrydashLeaderboardsGetHandler(context, 4),
|
||||
{
|
||||
detail: {
|
||||
description: 'The endpoint for getting the total leaderboards.',
|
||||
tags: ['Berry Dash', 'Leaderboards']
|
||||
}
|
||||
}
|
||||
)
|
||||
app.get('/berrydash/profile', context => berrydashProfileGetHandler(context))
|
||||
app.delete('/berrydash/profile/posts', context =>
|
||||
berrydashProfilePostsDeleteHandler(context)
|
||||
app.get('/berrydash/profile', context => berrydashProfileGetHandler(context), {
|
||||
detail: {
|
||||
description:
|
||||
"The endpoint for getting a user's profile." + intNotStr('userId'),
|
||||
tags: ['Berry Dash', 'Profiles']
|
||||
},
|
||||
params: t.Object({
|
||||
userId: t.String()
|
||||
})
|
||||
})
|
||||
app.delete(
|
||||
'/berrydash/profile/posts',
|
||||
context => berrydashProfilePostsDeleteHandler(context),
|
||||
{
|
||||
detail: {
|
||||
description: 'This endpoint is for deleting a post.' + intNotStr('id'),
|
||||
tags: ['Berry Dash', 'Profiles']
|
||||
},
|
||||
headers: t.Object({
|
||||
authorization: t.String()
|
||||
}),
|
||||
params: t.Object({
|
||||
id: t.String()
|
||||
})
|
||||
}
|
||||
)
|
||||
app.get('/berrydash/profile/posts', context =>
|
||||
berrydashProfilePostsGetHandler(context)
|
||||
app.get(
|
||||
'/berrydash/profile/posts',
|
||||
context => berrydashProfilePostsGetHandler(context),
|
||||
{
|
||||
detail: {
|
||||
description:
|
||||
'This endpoint is for getting posts from a user.' + intNotStr('userId'),
|
||||
tags: ['Berry Dash', 'Profiles']
|
||||
},
|
||||
params: t.Object({
|
||||
userId: t.String()
|
||||
})
|
||||
}
|
||||
)
|
||||
app.post('/berrydash/profile/posts', context =>
|
||||
berrydashProfilePostsPostHandler(context)
|
||||
app.post(
|
||||
'/berrydash/profile/posts',
|
||||
context => berrydashProfilePostsPostHandler(context),
|
||||
{
|
||||
detail: {
|
||||
description: 'This endpoint is for uploading a new post.',
|
||||
tags: ['Berry Dash', 'Profiles']
|
||||
},
|
||||
body: t.Object({
|
||||
content: t.String()
|
||||
})
|
||||
}
|
||||
)
|
||||
app.put('/berrydash/profile/posts', context =>
|
||||
berrydashProfilePostsPutHandler(context)
|
||||
app.put(
|
||||
'/berrydash/profile/posts',
|
||||
context => berrydashProfilePostsPutHandler(context),
|
||||
{
|
||||
detail: {
|
||||
description:
|
||||
'This endpoint is for liking/disliking a post.' +
|
||||
intNotStr('id') +
|
||||
boolNotStr('likedQuery'),
|
||||
tags: ['Berry Dash', 'Profiles']
|
||||
},
|
||||
params: t.Object({
|
||||
id: t.String(),
|
||||
likedQuery: t.String()
|
||||
})
|
||||
}
|
||||
)
|
||||
app.post(
|
||||
'/berrydash/icon-marketplace',
|
||||
context => berryDashIconMarketplacePostHandler(context),
|
||||
{
|
||||
detail: {
|
||||
description:
|
||||
'The endpoint for getting the icon marketplace icons.\n\nPretty much none of the body types are correct.',
|
||||
tags: ['Berry Dash', 'Icon Marketplace']
|
||||
},
|
||||
body: t.Object({
|
||||
sortBy: t.String(),
|
||||
priceRangeEnabled: t.String(),
|
||||
priceRangeMin: t.String(),
|
||||
priceRangeMax: t.String(),
|
||||
searchForEnabled: t.String(),
|
||||
searchForValue: t.String(),
|
||||
onlyShowEnabled: t.String(),
|
||||
onlyShowValue: t.String(),
|
||||
currentIcons: t.String()
|
||||
})
|
||||
}
|
||||
)
|
||||
app.all('*', () =>
|
||||
jsonResponse(
|
||||
|
||||
178
src/routes/berrydash/icon-marketplace/post.ts
Normal file
178
src/routes/berrydash/icon-marketplace/post.ts
Normal file
@@ -0,0 +1,178 @@
|
||||
import { Context } from 'elysia'
|
||||
import { getDatabaseConnection, jsonResponse } from '../../../lib/util'
|
||||
import {
|
||||
berryDashMarketplaceIcons,
|
||||
berryDashUserData,
|
||||
users
|
||||
} from '../../../lib/tables'
|
||||
import { and, eq, inArray, or, sql, not, like } from 'drizzle-orm'
|
||||
|
||||
type Body = {
|
||||
sortBy: number
|
||||
priceRangeEnabled: boolean
|
||||
priceRangeMin: number
|
||||
priceRangeMax: number
|
||||
searchForEnabled: boolean
|
||||
searchForValue: string
|
||||
onlyShowEnabled: boolean
|
||||
onlyShowValue: number
|
||||
currentIcons: string[]
|
||||
}
|
||||
|
||||
const requiredKeys = [
|
||||
'sortBy',
|
||||
'priceRangeEnabled',
|
||||
'priceRangeMin',
|
||||
'priceRangeMax',
|
||||
'searchForEnabled',
|
||||
'searchForValue',
|
||||
'onlyShowEnabled',
|
||||
'onlyShowValue',
|
||||
'currentIcons'
|
||||
]
|
||||
|
||||
export async function handler (context: Context) {
|
||||
const dbInfo0 = getDatabaseConnection(0)
|
||||
const dbInfo1 = getDatabaseConnection(1)
|
||||
|
||||
if (!dbInfo0 || !dbInfo1)
|
||||
return jsonResponse(
|
||||
{ success: false, message: 'Failed to connect to database', data: null },
|
||||
500
|
||||
)
|
||||
|
||||
const { connection: connection0, db: db0 } = dbInfo0
|
||||
const { connection: connection1, db: db1 } = dbInfo1
|
||||
|
||||
const body: { [key: string]: any } = context.body as any
|
||||
|
||||
for (const key of requiredKeys) {
|
||||
if (!(key in body)) {
|
||||
return jsonResponse(
|
||||
{ success: false, message: 'Invalid POST data', data: null },
|
||||
400
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
let body2: { [key: string]: any } = {}
|
||||
body2.sortBy = Number(body.sortBy)
|
||||
body2.priceRangeEnabled = body.priceRangeEnabled.toLowerCase() === 'true'
|
||||
body2.priceRangeMin = Number(body.priceRangeMin)
|
||||
body2.priceRangeMax = Number(body.priceRangeMax)
|
||||
body2.searchForEnabled = body.searchForEnabled.toLowerCase() === 'true'
|
||||
body2.searchForValue = body.searchForValue as string
|
||||
body2.onlyShowEnabled = body.onlyShowEnabled.toLowerCase() === 'true'
|
||||
body2.onlyShowValue = Number(body.onlyShowValue)
|
||||
body2.currentIcons = JSON.parse(atob(body.currentIcons))
|
||||
const body3: Body = body2 as Body
|
||||
|
||||
const authorizationToken = context.headers.authorization
|
||||
if (!authorizationToken) {
|
||||
connection0.end()
|
||||
connection1.end()
|
||||
return jsonResponse(
|
||||
{ success: false, message: 'Unauthorized', data: null },
|
||||
401
|
||||
)
|
||||
}
|
||||
|
||||
const userData = await db1
|
||||
.select({ id: berryDashUserData.id })
|
||||
.from(berryDashUserData)
|
||||
.where(eq(berryDashUserData.token, authorizationToken))
|
||||
.execute()
|
||||
|
||||
if (!userData[0]) {
|
||||
connection0.end()
|
||||
connection1.end()
|
||||
return jsonResponse(
|
||||
{ success: false, message: 'Unauthorized', data: null },
|
||||
401
|
||||
)
|
||||
}
|
||||
|
||||
const userId = userData[0].id
|
||||
|
||||
const filters: any[] = [
|
||||
or(
|
||||
eq(berryDashMarketplaceIcons.state, 1),
|
||||
eq(berryDashMarketplaceIcons.state, 2)
|
||||
)
|
||||
]
|
||||
|
||||
if (body3.priceRangeEnabled) {
|
||||
filters.push(
|
||||
sql`${berryDashMarketplaceIcons.price} >= ${body3.priceRangeMin}`,
|
||||
sql`${berryDashMarketplaceIcons.price} <= ${body3.priceRangeMax}`
|
||||
)
|
||||
}
|
||||
|
||||
if (body3.searchForEnabled) {
|
||||
filters.push(
|
||||
sql`FROM_BASE64(${
|
||||
berryDashMarketplaceIcons.name
|
||||
}) LIKE ${`%${body3.searchForValue}%`}`
|
||||
)
|
||||
}
|
||||
|
||||
if (body3.onlyShowEnabled) {
|
||||
if (body3.onlyShowValue === 0) {
|
||||
filters.push(eq(berryDashMarketplaceIcons.userId, userId))
|
||||
} else if (body3.onlyShowValue === 1) {
|
||||
filters.push(sql`${berryDashMarketplaceIcons.userId} != ${userId}`)
|
||||
} else if (body3.onlyShowValue === 2) {
|
||||
filters.push(inArray(berryDashMarketplaceIcons.uuid, body3.currentIcons))
|
||||
} else if (body3.onlyShowValue === 3) {
|
||||
filters.push(
|
||||
not(inArray(berryDashMarketplaceIcons.uuid, body3.currentIcons))
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
let orderBy: any
|
||||
switch (body3.sortBy) {
|
||||
case 1:
|
||||
orderBy = sql`price ASC`
|
||||
break
|
||||
case 2:
|
||||
orderBy = sql`id ASC`
|
||||
break
|
||||
case 3:
|
||||
orderBy = sql`id DESC`
|
||||
break
|
||||
default:
|
||||
orderBy = sql`price DESC`
|
||||
break
|
||||
}
|
||||
|
||||
const icons = await db1
|
||||
.select()
|
||||
.from(berryDashMarketplaceIcons)
|
||||
.where(and(...filters))
|
||||
.orderBy(orderBy)
|
||||
.execute()
|
||||
|
||||
const userIds = Array.from(new Set(icons.map(i => i.userId)))
|
||||
const usersData = await db0
|
||||
.select({ id: users.id, username: users.username })
|
||||
.from(users)
|
||||
.where(inArray(users.id, userIds))
|
||||
.execute()
|
||||
|
||||
const usersMap = Object.fromEntries(usersData.map(u => [u.id, u.username]))
|
||||
|
||||
const result = icons.map(i => ({
|
||||
username: usersMap[i.userId] ?? 'Unknown',
|
||||
userid: i.userId,
|
||||
data: i.data,
|
||||
uuid: i.uuid,
|
||||
price: i.state === 2 ? 100000000 : i.price,
|
||||
name: atob(i.name)
|
||||
}))
|
||||
|
||||
connection0.end()
|
||||
connection1.end()
|
||||
|
||||
return jsonResponse({ success: true, message: null, data: result })
|
||||
}
|
||||
Reference in New Issue
Block a user