Skip to content

Commit

Permalink
+middleware example with enhance util
Browse files Browse the repository at this point in the history
  • Loading branch information
magne4000 committed Jan 29, 2025
1 parent 7848f67 commit 28e7edb
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 76 deletions.
84 changes: 46 additions & 38 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 5 additions & 14 deletions test/middleware-config/express-entry.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { dirname } from 'node:path'
import { fileURLToPath } from 'node:url'

import { createMiddleware } from '@universal-middleware/express'
import type { EnhancedMiddleware } from '@universal-middleware/core'
import { apply } from '@universal-middleware/express'
import express from 'express'
import { getMiddlewares } from 'vike/__internal'
import type { Middleware } from './pages/Middleware'
import { createDevMiddleware } from 'vike/server'

const __filename = fileURLToPath(import.meta.url)
Expand All @@ -24,22 +24,13 @@ async function startServer() {
app.use(devMiddleware)
}

const middlewares = (await getMiddlewares()) as Middleware[]
middlewares.sort((m1, m2) => getOrder(m1.order) - getOrder(m2.order))
middlewares.forEach((middlewareSpec) => {
const middleware = middlewareSpec.value
app.all('*', createMiddleware(() => middleware)())
})
const middlewares = (await getMiddlewares()) as EnhancedMiddleware[]
// @ts-ignore Typescript seems to think that apply requires express 5
apply(app, middlewares)

app.listen(port, () => {
console.log(`Server listening on http://localhost:${port}`)
})

return app
}

function getOrder(order: Middleware['order']): number {
if (order === 'post') return 100
if (order === 'pre') return -100
return order
}
4 changes: 2 additions & 2 deletions test/middleware-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
"preview": "cross-env NODE_ENV=production tsx ./express-entry.ts"
},
"dependencies": {
"@universal-middleware/express": "^0.3.1",
"@universal-middleware/express": "^0.4.0",
"express": "^4.21.2",
"@vitejs/plugin-react": "^4.3.4",
"react": "^19.0.0",
"react-dom": "^19.0.0",
"vike-react": "^0.5.12",
"@universal-middleware/core": "^0.3.1",
"@universal-middleware/core": "^0.4.0",
"telefunc": "^0.1.82"
},
"devDependencies": {
Expand Down
18 changes: 6 additions & 12 deletions test/middleware-config/pages/middlewareTelefunc.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
export { middlewareTelefunc }

import { type UniversalMiddleware, enhance } from '@universal-middleware/core'
import { telefunc } from 'telefunc'
import type { UniversalMiddleware } from '@universal-middleware/core'
import type { Middleware } from './Middleware'

const telefuncUniversalMiddleware: UniversalMiddleware = async (request, context, runtime) => {
const url = request.url.toString()
const urlParsed = new URL(url)

if (urlParsed.pathname !== '/_telefunc') return

const httpResponse = await telefunc({
url,
Expand All @@ -28,10 +24,8 @@ const telefuncUniversalMiddleware: UniversalMiddleware = async (request, context
})
}

const middlewareTelefunc: Middleware[] = [
{
name: 'telefunc',
order: 'pre',
value: telefuncUniversalMiddleware
}
]
const middlewareTelefunc = enhance(telefuncUniversalMiddleware, {
name: 'telefunc',
method: 'POST',
path: '/_telefunc'
})
17 changes: 7 additions & 10 deletions test/middleware-config/pages/middlewareVike.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
export { middlewareVike }

import { renderPage } from 'vike/server'
// TODO: stop using universal-middleware and directly integrate server middlewares instead. (Bati generates boilerplates that use universal-middleware https://github.com/magne4000/universal-middleware to make Bati's internal logic easier. This is temporary and will be removed soon.)
import type { UniversalMiddleware } from '@universal-middleware/core'
import type { Middleware } from './Middleware'
import { type UniversalMiddleware, enhance } from '@universal-middleware/core'
import { renderPage } from 'vike/server'

const vikeUniversalMiddleware: UniversalMiddleware = async (request, context, runtime) => {
const pageContextInit = { ...context, ...runtime, urlOriginal: request.url, headersOriginal: request.headers }
Expand All @@ -19,10 +18,8 @@ const vikeUniversalMiddleware: UniversalMiddleware = async (request, context, ru
})
}

const middlewareVike: Middleware[] = [
{
name: 'vike',
order: 'post',
value: vikeUniversalMiddleware
}
]
const middlewareVike = enhance(vikeUniversalMiddleware, {
name: 'vike',
method: 'GET',
path: '/**'
})

0 comments on commit 28e7edb

Please sign in to comment.