Skip to content
Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @123ishatest/louter

## 0.4.0

### Minor Changes

- Add getSchema to retrieve schemas by kind

## 0.3.1

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@123ishatest/louter",
"private": false,
"version": "0.3.1",
"version": "0.4.0",
"publishConfig": {
"access": "public",
"provenance": true
Expand Down
8 changes: 8 additions & 0 deletions src/content/ContentManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ export class ContentManager<const Kinds extends Record<string, ZodType<{ id: str
return content;
}

public getSchema<Kind extends keyof Kinds>(kind: Kind): Kinds[Kind] {
const schema = this._kinds[kind];
if (!schema) {
throw new ContentKindNotFoundError(`Could not get schema of ${kind.toString()}`);
}
return schema;
}

/**
* Throw away all currently managed content
*/
Expand Down
33 changes: 33 additions & 0 deletions tests/content/content-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,36 @@ it('throws an error when a piece of content does not exist', () => {
manager.get('wrong', 'example');
}).toThrow(ContentNotFoundError);
});

it('can get schemas', () => {
// Arrange
const exampleSchema = z.strictObject({
id: z.string(),
amount: z.number(),
});
const manager = new ContentManager({
example: exampleSchema,
});

// Act
const schema = manager.getSchema('example');

// Assert
expect(schema).toBe(exampleSchema);
});

it('throws an error when a schema does not exist', () => {
// Arrange
const manager = new ContentManager({
example: z.strictObject({
id: z.string(),
amount: z.number(),
}),
});

// Act
expect(() => {
// @ts-expect-error wrong kind
manager.getSchema('wrong');
}).toThrow(ContentKindNotFoundError);
});
Loading