galbe 0.1.6 → 0.1.7
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/bin/cli.ts +11 -9
- package/bun.lockb +0 -0
- package/docs/getting-started.md +221 -3
- package/docs/routes.md +124 -0
- package/docs/schemas.md +244 -0
- package/package.json +1 -2
- package/src/index.ts +122 -219
- package/src/parser.ts +98 -100
- package/src/routes.ts +3 -1
- package/src/schema.ts +378 -0
- package/src/server.ts +1 -1
- package/src/types.ts +128 -147
- package/src/validator.ts +33 -35
- package/test/parser.test.ts +67 -67
- package/test/requests.test.ts +64 -118
- package/test/resources/test.route.comment.ts +3 -3
- package/test/responses.test.ts +3 -3
- package/test/routeFiles.test.ts +13 -0
- package/test/test.utils.ts +14 -12
- package/docs/Getting Started/basic.md +0 -1
- package/docs/Guides/routes.md +0 -1
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 } from '../src'
|
|
3
3
|
import { decoder } from './test.utils'
|
|
4
4
|
|
|
5
5
|
const port = 7359
|
|
@@ -28,8 +28,8 @@ describe('responses', () => {
|
|
|
28
28
|
galbe.post(
|
|
29
29
|
'/response',
|
|
30
30
|
{
|
|
31
|
-
body: T.
|
|
32
|
-
query: { text: T.
|
|
31
|
+
body: $T.optional($T.object($T.any())),
|
|
32
|
+
query: { text: $T.optional($T.string()), stream: $T.optional($T.string()) }
|
|
33
33
|
},
|
|
34
34
|
ctx => {
|
|
35
35
|
if (ctx.query.text) {
|
package/test/routeFiles.test.ts
CHANGED
|
@@ -68,6 +68,19 @@ describe('routeFiles', () => {
|
|
|
68
68
|
})
|
|
69
69
|
})
|
|
70
70
|
|
|
71
|
+
test('define routes, no route (false)', async () => {
|
|
72
|
+
const k = new Galbe({ routes: false })
|
|
73
|
+
await defineRoutes({}, k)
|
|
74
|
+
expect(k.router.routes).toEqual({
|
|
75
|
+
GET: {},
|
|
76
|
+
POST: {},
|
|
77
|
+
PUT: {},
|
|
78
|
+
PATCH: {},
|
|
79
|
+
DELETE: {},
|
|
80
|
+
OPTIONS: {}
|
|
81
|
+
})
|
|
82
|
+
})
|
|
83
|
+
|
|
71
84
|
test('define routes, no route found', async () => {
|
|
72
85
|
const k = new Galbe()
|
|
73
86
|
await defineRoutes({ routes: 'unexisting_route' }, k)
|
package/test/test.utils.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { T, type Context } from '../src'
|
|
1
|
+
import { $T, type Context } from '../src'
|
|
2
2
|
|
|
3
3
|
export type Case = {
|
|
4
4
|
body?: any
|
|
@@ -22,17 +22,17 @@ export const fileHash = async (ba: any) =>
|
|
|
22
22
|
export const decoder = new TextDecoder()
|
|
23
23
|
|
|
24
24
|
export const schema_objectBase = {
|
|
25
|
-
ba: T.
|
|
26
|
-
string: T.
|
|
27
|
-
number: T.
|
|
28
|
-
bool: T.
|
|
29
|
-
any: T.
|
|
30
|
-
optional: T.
|
|
25
|
+
ba: $T.byteArray(),
|
|
26
|
+
string: $T.string(),
|
|
27
|
+
number: $T.number(),
|
|
28
|
+
bool: $T.boolean(),
|
|
29
|
+
any: $T.any(),
|
|
30
|
+
optional: $T.optional($T.any())
|
|
31
31
|
}
|
|
32
32
|
export const schema_object = {
|
|
33
33
|
...schema_objectBase,
|
|
34
|
-
object: T.
|
|
35
|
-
array: T.
|
|
34
|
+
object: $T.object($T.any()),
|
|
35
|
+
array: $T.array()
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
export const isAsyncIterator = (obj: any) => {
|
|
@@ -47,12 +47,13 @@ const parseAsyncIterator = async (body: any): Promise<any> => {
|
|
|
47
47
|
const chunks = []
|
|
48
48
|
let type
|
|
49
49
|
for await (const chunk of body) {
|
|
50
|
-
if (chunk instanceof Uint8Array) type = '
|
|
50
|
+
if (chunk instanceof Uint8Array) type = 'byteArray'
|
|
51
51
|
else if (typeof chunk === 'string') type = 'string'
|
|
52
|
+
// @ts-ignore
|
|
52
53
|
chunks.push(chunk)
|
|
53
54
|
}
|
|
54
55
|
// @ts-ignore
|
|
55
|
-
if (type === '
|
|
56
|
+
if (type === 'byteArray') return new Uint8Array(chunks.map(c => Array.from(c)).flat())
|
|
56
57
|
if (type === 'string') return chunks.join('')
|
|
57
58
|
return chunks
|
|
58
59
|
}
|
|
@@ -83,7 +84,7 @@ export const handleBody = async (ctx: any) => {
|
|
|
83
84
|
return { type: 'array', content: ctx.body }
|
|
84
85
|
}
|
|
85
86
|
if (ctx?.body instanceof Uint8Array) {
|
|
86
|
-
return { type: '
|
|
87
|
+
return { type: 'byteArray', content: decoder.decode(ctx.body) }
|
|
87
88
|
}
|
|
88
89
|
if (isAsyncIterator(ctx.body)) {
|
|
89
90
|
return { type: 'AsyncIterator', content: await parseAsyncIterator(ctx.body) }
|
|
@@ -93,6 +94,7 @@ export const handleBody = async (ctx: any) => {
|
|
|
93
94
|
}
|
|
94
95
|
export const handleUrlFormStream = async (ctx: Context) => {
|
|
95
96
|
let resp: Record<string, any> = {}
|
|
97
|
+
// @ts-ignore
|
|
96
98
|
for await (const [k, v] of ctx.body) {
|
|
97
99
|
if (k in resp) {
|
|
98
100
|
resp[k] = Array.isArray(resp[k]) ? [...resp[k], v] : [resp[k], v]
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
TODO
|
package/docs/Guides/routes.md
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
TODO: Routes documentation
|