galbe 0.9.0 → 0.9.1
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/bun.lock +904 -0
- package/docs/getting-started.md +72 -37
- package/package.json +2 -1
- package/src/extras/spec/openapi.serializer.ts +2 -1
- package/src/parser.ts +1 -1
- package/src/types.ts +7 -7
- package/src/validator.ts +1 -0
- package/test/plugins.test.ts +1 -1
- package/test/responses.test.ts +3 -2
- package/test/routeFiles.test.ts +6 -6
- package/tsconfig.json +1 -1
- package/bun.lockb +0 -0
package/docs/getting-started.md
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
# Getting started
|
|
2
2
|
|
|
3
|
-
Galbe is a Javascript web framework for building fast and versatile backend
|
|
3
|
+
Galbe is a Javascript web framework for building fast and versatile backend
|
|
4
|
+
servers with Bun.
|
|
4
5
|
|
|
5
|
-
Designed with simplicity in mind, Galbe allows you to quickly create and set up
|
|
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.
|
|
6
9
|
|
|
7
10
|
## Requirements
|
|
8
11
|
|
|
9
|
-
To start developing your Galbe project, you first need to install
|
|
12
|
+
To start developing your Galbe project, you first need to install
|
|
13
|
+
[Bun](https://bun.sh).
|
|
10
14
|
|
|
11
15
|
## Automatic installation
|
|
12
16
|
|
|
@@ -16,7 +20,9 @@ This is the recommended way of setting up a Galbe project.
|
|
|
16
20
|
$ bun create galbe app
|
|
17
21
|
```
|
|
18
22
|
|
|
19
|
-
The Galbe starter CLI will request you to chose a template and a target language
|
|
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.
|
|
20
26
|
|
|
21
27
|
Now you can navigate to your newly created project and install it:
|
|
22
28
|
|
|
@@ -46,8 +52,9 @@ $ curl localhost:3000/hello/John?age=32
|
|
|
46
52
|
Hello John! You're 32 y.o.
|
|
47
53
|
```
|
|
48
54
|
|
|
49
|
-
> [!TIP]
|
|
50
|
-
> If you want to have a more complete view of Galbe capabilities, feel free to
|
|
55
|
+
> [!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.
|
|
51
58
|
|
|
52
59
|
## Manual installation
|
|
53
60
|
|
|
@@ -70,30 +77,37 @@ Open `package.json` file and add the following scripts:
|
|
|
70
77
|
}
|
|
71
78
|
```
|
|
72
79
|
|
|
73
|
-
As you can see, those scripts rely on Galbe CLI to run and build the
|
|
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.
|
|
74
82
|
|
|
75
|
-
This require your `index.ts` to export a default Galbe instance in order to
|
|
83
|
+
This require your `index.ts` to export a default Galbe instance in order to
|
|
84
|
+
work. As in the following example:
|
|
76
85
|
|
|
77
86
|
```ts
|
|
78
|
-
import { Galbe } from
|
|
87
|
+
import { Galbe } from "galbe";
|
|
79
88
|
|
|
80
|
-
const galbe = new Galbe({ port: 3000 })
|
|
81
|
-
galbe.get(
|
|
89
|
+
const galbe = new Galbe({ port: 3000 });
|
|
90
|
+
galbe.get("/hello", () => "Hello Mom!");
|
|
82
91
|
|
|
83
|
-
export default galbe
|
|
92
|
+
export default galbe;
|
|
84
93
|
```
|
|
85
94
|
|
|
86
|
-
This is the recommended way to proceed but it is not mandatory. Galbe instances
|
|
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.
|
|
87
98
|
|
|
88
99
|
> [!WARNING]
|
|
89
|
-
> In the case you decide to not rely on Galbe CLI to run/build your app, you
|
|
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.
|
|
90
103
|
|
|
91
104
|
## Configuration
|
|
92
105
|
|
|
93
|
-
To configure your Galbe server, you should pass your configuration to the Galbe
|
|
106
|
+
To configure your Galbe server, you should pass your configuration to the Galbe
|
|
107
|
+
constructor when you instanciate it.
|
|
94
108
|
|
|
95
109
|
```ts
|
|
96
|
-
const galbe = new Galbe(configuration)
|
|
110
|
+
const galbe = new Galbe(configuration);
|
|
97
111
|
```
|
|
98
112
|
|
|
99
113
|
### Properties
|
|
@@ -112,11 +126,14 @@ The base path is added as a prefix to all the routes created.
|
|
|
112
126
|
|
|
113
127
|
**routes**
|
|
114
128
|
|
|
115
|
-
A Glob Pattern or a list of Glob patterns defining the route files to be
|
|
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}`.
|
|
116
132
|
|
|
117
133
|
**plugin**
|
|
118
134
|
|
|
119
|
-
A property that can be used by plugins to add plugin's specific configuration.
|
|
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).
|
|
120
137
|
|
|
121
138
|
**tls**
|
|
122
139
|
|
|
@@ -130,15 +147,21 @@ Enable or disable TLS support. Default value is `false`.
|
|
|
130
147
|
|
|
131
148
|
**requestValidator.enabled**
|
|
132
149
|
|
|
133
|
-
Enable or disable the _request_ schema validation (See
|
|
150
|
+
Enable or disable the _request_ schema validation (See
|
|
151
|
+
[Request Schema definition](schemas.md#request-schema-definition)). Default
|
|
152
|
+
value is `true`.
|
|
134
153
|
|
|
135
154
|
**responseValidator.enabled**
|
|
136
155
|
|
|
137
|
-
Enable or disable the _response_ schema validation (See
|
|
156
|
+
Enable or disable the _response_ schema validation (See
|
|
157
|
+
[Request Schema definition](schemas.md#request-schema-definition)). Default
|
|
158
|
+
value is `true`.
|
|
138
159
|
|
|
139
160
|
### Examples
|
|
140
161
|
|
|
141
|
-
A common way to handle server configuration is to create new file
|
|
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:
|
|
142
165
|
|
|
143
166
|
galbe.config.js
|
|
144
167
|
|
|
@@ -152,27 +175,31 @@ export default {
|
|
|
152
175
|
index.js
|
|
153
176
|
|
|
154
177
|
```js
|
|
155
|
-
import { Galbe } from
|
|
156
|
-
import config from
|
|
178
|
+
import { Galbe } from "galbe";
|
|
179
|
+
import config from "./galbe.config";
|
|
157
180
|
|
|
158
|
-
export default new Galbe(config)
|
|
181
|
+
export default new Galbe(config);
|
|
159
182
|
```
|
|
160
183
|
|
|
161
|
-
> [!TIP]
|
|
162
|
-
> If you are using Typescript, you can import `GalbeConfig` type from galbe
|
|
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:
|
|
163
187
|
>
|
|
164
188
|
> ```ts
|
|
165
|
-
> import type { GalbeConfig } from
|
|
189
|
+
> import type { GalbeConfig } from "galbe";
|
|
166
190
|
> const config: GalbeConfig = {
|
|
167
191
|
> port: Number(Bun.env.GALBE_PORT),
|
|
168
|
-
> routes:
|
|
169
|
-
> }
|
|
170
|
-
> export default config
|
|
192
|
+
> routes: "routes/*.route.ts",
|
|
193
|
+
> };
|
|
194
|
+
> export default config;
|
|
171
195
|
> ```
|
|
172
196
|
|
|
173
197
|
## Project structure
|
|
174
198
|
|
|
175
|
-
One key aspect of Galbe, is its versatility in terms of project structure. This
|
|
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}`.
|
|
176
203
|
|
|
177
204
|
Here are two examples of valid project structures by default:
|
|
178
205
|
|
|
@@ -214,18 +241,26 @@ Here are two examples of valid project structures by default:
|
|
|
214
241
|
└── tsconfig.json
|
|
215
242
|
```
|
|
216
243
|
|
|
217
|
-
In both cases, the
|
|
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.
|
|
218
247
|
|
|
219
|
-
You can find more info about Route Files definition in the
|
|
248
|
+
You can find more info about Route Files definition in the
|
|
249
|
+
[Routes Files](routes.md#route-files) section.
|
|
220
250
|
|
|
221
251
|
> [!NOTE]
|
|
222
|
-
> The examples provided above will work with the default configuration, but you
|
|
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.
|
|
223
256
|
|
|
224
257
|
## How to debug
|
|
225
258
|
|
|
226
|
-
The easiest way to debug your app is by installing the
|
|
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).
|
|
227
261
|
|
|
228
|
-
You can then create a `.vscode/launch.json` config file in your project root
|
|
262
|
+
You can then create a `.vscode/launch.json` config file in your project root
|
|
263
|
+
directory. Here is an example of configuration:
|
|
229
264
|
|
|
230
265
|
```json
|
|
231
266
|
{
|
|
@@ -239,7 +274,7 @@ You can then create a `.vscode/launch.json` config file in your project root dir
|
|
|
239
274
|
"env": { "TERM": "xterm" },
|
|
240
275
|
"cwd": "${workspaceFolder}",
|
|
241
276
|
"runtime": "bun",
|
|
242
|
-
"runtimeArgs": ["dev", "index.ts", "
|
|
277
|
+
"runtimeArgs": ["dev", "index.ts", "-w", "."]
|
|
243
278
|
}
|
|
244
279
|
]
|
|
245
280
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "galbe",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.1",
|
|
4
4
|
"description": "Fast, lightweight and highly customizable JavaScript web framework based on Bun",
|
|
5
5
|
"author": "Pierre Caillaud M (https://github.com/pierre-cm)",
|
|
6
6
|
"type": "module",
|
|
@@ -29,6 +29,7 @@
|
|
|
29
29
|
"license": "MIT",
|
|
30
30
|
"scripts": {
|
|
31
31
|
"test": "bun test",
|
|
32
|
+
"typecheck": "tsc --noEmit --emitDeclarationOnly false",
|
|
32
33
|
"postinstall": "bun run ./scripts/postinstall.ts",
|
|
33
34
|
"release": "release-it"
|
|
34
35
|
},
|
|
@@ -239,6 +239,7 @@ export const OpenAPISerializer = async (g: Galbe, version = '3.0.3'): Promise<Op
|
|
|
239
239
|
if (r.schema.response && Object.keys(r.schema.response).length) {
|
|
240
240
|
responses = Object.fromEntries(
|
|
241
241
|
Object.entries(r.schema.response).map(([status, v]) => {
|
|
242
|
+
if(!v) return []
|
|
242
243
|
let s = Number(status) as keyof typeof HttpStatus
|
|
243
244
|
let { schema, isJson } = schemaToOpenapi(v)
|
|
244
245
|
let { type, format } = resolveRef(schema)
|
|
@@ -248,7 +249,7 @@ export const OpenAPISerializer = async (g: Galbe, version = '3.0.3'): Promise<Op
|
|
|
248
249
|
content: { [media]: { schema: schema } }
|
|
249
250
|
}
|
|
250
251
|
if (components.responses && r.schema.response?.[s]?.id) {
|
|
251
|
-
components.responses[r.schema.response?.[s]
|
|
252
|
+
components.responses[r.schema.response?.[s]?.id as string] = response
|
|
252
253
|
//@ts-ignore
|
|
253
254
|
response = { $ref: `#/components/responses/${r.schema.response?.[s].id}` }
|
|
254
255
|
}
|
package/src/parser.ts
CHANGED
|
@@ -655,7 +655,7 @@ export const responseParser = (response: any, ctx: Context, schema?: STResponse)
|
|
|
655
655
|
if (response instanceof Response) return response
|
|
656
656
|
else if (typeof response === 'string') {
|
|
657
657
|
if (!details?.headers?.has('content-type')) {
|
|
658
|
-
if (schema?.[details.status][Kind] === 'json') {
|
|
658
|
+
if (schema?.[details.status]?.[Kind] === 'json') {
|
|
659
659
|
details?.headers?.set('content-type', 'application/json')
|
|
660
660
|
response = `"${response}"`
|
|
661
661
|
} else details?.headers?.set('content-type', 'text/plain')
|
package/src/types.ts
CHANGED
|
@@ -52,7 +52,7 @@ export type STResponseValue =
|
|
|
52
52
|
| STStream
|
|
53
53
|
| STAny
|
|
54
54
|
| STNull
|
|
55
|
-
export type STResponse = Record<number | 'default', STResponseValue
|
|
55
|
+
export type STResponse = Partial<Record<number | 'default', STResponseValue>>
|
|
56
56
|
|
|
57
57
|
export type MaybeArray<T> = T | T[]
|
|
58
58
|
export type MaybeSTArray<T extends STSchema> = T | STArray<T>
|
|
@@ -136,7 +136,7 @@ export type RequestSchema<
|
|
|
136
136
|
P extends Partial<STParams<Path>> = Partial<STParams<Path>>,
|
|
137
137
|
Q extends STQuery = STQuery,
|
|
138
138
|
B extends STBody = STBody,
|
|
139
|
-
R extends Partial<STResponse> =
|
|
139
|
+
R extends Partial<STResponse> = STResponse
|
|
140
140
|
> = {
|
|
141
141
|
headers?: H
|
|
142
142
|
params?: P
|
|
@@ -194,7 +194,7 @@ export type Endpoint<M extends Method> = {
|
|
|
194
194
|
H extends STHeaders = any,
|
|
195
195
|
Q extends STQuery = any,
|
|
196
196
|
B extends STBody = any,
|
|
197
|
-
R extends
|
|
197
|
+
R extends STResponse = STResponse
|
|
198
198
|
>(
|
|
199
199
|
path: Path,
|
|
200
200
|
schema: RequestSchema<M, Path, H, P, Q, B, R>,
|
|
@@ -207,7 +207,7 @@ export type Endpoint<M extends Method> = {
|
|
|
207
207
|
H extends STHeaders = any,
|
|
208
208
|
Q extends STQuery = any,
|
|
209
209
|
B extends STBody = any,
|
|
210
|
-
R extends
|
|
210
|
+
R extends STResponse = STResponse
|
|
211
211
|
>(
|
|
212
212
|
path: Path,
|
|
213
213
|
schema: RequestSchema<M, Path, H, P, Q, B, R>,
|
|
@@ -219,7 +219,7 @@ export type Endpoint<M extends Method> = {
|
|
|
219
219
|
H extends STHeaders = any,
|
|
220
220
|
Q extends STQuery = any,
|
|
221
221
|
B extends STBody = any,
|
|
222
|
-
R extends
|
|
222
|
+
R extends STResponse = STResponse
|
|
223
223
|
>(
|
|
224
224
|
path: Path,
|
|
225
225
|
hooks: Hook<M, Path, RequestSchema<M, Path, H, P, Q, B, R>>[],
|
|
@@ -231,7 +231,7 @@ export type Endpoint<M extends Method> = {
|
|
|
231
231
|
H extends STHeaders = any,
|
|
232
232
|
Q extends STQuery = any,
|
|
233
233
|
B extends STBody = any,
|
|
234
|
-
R extends
|
|
234
|
+
R extends STResponse = STResponse
|
|
235
235
|
>(
|
|
236
236
|
path: Path,
|
|
237
237
|
handler: Handler<M, Path, RequestSchema<M, Path, H, P, Q, B, R>>
|
|
@@ -267,7 +267,7 @@ export type Route<
|
|
|
267
267
|
H extends STHeaders = STHeaders,
|
|
268
268
|
Q extends STQuery = STQuery,
|
|
269
269
|
B extends STBody = STBody,
|
|
270
|
-
R extends
|
|
270
|
+
R extends STResponse = STResponse,
|
|
271
271
|
SP extends string = string,
|
|
272
272
|
SR extends string = string
|
|
273
273
|
> = {
|
package/src/validator.ts
CHANGED
|
@@ -96,6 +96,7 @@ export const validate = (elt: any, schema: STSchema, parse = false): any => {
|
|
|
96
96
|
export const validateResponse = (response: any, schema: STResponse, status: number) => {
|
|
97
97
|
if (!(status in schema)) return
|
|
98
98
|
const s = schema?.[status] || schema?.['default']
|
|
99
|
+
if(!s) return
|
|
99
100
|
if (response instanceof ReadableStream) {
|
|
100
101
|
if (!s[Stream]) throw new InternalError(`Expected ${s[Kind]} response, but got ReadableStream`)
|
|
101
102
|
} else if (isIterator(response)) {
|
package/test/plugins.test.ts
CHANGED
package/test/responses.test.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { expect, test, describe, beforeAll } from 'bun:test'
|
|
2
|
-
import { Galbe, $T } from '../src'
|
|
2
|
+
import { Galbe, $T, type Context } from '../src'
|
|
3
3
|
import { decoder } from './test.utils'
|
|
4
4
|
|
|
5
5
|
const port = 7359
|
|
@@ -21,7 +21,7 @@ const rsTxt = (text: string) => {
|
|
|
21
21
|
})
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
const handleResp = ctx => ctx.body
|
|
24
|
+
const handleResp = (ctx: Context) => ctx.body
|
|
25
25
|
|
|
26
26
|
describe('responses', () => {
|
|
27
27
|
beforeAll(async () => {
|
|
@@ -143,6 +143,7 @@ describe('responses', () => {
|
|
|
143
143
|
|
|
144
144
|
expect(resp.status).toBe(200)
|
|
145
145
|
expect(resp.headers.get('content-type')).toBe('application/octet-stream')
|
|
146
|
+
//@ts-ignore
|
|
146
147
|
expect(body).toEqual(reqBody)
|
|
147
148
|
})
|
|
148
149
|
|
package/test/routeFiles.test.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { expect, test, describe } from 'bun:test'
|
|
2
|
-
import { defineRoutes, metaAnalysis } from '../src/routes'
|
|
2
|
+
import { defineRoutes, GalbeProxy, metaAnalysis } from '../src/routes'
|
|
3
3
|
import { Galbe } from '../src'
|
|
4
4
|
|
|
5
5
|
describe('routeFiles', () => {
|
|
@@ -68,7 +68,7 @@ describe('routeFiles', () => {
|
|
|
68
68
|
})
|
|
69
69
|
|
|
70
70
|
test('define routes, no route', async () => {
|
|
71
|
-
const k = new Galbe()
|
|
71
|
+
const k = new GalbeProxy(new Galbe())
|
|
72
72
|
await defineRoutes({}, k)
|
|
73
73
|
expect(k.router.routes).toEqual({
|
|
74
74
|
routes: {}
|
|
@@ -76,7 +76,7 @@ describe('routeFiles', () => {
|
|
|
76
76
|
})
|
|
77
77
|
|
|
78
78
|
test('define routes, no route (false)', async () => {
|
|
79
|
-
const k = new Galbe({ routes: false })
|
|
79
|
+
const k = new GalbeProxy(new Galbe({ routes: false }))
|
|
80
80
|
await defineRoutes({}, k)
|
|
81
81
|
expect(k.router.routes).toEqual({
|
|
82
82
|
routes: {}
|
|
@@ -84,7 +84,7 @@ describe('routeFiles', () => {
|
|
|
84
84
|
})
|
|
85
85
|
|
|
86
86
|
test('define routes, no route found', async () => {
|
|
87
|
-
const k = new Galbe()
|
|
87
|
+
const k = new GalbeProxy(new Galbe())
|
|
88
88
|
await defineRoutes({ routes: 'unexisting_route' }, k)
|
|
89
89
|
expect(k.router.routes).toEqual({
|
|
90
90
|
routes: {}
|
|
@@ -92,7 +92,7 @@ describe('routeFiles', () => {
|
|
|
92
92
|
})
|
|
93
93
|
|
|
94
94
|
test('define routes, route.empty', async () => {
|
|
95
|
-
const k = new Galbe()
|
|
95
|
+
const k = new GalbeProxy(new Galbe())
|
|
96
96
|
|
|
97
97
|
await defineRoutes({ routes: 'test/resources/test.route.empty.ts' }, k)
|
|
98
98
|
|
|
@@ -132,7 +132,7 @@ describe('routeFiles', () => {
|
|
|
132
132
|
})
|
|
133
133
|
|
|
134
134
|
test('define routes, all', async () => {
|
|
135
|
-
const k = new Galbe()
|
|
135
|
+
const k = new GalbeProxy(new Galbe())
|
|
136
136
|
|
|
137
137
|
await defineRoutes({ routes: ['test/resources/test.route.*.ts'] }, k)
|
|
138
138
|
|
package/tsconfig.json
CHANGED
package/bun.lockb
DELETED
|
Binary file
|