Building Custom Generators with Skmtc
One of Skmtc's most powerful features is the ability to create custom generators tailored to your specific needs. Whether you're building internal tools or sharing with the community, custom generators give you complete control.
Understanding Generator Structure
A Skmtc generator is a TypeScript package that transforms API schemas into code. The basic structure looks like this:
import { Generator } from '@skmtc/core'
export const generator: Generator = {
name: 'my-custom-generator',
version: '1.0.0',
async generate(schema, options) {
// Your generation logic here
return {
files: [
{
path: 'client.ts',
content: generatedCode
}
]
}
}
}
Key Concepts
Schema Parsing
Skmtc supports multiple schema formats:
- OpenAPI 3.x
- TypeSpec
- JSON Schema
Your generator receives a normalized schema object, making it easy to work with any format.
Template Systems
You can use any templating approach:
- String templates
- Template literals
- AST manipulation
- Or a combination
Choose what works best for your use case.
Configuration
Generators can accept configuration options:
export interface GeneratorConfig {
outputPath?: string
includeTests?: boolean
authStrategy?: 'bearer' | 'api-key' | 'oauth'
}
A Practical Example
Let's build a simple generator that creates React hooks for API endpoints:
import { Generator } from '@skmtc/core'
export const reactHooksGenerator: Generator = {
name: 'react-hooks',
version: '1.0.0',
async generate(schema, options) {
const hooks = schema.paths.map(path => {
const hookName = `use${pascalCase(path.operationId)}`
return `
export function ${hookName}() {
return useQuery({
queryKey: ['${path.operationId}'],
queryFn: async () => {
const response = await fetch('${path.url}')
return response.json()
}
})
}
`.trim()
})
return {
files: [
{
path: 'hooks.ts',
content: [
"import { useQuery } from '@tanstack/react-query'",
'',
...hooks
].join('\n')
}
]
}
}
}
Testing Your Generator
Skmtc provides testing utilities:
import { testGenerator } from '@skmtc/testing'
describe('reactHooksGenerator', () => {
it('generates hooks for GET endpoints', async () => {
const result = await testGenerator(
reactHooksGenerator,
mockSchema
)
expect(result.files[0].content).toContain('useGetUsers')
})
})
Publishing Your Generator
Once your generator is ready, publish it to npm:
npm publish --access public
Then others can use it:
npx skmtc install @yourname/react-hooks
npx skmtc generate @yourname/react-hooks
Best Practices
Keep It Focused
Each generator should do one thing well. Create separate generators for different output formats rather than one monolithic generator.
Provide Defaults
Make generators work out-of-the-box with sensible defaults, but allow customization.
Document Everything
Include clear README files with examples and configuration options.
Version Carefully
Use semantic versioning and document breaking changes.
Community Resources
Join our Discord to:
- Share your generators
- Get help with development
- Collaborate with other generator authors
- Suggest new features for the platform
What's Next
Explore our generator libraries to see examples and inspiration for your own generators.
The possibilities are endless - we can't wait to see what you build!