schemantic
Version:
A fully typed, extensible TypeScript type generator for FastAPI OpenAPI schemas
233 lines • 7.1 kB
JavaScript
;
/**
* Plugin manager for the schemantic system
* Handles loading, registration, and execution of plugins
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.PluginManager = void 0;
/**
* Plugin manager class
*/
class PluginManager {
constructor() {
this.plugins = new Map();
this.enabledPlugins = new Set();
}
/**
* Register a plugin
*/
registerPlugin(plugin) {
this.plugins.set(plugin.name, plugin);
}
/**
* Unregister a plugin
*/
unregisterPlugin(name) {
this.plugins.delete(name);
this.enabledPlugins.delete(name);
}
/**
* Enable a plugin
*/
enablePlugin(name) {
if (this.plugins.has(name)) {
this.enabledPlugins.add(name);
}
}
/**
* Disable a plugin
*/
disablePlugin(name) {
this.enabledPlugins.delete(name);
}
/**
* Check if a plugin is enabled
*/
isPluginEnabled(name) {
return this.enabledPlugins.has(name);
}
/**
* Get all registered plugins
*/
getAllPlugins() {
return Array.from(this.plugins.values());
}
/**
* Get enabled plugins
*/
getEnabledPlugins() {
return Array.from(this.enabledPlugins)
.map((name) => this.plugins.get(name))
.filter((plugin) => plugin !== undefined);
}
/**
* Get a specific plugin
*/
getPlugin(name) {
return this.plugins.get(name);
}
/**
* Execute before generation hook
*/
async executeBeforeGeneration(context) {
const enabledPlugins = this.getEnabledPlugins();
for (const plugin of enabledPlugins) {
if (plugin.beforeGeneration) {
try {
await plugin.beforeGeneration(context);
}
catch (error) {
console.warn(`Plugin ${plugin.name} beforeGeneration hook failed:`, error);
}
}
}
}
/**
* Execute after generation hook
*/
async executeAfterGeneration(context, result) {
const enabledPlugins = this.getEnabledPlugins();
for (const plugin of enabledPlugins) {
if (plugin.afterGeneration) {
try {
await plugin.afterGeneration(context, result);
}
catch (error) {
console.warn(`Plugin ${plugin.name} afterGeneration hook failed:`, error);
}
}
}
}
/**
* Execute before type generation hook
*/
async executeBeforeTypeGeneration(typeName, schema, context) {
const enabledPlugins = this.getEnabledPlugins();
for (const plugin of enabledPlugins) {
if (plugin.beforeTypeGeneration) {
try {
await plugin.beforeTypeGeneration(typeName, schema, context);
}
catch (error) {
console.warn(`Plugin ${plugin.name} beforeTypeGeneration hook failed:`, error);
}
}
}
}
/**
* Execute after type generation hook
*/
async executeAfterTypeGeneration(typeName, generatedType, context) {
const enabledPlugins = this.getEnabledPlugins();
for (const plugin of enabledPlugins) {
if (plugin.afterTypeGeneration) {
try {
await plugin.afterTypeGeneration(typeName, generatedType, context);
}
catch (error) {
console.warn(`Plugin ${plugin.name} afterTypeGeneration hook failed:`, error);
}
}
}
}
/**
* Execute before client generation hook
*/
async executeBeforeClientGeneration(context) {
const enabledPlugins = this.getEnabledPlugins();
for (const plugin of enabledPlugins) {
if (plugin.beforeClientGeneration) {
try {
await plugin.beforeClientGeneration(context);
}
catch (error) {
console.warn(`Plugin ${plugin.name} beforeClientGeneration hook failed:`, error);
}
}
}
}
/**
* Execute after client generation hook
*/
async executeAfterClientGeneration(generatedClient, context) {
const enabledPlugins = this.getEnabledPlugins();
for (const plugin of enabledPlugins) {
if (plugin.afterClientGeneration) {
try {
await plugin.afterClientGeneration(generatedClient, context);
}
catch (error) {
console.warn(`Plugin ${plugin.name} afterClientGeneration hook failed:`, error);
}
}
}
}
/**
* Transform schema using plugins
*/
transformSchema(schema, context) {
let transformedSchema = schema;
const enabledPlugins = this.getEnabledPlugins();
for (const plugin of enabledPlugins) {
if (plugin.transformSchema) {
try {
transformedSchema = plugin.transformSchema(transformedSchema, context);
}
catch (error) {
console.warn(`Plugin ${plugin.name} transformSchema hook failed:`, error);
}
}
}
return transformedSchema;
}
/**
* Get custom type generators from plugins
*/
getCustomTypeGenerators() {
const generators = new Map();
const enabledPlugins = this.getEnabledPlugins();
for (const plugin of enabledPlugins) {
if (plugin.customTypeGenerators) {
for (const [name, generator] of Object.entries(plugin.customTypeGenerators)) {
generators.set(`${plugin.name}:${name}`, generator);
}
}
}
return generators;
}
/**
* Get custom client generators from plugins
*/
getCustomClientGenerators() {
const generators = new Map();
const enabledPlugins = this.getEnabledPlugins();
for (const plugin of enabledPlugins) {
if (plugin.customClientGenerators) {
for (const [name, generator] of Object.entries(plugin.customClientGenerators)) {
generators.set(`${plugin.name}:${name}`, generator);
}
}
}
return generators;
}
/**
* Clear all plugins
*/
clear() {
this.plugins.clear();
this.enabledPlugins.clear();
}
/**
* Get plugin statistics
*/
getStatistics() {
return {
totalPlugins: this.plugins.size,
enabledPlugins: this.enabledPlugins.size,
disabledPlugins: this.plugins.size - this.enabledPlugins.size,
pluginNames: Array.from(this.plugins.keys()),
};
}
}
exports.PluginManager = PluginManager;
//# sourceMappingURL=manager.js.map