@bernierllc/content-type-registry
v1.0.4
Published
Central registry for content type discovery, registration, and validation with Zod schemas
Readme
@bernierllc/content-type-registry
Central registry for content type discovery, registration, and validation with Zod schemas.
Installation
npm install @bernierllc/content-type-registryUsage
import { ContentTypeRegistry, BaseContentType } from '@bernierllc/content-type-registry';
import { z } from 'zod';
// Create a registry instance
const registry = new ContentTypeRegistry();
// Register a content type
const result = registry.register({
id: 'blog-post',
name: 'Blog Post',
description: 'A long-form article',
baseType: BaseContentType.TEXT,
icon: 'document',
schema: z.object({
title: z.string().min(1).max(200),
content: z.string(),
publishedAt: z.date().optional()
}),
editor: {
component: 'RichTextEditor',
fields: [
{ name: 'title', label: 'Title', type: 'text', required: true },
{ name: 'content', label: 'Content', type: 'textarea', required: true }
]
},
storage: {
method: 'database'
},
publishing: {
methods: ['web', 'api'],
defaultMethod: 'web'
}
});
if (result.success) {
console.log('Content type registered successfully');
}
// Retrieve a content type
const typeResult = registry.get('blog-post');
if (typeResult.success) {
console.log('Retrieved type:', typeResult.data);
}
// List all content types
const listResult = registry.list();
if (listResult.success) {
console.log('All types:', listResult.data);
}
// Filter content types
const textTypes = registry.list({ baseType: BaseContentType.TEXT });
// Validate content against a type's schema
const validateResult = registry.validate('blog-post', {
title: 'My First Post',
content: 'This is the content of my post'
});
if (validateResult.success) {
console.log('Valid content:', validateResult.data);
} else {
console.error('Validation failed:', validateResult.error);
}API Reference
ContentTypeRegistry
The main registry class for managing content types.
register(definition: ContentTypeDefinition): RegistryResult<void>
Registers a new content type in the registry.
Parameters:
definition- Complete content type definition with schema, editor config, etc.
Returns: RegistryResult<void> - Success/failure result
unregister(typeId: string): RegistryResult<void>
Unregisters a content type from the registry.
Parameters:
typeId- ID of the type to unregister
Returns: RegistryResult<void> - Success/failure result
get(typeId: string): RegistryResult<ContentTypeDefinition>
Retrieves a specific content type definition.
Parameters:
typeId- ID of the type to retrieve
Returns: RegistryResult<ContentTypeDefinition> - The type definition or error
list(filter?: ContentTypeFilter): RegistryResult<ContentTypeDefinition[]>
Lists all registered content types with optional filtering.
Parameters:
filter- Optional filter criteria (baseType, ids, search, metadata)
Returns: RegistryResult<ContentTypeDefinition[]> - Array of matching types
getValidator(typeId: string): RegistryResult<z.ZodSchema>
Gets the Zod validator schema for a content type.
Parameters:
typeId- ID of the type
Returns: RegistryResult<z.ZodSchema> - The Zod schema for validation
getBaseType(typeId: string): RegistryResult<BaseContentType>
Gets the base type for a content type.
Parameters:
typeId- ID of the type
Returns: RegistryResult<BaseContentType> - The base type (TEXT, IMAGE, AUDIO, VIDEO)
listExtendedTypes(baseType: BaseContentType): RegistryResult<ContentTypeDefinition[]>
Lists all types that extend from a specific base type.
Parameters:
baseType- The base type to filter by
Returns: RegistryResult<ContentTypeDefinition[]> - Array of types extending the base type
validate(typeId: string, content: any): RegistryResult<any>
Validates content against a type's schema.
Parameters:
typeId- ID of the content typecontent- Content to validate
Returns: RegistryResult<any> - Validated content or error
has(typeId: string): boolean
Checks if a type is registered.
Parameters:
typeId- ID of the type to check
Returns: boolean - True if registered, false otherwise
count(): number
Returns the number of registered types.
Returns: number - Count of registered types
clear(): void
Clears all registered types (useful for testing).
Base Content Types
enum BaseContentType {
TEXT = 'text',
IMAGE = 'image',
AUDIO = 'audio',
VIDEO = 'video'
}Types
All type definitions are exported for TypeScript users:
ContentTypeDefinition- Complete type definitionContentTypeFilter- Filter criteria for listingRegistryResult<T>- Standard result typeFieldConfig- Editor field configurationEditorConfig- Editor configurationStorageConfig- Storage configurationPublishingConfig- Publishing configurationWorkflowTemplate- Workflow template definitionWorkflowStep- Individual workflow step
Singleton Instance
A singleton registry instance is exported for convenience:
import { contentTypeRegistry } from '@bernierllc/content-type-registry';
contentTypeRegistry.register(myType);Integration Status
- Logger: not-applicable - Pure registry package with no I/O operations requiring @bernierllc/logger integration
- Docs-Suite: ready - Complete TypeDoc API documentation
- NeverHub: optional - Can publish registration events for observability using @bernierllc/neverhub-adapter with detectNeverHub()
License
Copyright (c) 2025 Bernier LLC. All rights reserved.
