Skip to content

Commit 668f402

Browse files
committed
flow work
1 parent 902e668 commit 668f402

File tree

3 files changed

+79
-16
lines changed

3 files changed

+79
-16
lines changed

example/e2e middleware.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { IRequest, Router } from 'Router'
2+
3+
type FooRequest = {
4+
foo: string
5+
} & IRequest
6+
7+
const router = Router<FooRequest>()
8+
9+
// MIDDLEWARE: adds foo to the request
10+
const withFoo = (request: IRequest) => {
11+
request.foo = 'bar' // no return = next handler gets fired (making this middleware)
12+
}
13+
14+
// ADD ROUTES
15+
router
16+
.all('*', withFoo) // we add Foo as global upstream middleware, but this could go anywhere
17+
18+
.get('/foo-test', (request) => {
19+
return request.foo // 'bar'
20+
})

src/flow.spec.ts

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ let router
1212
beforeEach(() => {
1313
router = Router()
1414
.get('/', () => 'index')
15-
.get('/items', () => [])
15+
.get('/items', () => [1,2,3])
1616
.get('/throw', (r) => r.a.b.c)
1717
})
1818

@@ -30,7 +30,7 @@ describe('flow(router: RouterType, options: FlowOptions): RequestHandler', () =>
3030

3131
it('formats as json', async () => {
3232
let response = await flow(router)(request('/items')).then(r => r.json())
33-
expect(response).toEqual([])
33+
expect(response).toEqual([1,2,3])
3434
})
3535

3636
it('catches errors', async () => {
@@ -47,6 +47,49 @@ describe('flow(router: RouterType, options: FlowOptions): RequestHandler', () =>
4747
})
4848

4949
describe('OPTIONS', () => {
50+
describe('errors?: Function | false', () => {
51+
it('should handle custom error function', async () => {
52+
const customError = () => ({ status: 418, body: 'I\'m a teapot' })
53+
let response = await flow(router, { errors: customError })(request('/throw'))
54+
expect(response.status).toBe(418)
55+
expect(response.body).toBe('I\'m a teapot')
56+
})
57+
58+
it('should not handle errors if set to false', async () => {
59+
const errorHandler = vi.fn()
60+
let response = await flow(router, { errors: false })(request('/throw')).catch(errorHandler)
61+
expect(errorHandler).toHaveBeenCalled()
62+
})
63+
})
64+
65+
describe('format?: Function | false', () => {
66+
it('should handle custom format function', async () => {
67+
const customFormat = (data) => ({ status: 200, body: `num items = ${data.length}` })
68+
let response = await flow(router, { format: customFormat })(request('/items'))
69+
expect(response.status).toBe(200)
70+
expect(response.body).toBe('num items = 3')
71+
})
72+
73+
it('should not format response if set to false', async () => {
74+
let response = await flow(router, { format: false })(request('/items'))
75+
expect(response.body).toBeUndefined()
76+
})
77+
})
78+
79+
describe('notFound?: RouteHandler | false', () => {
80+
it('should handle custom notFound function', async () => {
81+
const customNotFound = () => ({ status: 404, body: 'Custom Not Found' })
82+
let response = await flow(router, { notFound: customNotFound })(request('/missing'))
83+
expect(response.status).toBe(404)
84+
expect(response.body).toBe('Custom Not Found')
85+
})
86+
87+
it('should not handle notFound if set to false', async () => {
88+
let response = await flow(router, { notFound: false })(request('/missing'))
89+
expect(response.status).toBeUndefined()
90+
})
91+
})
92+
5093
describe('cors?: CorsOptions | true', () => {
5194
it('will embed CORS headers if provided', async () => {
5295
let response = await flow(router, {
@@ -68,14 +111,14 @@ describe('flow(router: RouterType, options: FlowOptions): RequestHandler', () =>
68111
describe('error?: RouteHandler | false', () => {
69112
it('does not catch internally if set to false', async () => {
70113
let onError = vi.fn()
71-
let response = await flow(router, { handleErrors: false })(request('/throw')).catch(onError)
114+
let response = await flow(router, { errors: false })(request('/throw')).catch(onError)
72115

73116
expect(onError).toHaveBeenCalled()
74117
})
75118

76119
it('can reshape errors if provided', async () => {
77120
let response = await flow(router, {
78-
handleErrors: () => error(418, 'CUSTOM'),
121+
errors: () => error(418, 'CUSTOM'),
79122
})(request('/throw'))
80123

81124
expect(response.status).toBe(418)
@@ -85,14 +128,14 @@ describe('flow(router: RouterType, options: FlowOptions): RequestHandler', () =>
85128
describe('format?: ResponseFormatter | false', () => {
86129
it('does not catch internally if set to false', async () => {
87130
let onError = vi.fn()
88-
let response = await flow(router, { handleErrors: false })(request('/throw')).catch(onError)
131+
let response = await flow(router, { errors: false })(request('/throw')).catch(onError)
89132

90133
expect(onError).toHaveBeenCalled()
91134
})
92135

93136
it('can reshape errors if provided', async () => {
94137
let response = await flow(router, {
95-
handleErrors: () => error(418, 'CUSTOM'),
138+
errors: () => error(418, 'CUSTOM'),
96139
})(request('/throw'))
97140

98141
expect(response.status).toBe(418)
@@ -102,7 +145,7 @@ describe('flow(router: RouterType, options: FlowOptions): RequestHandler', () =>
102145
describe('notFound?: RouteHandler | false', () => {
103146
it('uses a custom 404 handler', async () => {
104147
let response = await flow(router, {
105-
handleNotFound: () => error(418, 'CUSTOM'),
148+
notFound: () => error(418, 'CUSTOM'),
106149
})(request('/missing')).then(r => r.json())
107150

108151
expect(response.status).toBe(418)
@@ -111,7 +154,7 @@ describe('flow(router: RouterType, options: FlowOptions): RequestHandler', () =>
111154

112155
it('if set to false, will not add notFound handler (allow undefined passthrough)', async () => {
113156
let response = await flow(router, {
114-
handleNotFound: false,
157+
notFound: false,
115158
format: false,
116159
})(request('/missing'))
117160

src/flow.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@ import { json } from './json'
55
import { withParams } from './withParams'
66

77
export type FlowOptions = {
8-
format?: Function | false
9-
handleErrors?: Function | false
10-
handleNotFound?: RouteHandler | false
118
cors?: CorsOptions | true
9+
errors?: Function | false
10+
format?: Function | false
11+
notFound?: RouteHandler | false
1212
}
1313

1414
export const flow = (router: RouterType, options: FlowOptions = {}) => {
1515
const {
1616
format = json,
1717
cors,
18-
handleErrors = error,
19-
handleNotFound = () => error(404),
18+
errors = error,
19+
notFound = () => error(404),
2020
} = options
2121
let corsHandlers: any
2222

2323
// register a notFound route, if given
24-
if (typeof handleNotFound === 'function') {
25-
router.all('*', handleNotFound)
24+
if (typeof notFound === 'function') {
25+
router.all('*', notFound)
2626
}
2727

2828
// Initialize CORS handlers if cors options are provided
@@ -37,7 +37,7 @@ export const flow = (router: RouterType, options: FlowOptions = {}) => {
3737
// @ts-expect-error - add optional formatting
3838
response = format ? response.then(format) : response
3939
// @ts-expect-error - add optional error handling
40-
response = handleErrors ? response.catch(handleErrors) : response
40+
response = errors ? response.catch(errors) : response
4141

4242
// add optional cors and return response
4343
return cors ? response.then(corsHandlers?.corsify) : response

0 commit comments

Comments
 (0)