Skip to content

Commit

Permalink
feat(1.2b): Send "Not Found" for non-existent resources
Browse files Browse the repository at this point in the history
  • Loading branch information
one-aalam committed Jul 13, 2021
1 parent 0214cf3 commit 5148ed6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/routes/categories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,11 @@ export default async function categories(fastify: FastifyInstance) {
// get the category by provided id
fastify.get<{
Params: CategoryParams
}>('/categories/:id', async (req) => RECIPE_CATEGORIES.find(recipeCategory => recipeCategory.id === parseInt(req.params.id)))
}>('/categories/:id', async (req, reply) => {
const recipeCategory = RECIPE_CATEGORIES.find(recipeCategory => recipeCategory.id === parseInt(req.params.id))
if(!recipeCategory) {
reply.code(404).type('text/html').send('Not Found')
}
return recipeCategory
})
}
8 changes: 7 additions & 1 deletion src/routes/recipes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,11 @@ export default async function recipes(fastify: FastifyInstance) {
// get the recipe by provided id
fastify.get<{
Params: RecipeParams
}>('/recipes/:id', async (req) => RECIPES.find(recipe => recipe.id === parseInt(req.params.id)))
}>('/recipes/:id', async (req, reply) => {
const recipe = RECIPES.find(recipe => recipe.id === parseInt(req.params.id))
if(!recipe) {
reply.code(404).type('text/html').send('Not Found')
}
return recipe
})
}
8 changes: 7 additions & 1 deletion src/routes/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,11 @@ export default async function users(fastify: FastifyInstance) {
// get the user by provided id
fastify.get<{
Params: UserParams
}>('/users/:id', async (req) => USERS.find(user => user.id === parseInt(req.params.id)))
}>('/users/:id', async (req, reply) => {
const user = USERS.find(user => user.id === parseInt(req.params.id))
if(!user) {
reply.code(404).type('text/html').send('Not Found')
}
return user
})
}

0 comments on commit 5148ed6

Please sign in to comment.