Skip to content

Commit

Permalink
refactor: rename schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
wd-David committed May 20, 2022
1 parent f173e43 commit 3578512
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 34 deletions.
38 changes: 20 additions & 18 deletions src/routes/v1/posts/handler.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { type RouteHandler } from 'fastify'
import {
type PostsParams,
type PostsQuery,
type PostsBody,
type GetPostsResponse,
type NotFoundResponse
type Params,
type Querystring,
type Body,
type Reply,
type PostNotFound
} from './schema'
import { posts } from './posts'

export const getPostsHandler: RouteHandler<{
Querystring: PostsQuery
Reply: GetPostsResponse
Querystring: Querystring
Reply: Reply
}> = async function (req, reply) {
const { deleted } = req.query
if (deleted !== undefined) {
Expand All @@ -20,8 +20,8 @@ export const getPostsHandler: RouteHandler<{
}

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

export const postPostsHandler: RouteHandler<{
Body: PostsBody
Body: Body
Reply: Body
}> = async function (req, reply) {
const newPostID = posts.length + 1
posts.push({
const newPost = {
id: newPostID,
...req.body
})
}
posts.push(newPost)
console.log(posts)
reply.code(201).header('Location', `/posts/${newPostID}`).send({})
reply.code(201).header('Location', `/posts/${newPostID}`).send(newPost)
}

export const putPostsHandler: RouteHandler<{
Params: PostsParams
Body: PostsBody
Reply: NotFoundResponse
Params: Params
Body: Body
Reply: PostNotFound
}> = async function (req, reply) {
const { postid } = req.params
const post = posts.find((p) => p.id == postid)
Expand All @@ -59,8 +61,8 @@ export const putPostsHandler: RouteHandler<{
}

export const deletePostsHandler: RouteHandler<{
Params: PostsParams
Reply: NotFoundResponse
Params: Params
Reply: PostNotFound
}> = async function (req, reply) {
const { postid } = req.params
const post = posts.find((p) => p.id == postid)
Expand Down
32 changes: 16 additions & 16 deletions src/routes/v1/posts/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ export const postNotFoundSchema = {
additionalProperties: false
} as const

export type NotFoundResponse = FromSchema<typeof postNotFoundSchema>
export type PostNotFound = FromSchema<typeof postNotFoundSchema>

// Params Schema
const postsParamsSchema = {
const paramsSchema = {
type: 'object',
require: ['postid'],
properties: {
Expand All @@ -39,24 +39,24 @@ const postsParamsSchema = {
additionalProperties: false
} as const

export type PostsParams = FromSchema<typeof postsParamsSchema>
export type Params = FromSchema<typeof paramsSchema>

// Query Schema
const postsQuerySchema = {
const querystringSchema = {
type: 'object',
properties: {
deleted: { type: 'boolean' }
},
additionalProperties: false
} as const

export type PostsQuery = FromSchema<typeof postsQuerySchema>
export type Querystring = FromSchema<typeof querystringSchema>

// Body Schema
export type PostsBody = FromSchema<typeof postSchema>
export type Body = FromSchema<typeof postSchema>

// Response Schema
const getPostsResponseSchema = {
const replySchema = {
type: 'object',
properties: {
posts: {
Expand All @@ -67,30 +67,30 @@ const getPostsResponseSchema = {
additionalProperties: false
} as const

export type GetPostsResponse = FromSchema<
typeof getPostsResponseSchema,
export type Reply = FromSchema<
typeof replySchema,
{ references: [typeof postSchema] }
>

/* Get */
export const getPostsSchema: FastifySchema = {
tags: ['Posts'],
description: 'Get posts',
querystring: postsQuerySchema,
querystring: querystringSchema,
response: {
200: {
...getPostsResponseSchema
...replySchema
}
}
}

export const getOnePostSchema: FastifySchema = {
tags: ['Posts'],
description: 'Get a post by id',
params: postsParamsSchema,
params: paramsSchema,
response: {
200: {
...getPostsResponseSchema
...replySchema
},
404: {
description: 'The post was not found',
Expand All @@ -113,7 +113,7 @@ export const postPostsSchema: FastifySchema = {
description: 'URL of the new resource'
}
},
type: 'object'
...postSchema
}
}
}
Expand All @@ -122,7 +122,7 @@ export const postPostsSchema: FastifySchema = {
export const putPostsSchema: FastifySchema = {
tags: ['Posts'],
description: 'Update a post',
params: postsParamsSchema,
params: paramsSchema,
body: postSchema,
response: {
204: {
Expand All @@ -140,7 +140,7 @@ export const putPostsSchema: FastifySchema = {
export const deletePostsSchema: FastifySchema = {
tags: ['Posts'],
description: 'Delete a post',
params: postsParamsSchema,
params: paramsSchema,
response: {
204: {
description: 'The post was deleted',
Expand Down

0 comments on commit 3578512

Please sign in to comment.