|
1 | | -# 🌌 Intergalactic Bank API |
| 1 | +# Book API |
2 | 2 |
|
3 | | -REST API for bank accounts and transactions with multi-currency support (COSMIC_COINS, GALAXY_GOLD, MOON_BUCKS). |
| 3 | +Simple REST API for books – CRUD only, no auth. Good for demos and learning. |
4 | 4 |
|
5 | | -## Quick Start |
| 5 | +## Quick start |
6 | 6 |
|
7 | 7 | ```bash |
8 | 8 | npm install && npm run dev |
9 | | -curl http://localhost:3000/health |
10 | 9 | ``` |
11 | 10 |
|
12 | | -Default: **http://localhost:3000**. Generate API key: `GET /api/v1/auth`. Default admin key: `1234`. |
| 11 | +Base URL: **http://localhost:3000** |
13 | 12 |
|
14 | 13 | ## Endpoints |
15 | 14 |
|
16 | | -| Endpoint | Method | Auth | Description | |
17 | | -|----------|--------|------|-------------| |
18 | | -| `/health` | GET | No | Health check | |
19 | | -| `/api/v1/auth` | GET | No | Generate API key | |
20 | | -| `/api/v1/accounts` | GET, POST | Yes | List / create accounts | |
21 | | -| `/api/v1/accounts/:id` | GET, PUT, DELETE | Yes | Get / update / delete (soft) | |
22 | | -| `/api/v1/transactions` | GET, POST | Yes | List / transfer or deposit | |
23 | | -| `/api/v1/transactions/:id` | GET | Yes | Get transaction | |
| 15 | +| Method | Endpoint | Description | |
| 16 | +|--------|----------|-------------| |
| 17 | +| GET | `/health` | Health check | |
| 18 | +| GET | `/api/v1/books` | List all books | |
| 19 | +| GET | `/api/v1/books/:id` | Get one book | |
| 20 | +| POST | `/api/v1/books` | Create a book | |
| 21 | +| PUT | `/api/v1/books/:id` | Update a book | |
| 22 | +| DELETE | `/api/v1/books/:id` | Delete a book | |
24 | 23 |
|
25 | | -**Auth:** send `x-api-key: your-key` on all protected routes. **Rate limit:** 300 req/min per key. |
| 24 | +No authentication required. |
26 | 25 |
|
27 | | -## Postman |
| 26 | +## Examples |
28 | 27 |
|
29 | | -1. Import `OpenAPI/Bank API Reference Documentation.postman_collection.json` |
30 | | -2. Set `baseUrl` → `http://localhost:3000`, `apiKey` → `1234` |
31 | | - |
32 | | -## Commands |
33 | | - |
34 | | -| Command | Description | |
35 | | -|---------|-------------| |
36 | | -| `npm run dev` | Dev server (auto-reload) | |
37 | | -| `npm start` | Production | |
38 | | -| `npm test` | Run tests | |
39 | | -| `npm test -- --coverage` | Tests + coverage | |
40 | | -| `npm run lint` | Lint | |
41 | | - |
42 | | -## Config (optional `.env`) |
43 | | - |
44 | | -``` |
45 | | -PORT=3000 |
46 | | -ADMIN_API_KEY=1234 |
47 | | -RATE_LIMIT_REQUESTS=300 |
48 | | -RATE_LIMIT_WINDOW_MS=60000 |
49 | | -``` |
50 | | - |
51 | | -## Project Layout |
52 | | - |
53 | | -``` |
54 | | -src/ |
55 | | -├── server.js # Entry |
56 | | -├── database/db.js # In-memory store |
57 | | -├── models/ # Account, Transaction |
58 | | -├── routes/ # admin, accounts, transactions |
59 | | -└── middleware/ # auth, errorHandler, rateLimit |
| 28 | +**List books** |
| 29 | +```bash |
| 30 | +curl http://localhost:3000/api/v1/books |
60 | 31 | ``` |
61 | 32 |
|
62 | | -## Important Behavior |
63 | | - |
64 | | -- **Ownership** – Accounts are scoped to the API key that created them. |
65 | | -- **Soft delete** – Deleted accounts are flagged; history kept. |
66 | | -- **Editable** – Only `owner` and `accountType`; balance/currency change via transactions only. |
67 | | - |
68 | | -## Example Requests |
69 | | - |
70 | | -**Create account** `POST /api/v1/accounts`: |
71 | | -```json |
72 | | -{ "owner": "John Doe", "currency": "COSMIC_COINS", "balance": 1000, "accountType": "STANDARD" } |
| 33 | +**Create book** |
| 34 | +```bash |
| 35 | +curl -X POST http://localhost:3000/api/v1/books \ |
| 36 | + -H "Content-Type: application/json" \ |
| 37 | + -d '{"title":"Dune","author":"Frank Herbert","year":1965}' |
73 | 38 | ``` |
74 | 39 |
|
75 | | -**Transfer** `POST /api/v1/transactions`: |
76 | | -```json |
77 | | -{ "fromAccountId": "123", "toAccountId": "456", "amount": 500, "currency": "COSMIC_COINS" } |
| 40 | +**Update book** (PUT with full or partial fields) |
| 41 | +```bash |
| 42 | +curl -X PUT http://localhost:3000/api/v1/books/1 \ |
| 43 | + -H "Content-Type: application/json" \ |
| 44 | + -d '{"title":"Dune","author":"Frank Herbert","year":1965}' |
78 | 45 | ``` |
79 | 46 |
|
80 | | -**Deposit** – Use `"fromAccountId": "0"`. |
81 | | - |
82 | | -## Error Format |
83 | | - |
84 | | -```json |
85 | | -{ "error": { "name": "errorType", "message": "Description" } } |
| 47 | +**Delete book** |
| 48 | +```bash |
| 49 | +curl -X DELETE http://localhost:3000/api/v1/books/1 |
86 | 50 | ``` |
87 | 51 |
|
88 | | -Common codes: **400** validation, **401** auth, **403** forbidden, **404** not found, **429** rate limit, **500** server error. |
| 52 | +## Book shape |
89 | 53 |
|
90 | | -## Sample Data (on startup) |
| 54 | +- **title** (string, required) |
| 55 | +- **author** (string, required) |
| 56 | +- **year** (number, optional) |
91 | 57 |
|
92 | | -- Nova Newman (10k COSMIC_COINS), Gary Galaxy (237 COSMIC_COINS), Luna Starlight (5k GALAXY_GOLD) – all under admin key `1234`. |
| 58 | +Responses use `{ book: {...} }` or `{ books: [...] }`. Errors use `{ error: { name, message } }`. |
93 | 59 |
|
94 | | -## Account Types & Currencies |
| 60 | +## Commands |
95 | 61 |
|
96 | | -**Types:** STANDARD, PREMIUM, BUSINESS. **Currencies:** COSMIC_COINS, GALAXY_GOLD, MOON_BUCKS. |
| 62 | +- `npm run dev` – dev server with reload |
| 63 | +- `npm start` – production |
| 64 | +- `npm test` – run tests |
| 65 | +- `npm run lint` – lint |
97 | 66 |
|
98 | | -## Replacing Storage |
| 67 | +## Project layout |
99 | 68 |
|
100 | | -Swap in a real DB by updating `src/database/db.js` with your driver and CRUD; the rest of the app stays the same. |
| 69 | +``` |
| 70 | +src/ |
| 71 | +├── server.js |
| 72 | +├── database/db.js # In-memory store |
| 73 | +├── models/Book.js |
| 74 | +├── routes/books.js |
| 75 | +└── middleware/errorHandler.js |
| 76 | +``` |
101 | 77 |
|
102 | | ---- |
| 78 | +## Config |
103 | 79 |
|
104 | | -**More detail** → `CLAUDE.md` · **Tests** → `npm test` |
| 80 | +Optional `.env`: `PORT=3000` |
105 | 81 |
|
106 | 82 | License: ISC |
0 commit comments