API
The API for Cooper is built using tRPC.
All of the API code is in the api
package. This package is responsible for handling all the requests that come into the API. The API is split into multiple routers, each responsible for a specific set of endpoints.
Routers
Types of Requests
list
- Fetch all the objects for a particular resource in the databaseget
- Fetch a single object by its ID, name, or other unique identifiercreate
- Create a new object in the databasedelete
- Delete an object from the database
tRPC Operations
tRPC provides a set of operations that can be used to interact with the API. These operations are:
- Query - Fetch data from the API
- Mutation - Modify data in the API
tRPC also has other operations like Subscription
, but they are not used in Cooper at the moment.
Authentication
tRPC allows for authentication to be handled at the endpoint level. This means that each endpoint can specify whether it is a public
endpoint (publicProcedure
) or a protected
endpoint (protectedProcedure
). If an endpoint is protected
, the user must be authenticated to access it.
In general, most of the read operations are public
, while the write operations are protected
.
New Router
Adding a New Router
To add a new router to the API, follow these steps:
- Create a new router in the
router/
folder - Export it from the
index.ts
file - Add the route in the
root.ts
file
Defining Endpoints
An endpoint has two parts:
- The
input
type, which defines the shape of the request. This makes uses of Zod to validate the input. - The operation (query or mutation) that the endpoint performs