From 1b4a7fc48c02a108dbdfce4caf4d9a8519a82ed2 Mon Sep 17 00:00:00 2001 From: Tomasz Pluskiewicz Date: Wed, 21 Aug 2024 13:35:53 +0200 Subject: [PATCH] fix: calling next after loading API once --- .changeset/witty-glasses-fail.md | 5 +++++ packages/express/index.ts | 15 +++++++++------ packages/express/test/index.test.ts | 10 ++++++++++ 3 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 .changeset/witty-glasses-fail.md diff --git a/.changeset/witty-glasses-fail.md b/.changeset/witty-glasses-fail.md new file mode 100644 index 0000000..f902266 --- /dev/null +++ b/.changeset/witty-glasses-fail.md @@ -0,0 +1,5 @@ +--- +"@kopflos-cms/express": patch +--- + +After first request, the server would stop responding diff --git a/packages/express/index.ts b/packages/express/index.ts index 00e8656..c029c8e 100644 --- a/packages/express/index.ts +++ b/packages/express/index.ts @@ -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['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, })) diff --git a/packages/express/test/index.test.ts b/packages/express/test/index.test.ts index 4225d5a..8bd03fc 100644 --- a/packages/express/test/index.test.ts +++ b/packages/express/test/index.test.ts @@ -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)