diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d8fe46..c2b5c23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # @123ishatest/louter +## 0.6.0 + +### Minor Changes + +- Add getById to ContentManager + ## 0.5.3 ### Patch Changes diff --git a/eslint.config.mts b/eslint.config.mts index 79c874a..ee59a04 100644 --- a/eslint.config.mts +++ b/eslint.config.mts @@ -4,7 +4,7 @@ import tseslint from 'typescript-eslint'; import { defineConfig, globalIgnores } from 'eslint/config'; export default defineConfig([ - globalIgnores(['dist/']), + globalIgnores(['dist/', 'coverage/']), { files: ['**/*.{js,mjs,cjs,ts,mts,cts}'], plugins: { js }, diff --git a/package.json b/package.json index 4ad3aff..27ab9b4 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@123ishatest/louter", "private": false, - "version": "0.5.3", + "version": "0.6.0", "publishConfig": { "access": "public", "provenance": true diff --git a/src/content/ContentManager.ts b/src/content/ContentManager.ts index c750cf9..b5ed1b3 100644 --- a/src/content/ContentManager.ts +++ b/src/content/ContentManager.ts @@ -58,6 +58,24 @@ export class ContentManager { + try { + return this.get(id, kind); + } catch { + return false; + } + }); + if (!kind) { + throw new ContentNotFoundError(`Could not get content with id '${id}'`); + } + return this.get(id, kind); + } + public getSchema(kind: Kind): Kinds[Kind] { const schema = this._kinds[kind]; if (!schema) { diff --git a/tests/content/content-manager.spec.ts b/tests/content/content-manager.spec.ts index eda3346..3323575 100644 --- a/tests/content/content-manager.spec.ts +++ b/tests/content/content-manager.spec.ts @@ -127,3 +127,60 @@ it('can get a list of all kinds', () => { // Assert expect(kinds).toStrictEqual(['example', 'other']); }); + +it('can get by id', () => { + // Arrange + const exampleA = { id: 'a', amount: 4 }; + const exampleB = { id: 'b', amount: 4 }; + const exampleContent = { a: exampleA, b: exampleB }; + + const manager = new ContentManager({ example: z.strictObject({ id: z.string(), amount: z.number() }) }); + + // Act + manager.load({ example: exampleContent }); + + // Act + const content = manager.getById('b'); + + // Assert + expect(content).toStrictEqual(exampleB); +}); + +it('can get by id when there are multiple kinds', () => { + // Arrange + const exampleA = { id: 'a', amount: 4 }; + const exampleB = { id: 'b', amount: 4 }; + + const manager = new ContentManager({ + first: z.strictObject({ id: z.string(), amount: z.number() }), + second: z.strictObject({ id: z.string(), amount: z.number() }), + }); + + // Act + manager.loadKind('first', [exampleA]); + manager.loadKind('second', [exampleB]); + + // Act + const contentA = manager.getById('a'); + const contentB = manager.getById('b'); + + // Assert + expect(contentA).toStrictEqual(exampleA); + expect(contentB).toStrictEqual(exampleB); +}); + +it('throws an error when it cannot find by id', () => { + // Arrange + const manager = new ContentManager({ + example: z.strictObject({ + id: z.string(), + amount: z.number(), + }), + }); + manager.load({ example: { b: { id: 'b', amount: 4 } } }); + + // Act + expect(() => { + manager.getById('a'); + }).toThrow(ContentNotFoundError); +});