Skip to content

Commit

Permalink
Merge pull request #129 from casesandberg/feature/api-cleanup
Browse files Browse the repository at this point in the history
API Cleanup
  • Loading branch information
casesandberg authored Dec 24, 2024
2 parents 15bd7ed + a0fb6f4 commit 41ca412
Show file tree
Hide file tree
Showing 159 changed files with 4,571 additions and 4,897 deletions.
4 changes: 2 additions & 2 deletions apps/api/app/api/v1/activity/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export const dynamic = 'force-dynamic'

export async function GET(_req: Request): Promise<SchemaResponse<typeof schema.get.responses>> {
try {
const activities = await getSiteActivity({})
const data = await getSiteActivity({})

return NextResponse.json({ activities })
return NextResponse.json({ data })
} catch (error) {
if (error instanceof CommentNotFoundError) {
return NextResponse.json({ error: error.message }, { status: 404 })
Expand Down
3 changes: 2 additions & 1 deletion apps/api/app/api/v1/activity/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { MarketActivitySchema } from '@play-money/markets/types'

export default {
get: {
summary: 'Get all site activity',
responses: {
200: zod.object({ activities: zod.array(MarketActivitySchema) }),
200: zod.object({ data: zod.array(MarketActivitySchema) }),
404: ServerErrorSchema,
500: ServerErrorSchema,
},
Expand Down
4 changes: 2 additions & 2 deletions apps/api/app/api/v1/comments/[id]/reaction/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ export async function POST(
const commentReaction = await reactToComment({ commentId: id, userId, ...data })

if (commentReaction === 'removed') {
return NextResponse.json({ message: 'Reaction removed' })
return new Response(null, { status: 204 }) as NextResponse<Record<string, never>>
}

return NextResponse.json(commentReaction)
return NextResponse.json({ data: commentReaction })
} catch (error) {
console.log(error) // eslint-disable-line no-console -- Log error for debugging
return NextResponse.json({ error: 'Error processing request' }, { status: 500 })
Expand Down
9 changes: 6 additions & 3 deletions apps/api/app/api/v1/comments/[id]/reaction/schema.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import zod from 'zod'
import { z } from 'zod'
import { ApiEndpoints, ServerErrorSchema } from '@play-money/api-helpers'
import { CommentReactionSchema } from '@play-money/database'

export default {
post: {
parameters: zod.object({ id: zod.string() }),
summary: 'React to a comment, will remove reaction if already exists',
security: true,
parameters: z.object({ id: z.string() }),
requestBody: CommentReactionSchema.pick({
emoji: true,
}),
responses: {
200: [CommentReactionSchema, zod.object({ message: zod.string() })],
200: z.object({ data: CommentReactionSchema }),
204: z.object({}),
404: ServerErrorSchema,
500: ServerErrorSchema,
},
Expand Down
6 changes: 3 additions & 3 deletions apps/api/app/api/v1/comments/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function GET(

const comment = await getComment({ id })

return NextResponse.json(comment)
return NextResponse.json({ data: comment })
} catch (error) {
if (error instanceof CommentNotFoundError) {
return NextResponse.json({ error: error.message }, { status: 404 })
Expand Down Expand Up @@ -51,7 +51,7 @@ export async function PATCH(

const updatedComment = await updateComment({ id, content })

return NextResponse.json(updatedComment)
return NextResponse.json({ data: updatedComment })
} catch (error) {
if (error instanceof CommentNotFoundError) {
return NextResponse.json({ error: error.message }, { status: 404 })
Expand Down Expand Up @@ -82,7 +82,7 @@ export async function DELETE(

await deleteComment({ id })

return NextResponse.json({ message: 'Comment deleted' })
return new Response(null, { status: 204 }) as NextResponse<Record<string, never>>
} catch (error) {
if (error instanceof CommentNotFoundError) {
return NextResponse.json({ error: error.message }, { status: 404 })
Expand Down
11 changes: 8 additions & 3 deletions apps/api/app/api/v1/comments/[id]/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,31 @@ import { CommentSchema } from '@play-money/database'

export default {
get: {
summary: 'Get a comment',
parameters: CommentSchema.pick({ id: true }),
responses: {
200: CommentSchema,
200: z.object({ data: CommentSchema }),
404: ServerErrorSchema,
500: ServerErrorSchema,
},
},
patch: {
summary: 'Update a comment',
security: true,
parameters: CommentSchema.pick({ id: true }),
requestBody: CommentSchema.pick({ content: true }),
responses: {
200: CommentSchema,
200: z.object({ data: CommentSchema }),
404: ServerErrorSchema,
500: ServerErrorSchema,
},
},
delete: {
summary: 'Delete a comment',
security: true,
parameters: CommentSchema.pick({ id: true }),
responses: {
200: z.object({ message: z.string() }),
204: z.object({}),
404: ServerErrorSchema,
500: ServerErrorSchema,
},
Expand Down
2 changes: 1 addition & 1 deletion apps/api/app/api/v1/comments/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function POST(req: Request): Promise<SchemaResponse<typeof schema.p

const comment = await createComment({ ...data, authorId: userId })

return NextResponse.json(comment)
return NextResponse.json({ data: comment })
} catch (error) {
console.log(error) // eslint-disable-line no-console -- Log error for debugging
return NextResponse.json({ error: 'Error processing request' }, { status: 500 })
Expand Down
5 changes: 4 additions & 1 deletion apps/api/app/api/v1/comments/schema.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { z } from 'zod'
import { ApiEndpoints, ServerErrorSchema } from '@play-money/api-helpers'
import { CommentSchema } from '@play-money/database'

export default {
post: {
summary: 'Create a comment on an entity',
security: true,
requestBody: CommentSchema.pick({
content: true,
parentId: true,
entityType: true,
entityId: true,
}),
responses: {
200: CommentSchema,
200: z.object({ data: CommentSchema }),
404: ServerErrorSchema,
500: ServerErrorSchema,
},
Expand Down
15 changes: 9 additions & 6 deletions apps/api/app/api/v1/leaderboard/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ export async function GET(req: Request): Promise<SchemaResponse<typeof schema.ge

const leaderboard = await getMonthlyLeaderboard(startDate, endDate, userId)

return NextResponse.json(leaderboard, {
headers: {
'Content-Type': 'application/json',
'Cache-Control': 'max-age=600, s-maxage=600, stale-while-revalidate=59',
},
})
return NextResponse.json(
{ data: leaderboard },
{
headers: {
'Content-Type': 'application/json',
'Cache-Control': 'max-age=600, s-maxage=600, stale-while-revalidate=59',
},
}
)
} catch (error) {
console.log(error) // eslint-disable-line no-console -- Log error for debugging

Expand Down
33 changes: 18 additions & 15 deletions apps/api/app/api/v1/leaderboard/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const LeaderboardUserSchema = z.object({

export default {
get: {
summary: 'Get the leaderboard',
parameters: z
.object({
year: z.coerce.number().optional(),
Expand All @@ -20,21 +21,23 @@ export default {
.optional(),
responses: {
200: z.object({
topTraders: z.array(LeaderboardUserSchema),
topCreators: z.array(LeaderboardUserSchema),
topPromoters: z.array(LeaderboardUserSchema),
topQuesters: z.array(LeaderboardUserSchema),
topReferrers: z.array(LeaderboardUserSchema),
userRankings: z
.object({
trader: LeaderboardUserSchema.optional(),
creator: LeaderboardUserSchema.optional(),
promoter: LeaderboardUserSchema.optional(),
quester: LeaderboardUserSchema.optional(),
referrer: LeaderboardUserSchema.optional(),
})
.or(z.null())
.optional(),
data: z.object({
topTraders: z.array(LeaderboardUserSchema),
topCreators: z.array(LeaderboardUserSchema),
topPromoters: z.array(LeaderboardUserSchema),
topQuesters: z.array(LeaderboardUserSchema),
topReferrers: z.array(LeaderboardUserSchema),
userRankings: z
.object({
trader: LeaderboardUserSchema.optional(),
creator: LeaderboardUserSchema.optional(),
promoter: LeaderboardUserSchema.optional(),
quester: LeaderboardUserSchema.optional(),
referrer: LeaderboardUserSchema.optional(),
})
.or(z.null())
.optional(),
}),
}),
404: ServerErrorSchema,
500: ServerErrorSchema,
Expand Down
25 changes: 0 additions & 25 deletions apps/api/app/api/v1/liquidity/route.ts

This file was deleted.

21 changes: 0 additions & 21 deletions apps/api/app/api/v1/liquidity/schema.ts

This file was deleted.

6 changes: 4 additions & 2 deletions apps/api/app/api/v1/lists/[id]/balance/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ export async function GET(
])

return NextResponse.json({
user: transformMarketBalancesToNumbers(userBalancesInList),
userPositions: transformMarketOptionPositionToNumbers(userPositions),
data: {
user: transformMarketBalancesToNumbers(userBalancesInList),
userPositions: transformMarketOptionPositionToNumbers(userPositions),
},
})
} catch (error) {
console.log(error) // eslint-disable-line no-console -- Log error for debugging
Expand Down
9 changes: 6 additions & 3 deletions apps/api/app/api/v1/lists/[id]/balance/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ import { ApiEndpoints, ServerErrorSchema } from '@play-money/api-helpers'

export default {
get: {
summary: 'Get the balance for a list',
parameters: z.object({ id: z.string() }),
responses: {
200: z.object({
// TODO: Hookup with NetBalance
user: z.array(z.object({})),
userPositions: z.array(z.object({})),
data: z.object({
// TODO: Hookup with NetBalance
user: z.array(z.object({})),
userPositions: z.array(z.object({})),
}),
}),
404: ServerErrorSchema,
500: ServerErrorSchema,
Expand Down
2 changes: 1 addition & 1 deletion apps/api/app/api/v1/lists/[id]/comments/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export async function GET(

const comments = await getCommentsOnList({ listId: id })

return NextResponse.json({ comments })
return NextResponse.json({ data: comments })
} catch (error) {
if (error instanceof CommentNotFoundError) {
return NextResponse.json({ error: error.message }, { status: 404 })
Expand Down
3 changes: 2 additions & 1 deletion apps/api/app/api/v1/lists/[id]/comments/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { CommentSchema } from '@play-money/database'

export default {
get: {
summary: 'Get comments for a list',
parameters: zod.object({ id: zod.string() }),
responses: {
200: zod.object({ comments: zod.array(CommentSchema) }),
200: zod.object({ data: zod.array(CommentSchema) }),
404: ServerErrorSchema,
500: ServerErrorSchema,
},
Expand Down
2 changes: 1 addition & 1 deletion apps/api/app/api/v1/lists/[id]/markets/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export async function POST(
},
})

return NextResponse.json({ market: newMarket })
return NextResponse.json({ data: newMarket })
} catch (error: unknown) {
console.log(error) // eslint-disable-line no-console -- Log error for debugging
if (error instanceof Error) {
Expand Down
4 changes: 3 additions & 1 deletion apps/api/app/api/v1/lists/[id]/markets/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { MarketOptionSchema, MarketSchema } from '@play-money/database'

export default {
post: {
summary: 'Create a market in a list',
security: true,
parameters: z.object({ id: z.string() }),
requestBody: MarketSchema.pick({
question: true,
Expand All @@ -19,7 +21,7 @@ export default {
),
}),
responses: {
200: z.object({ market: MarketSchema.optional() }),
200: z.object({ data: MarketSchema }),
404: ServerErrorSchema,
500: ServerErrorSchema,
},
Expand Down
2 changes: 1 addition & 1 deletion apps/api/app/api/v1/lists/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function GET(

const list = await getList({ id, extended })

return NextResponse.json(list)
return NextResponse.json({ data: list })
} catch (error) {
console.log(error) // eslint-disable-line no-console -- Log error for debugging
return NextResponse.json({ error: 'Error processing request' }, { status: 500 })
Expand Down
3 changes: 2 additions & 1 deletion apps/api/app/api/v1/lists/[id]/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { ListSchema } from '@play-money/database'

export default {
get: {
summary: 'Get a list',
parameters: ListSchema.pick({ id: true }).extend({ extended: z.boolean().optional() }),
responses: {
200: ListSchema,
200: z.object({ data: ListSchema }),
404: ServerErrorSchema,
500: ServerErrorSchema,
},
Expand Down
14 changes: 3 additions & 11 deletions apps/api/app/api/v1/lists/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,11 @@ export async function GET(req: Request): Promise<SchemaResponse<typeof schema.ge
const searchParams = new URLSearchParams(url.search)
const params = Object.fromEntries(searchParams)

const { pageSize = 50, ownerId } = schema.get.parameters.parse(params) ?? {}
const { ownerId, ...paginationParams } = schema.get.parameters.parse(params) ?? {}

const { lists, total } = await getLists({ ownerId }, undefined, {
skip: (1 - 1) * pageSize,
take: pageSize,
})
const results = await getLists({ ownerId }, paginationParams)

return NextResponse.json({
lists,
page: 1,
pageSize,
totalPages: Math.ceil(total / pageSize),
})
return NextResponse.json(results)
} catch (error) {
console.log(error) // eslint-disable-line no-console -- Log error for debugging
if (error instanceof Error) {
Expand Down
Loading

0 comments on commit 41ca412

Please sign in to comment.