An open API service indexing awesome lists of open source software.

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.

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.