Skip to content

Commit

Permalink
fix: calling next after loading API once
Browse files Browse the repository at this point in the history
  • Loading branch information
tpluscode committed Aug 21, 2024
1 parent 7974500 commit 1b4a7fc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/witty-glasses-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kopflos-cms/express": patch
---

After first request, the server would stop responding
15 changes: 9 additions & 6 deletions packages/express/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ import asyncMiddleware from 'middleware-async'
export default (options: KopflosConfig): RequestHandler => {
const kopflos = new Kopflos(options)

const loadApiGraphs = onetime(async (graphs: Required<KopflosConfig>['apiGraphs']) => {
await Kopflos.fromGraphs(kopflos, ...graphs)
})

return Router()
.use(onetime(async (req, res, next) => {
if (options.apiGraphs) {
await Kopflos.fromGraphs(kopflos, ...options.apiGraphs)
} else {
.use((req, res, next) => {
if (!options.apiGraphs) {
return next(new Error('No API graphs configured. In future release it will be possible to select graphs dynamically.'))
}
next()
}))

loadApiGraphs(options.apiGraphs).then(next).catch(next)
})
.use(rdfHandler({
factory,
}))
Expand Down
10 changes: 10 additions & 0 deletions packages/express/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ describe('@kopflos-cms/express', () => {
.expect(200, 'not found')
})

it('must not stall after first request', async () => {
await request(app)
.get('/not-found')
.expect(404)

await request(app)
.get('/not-found')
.expect(404)
})

context('request to resource without explicit handler', () => {
it('should should return Core representation', async () => {
const response = await request(app)
Expand Down

0 comments on commit 1b4a7fc

Please sign in to comment.