https://github.com/antoniojvargas/smartcar
A Node.js and Express API that simulates Smartcar vehicle endpoints, including vehicle info, doors, fuel, battery, and engine control. Includes Swagger (OpenAPI) documentation and unit tests with Mocha, Chai, and Sinon.
https://github.com/antoniojvargas/smartcar
chai express mocha nodejs openapi rest-api sinon smartcar swagger
Last synced: 4 months ago
JSON representation
A Node.js and Express API that simulates Smartcar vehicle endpoints, including vehicle info, doors, fuel, battery, and engine control. Includes Swagger (OpenAPI) documentation and unit tests with Mocha, Chai, and Sinon.
- Host: GitHub
- URL: https://github.com/antoniojvargas/smartcar
- Owner: antoniojvargas
- License: mit
- Created: 2025-09-09T01:02:49.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2025-09-19T17:38:06.000Z (5 months ago)
- Last Synced: 2025-09-19T19:56:54.396Z (5 months ago)
- Topics: chai, express, mocha, nodejs, openapi, rest-api, sinon, smartcar, swagger
- Language: JavaScript
- Homepage:
- Size: 202 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# smartcar# ๐ Smartcar API
A Node.js + Express implementation of a **Smartcar-like API** that provides vehicle information, security status, fuel/battery levels, and engine control.
The project follows clean architecture principles with controllers, services, and logging integration.
---
## ๐ Features
- **Vehicle Info** โ `GET /vehicles/:id`
- **Door Security Status** โ `GET /vehicles/:id/doors`
- **Fuel Level** โ `GET /vehicles/:id/fuel`
- **Battery Level** โ `GET /vehicles/:id/battery`
- **Start/Stop Engine** โ `POST /vehicles/:id/engine`
- Structured **logging** using [winston](https://github.com/winstonjs/winston).
- Unit tests with **Mocha + Chai + Sinon**.
- API documentation via **OpenAPI (Swagger)**.
---
## โ๏ธ Installation
Clone the repository and install dependencies:
```bash
git clone https://github.com/antoniojvargas/smartcar.git
cd smartcar
```
## ๐ณ Running with Docker
This project is containerized using Docker Compose.
No need to install Node.js locally โ just run:
```bash
docker-compose up -d --build
```
This will:
- Build the smartcar image
- Start the container in development mode (nodemon)
- Expose the API on http://localhost:3000
To stop the container:
```bash
docker-compose down
```
---
## ๐งช Testing
Run the unit tests with Mocha + Chai + Sinon:
```bash
npm test
```
The test suite includes:
- โ
Happy path responses
- โ Error handling (bad input, failed API responses)
- โก Edge cases (incomplete/malformed data)
## ๐ API Endpoints
Vehicle Info
GET /vehicles/:id
Example
```bash
curl -X GET http://localhost:3000/vehicles/1234
```
Response
```json
{
"vin": "1213231",
"color": "Metallic Silver",
"doorCount": 4,
"driveTrain": "v8"
}
```
---
Security (Doors)
GET /vehicles/:id/doors
Example
```bash
curl -X GET http://localhost:3000/vehicles/1234/doors
```
Response
```json
[
{ "location": "frontLeft", "locked": true },
{ "location": "frontRight", "locked": true },
{ "location": "backLeft", "locked": true },
{ "location": "backRight", "locked": false }
]
```
---
Fuel Range
GET /vehicles/:id/fuel
Example
```bash
curl -X GET http://localhost:3000/vehicles/1234/fuel
```
Response
```json
{ "percent": 30.2 }
```
---
Battery Range
GET /vehicles/:id/battery
Example
```bash
curl -X GET http://localhost:3000/vehicles/1234/battery
```
Response
```json
{ "percent": 50.3 }
```
---
Start/Stop Engine
POST /vehicles/:id/engine
Content-Type: application/json
Example
```bash
curl -X POST http://localhost:3000/vehicles/1234/engine \
-H "Content-Type: application/json" \
-d '{"action":"START"}'
```
Response
```json
{ "status": "success" }
```
---
## ๐ ๐ Interactive API Documentation (Swagger UI)
You can explore and test all endpoints using the built-in Swagger UI:
โก๏ธ http://localhost:3000/api-docs
This interactive documentation is automatically generated from the openapi.yml specification.
---
## ๐ Project Structure
```bash
src/
doc/ # API documentation files (e.g. OpenAPI/Swagger specs, markdown docs)
errors/ # Custom error classes (ValidationError, NotFoundError, etc.)
middleware/ # Express middleware (validation, sanitization, logging, etc.)
MMApi/
providers/ # External API providers (e.g. MM API integration layer)
routes/ # Express route definitions (map endpoints to controllers)
schemas/ # Joi schemas and validation logic
transformers/ # Transform raw API data
utils/logger.js # Winston logger configuration and setup
validators # Validate API responses (status codes, shapes)
app.js # Express app setup (middleware, routes, error handling)
server.js # Application entry point (starts the HTTP server)
tests/ # Unit tests
```
---
## ๐ Logging
Logging is handled by Winston:
- info โ normal requests
- warn โ API/data issues
- error โ unexpected failures
Logs are printed to the console, and can be extended to files or external monitoring services.
---
## ๐ License
MIT License. Free to use and modify.