This project is a serverless Blog API built using Cloudflare Workers, Prisma with Accelerate Extension, and Zod for schema validation. It features secure user authentication using JWT, API routes for blog CRUD operations, and a monorepo structure for shared types using @amrit_16/social-common.
- Features
- Project Structure
- Setup and Installation
- Environment Variables
- Routes
- Usage
- Deployment
- Monorepo with Shared Types
- Cloudflare Workers for serverless, scalable hosting.
- Prisma ORM with Accelerate extension for optimized database queries.
- JWT Authentication for secure access to API routes.
- Zod for request validation, ensuring consistent data structures.
- Monorepo setup with a shared
@amrit_16/social-commonpackage for data types.
The project is organized as follows:
.
├── backend/ # Backend routes and server setup
├── common/ # Shared Zod validation schemas and data types
└── README.md # Project documentation
- Node.js installed
- NPM
- Cloudflare CLI (
npx wrangler login) - Prisma and Prisma Client for database interaction
-
Clone the repository:
bash
Copy code
git clone <repository-url> cd blog-api -
Install dependencies for both backend and common:
bash
Copy code
`cd common npm install npm run build npm publish --access public # optional, for publishing to npm
cd ../backend npm install`
-
Initialize Prisma and configure your database:
bash
Copy code
npx prisma init -
Generate Prisma Client:
bash
Copy code
npx prisma generate -
Install Cloudflare Wrangler CLI:
bash
Copy code
npm install -g wrangler -
Login to Cloudflare:
bash
Copy code
npx wrangler login -
Add Environment Variables (explained below).
Configure the following environment variables in your Cloudflare dashboard or a .env file (for local testing):
JWT_SECRET- Secret key for JWT signing and verification.DATABASE_URL- Database connection URL for Prisma.
- Sign Up -
POST /api/v1/signup- Request Body:
{ "email": "user@example.com", "password": "securepass", "name": "User" } - Response:
{ "jwt": "user-token" }
- Request Body:
- Sign In -
POST /api/v1/signin- Request Body:
{ "email": "user@example.com", "password": "securepass" } - Response:
{ "jwt": "user-token" }
- Request Body:
-
Create Blog Post -
POST /api/v1/blog- Headers:
Authorization: Bearer <token> - Request Body:
{ "title": "Blog Title", "content": "Blog Content" } - Response:
{ "id": "post-id" }
- Headers:
-
Update Blog Post -
PUT /api/v1/blog- Headers:
Authorization: Bearer <token> - Request Body:
{ "id": "post-id", "title": "Updated Title", "content": "Updated Content" } - Response:
updated post
- Headers:
-
Get Blog Post by ID -
GET /api/v1/blog/:id- Response:
{ "title": "Blog Title", "content": "Blog Content", "authorId": "user-id" }
- Response:
To test API endpoints, use Postman or any API client.
- Sign Up to create a new user and obtain a JWT token.
- Sign In with the registered user credentials to obtain a JWT token.
- Use the JWT token in the
Authorizationheader (e.g.,Authorization: Bearer <token>) to access the blog routes.
bash
Copy code
curl --request POST \ --url http://localhost:8787/api/v1/signup \ --header 'Content-Type: application/json' \ --data '{ "email": "user@example.com", "password": "password123" }'
-
Build and deploy your app using Wrangler.
bash
Copy code
npm run deploy -
Update environment variables on Cloudflare Dashboard under Workers > Settings.
-
Test production API URL in Postman.
- Create a folder named
commonwith Zod types for data validation. - Publish to npm if needed for use in multiple projects.
- signupInput: Input validation for sign-up.
- signinInput: Input validation for sign-in.
- createPostInput: Input validation for creating a post.
- updatePostInput: Input validation for updating a post.