galbe 0.2.0 → 0.4.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.
@@ -1,5 +1,5 @@
1
1
  import { expect, test, describe } from 'bun:test'
2
- import { Galbe, NotFoundError, type RouteNode } from '../src'
2
+ import { Galbe, MethodNotAllowedError, NotFoundError, type RouteNode } from '../src'
3
3
 
4
4
  describe('router', () => {
5
5
  test('empty', async () => {
@@ -7,12 +7,7 @@ describe('router', () => {
7
7
  const router = galbe.router
8
8
 
9
9
  expect(router.prefix).toBe('')
10
- expect(router.routes.GET).toEqual({})
11
- expect(router.routes.POST).toEqual({})
12
- expect(router.routes.PUT).toEqual({})
13
- expect(router.routes.PATCH).toEqual({})
14
- expect(router.routes.DELETE).toEqual({})
15
- expect(router.routes.OPTIONS).toEqual({})
10
+ expect(router.routes).toEqual({ routes: {} })
16
11
  })
17
12
 
18
13
  test('routes, bad syntax', async () => {
@@ -45,13 +40,13 @@ describe('router', () => {
45
40
  const galbe = new Galbe()
46
41
  const router = galbe.router
47
42
 
48
- expect(router.routes.GET).toEqual({})
43
+ expect(router.routes).toEqual({ routes: {} })
49
44
 
50
45
  galbe.get('/', () => {})
51
- let r: RouteNode | undefined = router.routes.GET
46
+ let r: RouteNode | undefined = router.routes
52
47
 
53
- expect(r?.route?.method).toBe('get')
54
- expect(r?.route?.path).toBe('/')
48
+ expect(r?.routes.get?.method).toBe('get')
49
+ expect(r?.routes.get?.path).toBe('/')
55
50
  expect(r?.param).toBeUndefined()
56
51
  expect(r?.children).toBeUndefined()
57
52
 
@@ -59,28 +54,28 @@ describe('router', () => {
59
54
  galbe.get('/test', mockHandler)
60
55
  r = r?.children?.test
61
56
 
62
- expect(r?.route?.method).toBe('get')
63
- expect(r?.route?.path).toBe('/test')
57
+ expect(r?.routes.get?.method).toBe('get')
58
+ expect(r?.routes.get?.path).toBe('/test')
64
59
  expect(r?.param).toBeUndefined()
65
60
  expect(r?.children).toBeUndefined()
66
- expect(r?.route?.handler).toBe(mockHandler)
61
+ expect(r?.routes.get?.handler).toBe(mockHandler)
67
62
 
68
63
  const mockHandler2 = () => {}
69
64
  galbe.get('/test/:foo', mockHandler2)
70
65
  r = r?.param
71
66
 
72
- expect(r?.route?.method).toBe('get')
73
- expect(r?.route?.path).toBe('/test/:foo')
67
+ expect(r?.routes.get?.method).toBe('get')
68
+ expect(r?.routes.get?.path).toBe('/test/:foo')
74
69
  expect(r?.param).toBeUndefined()
75
70
  expect(r?.children).toBeUndefined()
76
- expect(r?.route?.handler).toBe(mockHandler2)
71
+ expect(r?.routes.get?.handler).toBe(mockHandler2)
77
72
 
78
73
  const mockHandler3 = () => {}
79
74
  galbe.get('/test/:foo/bar', mockHandler3)
80
75
 
81
76
  expect(r?.children).toHaveProperty('bar')
82
77
  expect(r?.param).toBeUndefined()
83
- expect(r?.children?.bar?.route?.handler).toBe(mockHandler3)
78
+ expect(r?.children?.bar?.routes.get?.handler).toBe(mockHandler3)
84
79
  })
85
80
 
86
81
  test('redefining root', async () => {
@@ -93,26 +88,26 @@ describe('router', () => {
93
88
  galbe.get('/test', h2)
94
89
  galbe.get('/', h3)
95
90
 
96
- let r: RouteNode | undefined = router.routes.GET
91
+ let r: RouteNode | undefined = router.routes
97
92
  expect(router.prefix).toBe('')
98
- expect(r?.route?.method).toBe('get')
99
- expect(r?.route?.path).toBe('/')
100
- expect(r?.route?.handler).toBe(h3)
101
- expect(r?.children?.test?.route?.method).toBe('get')
102
- expect(r?.children?.test?.route?.handler).toBe(h2)
93
+ expect(r?.routes.get?.method).toBe('get')
94
+ expect(r?.routes.get?.path).toBe('/')
95
+ expect(r?.routes.get?.handler).toBe(h3)
96
+ expect(r?.children?.test?.routes.get?.method).toBe('get')
97
+ expect(r?.children?.test?.routes.get?.handler).toBe(h2)
103
98
 
104
99
  const [h4, h5] = [() => {}, () => {}]
105
100
 
106
101
  galbe.get('/foo/bar', h4)
107
102
  galbe.get('/foo', h5)
108
103
 
109
- r = router?.routes?.GET?.children?.foo
110
- expect(r?.route?.method).toBe('get')
111
- expect(r?.route?.path).toBe('/foo')
112
- expect(r?.route?.handler).toBe(h5)
113
- expect(r?.children?.bar?.route?.method).toBe('get')
114
- expect(r?.children?.bar?.route?.path).toBe('/foo/bar')
115
- expect(r?.children?.bar?.route?.handler).toBe(h4)
104
+ r = router?.routes?.children?.foo
105
+ expect(r?.routes.get?.method).toBe('get')
106
+ expect(r?.routes.get?.path).toBe('/foo')
107
+ expect(r?.routes.get?.handler).toBe(h5)
108
+ expect(r?.children?.bar?.routes.get?.method).toBe('get')
109
+ expect(r?.children?.bar?.routes.get?.path).toBe('/foo/bar')
110
+ expect(r?.children?.bar?.routes.get?.handler).toBe(h4)
116
111
  })
117
112
 
118
113
  test('find route', async () => {
@@ -125,18 +120,18 @@ describe('router', () => {
125
120
  galbe.get('/test/foo', h3)
126
121
  galbe.get('/test/foo/bar', h4)
127
122
 
128
- let r1 = router.find('GET', '/')
123
+ let r1 = router.find('get', '/')
129
124
  expect(r1.path).toBe('/')
130
125
  expect(r1.handler).toBe(h1)
131
- let r2 = router.find('GET', '/test/foo')
126
+ let r2 = router.find('get', '/test/foo')
132
127
  expect(r2.path).toBe('/test/foo')
133
128
  expect(r2.handler).toBe(h3)
134
- let r3 = router.find('GET', '/test/foo/bar')
129
+ let r3 = router.find('get', '/test/foo/bar')
135
130
  expect(r3.path).toBe('/test/foo/bar')
136
131
  expect(r3.handler).toBe(h4)
137
132
 
138
133
  try {
139
- router.find('GET', '/test')
134
+ router.find('get', '/test')
140
135
  expect.unreachable()
141
136
  } catch (err: any) {
142
137
  expect(err).toBeInstanceOf(NotFoundError)
@@ -145,7 +140,7 @@ describe('router', () => {
145
140
  }
146
141
 
147
142
  try {
148
- router.find('GET', '/test/bar/bar')
143
+ router.find('get', '/test/bar/bar')
149
144
  expect.unreachable()
150
145
  } catch (err: any) {
151
146
  expect(err).toBeInstanceOf(NotFoundError)
@@ -163,11 +158,11 @@ describe('router', () => {
163
158
  galbe.get('/test/:foo', h1)
164
159
  galbe.get('/test/test', h2)
165
160
 
166
- let r1 = router.find('GET', '/test/42')
161
+ let r1 = router.find('get', '/test/42')
167
162
  expect(r1.path).toBe('/test/:foo')
168
163
  expect(r1.handler).toBe(h1)
169
164
 
170
- let r2 = router.find('GET', '/test/test')
165
+ let r2 = router.find('get', '/test/test')
171
166
  expect(r2.path).toBe('/test/test')
172
167
  expect(r2.handler).toBe(h2)
173
168
  })
@@ -184,20 +179,50 @@ describe('router', () => {
184
179
 
185
180
  galbe.get('/test/foo/*/lol', h4)
186
181
 
187
- let r1 = router.find('GET', '/test/foo/bar/42')
182
+ let r1 = router.find('get', '/test/foo/bar/42')
188
183
  expect(r1.path).toBe('/test/foo/*')
189
184
  expect(r1.handler).toBe(h1)
190
185
 
191
- let r3 = router.find('GET', '/test/foo/bar/bar')
186
+ let r3 = router.find('get', '/test/foo/bar/bar')
192
187
  expect(r3.path).toBe('/test/foo/:p/bar')
193
188
  expect(r3.handler).toBe(h3)
194
189
 
195
- let r4 = router.find('GET', '/test/foo/foo')
190
+ let r4 = router.find('get', '/test/foo/foo')
196
191
  expect(r4.path).toBe('/test/foo/*')
197
192
  expect(r4.handler).toBe(h1)
198
193
 
199
- let r5 = router.find('GET', '/test/foo/toto/lol')
194
+ let r5 = router.find('get', '/test/foo/toto/lol')
200
195
  expect(r5.path).toBe('/test/foo/*/lol')
201
196
  expect(r5.handler).toBe(h4)
202
197
  })
198
+
199
+ test('method not allowed', async () => {
200
+ const galbe = new Galbe()
201
+ const router = galbe.router
202
+
203
+ const [h1, h2] = [() => {}, () => {}]
204
+
205
+ galbe.put('/test/foo', h1)
206
+ galbe.post('/test/bar/*', h2)
207
+
208
+ let r1 = router.find('put', '/test/foo')
209
+ expect(r1.path).toBe('/test/foo')
210
+ expect(r1.handler).toBe(h1)
211
+ try {
212
+ router.find('get', '/test/foo')
213
+ expect.unreachable()
214
+ } catch (err) {
215
+ expect(err).toBeInstanceOf(MethodNotAllowedError)
216
+ }
217
+
218
+ let r2 = router.find('post', '/test/bar/tender')
219
+ expect(r2.path).toBe('/test/bar/*')
220
+ expect(r2.handler).toBe(h2)
221
+ try {
222
+ router.find('delete', '/test/bar/toto/titi')
223
+ expect.unreachable()
224
+ } catch (err) {
225
+ expect(err).toBeInstanceOf(MethodNotAllowedError)
226
+ }
227
+ })
203
228
  })
package/scripts/build.ts DELETED
@@ -1,14 +0,0 @@
1
- import { $ } from 'bun'
2
-
3
- await Bun.build({
4
- entrypoints: ['./src/index.ts'],
5
- outdir: './dist',
6
- minify: true,
7
- target: 'bun',
8
- sourcemap: 'external',
9
- external: ['bun']
10
- })
11
-
12
- await $`bun x tsc --outdir ./dist`
13
-
14
- process.exit()