diff --git a/example/api/api.test.ts b/example/api/api.test.ts index 136bce9..f6c0210 100644 --- a/example/api/api.test.ts +++ b/example/api/api.test.ts @@ -48,6 +48,22 @@ describe('CacheManager - OpenAPI File-based Sources', () => { assert.ok(deleteUserEndpoint, 'Should have DELETE /users/{id} endpoint'); }); + it('should replace an existing cache entry when refreshing a source', async () => { + const source = { + name: 'TestOpenAPIJSON', + type: SourceType.API, + path: join(fixturesPath, 'json', 'openapi-schema.json') + }; + + await setupCacheWithSources([source]); + await CacheManager.refresh(); + + const docs = await CacheManager.getDocs(); + + assert.strictEqual(docs.length, 1, 'Should keep one cache entry per source'); + assert.strictEqual(docs[0].name, source.name, 'Cache entry name should match'); + }); + it('should retrieve specific endpoint details from OpenAPI JSON schema', async () => { await setupCacheWithSources([ { diff --git a/example/xquik-api-sources.json b/example/xquik-api-sources.json new file mode 100644 index 0000000..3412dbd --- /dev/null +++ b/example/xquik-api-sources.json @@ -0,0 +1,8 @@ +[ + { + "name": "Xquik", + "type": "api", + "method": "GET", + "url": "https://xquik.com/openapi.json" + } +] diff --git a/src/utils/cache.ts b/src/utils/cache.ts index bc763f1..fe1b783 100644 --- a/src/utils/cache.ts +++ b/src/utils/cache.ts @@ -63,7 +63,14 @@ class Cache { } public static set(entry: CacheEntry): void { - this.docs.push(entry); + const existingEntryIndex = this.docs.findIndex(doc => doc.source.name === entry.source.name); + + if (existingEntryIndex === -1) { + this.docs.push(entry); + return; + } + + this.docs[existingEntryIndex] = entry; } public static hasExpired(entry: CacheEntry): boolean {