Skip to content

Commit

Permalink
fixing lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kwhitley committed Nov 25, 2023
1 parent ab1364f commit acd93ae
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/extras/StatusError.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export class StatusError extends Error {
status: number

constructor(status: number = 500, message: string = 'Internal Error.') {
constructor(status = 500, message = 'Internal Error.') {
super(message)
this.name = 'StatusError'
this.status = status
Expand Down
18 changes: 4 additions & 14 deletions src/flow.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Router } from './Router'
import { error } from './error'
import { flow } from './flow'

const request = (path: string, method: string = 'GET') =>
const request = (path: string, method = 'GET') =>
({ method, url: `https://itty.dev${path}` })

let router
Expand Down Expand Up @@ -35,13 +35,11 @@ describe('flow(router: RouterType, options: FlowOptions): RequestHandler', () =>

it('catches errors', async () => {
let response = await flow(router)(request('/throw'))

expect(response.status).toBe(500)
})

it('does not include CORS', async () => {
let response = await flow(router)(request('/items'))

expect(response.headers.get('access-control-allow-methods')).toBe(null)
})
})
Expand All @@ -57,7 +55,7 @@ describe('flow(router: RouterType, options: FlowOptions): RequestHandler', () =>

it('should not handle errors if set to false', async () => {
const errorHandler = vi.fn()
let response = await flow(router, { errors: false })(request('/throw')).catch(errorHandler)
await flow(router, { errors: false })(request('/throw')).catch(errorHandler)
expect(errorHandler).toHaveBeenCalled()
})
})
Expand All @@ -83,47 +81,41 @@ describe('flow(router: RouterType, options: FlowOptions): RequestHandler', () =>
methods: ['GET', 'POST'],
},
})(request('/items'))

expect(response.headers.get('access-control-allow-methods')).toBe('GET, POST')
})

it('will embed default CORS headers if set to true', async () => {
let response = await flow(router, { cors: true })(request('/items'))

expect(response.headers.get('access-control-allow-methods')).toBe('GET')
})
})

describe('error?: RouteHandler | false', () => {
it('does not catch internally if set to false', async () => {
let onError = vi.fn()
let response = await flow(router, { errors: false })(request('/throw')).catch(onError)

await flow(router, { errors: false })(request('/throw')).catch(onError)
expect(onError).toHaveBeenCalled()
})

it('can reshape errors if provided', async () => {
let response = await flow(router, {
errors: () => error(418, 'CUSTOM'),
})(request('/throw'))

expect(response.status).toBe(418)
})
})

describe('format?: ResponseFormatter | false', () => {
it('does not catch internally if set to false', async () => {
let onError = vi.fn()
let response = await flow(router, { errors: false })(request('/throw')).catch(onError)

await flow(router, { errors: false })(request('/throw')).catch(onError)
expect(onError).toHaveBeenCalled()
})

it('can reshape errors if provided', async () => {
let response = await flow(router, {
errors: () => error(418, 'CUSTOM'),
})(request('/throw'))

expect(response.status).toBe(418)
})
})
Expand All @@ -133,14 +125,12 @@ describe('flow(router: RouterType, options: FlowOptions): RequestHandler', () =>
let response = await flow(router, {
notFound: () => error(418, 'CUSTOM'),
})(request('/missing')).then(r => r.json())

expect(response.status).toBe(418)
expect(response.error).toBe('CUSTOM')
})

it('if set to false, will not add notFound handler (allow undefined passthrough)', async () => {
let response = await flow(router, { notFound: false })(request('/missing'))

expect(response).toBe(undefined)
})
})
Expand Down
6 changes: 4 additions & 2 deletions src/flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { error } from './error'
import { json } from './json'
import { withParams } from './withParams'

type ResponseGenerator = (...args: any) => Response

export type FlowOptions = {
cors?: CorsOptions | true
errors?: Function | false
format?: Function | false
errors?: ResponseGenerator | false
format?: ResponseGenerator | false
notFound?: RouteHandler | false
}

Expand Down

0 comments on commit acd93ae

Please sign in to comment.