Skip to content

Commit 0bd5b5b

Browse files
[autofix.ci] apply automated fixes
1 parent fbbbbaa commit 0bd5b5b

File tree

1 file changed

+24
-29
lines changed

1 file changed

+24
-29
lines changed

content/4.resources/1.learn/2.h3-101-first-hand.md

+24-29
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ resources:
66
- # add a link to the documentation
77
---
88

9-
109
H3 is a minimal http framework for high performance and portability.
1110

1211
During this tutorial, we will create a simple app to get a wide overview of H3 capabilities. This app will serve an HTML file populated with data. There will be some forms to add and remove data. At the end, we will see how to add an API endpoint to get the data in JSON format.
@@ -44,9 +43,9 @@ And that's it! We are ready to start coding.
4443
To create our first H3 app, we need to create an `app.ts` file at the root of our project. Inside, we will create a new app by importing the `createApp` function from H3 and calling it:
4544

4645
```ts [app.ts]
47-
import { createApp } from 'h3';
46+
import { createApp } from 'h3'
4847

49-
export const app = createApp();
48+
export const app = createApp()
5049
```
5150

5251
:read-more{to="https://h3.unjs.io/concepts/app" title="App"}
@@ -80,13 +79,13 @@ Now that our app is ready to accept HTTP requests, we need to create a router to
8079
With H3, we've just to use the function `createRouter` and add it to our app:
8180

8281
```ts [app.ts]
83-
import { createApp, createRouter } from 'h3';
82+
import { createApp, createRouter } from 'h3'
8483

85-
export const app = createApp();
84+
export const app = createApp()
8685

87-
const router = createRouter();
86+
const router = createRouter()
8887

89-
app.use(router);
88+
app.use(router)
9089
```
9190

9291
The `app.use(router)`{lang="ts"} is necessary to add the router to our app.
@@ -105,11 +104,11 @@ To add a handler, we can use any of the HTTP methods available on the router. Fo
105104
```ts [app.ts]
106105
// ...
107106

108-
const router = createRouter();
107+
const router = createRouter()
109108

110109
router.get('/', () => {
111-
return 'Hello World!';
112-
});
110+
return 'Hello World!'
111+
})
113112
```
114113

115114
In the code above, we added a handler for the `/` route. This handler will send the string `Hello World!` to the client with a simple `return`{lang="ts"}.
@@ -123,51 +122,48 @@ For our app, we will return an HTML page populated with some data. This part wil
123122
To create our fake database (a JavaScript array) with some getters and setters, we need a file named `database.ts`:
124123

125124
```ts [database.ts]
126-
import { Book } from "./types";
125+
import { Book } from './types'
127126

128127
/**
129128
* This is a fake database since it's just an array of objects.
130129
*
131130
* For this example, it's sufficient but do not use this in production.
132131
*/
133132
const database: Book[] = [{
134-
title: "Anna Karenina",
133+
title: 'Anna Karenina',
135134
price: 42,
136135
}, {
137-
title: "Madame Bovary",
136+
title: 'Madame Bovary',
138137
price: 15,
139138
}, {
140-
title: "War and Peace",
139+
title: 'War and Peace',
141140
price: 36,
142141
}, {
143-
title: "The Great Gatsby",
142+
title: 'The Great Gatsby',
144143
price: 87,
145144
}, {
146-
title: "Lolita",
145+
title: 'Lolita',
147146
price: 23,
148-
}
149-
];
147+
}]
150148

151149
export function getBooks(): Book[] {
152-
return database;
150+
return database
153151
}
154152

155153
export function addBook(book: Book) {
156-
database.push(book);
154+
database.push(book)
157155
}
158156

159157
export function removeBook(title: string) {
160-
const item = database.find((item) => item.title === title);
158+
const item = database.find(item => item.title === title)
161159

162-
if (!item) {
160+
if (!item)
163161
return
164-
}
165162

166-
const index = database.indexOf(item);
163+
const index = database.indexOf(item)
167164

168-
if (index > -1) {
169-
database.splice(index, 1);
170-
}
165+
if (index > -1)
166+
database.splice(index, 1)
171167
}
172168
```
173169

@@ -189,7 +185,6 @@ For this first route, we will get the books from the database and render them in
189185

190186
For the style, we will use [Pico CSS](https://picocss.com/).
191187

192-
193188
```ts [app.ts]
194189
// ...
195190

@@ -209,7 +204,7 @@ router.get('/', defineEventHandler(() => {
209204
<section>
210205
<h1>Books</h1>
211206
<ul>
212-
${books.map((book) => /* html */`
207+
${books.map(book => /* html */`
213208
<li>
214209
${book.title} - ${book.price}
215210
<form action="/remove" method="post">

0 commit comments

Comments
 (0)