-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #155 from kk5190/Add-Vehicle-Endpoint
feat: Add vehicle endpoint
- Loading branch information
Showing
7 changed files
with
139 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Request, Response } from 'express'; | ||
import * as core from 'express-serve-static-core'; | ||
import { getQtyFromRequest } from '../../../utils/route-utils'; | ||
import getRandomVehicles from '../utils/getRandomVehicles'; | ||
|
||
module.exports = function (app: core.Express) { | ||
/** | ||
* @openapi | ||
* "/vehicles/{qty}": | ||
* get: | ||
* tags: | ||
* - Vehicles | ||
* summary: Obtain a random list of vehicles | ||
* parameters: | ||
* - in: path | ||
* name: qty | ||
* description: The amount of vehicles you require | ||
* type: string | ||
* default: 10 | ||
* responses: | ||
* '200': | ||
* description: OK | ||
* schema: | ||
* $ref: '#/definitions/MockVehicles' | ||
*/ | ||
app.get('/vehicles/:qty?', (req: Request, res: Response) => { | ||
const qty = getQtyFromRequest(req, 10); | ||
const users = getRandomVehicles(qty); | ||
res.json(users); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* @openapi | ||
* definitions: | ||
* MockVehicles: | ||
* type: array | ||
* items: | ||
* type: object | ||
* properties: | ||
* name: | ||
* type: string | ||
* example: Jeep Model X | ||
* type: | ||
* type: string | ||
* example: Minivan | ||
* color: | ||
* type: string | ||
* example: tan | ||
* fuel: | ||
* type: string | ||
* example: Hybrid | ||
* manufacturer: | ||
* type: string | ||
* example: Jeep | ||
* model: | ||
* type: string | ||
* example: A8 | ||
* vin: | ||
* type: string | ||
* example: NP1WJW8L8WWM52689 | ||
* vrm: | ||
* type: string | ||
* example: JX30UEU | ||
*/ | ||
|
||
type Vehicle = { | ||
name: string; | ||
type: string; | ||
color: string; | ||
fuel: string; | ||
manufacturer: string; | ||
model: string; | ||
vin: string; | ||
vrm: string; | ||
}; | ||
|
||
export default Vehicle; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const request = require('supertest'); | ||
const baseURL = 'http://localhost:3000'; | ||
|
||
describe('vehicles api endpoints', () => { | ||
describe('GET /vehicles', () => { | ||
it('should return a vehicle', async () => { | ||
const response = await request(baseURL).get(`/vehicles/`); | ||
|
||
expect(response.body[0]).toHaveProperty('name'); | ||
expect(response.body[0]).toHaveProperty('type'); | ||
expect(response.body[0]).toHaveProperty('color'); | ||
expect(response.body[0]).toHaveProperty('fuel'); | ||
expect(response.body[0]).toHaveProperty('manufacturer'); | ||
expect(response.body[0]).toHaveProperty('model'); | ||
expect(response.body[0]).toHaveProperty('vin'); | ||
expect(response.body[0]).toHaveProperty('vrm'); | ||
}); | ||
}); | ||
|
||
describe('GET /vehicles/qty', () => { | ||
const qty = 5; | ||
|
||
it('should return a list of users ', async () => { | ||
const response = await request(baseURL).get(`/users/${qty}`); | ||
expect(response.body.length).toEqual(qty); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import getRandomVehicles from '../../utils/getRandomVehicles'; | ||
|
||
describe('get random vehicles', () => { | ||
it('should return a list of random vehicles', () => { | ||
const amount = 4; | ||
const res = getRandomVehicles(4); | ||
expect(res.length).toBe(amount); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import Vehicle from '../consts/Vehicle'; | ||
import { faker } from '@faker-js/faker'; | ||
|
||
const getRandomVehicles = (qty: number) => { | ||
const vehicles: Vehicle[] = []; | ||
|
||
Array.from({ length: qty }).forEach(() => { | ||
vehicles.push({ | ||
name: faker.vehicle.vehicle(), | ||
type: faker.vehicle.type(), | ||
color: faker.vehicle.color(), | ||
fuel: faker.vehicle.fuel(), | ||
manufacturer: faker.vehicle.manufacturer(), | ||
model: faker.vehicle.model(), | ||
vin: faker.vehicle.vin(), | ||
vrm: faker.vehicle.vrm(), | ||
}); | ||
}); | ||
|
||
return vehicles; | ||
}; | ||
|
||
export default getRandomVehicles; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2664,4 +2664,4 @@ | |
} | ||
} | ||
} | ||
} | ||
} |