Skip to content

Commit 3578512

Browse files
committed
refactor: rename schemas
1 parent f173e43 commit 3578512

File tree

2 files changed

+36
-34
lines changed

2 files changed

+36
-34
lines changed

src/routes/v1/posts/handler.ts

+20-18
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import { type RouteHandler } from 'fastify'
22
import {
3-
type PostsParams,
4-
type PostsQuery,
5-
type PostsBody,
6-
type GetPostsResponse,
7-
type NotFoundResponse
3+
type Params,
4+
type Querystring,
5+
type Body,
6+
type Reply,
7+
type PostNotFound
88
} from './schema'
99
import { posts } from './posts'
1010

1111
export const getPostsHandler: RouteHandler<{
12-
Querystring: PostsQuery
13-
Reply: GetPostsResponse
12+
Querystring: Querystring
13+
Reply: Reply
1414
}> = async function (req, reply) {
1515
const { deleted } = req.query
1616
if (deleted !== undefined) {
@@ -20,8 +20,8 @@ export const getPostsHandler: RouteHandler<{
2020
}
2121

2222
export const getOnePostHandler: RouteHandler<{
23-
Params: PostsParams
24-
Reply: GetPostsResponse | NotFoundResponse
23+
Params: Params
24+
Reply: Reply | PostNotFound
2525
}> = async function (req, reply) {
2626
const { postid } = req.params
2727
const post = posts.find((p) => p.id == postid)
@@ -30,21 +30,23 @@ export const getOnePostHandler: RouteHandler<{
3030
}
3131

3232
export const postPostsHandler: RouteHandler<{
33-
Body: PostsBody
33+
Body: Body
34+
Reply: Body
3435
}> = async function (req, reply) {
3536
const newPostID = posts.length + 1
36-
posts.push({
37+
const newPost = {
3738
id: newPostID,
3839
...req.body
39-
})
40+
}
41+
posts.push(newPost)
4042
console.log(posts)
41-
reply.code(201).header('Location', `/posts/${newPostID}`).send({})
43+
reply.code(201).header('Location', `/posts/${newPostID}`).send(newPost)
4244
}
4345

4446
export const putPostsHandler: RouteHandler<{
45-
Params: PostsParams
46-
Body: PostsBody
47-
Reply: NotFoundResponse
47+
Params: Params
48+
Body: Body
49+
Reply: PostNotFound
4850
}> = async function (req, reply) {
4951
const { postid } = req.params
5052
const post = posts.find((p) => p.id == postid)
@@ -59,8 +61,8 @@ export const putPostsHandler: RouteHandler<{
5961
}
6062

6163
export const deletePostsHandler: RouteHandler<{
62-
Params: PostsParams
63-
Reply: NotFoundResponse
64+
Params: Params
65+
Reply: PostNotFound
6466
}> = async function (req, reply) {
6567
const { postid } = req.params
6668
const post = posts.find((p) => p.id == postid)

src/routes/v1/posts/schema.ts

+16-16
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ export const postNotFoundSchema = {
2727
additionalProperties: false
2828
} as const
2929

30-
export type NotFoundResponse = FromSchema<typeof postNotFoundSchema>
30+
export type PostNotFound = FromSchema<typeof postNotFoundSchema>
3131

3232
// Params Schema
33-
const postsParamsSchema = {
33+
const paramsSchema = {
3434
type: 'object',
3535
require: ['postid'],
3636
properties: {
@@ -39,24 +39,24 @@ const postsParamsSchema = {
3939
additionalProperties: false
4040
} as const
4141

42-
export type PostsParams = FromSchema<typeof postsParamsSchema>
42+
export type Params = FromSchema<typeof paramsSchema>
4343

4444
// Query Schema
45-
const postsQuerySchema = {
45+
const querystringSchema = {
4646
type: 'object',
4747
properties: {
4848
deleted: { type: 'boolean' }
4949
},
5050
additionalProperties: false
5151
} as const
5252

53-
export type PostsQuery = FromSchema<typeof postsQuerySchema>
53+
export type Querystring = FromSchema<typeof querystringSchema>
5454

5555
// Body Schema
56-
export type PostsBody = FromSchema<typeof postSchema>
56+
export type Body = FromSchema<typeof postSchema>
5757

5858
// Response Schema
59-
const getPostsResponseSchema = {
59+
const replySchema = {
6060
type: 'object',
6161
properties: {
6262
posts: {
@@ -67,30 +67,30 @@ const getPostsResponseSchema = {
6767
additionalProperties: false
6868
} as const
6969

70-
export type GetPostsResponse = FromSchema<
71-
typeof getPostsResponseSchema,
70+
export type Reply = FromSchema<
71+
typeof replySchema,
7272
{ references: [typeof postSchema] }
7373
>
7474

7575
/* Get */
7676
export const getPostsSchema: FastifySchema = {
7777
tags: ['Posts'],
7878
description: 'Get posts',
79-
querystring: postsQuerySchema,
79+
querystring: querystringSchema,
8080
response: {
8181
200: {
82-
...getPostsResponseSchema
82+
...replySchema
8383
}
8484
}
8585
}
8686

8787
export const getOnePostSchema: FastifySchema = {
8888
tags: ['Posts'],
8989
description: 'Get a post by id',
90-
params: postsParamsSchema,
90+
params: paramsSchema,
9191
response: {
9292
200: {
93-
...getPostsResponseSchema
93+
...replySchema
9494
},
9595
404: {
9696
description: 'The post was not found',
@@ -113,7 +113,7 @@ export const postPostsSchema: FastifySchema = {
113113
description: 'URL of the new resource'
114114
}
115115
},
116-
type: 'object'
116+
...postSchema
117117
}
118118
}
119119
}
@@ -122,7 +122,7 @@ export const postPostsSchema: FastifySchema = {
122122
export const putPostsSchema: FastifySchema = {
123123
tags: ['Posts'],
124124
description: 'Update a post',
125-
params: postsParamsSchema,
125+
params: paramsSchema,
126126
body: postSchema,
127127
response: {
128128
204: {
@@ -140,7 +140,7 @@ export const putPostsSchema: FastifySchema = {
140140
export const deletePostsSchema: FastifySchema = {
141141
tags: ['Posts'],
142142
description: 'Delete a post',
143-
params: postsParamsSchema,
143+
params: paramsSchema,
144144
response: {
145145
204: {
146146
description: 'The post was deleted',

0 commit comments

Comments
 (0)