galbe 0.1.4 → 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.
@@ -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.Optional(T.Object(T.Any())),
32
- query: { text: T.Optional(T.String()), stream: T.Optional(T.String()) }
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) {
@@ -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)
@@ -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.ByteArray(),
26
- string: T.String(),
27
- number: T.Number(),
28
- bool: T.Boolean(),
29
- any: T.Any(),
30
- optional: T.Optional(T.Any())
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.Object(T.Any()),
35
- array: T.Array(T.Any())
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 = 'ByteArray'
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 === 'ByteArray') return new Uint8Array(chunks.map(c => Array.from(c)).flat())
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: 'ByteArray', content: decoder.decode(ctx.body) }
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
@@ -1 +0,0 @@
1
- TODO: Routes documentation