Skip to content

Commit

Permalink
feat: add in notFoundResponse option
Browse files Browse the repository at this point in the history
  • Loading branch information
willfarrell committed Sep 16, 2024
1 parent f86f9ee commit f750b1b
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 9 deletions.
27 changes: 27 additions & 0 deletions packages/http-router/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,33 @@ test('It should thrown 404 when route not found', async (t) => {
}
})

test('It should thrown 200 when route not found, using notFoundResponse', async (t) => {
const event = {
httpMethod: 'GET',
path: '/notfound'
}
const handler = httpRouter({
routes: [
{
method: 'GET',
path: '/',
handler: () => true
}
],
notFoundResponse: (args) => {
return {
statusCode: 200,
body: JSON.stringify(args)
}
}
})

const res = await handler(event, context)

equal(res.statusCode, 200)
deepEqual(JSON.parse(res.body), { method: 'GET', path: '/notfound' })
})

// route methods
test('It should route to a static POST method', async (t) => {
const event = {
Expand Down
20 changes: 16 additions & 4 deletions packages/http-router/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
import { createError } from '@middy/util'

const httpRouteHandler = (routes) => {
const defaults = {
routes: [],
notFoundResponse: ({ method, path }) => {
const err = createError(404, 'Route does not exist', {
cause: { package: '@middy/http-router', data: { method, path } }
})
throw err
}
}
const httpRouteHandler = (opts = {}) => {
if (Array.isArray(opts)) {
opts = { routes: opts }
}
const { routes, notFoundResponse } = { ...defaults, ...opts }

const routesStatic = {}
const routesDynamic = {}
const enumMethods = methods.concat('ANY')
Expand Down Expand Up @@ -56,9 +70,7 @@ const httpRouteHandler = (routes) => {
}

// Not Found
throw createError(404, 'Route does not exist', {
cause: { package: '@middy/http-router', data: path }
})
return notFoundResponse({ method, path })
}
}

Expand Down
28 changes: 27 additions & 1 deletion packages/ws-router/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test } from 'node:test'
import { ok, equal } from 'node:assert/strict'
import { ok, equal, deepEqual } from 'node:assert/strict'
import middy from '../../core/index.js'
import wsRouter from '../index.js'

Expand Down Expand Up @@ -45,6 +45,32 @@ test('It should thrown 404 when route not found', async (t) => {
}
})

test('It should thrown 200 when route not found, using notFoundResponse', async (t) => {
const event = {
requestContext: {
routeKey: 'missing'
}
}
const handler = wsRouter({
routes: [
{
routeKey: '$connect',
handler: () => true
}
],
notFoundResponse: (args) => {
return {
statusCode: 200,
body: JSON.stringify(args)
}
}
})
const res = await handler(event, context)

equal(res.statusCode, 200)
deepEqual(JSON.parse(res.body), { routeKey: 'missing' })
})

// with middleware
test('It should run middleware that are part of route handler', async (t) => {
const event = {
Expand Down
19 changes: 15 additions & 4 deletions packages/ws-router/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import { createError } from '@middy/util'
const defaults = {
routes: [],
notFoundResponse: ({ routeKey }) => {
const err = createError(404, 'Route does not exist', {
cause: { package: '@middy/ws-router', data: { routeKey } }
})
throw err
}
}
const wsRouteHandler = (opts = {}) => {
if (Array.isArray(opts)) {
opts = { routes: opts }
}
const { routes, notFoundResponse } = { ...defaults, ...opts }

const wsRouteHandler = (routes) => {
const routesStatic = {}
for (const route of routes) {
const { routeKey, handler } = route
Expand All @@ -24,9 +37,7 @@ const wsRouteHandler = (routes) => {
}

// Not Found
throw createError(404, 'Route does not exist', {
cause: { package: '@middy/ws-router', data: routeKey }
})
return notFoundResponse({ routeKey })
}
}

Expand Down

0 comments on commit f750b1b

Please sign in to comment.