galbe 0.1.7 → 0.1.13

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/src/types.ts CHANGED
@@ -72,6 +72,7 @@ export type GalbeConfig = {
72
72
  basePath?: string
73
73
  server?: Exclude<ServeOptions, 'port'> | TLSServeOptions
74
74
  routes?: boolean | string | string[]
75
+ router?: { cacheEnabled: boolean }
75
76
  plugin?: Record<string, any>
76
77
  }
77
78
  /**
@@ -129,7 +130,6 @@ export type Context<Path extends string = string, S extends RequestSchema = Requ
129
130
  [header: string]: string
130
131
  }
131
132
  status?: number
132
- redirect?: string
133
133
  }
134
134
  }
135
135
  export type Next = () => void | Promise<void>
@@ -146,7 +146,7 @@ export type Endpoint = {
146
146
  H extends STHeaders,
147
147
  P extends Partial<STParams<Path>>,
148
148
  Q extends STQuery,
149
- B extends STBody = STObject
149
+ B extends STBody = any
150
150
  >(
151
151
  path: Path,
152
152
  schema: RequestSchema<Path, H, P, Q, B>,
@@ -158,7 +158,7 @@ export type Endpoint = {
158
158
  H extends STHeaders,
159
159
  P extends Partial<STParams<Path>>,
160
160
  Q extends STQuery,
161
- B extends STBody = STObject
161
+ B extends STBody = any
162
162
  >(
163
163
  path: Path,
164
164
  schema: RequestSchema<Path, H, P, Q, B>,
@@ -169,7 +169,7 @@ export type Endpoint = {
169
169
  H extends STHeaders,
170
170
  P extends Partial<STParams<Path>>,
171
171
  Q extends STQuery,
172
- B extends STBody = STObject
172
+ B extends STBody = any
173
173
  >(
174
174
  path: Path,
175
175
  hooks: Hook<Path, RequestSchema<Path, H, P, Q, B>>[],
@@ -180,7 +180,7 @@ export type Endpoint = {
180
180
  H extends STHeaders,
181
181
  P extends Partial<STParams<Path>>,
182
182
  Q extends STQuery,
183
- B extends STBody = STObject
183
+ B extends STBody = any
184
184
  >(
185
185
  path: Path,
186
186
  handler: Handler<Path, RequestSchema<Path, H, P, Q, B>>
@@ -189,10 +189,10 @@ export type Endpoint = {
189
189
 
190
190
  export class RequestError {
191
191
  status: number
192
- error: any
193
- constructor(options: { status?: number; error?: any }) {
192
+ payload: any
193
+ constructor(options: { status?: number; payload?: any }) {
194
194
  this.status = options.status ?? 500
195
- this.error = options.error ?? 'Internal server error'
195
+ this.payload = options.payload ?? 'Internal server error'
196
196
  }
197
197
  }
198
198
 
@@ -225,7 +225,13 @@ export type RouteTree = {
225
225
 
226
226
  export class NotFoundError extends RequestError {
227
227
  constructor(message?: string) {
228
- super({ status: 404, error: message ?? 'Not found' })
228
+ super({ status: 404, payload: message ?? 'Not found' })
229
+ }
230
+ }
231
+
232
+ export class InternalError extends RequestError {
233
+ constructor(message?: string) {
234
+ super({ status: 500, payload: message ?? 'Internal Server Error' })
229
235
  }
230
236
  }
231
237
 
@@ -174,4 +174,27 @@ describe('hooks', async () => {
174
174
  expect(resp.status).toBe(200)
175
175
  expect(await resp?.text()).toBe('handled')
176
176
  })
177
+
178
+ test('hooks, early response', async () => {
179
+ let hook = 0
180
+ galbe.get(
181
+ '/hooks',
182
+ [
183
+ async _ => {
184
+ hook++
185
+ return 'hook'
186
+ }
187
+ ],
188
+ () => {
189
+ expect.unreachable()
190
+ }
191
+ )
192
+ expect(hook).toBe(0)
193
+ let resp = await fetch(`http://localhost:${port}/hooks`, {
194
+ method: 'GET'
195
+ })
196
+ expect(hook).toBe(1)
197
+ expect(resp.status).toBe(200)
198
+ expect(await resp?.text()).toBe('hook')
199
+ })
177
200
  })
@@ -141,7 +141,7 @@ describe('router', () => {
141
141
  } catch (err: any) {
142
142
  expect(err).toBeInstanceOf(NotFoundError)
143
143
  expect(err.status).toBe(404)
144
- expect(err.error).toBe('Not found')
144
+ expect(err.payload).toBe('Not found')
145
145
  }
146
146
 
147
147
  try {
@@ -150,7 +150,7 @@ describe('router', () => {
150
150
  } catch (err: any) {
151
151
  expect(err).toBeInstanceOf(NotFoundError)
152
152
  expect(err.status).toBe(404)
153
- expect(err.error).toBe('Not found')
153
+ expect(err.payload).toBe('Not found')
154
154
  }
155
155
  })
156
156
 
@@ -188,10 +188,6 @@ describe('router', () => {
188
188
  expect(r1.path).toBe('/test/foo/*')
189
189
  expect(r1.handler).toBe(h1)
190
190
 
191
- let r2 = router.find('GET', '/test//foo/bar')
192
- expect(r2.path).toBe('/test/foo/bar')
193
- expect(r2.handler).toBe(h2)
194
-
195
191
  let r3 = router.find('GET', '/test/foo/bar/bar')
196
192
  expect(r3.path).toBe('/test/foo/:p/bar')
197
193
  expect(r3.handler).toBe(h3)