-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow setting consumer name and group (#23)
* WIP * Add test * Check for consumer in tests * Fix
- Loading branch information
Showing
13 changed files
with
184 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { ApitallyConsumer } from "./types.js"; | ||
|
||
export const consumerFromStringOrObject = ( | ||
consumer: ApitallyConsumer | string, | ||
) => { | ||
if (typeof consumer === "string") { | ||
consumer = String(consumer).trim().substring(0, 128); | ||
return consumer ? { identifier: consumer } : null; | ||
} else { | ||
consumer.identifier = String(consumer.identifier).trim().substring(0, 128); | ||
consumer.name = consumer.name?.trim().substring(0, 64); | ||
consumer.group = consumer.group?.trim().substring(0, 64); | ||
return consumer.identifier ? consumer : null; | ||
} | ||
}; | ||
|
||
export default class ConsumerRegistry { | ||
private consumers: Map<string, ApitallyConsumer>; | ||
private updated: Set<string>; | ||
|
||
constructor() { | ||
this.consumers = new Map(); | ||
this.updated = new Set(); | ||
} | ||
|
||
public addOrUpdateConsumer(consumer?: ApitallyConsumer | null) { | ||
if (!consumer || (!consumer.name && !consumer.group)) { | ||
return; | ||
} | ||
const existing = this.consumers.get(consumer.identifier); | ||
if (!existing) { | ||
this.consumers.set(consumer.identifier, consumer); | ||
this.updated.add(consumer.identifier); | ||
} else { | ||
if (consumer.name && consumer.name !== existing.name) { | ||
existing.name = consumer.name; | ||
this.updated.add(consumer.identifier); | ||
} | ||
if (consumer.group && consumer.group !== existing.group) { | ||
existing.group = consumer.group; | ||
this.updated.add(consumer.identifier); | ||
} | ||
} | ||
} | ||
|
||
public getAndResetUpdatedConsumers() { | ||
const data: Array<ApitallyConsumer> = []; | ||
this.updated.forEach((identifier) => { | ||
const consumer = this.consumers.get(identifier); | ||
if (consumer) { | ||
data.push(consumer); | ||
} | ||
}); | ||
this.updated.clear(); | ||
return data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { describe, expect, it } from "vitest"; | ||
|
||
import ConsumerRegistry, { | ||
consumerFromStringOrObject, | ||
} from "../../src/common/consumerRegistry.js"; | ||
|
||
describe("Consumer registry", () => { | ||
it("Consumer from string or object", () => { | ||
let consumer = consumerFromStringOrObject(""); | ||
expect(consumer).toBeNull(); | ||
|
||
consumer = consumerFromStringOrObject({ identifier: " " }); | ||
expect(consumer).toBeNull(); | ||
|
||
consumer = consumerFromStringOrObject("test"); | ||
expect(consumer).toEqual({ | ||
identifier: "test", | ||
}); | ||
|
||
consumer = consumerFromStringOrObject({ identifier: "test" }); | ||
expect(consumer).toEqual({ | ||
identifier: "test", | ||
}); | ||
|
||
consumer = consumerFromStringOrObject({ | ||
identifier: "test", | ||
name: "Test ", | ||
group: " Testers ", | ||
}); | ||
expect(consumer).toEqual({ | ||
identifier: "test", | ||
name: "Test", | ||
group: "Testers", | ||
}); | ||
}); | ||
|
||
it("Add or update consumers", () => { | ||
const consumerRegistry = new ConsumerRegistry(); | ||
consumerRegistry.addOrUpdateConsumer(null); | ||
consumerRegistry.addOrUpdateConsumer({ identifier: "test" }); | ||
let data = consumerRegistry.getAndResetUpdatedConsumers(); | ||
expect(data.length).toBe(0); | ||
|
||
const testConsumer = { | ||
identifier: "test", | ||
name: "Test", | ||
group: "Testers", | ||
}; | ||
consumerRegistry.addOrUpdateConsumer(testConsumer); | ||
data = consumerRegistry.getAndResetUpdatedConsumers(); | ||
expect(data.length).toBe(1); | ||
expect(data[0]).toEqual(testConsumer); | ||
|
||
consumerRegistry.addOrUpdateConsumer(testConsumer); | ||
data = consumerRegistry.getAndResetUpdatedConsumers(); | ||
expect(data.length).toBe(0); | ||
|
||
consumerRegistry.addOrUpdateConsumer({ | ||
identifier: "test", | ||
name: "Test 2", | ||
group: "Testers 2", | ||
}); | ||
data = consumerRegistry.getAndResetUpdatedConsumers(); | ||
expect(data.length).toBe(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.