galbe 0.10.1 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,37 +1,31 @@
1
- # Getting started
1
+ # Getting Started
2
2
 
3
- Galbe is a Javascript web framework for building fast and versatile backend
4
- servers with Bun.
3
+ Galbe is a JavaScript web framework for building fast and versatile backend servers with Bun.
5
4
 
6
- Designed with simplicity in mind, Galbe allows you to quickly create and set up
7
- a project. In addition to its ease of use, Galbe also offers a range of useful
8
- features that help you focus on the core logic of your application.
5
+ Designed for simplicity, Galbe allows you to quickly create and configure a project. In addition to its ease of use, it offers various features that help you focus on your application's core logic.
9
6
 
10
7
  ## Requirements
11
8
 
12
- To start developing your Galbe project, you first need to install
13
- [Bun](https://bun.sh).
9
+ To start developing with Galbe, you first need to install [Bun](https://bun.sh).
14
10
 
15
- ## Automatic installation
11
+ ## Automatic Installation (Recommended)
16
12
 
17
- This is the recommended way of setting up a Galbe project.
13
+ This is the recommended way to set up a Galbe project.
18
14
 
19
15
  ```bash
20
16
  $ bun create galbe app
21
17
  ```
22
18
 
23
- The Galbe starter CLI will request you to chose a template and a target language
24
- for your project. Let's select `hello` as template and `ts` as language. This
25
- will create a new project under `app` directory.
19
+ The Galbe starter CLI will prompt you to choose a template and a target language. Select `hello` as the template and `typescript` as the language. This will create a new project in the `app` directory.
26
20
 
27
- Now you can navigate to your newly created project and install it:
21
+ Now, navigate to your newly created project and install dependencies:
28
22
 
29
23
  ```bash
30
24
  $ cd app
31
- $ bun install
25
+ $ bun i
32
26
  ```
33
27
 
34
- And start the dev server by running:
28
+ Start the development server by running:
35
29
 
36
30
  ```bash
37
31
  $ bun dev
@@ -45,7 +39,7 @@ done
45
39
  🚀 Server running at http://localhost:3000
46
40
  ```
47
41
 
48
- Let's try to reach the hello endpoint:
42
+ Try accessing the hello endpoint:
49
43
 
50
44
  ```bash
51
45
  $ curl localhost:3000/hello/John?age=32
@@ -53,167 +47,64 @@ Hello John! You're 32 y.o.
53
47
  ```
54
48
 
55
49
  > [!TIP]
56
- > If you want to have a more complete view of Galbe capabilities, feel free to
57
- > take a look at the `demo` template from the Galbe starter CLI.
50
+ > To explore more of Galbe's capabilities, check out the `demo` template in the Galbe starter CLI.
58
51
 
59
- ## Manual installation
52
+ ## Manual Installation
60
53
 
61
- Init a new Bun project and add Galbe as dependency:
54
+ Initialize a new Bun project and add Galbe as a dependency:
62
55
 
63
56
  ```bash
64
57
  $ bun init
65
58
  $ bun add galbe
66
59
  ```
67
60
 
68
- Open `package.json` file and add the following scripts:
61
+ Open `package.json` and add the following scripts:
69
62
 
70
63
  ```json
71
64
  {
72
65
  "scripts": {
73
- "dev": "galbe dev index.ts",
66
+ "dev": "galbe dev index.ts -w .",
74
67
  "build": "galbe build index.ts",
75
68
  "test": "bun test"
76
69
  }
77
70
  }
78
71
  ```
79
72
 
80
- As you can see, those scripts rely on Galbe CLI to run and build the
81
- application. You will find more info about it on the [CLI](cli.md) page.
73
+ These scripts use the Galbe CLI to run and build the application. More details are available in the [CLI](cli.md) section.
82
74
 
83
- This require your `index.ts` to export a default Galbe instance in order to
84
- work. As in the following example:
75
+ Your `index.ts` file must export a default Galbe instance:
85
76
 
86
77
  ```ts
87
- import { Galbe } from "galbe";
78
+ import { Galbe } from "galbe"
88
79
 
89
- const galbe = new Galbe({ port: 3000 });
90
- galbe.get("/hello", () => "Hello Mom!");
80
+ const galbe = new Galbe({ port: 3000 })
81
+ galbe.get("/hello", () => "Hello Mom!")
91
82
 
92
- export default galbe;
83
+ export default galbe
93
84
  ```
94
85
 
95
- This is the recommended way to proceed but it is not mandatory. Galbe instances
96
- also provide a `listen` method that will allow you to manually start your server
97
- instance from the code.
86
+ This approach is recommended but not mandatory. Galbe instances also provide a `listen` method, allowing you to manually start your server from within your code.
98
87
 
99
88
  > [!WARNING]
100
- > In the case you decide to not rely on Galbe CLI to run/build your app, you
101
- > will not have access to
102
- > [Automatic Route Analyzer](routes.md#automatic-route-analyzer) feature.
89
+ > If you choose not to use the Galbe CLI for running or building your app, you will not have access to features such as the [Automatic Route Analyzer](routes.md#automatic-route-analyzer).
103
90
 
104
- ## Configuration
91
+ ## Project Structure
105
92
 
106
- To configure your Galbe server, you should pass your configuration to the Galbe
107
- constructor when you instanciate it.
93
+ Galbe is highly flexible in terms of project structure. The [Automatic Route Analyzer](routes.md#automatic-route-analyzer), triggered by the `routes` configuration option (default: `src/**/*.route.{js,ts}`), enables versatile project organization.
108
94
 
109
- ```ts
110
- const galbe = new Galbe(configuration);
111
- ```
112
-
113
- ### Properties
114
-
115
- **hostname**
116
-
117
- The hostname of the server. Default is `localhost`.
118
-
119
- **port**
120
-
121
- The port number that the server will be listening on. Default is `3000`.
122
-
123
- **basePath**
124
-
125
- The base path is added as a prefix to all the routes created.
126
-
127
- **routes**
128
-
129
- A Glob Pattern or a list of Glob patterns defining the route files to be
130
- analyzed by the [Automatic Route Analyzer](routes.md#automatic-route-analyzer).
131
- Default is `src/**/*.route.{js,ts}`.
132
-
133
- **plugin**
134
-
135
- A property that can be used by plugins to add plugin's specific configuration.
136
- Every key should correspond to a [Unique Plugin Identifier](plugins.md).
137
-
138
- **tls**
139
-
140
- Enable or disable TLS support. Default value is `false`.
141
-
142
- - **tls.key**: The path to the private key file
143
-
144
- - **tls.cert**: The path to the certificate file
145
-
146
- - **tls.ca**: The path to the certificate authority file
147
-
148
- **requestValidator.enabled**
149
-
150
- Enable or disable the _request_ schema validation (See
151
- [Request Schema definition](schemas.md#request-schema-definition)). Default
152
- value is `true`.
153
-
154
- **responseValidator.enabled**
155
-
156
- Enable or disable the _response_ schema validation (See
157
- [Request Schema definition](schemas.md#request-schema-definition)). Default
158
- value is `true`.
159
-
160
- ### Examples
161
-
162
- A common way to handle server configuration is to create new file
163
- `galbe.config.(js|ts|json)` at the root of your project directory and import it
164
- in your code. Here is an example:
165
-
166
- galbe.config.js
167
-
168
- ```js
169
- export default {
170
- port: Bun.env.GALBE_PORT
171
- routes: 'src/**/*.route.ts',
172
- }
173
- ```
174
-
175
- index.js
176
-
177
- ```js
178
- import { Galbe } from "galbe";
179
- import config from "./galbe.config";
180
-
181
- export default new Galbe(config);
182
- ```
183
-
184
- > [!TIP]
185
- > If you are using Typescript, you can import `GalbeConfig` type from galbe
186
- > package to ensure type consistency for your configuration. Here is an example:
187
- >
188
- > ```ts
189
- > import type { GalbeConfig } from "galbe";
190
- > const config: GalbeConfig = {
191
- > port: Number(Bun.env.GALBE_PORT),
192
- > routes: "routes/*.route.ts",
193
- > };
194
- > export default config;
195
- > ```
196
-
197
- ## Project structure
198
-
199
- One key aspect of Galbe, is its versatility in terms of project structure. This
200
- is partly allowed by the
201
- [Automatic Route Analyzer](routes.md#automatic-route-analyzer) and the `routes`
202
- config property which defaults to `src/**/*.route.{js,ts}`.
203
-
204
- Here are two examples of valid project structures by default:
95
+ Here are two examples of valid project structures:
205
96
 
206
97
  **Example 1**
207
98
 
208
99
  ```txt
209
100
  ┌── src
210
101
  │ ├── hooks
211
- │ │   └── log.hook.ts
102
+ │ │ └── log.hook.ts
212
103
  │ ├── routes
213
- │ │   ├── foo.route.ts
214
- │ │   └── foo.route.ts
104
+ │ │ ├── foo.route.ts
105
+ │ │ └── bar.route.ts
215
106
  │ └── schemas
216
- │ ├── bar.schema.ts
107
+ │ ├── foo.schema.ts
217
108
  │ └── bar.schema.ts
218
109
  ├── galbe.config.ts
219
110
  ├── index.ts
@@ -227,10 +118,10 @@ Here are two examples of valid project structures by default:
227
118
  ```txt
228
119
  ┌── src
229
120
  │ ├── hooks
230
- │ │   └── log.hook.ts
121
+ │ │ └── log.hook.ts
231
122
  │ ├── foo
232
- │ │   ├── foo.route.ts
233
- │ │   └── foo.schema.ts
123
+ │ │ ├── foo.route.ts
124
+ │ │ └── foo.schema.ts
234
125
  │ └── bar
235
126
  │ ├── bar.route.ts
236
127
  │ └── bar.schema.ts
@@ -241,26 +132,18 @@ Here are two examples of valid project structures by default:
241
132
  └── tsconfig.json
242
133
  ```
243
134
 
244
- In both cases, the
245
- [Automatic Route Analyzer](routes.md#automatic-route-analyzer) will analyze
246
- `foo.route.ts` and `bar.route.ts` Route Files to find route definitions.
135
+ In both cases, the [Automatic Route Analyzer](routes.md#automatic-route-analyzer) will detect `foo.route.ts` and `bar.route.ts` to set up route definitions.
247
136
 
248
- You can find more info about Route Files definition in the
249
- [Routes Files](routes.md#route-files) section.
137
+ For more details, see the [Route Files](routes.md#route-files) section.
250
138
 
251
139
  > [!NOTE]
252
- > The examples provided above will work with the default configuration, but you
253
- > can easily customize the routes property to fit your own project structure.
254
- > Simply redefine the `routes` property with your own pattern(s) to to fit your
255
- > own project structure.
140
+ > These examples work with the default configuration, but you can customize the `routes` property to fit your project structure. Define `routes` with your preferred pattern(s) to match your file organization.
256
141
 
257
- ## How to debug
142
+ ## Debugging
258
143
 
259
- The easiest way to debug your app is by installing the
260
- [VSCode Bun extension](https://marketplace.visualstudio.com/items?itemName=oven.bun-vscode).
144
+ The easiest way to debug your application is by using the [VSCode Bun extension](https://marketplace.visualstudio.com/items?itemName=oven.bun-vscode).
261
145
 
262
- You can then create a `.vscode/launch.json` config file in your project root
263
- directory. Here is an example of configuration:
146
+ Create a `.vscode/launch.json` configuration file in your project root with the following content:
264
147
 
265
148
  ```json
266
149
  {
package/docs/handler.md CHANGED
@@ -2,17 +2,17 @@
2
2
 
3
3
  A handler is a function that gets executed when a request matches the route definition. It is responsible for processing the request and sending a response.
4
4
 
5
- ## Handler declaration
5
+ ## Handler Declaration
6
6
 
7
- The handler should be declared as last argument of the [Route Definition](routes.md#route-defintion) method.
7
+ The handler should be declared as the last argument of the [Route Definition](routes.md#route-definition) method.
8
8
 
9
9
  ```js
10
10
  galbe.get('foo', schema, [hook1, hook2], ctx => {})
11
11
  ```
12
12
 
13
- Handler are called after the last hook call, or right after the request parsing if no hook is declared. To get a better understanding of the request lifecycle, you can refer to the [Lifecycle](https://galbe.dev/documentation/lifecycle) section.
13
+ Handlers are called after the last hook call, or right after the request parsing if no hook is declared. To get a better understanding of the request lifecycle, you can refer to the [Lifecycle](https://galbe.dev/documentation/lifecycle) section.
14
14
 
15
- ## Handler definition
15
+ ## Handler Definition
16
16
 
17
17
  ```js
18
18
  const handler = ctx => {
@@ -21,29 +21,29 @@ const handler = ctx => {
21
21
  }
22
22
  ```
23
23
 
24
- The handler function takes a `context` object as single argument and might return a `response`.
24
+ The handler function takes a `context` object as its single argument and might return a `response`.
25
25
 
26
- **context**
26
+ ### Context
27
27
 
28
- The `context` object contains the request information as well as a `set` object that serves as a response modifier. You can find more detailed informations about the `context` object in the [Context](context.md) section.
28
+ The `context` object contains the request information as well as a `set` object that serves as a response modifier. You can find more detailed information about the `context` object in the [Context](context.md) section.
29
29
 
30
- **response**
30
+ ### Response
31
31
 
32
32
  To send a response, your handler can return an object. The response sent will depend on the type of the object returned. There are four types of responses that can be returned by a handler method. More about that in the next section.
33
33
 
34
- ## Response types
34
+ ## Response Types
35
35
 
36
36
  > [!NOTE]
37
- > This section only cover response body payloads, to return specific response headers and/or status, you should define them with the `context.set` object before the return statement. More about it in the [Context](context.md) section.
37
+ > This section only covers response body payloads. To return specific response headers and/or status, you should define them with the `context.set` object before the return statement. More about it in the [Context](context.md) section.
38
38
 
39
39
  ### String
40
40
 
41
- Case where `string` is returned by the handler.
41
+ Case where a `string` is returned by the handler.
42
42
 
43
43
  - status: 200
44
- - content-type: `text-plain`
44
+ - content-type: `text/plain`
45
45
 
46
- **Example**
46
+ #### Example
47
47
 
48
48
  ```js
49
49
  galbe.get('/example', ctx => {
@@ -58,29 +58,29 @@ Case where an `object` is returned by the handler.
58
58
  - status: 200
59
59
  - content-type: `application/json`
60
60
 
61
- **Example**
61
+ #### Example
62
62
 
63
63
  ```js
64
64
  galbe.get('/example', ctx => {
65
- return 'Hello Mom!'
65
+ return { message: 'Hello Mom!' }
66
66
  })
67
67
  ```
68
68
 
69
- ### Response instance
69
+ ### Response Instance
70
70
 
71
71
  Case where a [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) instance is returned by the handler.
72
72
 
73
- In that case, `context.set` properties are not taken into account to contruct the response.
73
+ In this case, `context.set` properties are not taken into account to construct the response.
74
74
 
75
- **Example**
75
+ #### Example
76
76
 
77
77
  <!-- prettier-ignore -->
78
78
  ```js
79
79
  galbe.get('/example', ctx => {
80
80
  return new Response(
81
81
  'Hello Mom',
82
- { status: 200, headers: { 'content-type': 'text/plain' }
83
- })
82
+ { status: 200, headers: { 'content-type': 'text/plain' } }
83
+ )
84
84
  })
85
85
  ```
86
86
 
@@ -91,7 +91,7 @@ Case where a [Generator](https://developer.mozilla.org/en-US/docs/Web/JavaScript
91
91
  - status: 200
92
92
  - content-type: `text/event-stream`
93
93
 
94
- **Example**
94
+ #### Example
95
95
 
96
96
  ```js
97
97
  async function* generator(array) {
@@ -103,3 +103,17 @@ async function* generator(array) {
103
103
 
104
104
  galbe.get('/example', ctx => generator(['one', 'two', 'three']))
105
105
  ```
106
+
107
+ ## Throwing Errors
108
+
109
+ Throwing a `RequestError` at any point in the handler execution will result in a response with the specified status and payload.
110
+
111
+ #### Example
112
+
113
+ ```ts
114
+ g.get("/test", () => {
115
+ throw new RequestError({ status: 418, payload: '🫖' })
116
+ })
117
+ ```
118
+
119
+ Any other kind of error will result in a `500` response with the error message `"Internal Server Error"` by default. You can always customize it by defining a custom [Error Handler](error-handler.md).
package/docs/hooks.md CHANGED
@@ -1,45 +1,45 @@
1
1
  # Hooks
2
2
 
3
- Hooks provide a simple way to perform specific actions before and/or after reaching a specific route endpoint.
3
+ Hooks provide a simple way to execute specific actions before and/or after reaching a route endpoint in Galbe.
4
4
 
5
- ## Hook definition
5
+ ## Defining Hooks
6
6
 
7
7
  ```ts
8
- const hook = (context, next) => {
8
+ const hook = async (context, next) => {
9
9
  context.state['foo'] = 'bar'
10
10
  await next()
11
11
  console.log('Hook end')
12
12
  }
13
13
  ```
14
14
 
15
- The hook takes only two arguments, a `context` object and a `next` function.
15
+ A hook takes two arguments: `context` and `next`.
16
16
 
17
- **context**
17
+ ### context
18
18
 
19
- The `context` object contains the request information along with a state property that is modifiable and preserved across all hooks and the handler. It is useful for sharing information or objects across hooks and handler. You can find more information about it in the [Context](context.md) section.
19
+ The `context` object contains request information and a modifiable `state` property that persists across all hooks and the handler. This is useful for sharing data across hooks and handlers. More details are available in the [Context](context.md) section.
20
20
 
21
- **next**
21
+ ### next
22
22
 
23
- The `next` function calls the next hook in the hook list or the handler if the current hook is the last one declared. The `next` function should be called at most once. If it is omitted, Galbe will call it automatically at the end of the execution of the current hook.
23
+ The `next` function calls the next hook in the list, or the handler if the current hook is the last one. The `next` function should be called at most once. If omitted, Galbe will automatically call it at the end of the current hook’s execution.
24
24
 
25
25
  > [!TIP]
26
- > Hooks are interruptible objects, meaning they can return a response at any moment. This provides a powerful mechanism for implementing custom logic, such as authentication, authorization, caching, and more.
26
+ > Hooks are interruptible, meaning they can return a response at any time. This is useful for implementing custom logic such as authentication, authorization, and caching.
27
27
  >
28
- > To learn more about response types, ou can take a look at the [Response types](handler.md#response-types) section.
28
+ > For more details on response handling, see [Response Types](handler.md#response-types).
29
29
 
30
- ## Hooks declaration
30
+ ## Declaring Hooks
31
31
 
32
- Hooks should be declared just before the handler method in the [Route Definition](routes.md#route-defintion) method as a list of Hooks.
32
+ Hooks should be declared before the handler method in the [Route Definition](routes.md#route-definition) as a list of hook functions.
33
33
 
34
34
  ```ts
35
- galbe.get('foo', [ hook1, hook2, ... ], ctx => {})
35
+ galbe.get('/foo', [hook1, hook2, ...], ctx => {})
36
36
  ```
37
37
 
38
- Hooks are called just before the [Handler](handler.md) in the order that they have been declared in the hook list of the [Route Definition](routes.md#route-defintion). To get a better understanding of hooks execution during the request lifecycle, you can refer to the [Lifecycle](https://galbe.dev/documentation/lifecycle) section.
38
+ Hooks execute in the order they are declared, just before the [Handler](handler.md). For a deeper understanding of their execution in the request lifecycle, see the [Lifecycle](https://galbe.dev/documentation/lifecycle) section.
39
39
 
40
40
  ### Examples
41
41
 
42
- Linear hooks declaration:
42
+ #### Linear Hook Execution
43
43
 
44
44
  ```ts
45
45
  const hook1 = context => {
@@ -61,7 +61,7 @@ hook2
61
61
  handler
62
62
  ```
63
63
 
64
- Nested hooks declaration:
64
+ #### Nested Hook Execution
65
65
 
66
66
  ```ts
67
67
  const hook1 = async (context, next) => {