Skip to content

Commit

Permalink
fix(middeware): Allow registration with async functions (redwoodjs#10413
Browse files Browse the repository at this point in the history
)
  • Loading branch information
dac09 authored Apr 4, 2024
1 parent d6e3268 commit f08c550
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 2 deletions.
1 change: 1 addition & 0 deletions .changesets/10413.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- fix(middeware): Allow registration with async functions (#10413) by @dac09
95 changes: 95 additions & 0 deletions packages/vite/src/middleware/createMiddlewareRouter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import type { ViteDevServer } from 'vite'
import { beforeEach, describe, expect, it, vi } from 'vitest'

import { createMiddlewareRouter } from './register'

vi.mock('@redwoodjs/project-config', async () => {
return {
getPaths: () => {
return {
web: {
base: '/proj/web',
dist: '/proj/web/dist',
distEntryServer: '/proj/web/dist/entry-server.mjs',
entryServer: '/proj/web/entry-server.tsx',
},
}
},
}
})

const distRegisterMwMock = vi.fn()
vi.mock('/proj/web/dist/entry-server.mjs', () => {
return {
registerMiddleware: distRegisterMwMock,
}
})

describe('createMiddlewareRouter', () => {
beforeEach(() => {
vi.resetAllMocks()
})

it('Should load using vite dev server, and load from entry-server source', async () => {
const mockVite = {
ssrLoadModule: vi.fn().mockReturnValue({
registerMiddleware: () => {
return []
},
}),
} as unknown as ViteDevServer

await createMiddlewareRouter(mockVite)

expect(mockVite.ssrLoadModule).toHaveBeenCalledWith(
'/proj/web/entry-server.tsx',
)
})

it('Should load import from distEntryServer for prod', async () => {
await createMiddlewareRouter()
expect(distRegisterMwMock).toHaveBeenCalled()
})

it('(async) should return a router with middleware handlers', async () => {
// Testing async case
distRegisterMwMock.mockResolvedValue([
[vi.fn(), '/bazinga'],
[vi.fn(), '/kittens'],
vi.fn(),
])

const result = await createMiddlewareRouter()

expect(result.prettyPrint()).toMatchInlineSnapshot(`
"└── (empty root node)
├── /
│ ├── bazinga (GET)
│ └── kittens (GET)
└── * (GET)
"
`)
expect(distRegisterMwMock).toHaveBeenCalled()
})

it('(sync) should return a router with middleware handlers', async () => {
// Testing sync case
distRegisterMwMock.mockReturnValue([
[vi.fn(), '/bazinga'],
[vi.fn(), '/kittens'],
vi.fn(),
])

const result = await createMiddlewareRouter()

expect(result.prettyPrint()).toMatchInlineSnapshot(`
"└── (empty root node)
├── /
│ ├── bazinga (GET)
│ └── kittens (GET)
└── * (GET)
"
`)
expect(distRegisterMwMock).toHaveBeenCalled()
})
})
4 changes: 2 additions & 2 deletions packages/vite/src/middleware/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export const chain = (mwList: Middleware[]) => {
}
}

export const addMiddlewareHandlers = (mwRegList: MiddlewareReg) => {
export const addMiddlewareHandlers = (mwRegList: MiddlewareReg = []) => {
const groupedMw = groupByRoutePatterns(mwRegList)
const mwRouter = fmw()

Expand Down Expand Up @@ -120,5 +120,5 @@ export const createMiddlewareRouter = async (
return fmw()
}

return addMiddlewareHandlers(registerMiddleware())
return addMiddlewareHandlers(await registerMiddleware())
}

0 comments on commit f08c550

Please sign in to comment.