This is a Strapi v5 plugin called "gen-types" that auto-generates TypeScript types after Strapi startup. The plugin scans Strapi schema files and generates TypeScript interfaces for content types and components.
npm run build- Build the plugin using strapi-plugin buildnpm test- Run tests using tsx test runnernpm run test:watch- Run tests in watch modenpm run lint- Run ESLintnpm run lint:fix- Run ESLint with auto-fixnpm run format- Format code with Prettier
-
Bootstrap (
server/src/bootstrap.ts): Plugin lifecycle hook that triggers type generation after Strapi server starts listening. Uses configurable delay to ensure Strapi's type generation is complete. -
TypeGenerator (
server/src/services/type-generator.ts): Main service class that handles:- Schema file discovery from
src/apiandsrc/componentsdirectories - Loading and parsing JSON schema files
- Generating TypeScript interfaces via the generators
- Writing output to configured path
- Schema file discovery from
-
Generators (
server/src/generators/):interfaces.ts- Core interface generation logictemplates.ts- Base types and file header generationattributes.ts- Attribute type mapping logic
-
Configuration (
server/src/config/):index.ts- Plugin configuration with validationoptional-custom-fields.ts- Optional field mappings for Strapi pluginsoptional-types.ts- TypeScript type definitions for Strapi plugin custom fields
- Optional Field Types: Configurable support for Strapi plugins via
enableOptionalFieldsconfig - Custom Field Mappings: Maps custom Strapi fields to specific TypeScript types
- Component Support: Generates types for both content types and reusable components
- Built-in Types: Provides base types for images, files, users, rich text blocks
- Debug Mode: Configurable logging for development
- Generation Delay: Configurable delay to ensure Strapi initialization is complete
- Bootstrap hook waits for Strapi server to start listening
- After configurable delay, TypeGenerator service is invoked
- Schema files are discovered in
src/apiandsrc/components - JSON schemas are loaded and parsed
- TypeScript interfaces are generated using the generators
- Output file is written to the configured path
server/src/bootstrap.ts- Plugin lifecycle hookserver/src/services/- Core services including TypeGeneratorserver/src/generators/- Type generation logic and templatesserver/src/config/- Configuration and optional field mappingsserver/src/types/- TypeScript type definitionsserver/src/utils/- Utility functions for file operations and naming
Tests use the tsx test runner and are located alongside source files with .test.ts suffix. The plugin includes tests for:
- Configuration validation
- Optional field type generation
- Attribute type generation
- Naming utilities
- String manipulation utilities