galbe 0.3.0 → 0.5.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.
@@ -49,6 +49,18 @@ describe('routeFiles', () => {
49
49
  put: {
50
50
  tags: 'tag1, tag2',
51
51
  other: 'Hello Mom!'
52
+ },
53
+ patch: {
54
+ head: 'patch method'
55
+ },
56
+ options: {
57
+ head: 'options method'
58
+ },
59
+ delete: {
60
+ head: 'delete method'
61
+ },
62
+ head: {
63
+ head: 'head method'
52
64
  }
53
65
  }
54
66
  }
@@ -59,12 +71,7 @@ describe('routeFiles', () => {
59
71
  const k = new Galbe()
60
72
  await defineRoutes({}, k)
61
73
  expect(k.router.routes).toEqual({
62
- GET: {},
63
- POST: {},
64
- PUT: {},
65
- PATCH: {},
66
- DELETE: {},
67
- OPTIONS: {}
74
+ routes: {}
68
75
  })
69
76
  })
70
77
 
@@ -72,12 +79,7 @@ describe('routeFiles', () => {
72
79
  const k = new Galbe({ routes: false })
73
80
  await defineRoutes({}, k)
74
81
  expect(k.router.routes).toEqual({
75
- GET: {},
76
- POST: {},
77
- PUT: {},
78
- PATCH: {},
79
- DELETE: {},
80
- OPTIONS: {}
82
+ routes: {}
81
83
  })
82
84
  })
83
85
 
@@ -85,12 +87,7 @@ describe('routeFiles', () => {
85
87
  const k = new Galbe()
86
88
  await defineRoutes({ routes: 'unexisting_route' }, k)
87
89
  expect(k.router.routes).toEqual({
88
- GET: {},
89
- POST: {},
90
- PUT: {},
91
- PATCH: {},
92
- DELETE: {},
93
- OPTIONS: {}
90
+ routes: {}
94
91
  })
95
92
  })
96
93
 
@@ -100,17 +97,17 @@ describe('routeFiles', () => {
100
97
  await defineRoutes({ routes: 'test/resources/test.route.empty.ts' }, k)
101
98
 
102
99
  const r = k.router.routes
103
- expect(r.GET?.children?.one?.route).toMatchObject({
100
+ expect(r?.children?.one?.routes.get).toMatchObject({
104
101
  method: 'get',
105
102
  path: '/one',
106
103
  handler: () => {}
107
104
  })
108
- expect(r.POST?.children?.two?.route).toMatchObject({
105
+ expect(r?.children?.two?.routes.post).toMatchObject({
109
106
  method: 'post',
110
107
  path: '/two',
111
108
  handler: () => {}
112
109
  })
113
- expect(r.PUT?.children?.three?.route).toMatchObject({
110
+ expect(r?.children?.three?.routes.put).toMatchObject({
114
111
  method: 'put',
115
112
  path: '/three',
116
113
  handler: () => {}
@@ -140,36 +137,56 @@ describe('routeFiles', () => {
140
137
  await defineRoutes({ routes: ['test/resources/test.route.*.ts'] }, k)
141
138
 
142
139
  const r = k.router.routes
143
- expect(r.GET?.children?.one?.route).toMatchObject({
140
+ expect(r?.children?.one?.routes.get).toMatchObject({
144
141
  method: 'get',
145
142
  path: '/one',
146
143
  handler: () => {}
147
144
  })
148
- expect(r.POST?.children?.two?.route).toMatchObject({
145
+ expect(r?.children?.two?.routes.post).toMatchObject({
149
146
  method: 'post',
150
147
  path: '/two',
151
148
  handler: () => {}
152
149
  })
153
- expect(r.PUT?.children?.three?.route).toMatchObject({
150
+ expect(r?.children?.three?.routes.put).toMatchObject({
154
151
  method: 'put',
155
152
  path: '/three',
156
153
  handler: () => {}
157
154
  })
158
- expect(r.GET?.children?.test?.param?.route).toMatchObject({
155
+ expect(r?.children?.test?.param?.routes.get).toMatchObject({
159
156
  method: 'get',
160
157
  path: '/test/:param1',
161
158
  handler: () => {}
162
159
  })
163
- expect(r.POST?.children?.test?.route).toMatchObject({
160
+ expect(r?.children?.test?.routes.post).toMatchObject({
164
161
  method: 'post',
165
162
  path: '/test',
166
163
  handler: () => {}
167
164
  })
168
- expect(r.PUT?.children?.test?.route).toMatchObject({
165
+ expect(r?.children?.test?.routes.put).toMatchObject({
169
166
  method: 'put',
170
167
  path: '/test',
171
168
  handler: () => {}
172
169
  })
170
+ expect(r?.children?.test?.routes.patch).toMatchObject({
171
+ method: 'patch',
172
+ path: '/test',
173
+ handler: () => {}
174
+ })
175
+ expect(r?.children?.test?.routes.options).toMatchObject({
176
+ method: 'options',
177
+ path: '/test',
178
+ handler: () => {}
179
+ })
180
+ expect(r?.children?.test?.routes.delete).toMatchObject({
181
+ method: 'delete',
182
+ path: '/test',
183
+ handler: () => {}
184
+ })
185
+ expect(r?.children?.test?.routes.head).toMatchObject({
186
+ method: 'head',
187
+ path: '/test',
188
+ handler: () => {}
189
+ })
173
190
  expect(k.meta?.sort((a, b) => (a.file < b.file ? 1 : -1))).toMatchObject([
174
191
  {
175
192
  header: {},
@@ -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()