Skip to content

Commit 134c26d

Browse files
committed
feat: add getOnePostHandler
1 parent f30ea17 commit 134c26d

File tree

4 files changed

+28
-28
lines changed

4 files changed

+28
-28
lines changed

src/plugins/swagger.ts

+1-10
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,7 @@ export default fp<FastifyDynamicSwaggerOptions>(async (fastify, opts) => {
1313
{
1414
url: 'http://localhost'
1515
}
16-
],
17-
components: {
18-
securitySchemes: {
19-
apiKey: {
20-
type: 'apiKey',
21-
name: 'apiKey',
22-
in: 'header'
23-
}
24-
}
25-
}
16+
]
2617
},
2718
hideUntagged: true,
2819
exposeRoute: true

src/routes/v1/posts/handler.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ import {
88
} from './schema'
99
import { posts } from './posts'
1010

11-
export const getAllPostsHandler: RouteHandler<{
11+
export const getPostsHandler: RouteHandler<{
1212
Querystring: PostsQuery
1313
Reply: GetPostsResponse
1414
}> = async function (req, reply) {
15-
if (req.query?.deleted !== undefined) {
16-
const { deleted } = req.query
15+
const { deleted } = req.query
16+
if (deleted !== undefined) {
1717
const filteredPosts = posts.filter((post) => post.deleted === deleted)
1818
reply.send({ posts: filteredPosts })
1919
} else reply.send({ posts })
2020
}
2121

22-
export const getPostsHandler: RouteHandler<{
22+
export const getOnePostHandler: RouteHandler<{
2323
Params: PostsParams
2424
Reply: GetPostsResponse | NotFoundResponse
2525
}> = async function (req, reply) {
@@ -44,29 +44,29 @@ export const postPostsHandler: RouteHandler<{
4444
export const putPostsHandler: RouteHandler<{
4545
Params: PostsParams
4646
Body: PostsBody
47-
Reply: Record<string, never> | NotFoundResponse
47+
Reply: NotFoundResponse
4848
}> = async function (req, reply) {
4949
const { postid } = req.params
5050
const post = posts.find((p) => p.id == postid)
5151
if (post) {
5252
post.title = req.body.title
5353
post.content = req.body.content
5454
post.tags = req.body.tags
55-
reply.code(204).send({})
55+
reply.code(204)
5656
} else {
5757
reply.code(404).send({ error: 'Post not found' })
5858
}
5959
}
6060

6161
export const deletePostsHandler: RouteHandler<{
6262
Params: PostsParams
63-
Reply: Record<string, never> | NotFoundResponse
63+
Reply: NotFoundResponse
6464
}> = async function (req, reply) {
6565
const { postid } = req.params
6666
const post = posts.find((p) => p.id == postid)
6767
if (post) {
6868
post.deleted = true
69-
reply.code(204).send({})
69+
reply.code(204)
7070
} else {
7171
reply.code(404).send({ error: 'Post not found' })
7272
}

src/routes/v1/posts/index.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
1-
import { FastifyPluginAsync } from 'fastify'
1+
import { type FastifyInstance } from 'fastify'
22
import {
33
postSchema,
44
postNotFoundSchema,
55
getPostsSchema,
6+
getOnePostSchema,
67
postPostsSchema,
78
putPostsSchema,
89
deletePostsSchema
910
} from './schema'
1011
import {
11-
getAllPostsHandler,
1212
getPostsHandler,
13+
getOnePostHandler,
1314
postPostsHandler,
1415
putPostsHandler,
1516
deletePostsHandler
1617
} from './handler'
1718

18-
const posts: FastifyPluginAsync = async (fastify): Promise<void> => {
19+
export default async (fastify: FastifyInstance) => {
1920
fastify.addSchema(postSchema)
2021
fastify.addSchema(postNotFoundSchema)
21-
fastify.get('/', { schema: getPostsSchema }, getAllPostsHandler)
22-
fastify.get('/:postid', { schema: getPostsSchema }, getPostsHandler)
22+
fastify.get('/', { schema: getPostsSchema }, getPostsHandler)
23+
fastify.get('/:postid', { schema: getOnePostSchema }, getOnePostHandler)
2324
fastify.post('/', { schema: postPostsSchema }, postPostsHandler)
2425
fastify.put('/:postid', { schema: putPostsSchema }, putPostsHandler)
2526
fastify.delete('/:postid', { schema: deletePostsSchema }, deletePostsHandler)
2627
}
27-
28-
export default posts

src/routes/v1/posts/schema.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,18 @@ export type GetPostsResponse = FromSchema<
7575
/* Get */
7676
export const getPostsSchema: FastifySchema = {
7777
tags: ['Posts'],
78-
description: 'Get a post by id',
78+
description: 'Get posts',
7979
querystring: postsQuerySchema,
80+
response: {
81+
200: {
82+
...getPostsResponseSchema
83+
}
84+
}
85+
}
86+
87+
export const getOnePostSchema: FastifySchema = {
88+
tags: ['Posts'],
89+
description: 'Get a post by id',
8090
params: postsParamsSchema,
8191
response: {
8292
200: {
@@ -117,7 +127,7 @@ export const putPostsSchema: FastifySchema = {
117127
response: {
118128
204: {
119129
description: 'The post was updated',
120-
type: 'object'
130+
type: 'null'
121131
},
122132
404: {
123133
description: 'The post was not found',
@@ -134,7 +144,7 @@ export const deletePostsSchema: FastifySchema = {
134144
response: {
135145
204: {
136146
description: 'The post was deleted',
137-
type: 'object'
147+
type: 'null'
138148
},
139149
404: {
140150
description: 'The post was not found',

0 commit comments

Comments
 (0)