Skip to content

Latest commit

 

History

History
229 lines (178 loc) · 4.51 KB

File metadata and controls

229 lines (178 loc) · 4.51 KB

RESTful API License: MIT

Supported packages, build your own http server less than 5 minutes:

  • Deserve - HTTP framework (file-based routing)
  • Jsonary - File-based database (JSON)

📦 Installation

# Clone or download this project
git clone https://github.com/NeaByteLab/Restful-API.git
cd Restful-API

🏃 Quick Start

1. Start the Server

deno task start

Server runs on: http://localhost:8000

2. Test the API

# 1. Get API info
curl http://localhost:8000/

# 2. List all users
curl http://localhost:8000/users

# 3. Create a new user
curl -X POST http://localhost:8000/users \
  -H "Content-Type: application/json" \
  -d '{"name":"Alice","email":"alice@example.com","age":30}'

# 4. Get user by ID (replace {id} with actual user ID from step 3)
curl http://localhost:8000/users/{id}

# 5. Update user (replace {id} with actual user ID)
curl -X PUT http://localhost:8000/users/{id} \
  -H "Content-Type: application/json" \
  -d '{"name":"Alice Updated","email":"alice.updated@example.com"}'

# 6. Delete user (replace {id} with actual user ID)
curl -X DELETE http://localhost:8000/users/{id}

# 7. Test error handling - Non-existent user (404)
curl http://localhost:8000/users/non-existent-id

# 8. Test validation - Missing required fields (400)
curl -X POST http://localhost:8000/users \
  -H "Content-Type: application/json" \
  -d '{"name":"Test"}'

📚 API Endpoints

Base URL

http://localhost:8000

Endpoints

Method Endpoint Description
GET / API information
GET /users List all users
POST /users Create a user
GET /users/:id Get user by ID
PUT /users/:id Update user
DELETE /users/:id Delete user

📖 API Documentation

GET / - API Info

Returns API information and available endpoints.

Response:

{
  "name": "RESTful API",
  "version": "1.0.0",
  "endpoints": {
    "users": {
      "list": "GET /users",
      "create": "POST /users",
      "get": "GET /users/:id",
      "update": "PUT /users/:id",
      "delete": "DELETE /users/:id"
    }
  }
}

GET /users - List All Users

Response:

{
  "total": 2,
  "users": [
    {
      "name": "Alice",
      "email": "alice@example.com",
      "age": 30,
      "id": "uuid-here",
      "createdAt": "2024-01-01T00:00:00.000Z",
      "updatedAt": "2024-01-01T00:00:00.000Z"
    }
  ]
}

POST /users - Create User

Request:

{
  "name": "Bob",
  "email": "bob@example.com",
  "age": 25
}

Required fields: name, email

Response (201):

{
  "message": "User created successfully",
  "user": {
    "name": "Bob",
    "email": "bob@example.com",
    "age": 25,
    "id": "uuid-here",
    "createdAt": "2024-01-01T00:00:00.000Z",
    "updatedAt": "2024-01-01T00:00:00.000Z"
  }
}

GET /users/:id - Get User

Response:

{
  "user": {
    "name": "Alice",
    "email": "alice@example.com",
    "age": 30,
    "id": "uuid-here",
    "createdAt": "2024-01-01T00:00:00.000Z",
    "updatedAt": "2024-01-01T00:00:00.000Z"
  }
}

PUT /users/:id - Update User

Request:

{
  "name": "Alice Updated",
  "email": "alice.new@example.com"
}

Response:

{
  "message": "User updated successfully",
  "user": {
    "name": "Alice Updated",
    "email": "alice.new@example.com",
    "age": 30,
    "id": "uuid-here",
    "updatedAt": "2024-01-01T01:00:00.000Z"
  }
}

DELETE /users/:id - Delete User

Response (200):

{
  "message": "User deleted successfully",
  "deleted": 1,
  "id": "uuid-here"
}

🏗️ Project Structure

Restful-API/
├── core/
│   ├── index.ts          # Server setup
│   ├── db.ts             # Database instance
│   └── mware/
│       └── logger.ts     # Request logging
├── routes/
│   ├── index.ts          # GET /
│   ├── users.ts          # GET, POST /users
│   └── users/
│       └── [id].ts       # GET, PUT, DELETE /users/:id
├── db.json               # Data storage (auto-generated)
├── deno.json             # Deno configuration
└── README.md             # This file

📄 License

This project is licensed under the MIT license. See the LICENSE file for more info.