Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions example/api/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
{
Expand Down
8 changes: 8 additions & 0 deletions example/xquik-api-sources.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"name": "Xquik",
"type": "api",
"method": "GET",
"url": "https://xquik.com/openapi.json"
}
]
9 changes: 8 additions & 1 deletion src/utils/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down