Skip to content

Commit f3d422e

Browse files
committed
uses valibot to check body schema
1 parent a69048d commit f3d422e

File tree

4 files changed

+17
-17
lines changed

4 files changed

+17
-17
lines changed

app/composables/useAtproto.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { ERROR_NEED_REAUTH } from '#imports'
22
import type { FetchError } from 'ofetch'
33
import type { UserSession } from '#shared/schemas/userSession'
4-
import type { PackageLikes } from '#server/utils/atproto/utils/likes'
54

65
export async function authRedirect(identifier: string, create: boolean = false) {
76
let query = { handle: identifier } as {}

server/api/auth/social/like.delete.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import * as v from 'valibot'
12
import { Client } from '@atproto/lex'
23
import * as dev from '#shared/types/lexicons/dev'
4+
import { PackageLikeBodySchema } from '#shared/schemas/social'
35
import { throwOnMissingOAuthScope } from '#server/utils/atproto/oauth'
46

57
export default eventHandlerWithOAuthSession(async (event, oAuthSession) => {
@@ -12,14 +14,7 @@ export default eventHandlerWithOAuthSession(async (event, oAuthSession) => {
1214
//Checks if the user has a scope to like packages
1315
await throwOnMissingOAuthScope(oAuthSession, LIKES_SCOPE)
1416

15-
const body = await readBody<{ packageName: string }>(event)
16-
17-
if (!body.packageName) {
18-
throw createError({
19-
status: 400,
20-
message: 'packageName is required',
21-
})
22-
}
17+
const body = v.parse(PackageLikeBodySchema, await readBody(event))
2318

2419
const likesUtil = new PackageLikesUtils()
2520

server/api/auth/social/like.post.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import * as v from 'valibot'
12
import { Client } from '@atproto/lex'
23
import * as dev from '#shared/types/lexicons/dev'
34
import type { UriString } from '@atproto/lex'
45
import { LIKES_SCOPE } from '#shared/utils/constants'
6+
import { PackageLikeBodySchema } from '#shared/schemas/social'
57
import { throwOnMissingOAuthScope } from '~~/server/utils/atproto/oauth'
68

79
export default eventHandlerWithOAuthSession(async (event, oAuthSession) => {
@@ -14,14 +16,7 @@ export default eventHandlerWithOAuthSession(async (event, oAuthSession) => {
1416
//Checks if the user has a scope to like packages
1517
await throwOnMissingOAuthScope(oAuthSession, LIKES_SCOPE)
1618

17-
const body = await readBody<{ packageName: string }>(event)
18-
19-
if (!body.packageName) {
20-
throw createError({
21-
status: 400,
22-
message: 'packageName is required',
23-
})
24-
}
19+
const body = v.parse(PackageLikeBodySchema, await readBody(event))
2520

2621
const likesUtil = new PackageLikesUtils()
2722

shared/schemas/social.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as v from 'valibot'
2+
import { PackageNameSchema } from './package'
3+
4+
/**
5+
* Schema for liking/unliking a package
6+
*/
7+
export const PackageLikeBodySchema = v.object({
8+
packageName: PackageNameSchema,
9+
})
10+
11+
export type PackageLikeBody = v.InferOutput<typeof PackageLikeBodySchema>

0 commit comments

Comments
 (0)