diff --git a/plugins/techdocs-backend/src/cache/cacheMiddleware.test.ts b/plugins/techdocs-backend/src/cache/cacheMiddleware.test.ts index 5c591bdda4..4bd680e8c7 100644 --- a/plugins/techdocs-backend/src/cache/cacheMiddleware.test.ts +++ b/plugins/techdocs-backend/src/cache/cacheMiddleware.test.ts @@ -67,39 +67,6 @@ describe('createCacheMiddleware', () => { }); }); - describe('invalidate', () => { - it('responds with 400 when no objects are provided', async () => { - const response = await request(app).post('/cache/invalidate'); - expect(response.status).toBe(400); - }); - - it('responds with 500 if invalidation throws', async () => { - cache.invalidateMultiple.mockRejectedValueOnce(new Error()); - const response = await request(app) - .post('/cache/invalidate') - .set('Content-Type', 'application/json') - .send({ - objects: ['one/index.html', 'two/index.html'], - }); - - expect(response.status).toBe(500); - }); - - it('responds with 204 if invalidation succeeds', async () => { - const expectedObjects = ['one/index.html', 'two/index.html']; - cache.invalidateMultiple.mockResolvedValue([]); - await request(app) - .post('/cache/invalidate') - .set('Content-Type', 'application/json') - .send({ - objects: expectedObjects, - }) - .expect(204); - - expect(cache.invalidateMultiple).toHaveBeenCalledWith(expectedObjects); - }); - }); - describe('middleware', () => { it('does not apply to non-static/docs paths', async () => { await request(app) diff --git a/plugins/techdocs-backend/src/cache/cacheMiddleware.ts b/plugins/techdocs-backend/src/cache/cacheMiddleware.ts index c25d002181..66471b0189 100644 --- a/plugins/techdocs-backend/src/cache/cacheMiddleware.ts +++ b/plugins/techdocs-backend/src/cache/cacheMiddleware.ts @@ -13,22 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { Router, Request, json } from 'express'; +import { Router } from 'express'; import router from 'express-promise-router'; import { Logger } from 'winston'; import { TechDocsCache } from '.'; -import { CacheInvalidationError } from './TechDocsCache'; - -type CacheClearRequestParams = { - objects: string[]; -}; - -type CacheClearRequest = Request< - any, - unknown, - CacheClearRequestParams, - unknown ->; type CacheMiddlewareOptions = { cache: TechDocsCache; @@ -39,44 +27,9 @@ type ErrorCallback = (err?: Error) => void; export const createCacheMiddleware = ({ cache, - logger, }: CacheMiddlewareOptions): Router => { const cacheMiddleware = router(); - // And endpoint for handling cache invalidation external to the Backstage - // Backend (e.g. from the TechDocs CLI). - cacheMiddleware.use(json()); - cacheMiddleware.post( - '/cache/invalidate', - async (req: CacheClearRequest, res) => { - if (req.body?.objects?.length) { - logger.debug( - `Clearing ${req.body.objects.length} cache entries: (eg: ${req.body.objects[0]})`, - ); - - try { - const invalidated = await cache.invalidateMultiple(req.body.objects); - logger.debug( - `Successfully invalidated ${invalidated.length} cache entries`, - ); - res.status(204).send(); - } catch (e) { - if (e instanceof CacheInvalidationError) { - const uniqueReasons = [ - ...new Set(e.rejections.map(r => r.reason.message)), - ].join(', '); - logger.warn( - `Problem invalidating ${e.rejections.length} entries: ${uniqueReasons}`, - ); - } - res.status(500).send(); - } - } else { - res.status(400).send(); - } - }, - ); - // Middleware that, through socket monkey patching, captures responses as // they're sent over /static/docs/* and caches them. Subsequent requests are // loaded from cache. Cache key is the object's path (after `/static/docs/`).