counterfact
Version:
Generate a TypeScript-based mock server from an OpenAPI spec in seconds — with stateful routes, hot reload, and REPL support.
56 lines (55 loc) • 1.72 kB
JavaScript
/**
* Registry of loaded scenario modules.
*
* Scenario modules are plain JavaScript/TypeScript files that export named
* functions. Each module is keyed by a slash-delimited path relative to the
* `scenarios/` directory (e.g. `"index"`, `"sub/reset"`).
*
* The registry is used by the REPL's `.scenario` command and by tab-completion
* to enumerate available scenarios and their exported function names.
*/
export class ScenarioRegistry {
modules = new Map();
/**
* Registers (or replaces) a scenario module.
*
* @param key - Slash-delimited file key (e.g. `"index"`, `"auth/setup"`).
* @param module - The module's exported values.
*/
add(key, module) {
this.modules.set(key, module);
}
/**
* Removes the scenario module for `key`.
*
* @param key - The file key to remove.
*/
remove(key) {
this.modules.delete(key);
}
/**
* Returns the module for `fileKey`, or `undefined` if not registered.
*
* @param fileKey - The file key to look up.
*/
getModule(fileKey) {
return this.modules.get(fileKey);
}
/**
* Returns the names of all exported functions for the given file key.
* Used for tab completion.
*/
getExportedFunctionNames(fileKey) {
const module = this.modules.get(fileKey);
if (!module)
return [];
return Object.keys(module).filter((k) => typeof module[k] === "function");
}
/**
* Returns all loaded file keys (e.g. "index", "myscript", "sub/script").
* Used for tab completion to enumerate available scenario files.
*/
getFileKeys() {
return [...this.modules.keys()];
}
}