galbe 0.10.0 → 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.
- package/README.md +3 -0
- package/bin/commands/build.ts +13 -1
- package/docs/cli.md +25 -22
- package/docs/configuration.md +80 -0
- package/docs/context.md +38 -52
- package/docs/error-handler.md +20 -31
- package/docs/getting-started.md +40 -157
- package/docs/handler.md +35 -21
- package/docs/hooks.md +16 -16
- package/docs/plugins.md +50 -57
- package/docs/router.md +2 -2
- package/docs/routes.md +56 -39
- package/docs/schemas.md +60 -51
- package/package.json +1 -1
- package/src/parser.ts +1 -1
- package/src/router.ts +2 -2
- package/src/server.ts +7 -1
- package/test/parser.test.ts +23 -8
- package/test/router.test.ts +14 -0
package/README.md
CHANGED
|
@@ -20,6 +20,9 @@ cd app
|
|
|
20
20
|
bun install && bun dev
|
|
21
21
|
```
|
|
22
22
|
|
|
23
|
+
The Galbe CLI ships with the package. You can invoke it with `bunx galbe` or
|
|
24
|
+
install it globally using `bun install -g galbe`.
|
|
25
|
+
|
|
23
26
|
## Documentation
|
|
24
27
|
|
|
25
28
|
The detailed documentation is available at [galbe.dev](https://galbe.dev).
|
package/bin/commands/build.ts
CHANGED
|
@@ -10,6 +10,7 @@ import { Galbe } from '../../src'
|
|
|
10
10
|
import { defineRoutes, GalbeProxy } from '../../src/routes'
|
|
11
11
|
import { BuildConfig } from 'bun'
|
|
12
12
|
import { existsSync } from 'fs'
|
|
13
|
+
import { softMerge } from '../../src/util'
|
|
13
14
|
|
|
14
15
|
const createBuildIndex = async (indexPath: string, g: Galbe, buildId: string) => {
|
|
15
16
|
const buildPath = resolve(tmpdir(), buildId)
|
|
@@ -61,10 +62,19 @@ export default (cmd: Command) => {
|
|
|
61
62
|
.option('-c, --config <file>', 'bun js or ts config file')
|
|
62
63
|
.action(async (index, props) => {
|
|
63
64
|
const { out, compile, config } = props
|
|
64
|
-
|
|
65
|
+
const indexPath = resolve(CWD, index)
|
|
66
|
+
const indexDir = dirname(indexPath)
|
|
65
67
|
const buildID = crypto.randomUUID()
|
|
66
68
|
const outPath = resolve(CWD, out)
|
|
67
69
|
|
|
70
|
+
let galbeConfig = {}
|
|
71
|
+
|
|
72
|
+
if (existsSync(`${indexDir}/galbe.config.ts`)) {
|
|
73
|
+
galbeConfig = (await import(`${indexDir}/galbe.config.ts`)).default
|
|
74
|
+
} else if (existsSync(`${indexDir}/galbe.config.js`)) {
|
|
75
|
+
galbeConfig = (await import(`${indexDir}/galbe.config.js`)).default
|
|
76
|
+
}
|
|
77
|
+
|
|
68
78
|
Bun.env.GALBE_BUILD = buildID
|
|
69
79
|
Bun.env.GALBE_BUILD_OUT = outPath
|
|
70
80
|
|
|
@@ -77,6 +87,8 @@ export default (cmd: Command) => {
|
|
|
77
87
|
let g: Galbe = await silentExec(async () => {
|
|
78
88
|
try {
|
|
79
89
|
const g = (await import(resolve(CWD, index))).default
|
|
90
|
+
let conf = g.config
|
|
91
|
+
g.config = softMerge(galbeConfig, conf)
|
|
80
92
|
return g
|
|
81
93
|
} catch (err) {
|
|
82
94
|
error = err
|
package/docs/cli.md
CHANGED
|
@@ -9,7 +9,7 @@ However, if you want to use it directly from your terminal, you must either:
|
|
|
9
9
|
Install it globally using the following command:
|
|
10
10
|
|
|
11
11
|
```bash
|
|
12
|
-
$ bun
|
|
12
|
+
$ bun i -g galbe
|
|
13
13
|
```
|
|
14
14
|
|
|
15
15
|
Or run it with `bunx`:
|
|
@@ -26,15 +26,16 @@ Start a dev server running your Galbe application.
|
|
|
26
26
|
|
|
27
27
|
| Name | Description |
|
|
28
28
|
| ----- | -------------------------------------------------------- |
|
|
29
|
-
| index | The js or ts file that export
|
|
29
|
+
| index | The js or ts file that export your Galbe server instance. |
|
|
30
30
|
|
|
31
31
|
#### Options
|
|
32
32
|
|
|
33
|
-
| Short | Long
|
|
34
|
-
| ----- |
|
|
35
|
-
| -p | --port
|
|
36
|
-
| -w | --watch
|
|
37
|
-
| -
|
|
33
|
+
| Short | Long | Descritpion | Default |
|
|
34
|
+
| ----- | --------------- | --------------------------- | ------- |
|
|
35
|
+
| -p | --port | port number [1-65535] | 3000 |
|
|
36
|
+
| -w | --watch | watch file changes dir | false |
|
|
37
|
+
| -wi | --watchignore | ignored watch files regex | |
|
|
38
|
+
| -nc | --noclear | don't clear on file changes | false |
|
|
38
39
|
|
|
39
40
|
#### Example
|
|
40
41
|
|
|
@@ -51,6 +52,13 @@ export default g
|
|
|
51
52
|
|
|
52
53
|
```bash
|
|
53
54
|
$ galbe dev index.js -p 7357 -w
|
|
55
|
+
🏗️ Constructing routes
|
|
56
|
+
|
|
57
|
+
[GET] /example
|
|
58
|
+
|
|
59
|
+
done
|
|
60
|
+
|
|
61
|
+
🚀 Server running at http://localhost:7357
|
|
54
62
|
```
|
|
55
63
|
|
|
56
64
|
## build
|
|
@@ -61,11 +69,11 @@ Bundle your Galbe application.
|
|
|
61
69
|
|
|
62
70
|
| Name | Description |
|
|
63
71
|
| ----- | -------------------------------------------------------- |
|
|
64
|
-
| index | The js or ts file that export
|
|
72
|
+
| index | The js or ts file that export your Galbe server instance. |
|
|
65
73
|
|
|
66
74
|
#### Options
|
|
67
75
|
|
|
68
|
-
| Short | Long |
|
|
76
|
+
| Short | Long | Description | Default |
|
|
69
77
|
| ----- | --------- | ------------------------------ | -------- |
|
|
70
78
|
| -o | --out | output directory | dist/app |
|
|
71
79
|
| -C | --compile | create a standalone executable | false |
|
|
@@ -74,7 +82,6 @@ Bundle your Galbe application.
|
|
|
74
82
|
#### Example
|
|
75
83
|
|
|
76
84
|
index.js
|
|
77
|
-
|
|
78
85
|
```js
|
|
79
86
|
import { Galbe } from 'galbe'
|
|
80
87
|
|
|
@@ -87,7 +94,7 @@ $ galbe build index.js
|
|
|
87
94
|
|
|
88
95
|
## generate
|
|
89
96
|
|
|
90
|
-
Generate resources
|
|
97
|
+
Generate resources around your Galbe application.
|
|
91
98
|
|
|
92
99
|
### client
|
|
93
100
|
|
|
@@ -97,11 +104,11 @@ Generate a client for your Galbe application.
|
|
|
97
104
|
|
|
98
105
|
| Name | Description |
|
|
99
106
|
| ----- | -------------------------------------------------------- |
|
|
100
|
-
| index | The js or ts file that export
|
|
107
|
+
| index | The js or ts file that export your Galbe server instance. |
|
|
101
108
|
|
|
102
109
|
#### Options
|
|
103
110
|
|
|
104
|
-
| Short | Long |
|
|
111
|
+
| Short | Long | Description | Default |
|
|
105
112
|
| ----- | -------- | -------------------------- | ------------------------------------ |
|
|
106
113
|
| -o | --out | output file | dist/(client.ts \| client.js \| cli) |
|
|
107
114
|
| -t | --target | build target [ts, js, cli] | ts |
|
|
@@ -116,10 +123,6 @@ $ cd galbe-example
|
|
|
116
123
|
$ bun install
|
|
117
124
|
```
|
|
118
125
|
|
|
119
|
-
> [!NOTE]
|
|
120
|
-
> In order for the following examples to work, you must ensure that an instance of you galbe app is running on port 3000.
|
|
121
|
-
> You can do that by running `bun run dev`.
|
|
122
|
-
|
|
123
126
|
##### JS or TS client
|
|
124
127
|
|
|
125
128
|
To generate a JS or TS client of that application, you can run the following command:
|
|
@@ -195,7 +198,7 @@ Hello Pierre! You're 29 y.o.
|
|
|
195
198
|
```
|
|
196
199
|
|
|
197
200
|
> [!IMPORTANT]
|
|
198
|
-
> A `GCLI_SERVER_URL` environment variable must be defined. It should
|
|
201
|
+
> A `GCLI_SERVER_URL` environment variable must be defined. It should indicate the url of the Galbe server you want to target.
|
|
199
202
|
> In that specific case `http://localhost:3000`.
|
|
200
203
|
|
|
201
204
|
### spec
|
|
@@ -206,11 +209,11 @@ Generate the spec of your Galbe application.
|
|
|
206
209
|
|
|
207
210
|
| Name | Description |
|
|
208
211
|
| ----- | -------------------------------------------------------- |
|
|
209
|
-
| index | The js or ts file that export
|
|
212
|
+
| index | The js or ts file that export your Galbe server instance. |
|
|
210
213
|
|
|
211
214
|
#### Options
|
|
212
215
|
|
|
213
|
-
| Short | Long |
|
|
216
|
+
| Short | Long | Description | Default |
|
|
214
217
|
| ----- | -------- | ------------------------------------------------ | ----------------------- |
|
|
215
218
|
| -t | --target | spec target [openapi:3.0:json, openapi:3.0:yaml] | openapi:3.0:yaml |
|
|
216
219
|
| -b | --base | base spec file | |
|
|
@@ -218,7 +221,7 @@ Generate the spec of your Galbe application.
|
|
|
218
221
|
|
|
219
222
|
#### Example
|
|
220
223
|
|
|
221
|
-
Let's try to generate the
|
|
224
|
+
Let's try to generate the spec of the project defined in the previous client section. You can then run:
|
|
222
225
|
|
|
223
226
|
```bash
|
|
224
227
|
$ galbe generate spec index.ts
|
|
@@ -263,7 +266,7 @@ Generate the code and project structure from spec.
|
|
|
263
266
|
|
|
264
267
|
#### Options
|
|
265
268
|
|
|
266
|
-
| Short | Long |
|
|
269
|
+
| Short | Long | Description | Default |
|
|
267
270
|
| ----- | -------- | ------------------------------------------------- | -------------------------- |
|
|
268
271
|
| -f | --format | input format [openapi:3.0:yaml, openapi:3.0:json] | openapi:3.0:(yaml \| json) |
|
|
269
272
|
| -t | --target | source target [ts, js] | ts |
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# Configuration
|
|
2
|
+
|
|
3
|
+
## Configuring Galbe
|
|
4
|
+
|
|
5
|
+
By default, Galbe automatically attempts to resolve a configuration file named `galbe.config.{js,ts}` located in the same directory as your entry file.
|
|
6
|
+
|
|
7
|
+
The configuration file should export a default object containing your settings:
|
|
8
|
+
|
|
9
|
+
```js
|
|
10
|
+
export default {
|
|
11
|
+
// config properties
|
|
12
|
+
}
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Alternatively, you can pass your configuration directly to your Galbe server during instantiation, as shown below:
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { Galbe } from "galbe"
|
|
19
|
+
|
|
20
|
+
const galbe = new Galbe({
|
|
21
|
+
// config properties
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
export default galbe
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
> [!NOTE]
|
|
28
|
+
> You can use both configuration methods simultaneously. Galbe will first apply the settings from `galbe.config.{js,ts}`, and any properties passed during instantiation will override the corresponding ones from the configuration file.
|
|
29
|
+
|
|
30
|
+
## Configuration Properties
|
|
31
|
+
|
|
32
|
+
### hostname
|
|
33
|
+
The hostname of the server. Default: `localhost`.
|
|
34
|
+
|
|
35
|
+
### port
|
|
36
|
+
The port number the server will listen on. Default: `3000`.
|
|
37
|
+
|
|
38
|
+
### basePath
|
|
39
|
+
A base path added as a prefix to all routes.
|
|
40
|
+
|
|
41
|
+
### routes
|
|
42
|
+
A glob pattern or list of glob patterns defining the route files to be analyzed by the [Automatic Route Analyzer](routes.md#automatic-route-analyzer). Default: `src/**/*.route.{js,ts}`.
|
|
43
|
+
|
|
44
|
+
### plugin
|
|
45
|
+
A property used by plugins to add specific configurations. Each key should correspond to a [Unique Plugin Identifier](plugins.md).
|
|
46
|
+
|
|
47
|
+
### tls
|
|
48
|
+
Enables or disables TLS support. Default: `false`.
|
|
49
|
+
- **tls.key**: Path to the private key file.
|
|
50
|
+
- **tls.cert**: Path to the certificate file.
|
|
51
|
+
- **tls.ca**: Path to the certificate authority file.
|
|
52
|
+
|
|
53
|
+
### requestValidator.enabled
|
|
54
|
+
Enables or disables _request_ schema validation (see [Request Schema Definition](schemas.md#request-schema-definition)). Default: `true`.
|
|
55
|
+
|
|
56
|
+
### responseValidator.enabled
|
|
57
|
+
Enables or disables _response_ schema validation (see [Response Schema Definition](schemas.md#request-schema-definition#response)). Default: `true`.
|
|
58
|
+
|
|
59
|
+
## Config Type Safety
|
|
60
|
+
|
|
61
|
+
To ensure type safety for your configuration, use the `config` helper method, which leverages your IDE’s IntelliSense:
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
import { config } from "galbe"
|
|
65
|
+
|
|
66
|
+
export default config({
|
|
67
|
+
// ...
|
|
68
|
+
})
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Alternatively, if you are using TypeScript, you can apply the `GalbeConfig` type to enforce type consistency:
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
import type { GalbeConfig } from "galbe"
|
|
75
|
+
|
|
76
|
+
export default {
|
|
77
|
+
// ...
|
|
78
|
+
} satisfies GalbeConfig
|
|
79
|
+
```
|
|
80
|
+
|
package/docs/context.md
CHANGED
|
@@ -1,41 +1,30 @@
|
|
|
1
1
|
# Context
|
|
2
2
|
|
|
3
|
-
An instance of the context object is created when a new request is initiated and
|
|
4
|
-
See the [Lifecycle](https://galbe.dev/documentation/lifecycle) section to get more details.
|
|
3
|
+
An instance of the context object is created when a new request is initiated and is carried throughout the entire request lifecycle. See the [Lifecycle](https://galbe.dev/documentation/lifecycle) section for more details.
|
|
5
4
|
|
|
6
|
-
Its purpose is to
|
|
5
|
+
Its purpose is to carry all relevant information about the request and facilitate data sharing between different stages of the request lifecycle.
|
|
7
6
|
|
|
8
7
|
## Definition
|
|
9
8
|
|
|
10
|
-
A context has the following properties:
|
|
9
|
+
A context object has the following properties:
|
|
11
10
|
|
|
12
|
-
|
|
11
|
+
### request
|
|
13
12
|
|
|
14
13
|
An instance of the [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) object created by the server.
|
|
15
14
|
|
|
16
|
-
|
|
15
|
+
### headers
|
|
17
16
|
|
|
18
|
-
A
|
|
17
|
+
A JavaScript object representing the headers of the current request.
|
|
19
18
|
|
|
20
|
-
- key (string):
|
|
21
|
-
- value
|
|
19
|
+
- **key** (string): Header name
|
|
20
|
+
- **value** (string | [schema defined](schemas.md#headers)): Header value
|
|
22
21
|
|
|
23
|
-
|
|
24
|
-
{
|
|
25
|
-
"accept": "*/*",
|
|
26
|
-
"accept-encoding": "gzip, deflate, br",
|
|
27
|
-
"cookie": "Cookie_1=value; Cookie_2=value",
|
|
28
|
-
"host": "localhost:3000",
|
|
29
|
-
"user-agent": "galbe/1.0.0"
|
|
30
|
-
}
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
**params**
|
|
22
|
+
### params
|
|
34
23
|
|
|
35
|
-
A
|
|
24
|
+
A JavaScript object representing the route parameters of the current request.
|
|
36
25
|
|
|
37
|
-
- key (string):
|
|
38
|
-
- value
|
|
26
|
+
- **key** (string): Parameter name
|
|
27
|
+
- **value** (string | [schema defined](schemas.md#params)): Parameter value
|
|
39
28
|
|
|
40
29
|
```js
|
|
41
30
|
galbe.get('/default/:p1/foo/:p2', ctx => console.log(ctx.params))
|
|
@@ -43,67 +32,64 @@ galbe.get('/default/:p1/foo/:p2', ctx => console.log(ctx.params))
|
|
|
43
32
|
{ p1: "four", p2: "2" }
|
|
44
33
|
```
|
|
45
34
|
|
|
46
|
-
|
|
35
|
+
### query
|
|
47
36
|
|
|
48
|
-
A
|
|
37
|
+
A JavaScript object representing the query parameters of the current request.
|
|
49
38
|
|
|
50
|
-
- key (string):
|
|
51
|
-
- value
|
|
39
|
+
- **key** (string): Query parameter name
|
|
40
|
+
- **value** (string | [schema defined](schemas.md#query)): Query parameter value
|
|
52
41
|
|
|
53
42
|
```js
|
|
54
|
-
galbe.get('/test', ctx => console.log(ctx.
|
|
43
|
+
galbe.get('/test', ctx => console.log(ctx.query))
|
|
55
44
|
// GET /test?one=1&two=2
|
|
56
45
|
{ one: "1", two: "2" }
|
|
57
46
|
```
|
|
58
47
|
|
|
59
|
-
|
|
48
|
+
### body
|
|
60
49
|
|
|
61
|
-
The body payload of the incoming request. The body type is
|
|
50
|
+
The body payload of the incoming request. The body type is determined based on the following rules:
|
|
62
51
|
|
|
63
|
-
If no [Schema](schemas.md) is defined, Galbe will parse the body type according to `
|
|
52
|
+
If no [Schema](schemas.md) is defined, Galbe will parse the body type according to the `Content-Type` header:
|
|
64
53
|
|
|
65
54
|
- `text/.*`: string
|
|
66
55
|
- `application/json`: object
|
|
67
|
-
- `application/x-www-form-urlencoded`: { [key: string]: any }
|
|
68
|
-
- `multipart/form-data`: { [key: string]:
|
|
69
|
-
|
|
70
|
-
content: any
|
|
71
|
-
} }
|
|
72
|
-
- `other`: AsyncGenerator\<Uint8Array\>
|
|
56
|
+
- `application/x-www-form-urlencoded`: `{ [key: string]: any }`
|
|
57
|
+
- `multipart/form-data`: `{ [key: string]: { headers: { name: string; type?: string; filename?: string }; content: any } }`
|
|
58
|
+
- `other`: `AsyncGenerator<Uint8Array>`
|
|
73
59
|
|
|
74
|
-
If a [Schema](schemas.md) is defined, Galbe will parse the body
|
|
60
|
+
If a [Schema](schemas.md) is defined, Galbe will parse the body according to the [Schema.body](schemas.md#body) definition for the current route.
|
|
75
61
|
|
|
76
|
-
|
|
62
|
+
### set
|
|
77
63
|
|
|
78
|
-
The set property contains modifiable
|
|
64
|
+
The `set` property contains modifiable attributes intended to provide information to the response parser.
|
|
79
65
|
|
|
80
|
-
-
|
|
81
|
-
-
|
|
66
|
+
- **status**: Sets the response status.
|
|
67
|
+
- **headers**: Sets the response headers.
|
|
82
68
|
|
|
83
69
|
```js
|
|
84
70
|
galbe.get('/example', ctx => {
|
|
85
|
-
ctx.set.status = 418
|
|
86
|
-
return "I don't do coffee"
|
|
71
|
+
ctx.set.status = 418;
|
|
72
|
+
return "I don't do coffee";
|
|
87
73
|
})
|
|
88
74
|
```
|
|
89
75
|
|
|
90
|
-
|
|
76
|
+
### state
|
|
91
77
|
|
|
92
|
-
The state property
|
|
78
|
+
The `state` property allows storing custom user-defined objects throughout the request lifecycle. It is commonly used to share data between [hooks](hooks.md) and the [handler](handler.md).
|
|
93
79
|
|
|
94
|
-
- key (string):
|
|
95
|
-
- value (any):
|
|
80
|
+
- **key** (string): User-defined key
|
|
81
|
+
- **value** (any): User-defined object
|
|
96
82
|
|
|
97
83
|
```js
|
|
98
84
|
galbe.get(
|
|
99
85
|
'/example',
|
|
100
86
|
[
|
|
101
87
|
ctx => {
|
|
102
|
-
ctx.state['foo'] = 'bar'
|
|
88
|
+
ctx.state['foo'] = 'bar';
|
|
103
89
|
}
|
|
104
90
|
],
|
|
105
91
|
ctx => {
|
|
106
|
-
return ctx.state.foo
|
|
92
|
+
return ctx.state.foo;
|
|
107
93
|
}
|
|
108
94
|
)
|
|
109
95
|
```
|
|
@@ -113,6 +99,6 @@ $ curl http://localhost:3000/example
|
|
|
113
99
|
bar
|
|
114
100
|
```
|
|
115
101
|
|
|
116
|
-
|
|
102
|
+
### remoteAddress
|
|
117
103
|
|
|
118
|
-
An instance of
|
|
104
|
+
An instance of [SocketAddress](https://github.com/oven-sh/bun/blob/fe62a614046948ebba260bed87db96287e67921f/packages/bun-types/bun.d.ts#L2600-L2613) representing the remote address of the client.
|
package/docs/error-handler.md
CHANGED
|
@@ -1,59 +1,48 @@
|
|
|
1
|
-
# Error
|
|
1
|
+
# Error Handler
|
|
2
2
|
|
|
3
|
-
Any error
|
|
4
|
-
handler.
|
|
3
|
+
Any error occurring during a request lifecycle is intercepted by the error handler.
|
|
5
4
|
|
|
6
|
-
You can customize the default error
|
|
7
|
-
handler using Galbe's intance `onError` method.
|
|
5
|
+
You can customize the default error-handling behavior by defining a custom error handler using Galbe's instance `onError` method.
|
|
8
6
|
|
|
9
7
|
```js
|
|
10
|
-
const galbe = new Galbe()
|
|
11
|
-
galbe.onError(customErrorHandler)
|
|
8
|
+
const galbe = new Galbe()
|
|
9
|
+
galbe.onError(customErrorHandler)
|
|
12
10
|
```
|
|
13
11
|
|
|
14
12
|
## Definition
|
|
15
13
|
|
|
16
|
-
The error handler should be a function that takes two
|
|
17
|
-
[Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)
|
|
18
|
-
and a [Context](context.md). This function may potentially return a
|
|
19
|
-
[Response type](handler.md#response-types).
|
|
14
|
+
The error handler should be a function that takes two arguments: an [Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) and a [Context](context.md). This function may return a [Response type](handler.md#response-types).
|
|
20
15
|
|
|
21
16
|
```js
|
|
22
17
|
galbe.onError((error, ctx) => {
|
|
23
18
|
if (error.status === 500) {
|
|
24
|
-
return new Response(`Server error ❌`, { status: 500 })
|
|
19
|
+
return new Response(`Server error ❌`, { status: 500 })
|
|
25
20
|
}
|
|
26
21
|
if (error.status === 404) {
|
|
27
|
-
return new Response(`Not found 🔎`, { status: 404 })
|
|
22
|
+
return new Response(`Not found 🔎`, { status: 404 })
|
|
28
23
|
}
|
|
29
|
-
})
|
|
24
|
+
})
|
|
30
25
|
```
|
|
31
26
|
|
|
32
|
-
The `error` argument
|
|
33
|
-
the error originates from Galbe framework, it will be an instance of
|
|
34
|
-
[RequestError](#request-error).
|
|
27
|
+
The `error` argument can be any type of error thrown by your application. If the error originates from the Galbe framework, it will be an instance of [RequestError](#request-error).
|
|
35
28
|
|
|
36
|
-
For
|
|
37
|
-
status if no route matches the incoming request path. Similarly, the Parser will
|
|
38
|
-
throw a `RequestError` with a `400` status.
|
|
29
|
+
For example, the [Router](router.md) will throw a `RequestError` with a `404` status if no route matches the incoming request path. Similarly, the parser will throw a `RequestError` with a `400` status in case of invalid input.
|
|
39
30
|
|
|
40
31
|
## Request Error
|
|
41
32
|
|
|
42
|
-
The `RequestError` class is
|
|
43
|
-
Galbe. It has two optional attributes: a `status` and a `payload`.
|
|
33
|
+
The `RequestError` class is used to instantiate a runtime request error in Galbe. It has three optional attributes: `status`, `payload`, and `headers`.
|
|
44
34
|
|
|
45
|
-
If your application throws a `RequestError` instance, Galbe will, by default,
|
|
46
|
-
construct a Response from your `RequestError` and send it back to the client.
|
|
35
|
+
If your application throws a `RequestError` instance, Galbe will, by default, construct a Response from your `RequestError` and send it back to the client.
|
|
47
36
|
|
|
48
37
|
```js
|
|
49
|
-
import {
|
|
50
|
-
|
|
51
|
-
const galbe = new Galbe()
|
|
38
|
+
import { RequestError } from 'galbe'
|
|
52
39
|
|
|
53
|
-
galbe.get('/coffee', () =>
|
|
40
|
+
galbe.get('/coffee', () => {
|
|
41
|
+
throw new RequestError({ status: 418, payload: '🫖' })
|
|
42
|
+
})
|
|
54
43
|
```
|
|
55
44
|
|
|
56
|
-
When called, above endpoint should respond:
|
|
45
|
+
When called, the above endpoint should respond:
|
|
57
46
|
|
|
58
47
|
```bash
|
|
59
48
|
$ curl -i http://localhost:3000/coffee
|
|
@@ -61,5 +50,5 @@ HTTP/1.1 418 I'm a Teapot
|
|
|
61
50
|
Content-Type: application/json
|
|
62
51
|
Content-Length: 6
|
|
63
52
|
|
|
64
|
-
|
|
65
|
-
```
|
|
53
|
+
🫖
|
|
54
|
+
```
|