# Hono API with Prisma on Cloudflare Workers
This project is a RESTful API built using [Hono](https://github.com/honojs/hono) and [Prisma](https://www.prisma.io/) deployed on Cloudflare Workers. It includes JWT authentication and CRUD operations for managing blogs.
## Table of Contents
- [Hono API with Prisma on Cloudflare Workers](#hono-api-with-prisma-on-cloudflare-workers)
- [Table of Contents](#table-of-contents)
- [Prerequisites](#prerequisites)
- [Setup](#setup)
- [Deployment](#deployment)
- [Environment Variables](#environment-variables)
- [API Endpoints](#api-endpoints)
- [User Signup](#user-signup)
- [User Signin](#user-signin)
- [Create a Blog](#create-a-blog)
- [Update a Blog](#update-a-blog)
- [Get a Blog by ID](#get-a-blog-by-id)
- [Delete a Blog by ID](#delete-a-blog-by-id)
- [Get All Blogs with Pagination](#get-all-blogs-with-pagination)
## Prerequisites
- Node.js and npm
- Wrangler CLI: `npm install -g wrangler`
- A Cloudflare account and a Workers KV namespace
## Setup
1. Clone the repository:
```bash
git clone https://github.com/hritikkkkk/medium-blog.git
cd medium-blog
- Install dependencies:
npm install
- Set up Prisma:
Update your prisma/schema.prisma
file with the appropriate data source and then generate the client:
npx prisma generate --no-engine
- Configure your
wrangler.toml
file:
name = "your-app-name"
type = "javascript"
compatibility_date = "2023-06-23"
[vars]
DATABASE_URL = "your-pooled-database-url"
JWT_SECRET = "your-jwt-secret"
- Deploy to Cloudflare Workers:
npm run deploy
DATABASE_URL
: URL of your database.JWT_SECRET
: Secret key for JWT authentication.
URL: POST /api/v1/user/signup
Body:
{
"email": "[email protected]",
"password": "password"
}
URL: PUT /api/v1/user/signin
Body:
{
"email": "[email protected]",
"password": "password"
}
URL: POST /api/v1/blog
Headers:
Authorization: Bearer YOUR_JWT_TOKEN
Content-Type: application/json
Body:
{
"title": "Sample Post Title",
"content": "This is the content of the sample post."
}
URL: PUT /api/v1/blog
Headers:
Authorization: Bearer YOUR_JWT_TOKEN
Content-Type: application/json
Body:
{
"id": "POST_ID_TO_UPDATE",
"title": "Updated Post Title",
"content": "This is the updated content of the post."
}
URL: GET /api/v1/blog/:id
Headers:
Authorization: Bearer YOUR_JWT_TOKEN
URL: DELETE /api/v1/blog/:id
Headers:
Authorization: Bearer YOUR_JWT_TOKEN
URL: GET /api/v1/blog
Headers:
Authorization: Bearer YOUR_JWT_TOKEN
Query Parameters:
page
: The page number to retrieve (e.g.,1
).pageSize
: The number of posts per page (e.g.,10
).
Example URL with query parameters: https://backend.hritik-7827.workers.dev/api/v1/blog?page=1&pageSize=10