Skip to content

Commit

Permalink
Add swagger
Browse files Browse the repository at this point in the history
  • Loading branch information
O4epegb committed Sep 11, 2024
1 parent 2f6bd2a commit 13364c8
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 89 deletions.
10 changes: 6 additions & 4 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@
},
"dependencies": {
"@bugsnag/js": "^7.25.0",
"@fastify/cors": "^8.5.0",
"@fastify/type-provider-typebox": "^3.6.0",
"@fastify/cors": "^9.0.1",
"@fastify/swagger": "^8.15.0",
"@fastify/type-provider-typebox": "^4.1.0",
"@prisma/client": "^5.18.0",
"@sinclair/typebox": "^0.29.6",
"@scalar/fastify-api-reference": "^1.25.10",
"@sinclair/typebox": "^0.33.9",
"axios": "^1.7.2",
"dayjs": "^1.11.11",
"dotenv": "^16.4.5",
Expand Down Expand Up @@ -61,7 +63,7 @@
"eslint-plugin-no-relative-import-paths": "^1.5.5",
"globals": "^15.8.0",
"prettier": "^3.3.3",
"typescript": "^5.5.3",
"typescript": "^5.6.2",
"typescript-eslint": "^7.16.1",
"vitest": "^2.0.3"
}
Expand Down
26 changes: 16 additions & 10 deletions apps/api/src/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { serversRoute } from './routes/servers'
import { statsRoute } from './routes/stats'
import { streamsRoute } from './routes/streams'
import { suggestRoute } from './routes/suggest'
import { initSwagger } from './swagger'

export * from './app'

Expand All @@ -19,13 +20,18 @@ app.register(cors, {
methods: ['GET'],
})

statsRoute(app)
logfilesRoute(app)
serversRoute(app)
combosRoute(app)
suggestRoute(app)
searchRoute(app)
gamesRoute(app)
playersRoute(app)
streamsRoute(app)
devRoute(app)
for (const route of [
initSwagger,
statsRoute,
logfilesRoute,
serversRoute,
combosRoute,
suggestRoute,
searchRoute,
gamesRoute,
playersRoute,
streamsRoute,
devRoute,
] as const) {
await route(app)
}
40 changes: 0 additions & 40 deletions apps/api/src/app/routes/dev/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,6 @@ import { getStaticData } from '~/app/getters/getStaticData'
import { prisma } from '~/prisma'

export const devRoute = (app: AppType) => {
app.get('/api/dev/migrateData', async (request, reply) => {
getStaticData.cache.clear?.()
// const [races] = await getStaticData();

const games = await prisma.game.findMany({
where: { normalizedClass: 'Arcane Marksman' },
})

for (const game of games) {
await prisma.game.update({
where: {
id: game.id,
},
data: {
normalizedClass: 'Hexslinger',
classAbbr: 'Hs',
char: game.raceAbbr + 'Hs',
},
})
}

// for (const race of races) {
// await prisma.game.updateMany({
// where: {
// raceAbbr: race.abbr,
// classAbbr: {
// in: ['AM', 'Am'],
// },
// },
// data: {
// char: race.abbr + 'Hs',
// classAbbr: 'Hs',
// normalizedClass: 'Hexslinger',
// },
// });
// }

return reply.send('Done')
})

app.get('/api/dev/info', async () => {
getStaticData.cache.clear?.()
const { races, classes } = await getStaticData()
Expand Down
4 changes: 0 additions & 4 deletions apps/api/src/app/routes/games/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,11 @@ export const gamesRoute = (app: AppType) => {

app.get<{
Querystring: Static<typeof GamesQuerystring>
// Reply: SomeResponseType
}>(
'/api/games',
{
schema: {
querystring: GamesQuerystring,
// response: {
// 200: SomeResponseType,
// },
},
},
async (request) => {
Expand Down
20 changes: 20 additions & 0 deletions apps/api/src/app/swagger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import fastifySwagger from '@fastify/swagger'
import fastifyScalarSwaggerUi from '@scalar/fastify-api-reference'
import { AppType } from './app'

export const initSwagger = async (app: AppType) => {
await app.register(fastifySwagger, {
openapi: {
openapi: '3.0.0',
info: {
title: 'DCSS Stats Swagger API',
description: 'Scalar Swagger UI for https://dcss-stats.vercel.app/',
version: 'WIP',
},
},
})

await app.register(fastifyScalarSwaggerUi, {
routePrefix: '/api/docs',
})
}
2 changes: 2 additions & 0 deletions apps/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ async function main() {
host: '0.0.0.0',
})

app.swagger()

startParsing()
} catch (err) {
app.log.error(err)
Expand Down
8 changes: 7 additions & 1 deletion apps/api/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
/// <reference types="vitest" />
import { defineConfig } from 'vite'

export default defineConfig({})
export default defineConfig({
test: {
alias: {
'~/': new URL('./src/', import.meta.url).pathname,
},
},
})
2 changes: 1 addition & 1 deletion apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"prettier": "^3.3.3",
"prettier-plugin-tailwindcss": "^0.6.5",
"tailwindcss": "^3.4.6",
"typescript": "^5.5.3",
"typescript": "^5.6.2",
"typescript-eslint": "^7.16.1"
}
}
Loading

0 comments on commit 13364c8

Please sign in to comment.