Skip to content

Commit

Permalink
flow working with tests - TODO: finalize API
Browse files Browse the repository at this point in the history
  • Loading branch information
kwhitley committed Nov 25, 2023
1 parent 668f402 commit 87e708d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 19 deletions.
9 changes: 9 additions & 0 deletions src/extras/StatusError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export class StatusError extends Error {
status: number

constructor(status: number = 500, message: string = 'Internal Error.') {
super(message)
this.name = 'StatusError'
this.status = status
}
}
20 changes: 3 additions & 17 deletions src/flow.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ describe('flow(router: RouterType, options: FlowOptions): RequestHandler', () =>

describe('format?: Function | false', () => {
it('should handle custom format function', async () => {
const customFormat = (data) => ({ status: 200, body: `num items = ${data.length}` })
const customFormat = (data) => ({ status: 201, body: `num items = ${data.length}` })
let response = await flow(router, { format: customFormat })(request('/items'))
expect(response.status).toBe(200)
expect(response.status).toBe(201)
expect(response.body).toBe('num items = 3')
})

Expand All @@ -76,20 +76,6 @@ describe('flow(router: RouterType, options: FlowOptions): RequestHandler', () =>
})
})

describe('notFound?: RouteHandler | false', () => {
it('should handle custom notFound function', async () => {
const customNotFound = () => ({ status: 404, body: 'Custom Not Found' })
let response = await flow(router, { notFound: customNotFound })(request('/missing'))
expect(response.status).toBe(404)
expect(response.body).toBe('Custom Not Found')
})

it('should not handle notFound if set to false', async () => {
let response = await flow(router, { notFound: false })(request('/missing'))
expect(response.status).toBeUndefined()
})
})

describe('cors?: CorsOptions | true', () => {
it('will embed CORS headers if provided', async () => {
let response = await flow(router, {
Expand Down Expand Up @@ -155,7 +141,7 @@ describe('flow(router: RouterType, options: FlowOptions): RequestHandler', () =>
it('if set to false, will not add notFound handler (allow undefined passthrough)', async () => {
let response = await flow(router, {
notFound: false,
format: false,
// format: false,
})(request('/missing'))

expect(response).toBe(undefined)
Expand Down
3 changes: 1 addition & 2 deletions src/flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ export const flow = (router: RouterType, options: FlowOptions = {}) => {
return async (...args: any[]) => {
// @ts-expect-error
let response = router.handle(...args)
// @ts-expect-error - add optional formatting
response = format ? response.then(format) : response
response = format ? response.then(v => v === undefined ? v : format(v)) : response
// @ts-expect-error - add optional error handling
response = errors ? response.catch(errors) : response

Expand Down

0 comments on commit 87e708d

Please sign in to comment.