Skip to content

Commit 0f75068

Browse files
committed
update api-server
1 parent b1b7a12 commit 0f75068

File tree

35 files changed

+1064
-872
lines changed

35 files changed

+1064
-872
lines changed

04-frameworks/08-nextjs/00-boilerplate/api-server/config/routes.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

04-frameworks/08-nextjs/00-boilerplate/api-server/mock-data/data.json

Lines changed: 0 additions & 121 deletions
This file was deleted.

04-frameworks/08-nextjs/00-boilerplate/api-server/package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
"description": "Car API",
55
"main": "index.js",
66
"scripts": {
7-
"mock-server": "json-server --routes ./config/routes.json --watch mock-data/data.json --port 3001"
7+
"mock-server": "tsx watch src/index.ts"
88
},
99
"author": "Lemoncode",
1010
"license": "MIT",
11+
"dependencies": {
12+
"@hono/node-server": "^1.12.2",
13+
"hono": "^4.5.11"
14+
},
1115
"devDependencies": {
12-
"json-server": "^1.0.0-beta.2"
16+
"tsx": "^4.19.0"
1317
}
1418
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { Hono } from 'hono';
2+
import { logger } from 'hono/logger';
3+
import { serve } from '@hono/node-server';
4+
import { serveStatic } from '@hono/node-server/serve-static';
5+
import { cars } from './mock-data';
6+
7+
let db = {
8+
cars,
9+
};
10+
11+
const app = new Hono();
12+
app.use(logger());
13+
app.use('/*', serveStatic({ root: './public' }));
14+
15+
app.get('/api/cars', (context) => {
16+
return context.json(db.cars);
17+
});
18+
19+
app.get('/api/cars/:id', (context) => {
20+
return context.json(db.cars.find((c) => c.id === context.req.param('id')));
21+
});
22+
23+
app.put('/api/cars/:id', async (context) => {
24+
const id = context.req.param('id');
25+
const car = await context.req.json();
26+
db.cars = db.cars.map((c) => (c.id === id ? car : c));
27+
return context.body(null, 204);
28+
});
29+
30+
serve({ fetch: app.fetch, port: 3001 }, (info) => {
31+
console.log(`API running on ${info.port}`);
32+
});
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
export const cars = [
2+
{
3+
id: '1',
4+
name: 'Audi Q8 Automático',
5+
imageUrl: '/audi-q8.png',
6+
features: [
7+
'5 asientos',
8+
'5 puertas',
9+
'Automático',
10+
'4 maletas',
11+
'Aire acondicionado',
12+
],
13+
isBooked: false,
14+
},
15+
{
16+
id: '2',
17+
name: 'BMW X7',
18+
imageUrl: '/bmw-x7.png',
19+
features: [
20+
'7 asientos',
21+
'5 puertas',
22+
'Automático',
23+
'2 maletas',
24+
'Aire acondicionado',
25+
'GPS',
26+
],
27+
isBooked: false,
28+
},
29+
{
30+
id: '3',
31+
name: 'Citroen C4 Cactus',
32+
imageUrl: '/citroen-c4-cactus.png',
33+
features: [
34+
'5 asientos',
35+
'5 puertas',
36+
'Manual',
37+
'1 maleta',
38+
'Aire acondicionado',
39+
],
40+
isBooked: false,
41+
},
42+
{
43+
id: '4',
44+
name: 'Jaguar F-Pace Automático',
45+
imageUrl: '/jaguar-f-pace.png',
46+
features: [
47+
'5 asientos',
48+
'5 puertas',
49+
'Automático',
50+
'2 maletas',
51+
'Aire acondicionado',
52+
],
53+
isBooked: false,
54+
},
55+
{
56+
id: '5',
57+
name: 'Mercedes-Benz Clase G',
58+
imageUrl: '/mb-g.png',
59+
features: [
60+
'5 asientos',
61+
'5 puertas',
62+
'Automático',
63+
'2 maletas',
64+
'Aire acondicionado',
65+
],
66+
isBooked: false,
67+
},
68+
{
69+
id: '6',
70+
name: 'Mercedes-Benz Clase V',
71+
imageUrl: '/mb-v-klasse.png',
72+
features: [
73+
'7 asientos',
74+
'5 puertas',
75+
'Automático',
76+
'3 maletas',
77+
'Aire acondicionado',
78+
],
79+
isBooked: false,
80+
},
81+
{
82+
id: '7',
83+
name: 'Peugeot Rifter',
84+
imageUrl: '/peugeot-rifter.png',
85+
features: [
86+
'5 asientos',
87+
'5 puertas',
88+
'Manual',
89+
'2 maletas',
90+
'Aire acondicionado',
91+
],
92+
isBooked: false,
93+
},
94+
{
95+
id: '8',
96+
name: 'Porsche Macan',
97+
imageUrl: '/porsche-macan.png',
98+
features: ['5 asientos', '5 puertas', 'Manual', '2 maletas'],
99+
isBooked: false,
100+
},
101+
{
102+
id: '9',
103+
name: 'VW Touran',
104+
imageUrl: '/vw-touran.png',
105+
features: [
106+
'5 asientos',
107+
'5 puertas',
108+
'Manual',
109+
'2 maletas',
110+
'Aire acondicionado',
111+
],
112+
isBooked: false,
113+
},
114+
];

04-frameworks/08-nextjs/01-config/api-server/config/routes.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)