Next.js-based API for managing recipes
Live demo available here.
Clone the repository:
$ git clone [email protected]:tylucaskelley/smoothies
Install the following:
- Node.js 15.3.0
- Or, run
nodenv install
from the root project folder, if using the Nodenv version manager
- Or, run
- Yarn 1.22.10
Run yarn install
to grab project dependencies. Next, configure a MySQL 8.0 database
for use during local development. Copy .env.example
to .env
and fill in the database
configuration information.
Then, run database migrations:
$ yarn migrate
Once done, start the local development server:
$ yarn dev
The following examples use the HTTPie client:
Creates a new recipe, can optionally include ingredients as well:
$ http post http://localhost:3000/api/recipes \
title="Strawberry Smoothie" author="Ty Kelley" description="A strawberry smoothie" prepTime=15 cookingTime=5 servings=4 \
ingredients:='[{ "name": "Sugar", "quantity": 5, "unit": "tsp" }, {"name": "Strawberries", "quantity": 2, "unit": "cups"}]' \
--verbose
Request body example:
{
"author": "Ty Kelley",
"description": "A strawberry smoothie",
"ingredients": [
{
"name": "Sugar",
"quantity": 5,
"unit": "tsp"
},
{
"name": "Strawberries",
"quantity": 2,
"unit": "cups"
}
],
"title": "Strawberry Smoothie",
"prepTime": 15,
"cookingTime": 5,
"servings": 4
}
The title of a recipe must be unique within the system.
Retrieve a list of recipes:
$ http get http://localhost:3000/api/recipes --verbose
Retrieve a single recipe, by ID:
$ http get http://localhost:3000/api/recipes/1 --verbose
Update a recipe and its ingredients. Only provide the fields you want to update:
$ http patch http://localhost:3000/api/recipes/1 title="Even better strawberry smoothie" --verbose
In the request body example below, note that new ingredients as well as existing ingredients can be handled.
- Passing in the ID of an existing ingredient will update it with the other changes
- Creating a new object (no ID) will add a new ingredient
- If the ingredients array is provided, any existing ingredients that are omitted will be deleted automatically.
{
"ingredients": [
{
"name": "Sugar",
"id": 14,
"quantity": 3,
"unit": "tsp"
},
{
"name": "Oranges",
"quantity": 2,
"unit": "slices"
}
],
"title": "Strawberry Smoothie w/ Orange"
}
Delete a recipe, by ID:
$ http delete http://localhost:3000/api/recipes/1 --verbose
Code is formatted automatically with Prettier, via a pre-commit hook. A few tasks are available as well:
yarn lint
: Check code style with ESLint, configured with TypeScript supportyarn format
: Format code using Prettieryarn type-check
: Validate TypeScript type annotations and catch errors
Run yarn test
to test the API endpoints; note that a working database connection is needed
for tests to function.
See LICENSE file for details.