| 37 | } |
| 38 | |
| 39 | export class FileSystemSpecService implements ISpecStore, ISpecExplorer { |
| 40 | private readonly config: Required<SpecServiceConfig>; |
| 41 | private readonly logger: Logger; |
| 42 | private readonly specCache: SimpleCache<string, OpenAPIV3.Document>; |
| 43 | private readonly folderPath: string; |
| 44 | private readonly catalogPath: string; |
| 45 | private readonly dereferencedPath: string; |
| 46 | private specs: { [specId: string]: OpenAPIV3.Document } = {}; |
| 47 | private catalog: SpecCatalogEntry[] = []; |
| 48 | |
| 49 | constructor( |
| 50 | private readonly scanner: ISpecScanner, |
| 51 | config: SpecServiceConfig, |
| 52 | logger?: Logger |
| 53 | ) { |
| 54 | this.config = { |
| 55 | basePath: config.basePath, |
| 56 | catalogDir: config.catalogDir || "_catalog", |
| 57 | dereferencedDir: config.dereferencedDir || "_dereferenced", |
| 58 | retryAttempts: config.retryAttempts || 3, |
| 59 | retryDelay: config.retryDelay || 1000, |
| 60 | cache: { |
| 61 | maxSize: config.cache?.maxSize || 500, |
| 62 | ttl: config.cache?.ttl || 60 * 60 * 1000, // 1 hour |
| 63 | }, |
| 64 | }; |
| 65 | |
| 66 | this.logger = logger || new ConsoleLogger(); |
| 67 | this.folderPath = this.config.basePath; |
| 68 | this.catalogPath = path.join(this.folderPath, this.config.catalogDir); |
| 69 | this.dereferencedPath = path.join( |
| 70 | this.folderPath, |
| 71 | this.config.dereferencedDir |
| 72 | ); |
| 73 | |
| 74 | this.specCache = new SimpleCache<string, OpenAPIV3.Document>({ |
| 75 | maxSize: this.config.cache.maxSize, |
| 76 | ttl: this.config.cache.ttl, |
| 77 | }); |
| 78 | } |
| 79 | |
| 80 | private async ensureDirectory(dir: string) { |
| 81 | try { |
| 82 | await fs.mkdir(dir, { recursive: true }); |
| 83 | } catch (error) { |
| 84 | throw new SpecServiceError( |
| 85 | `Failed to create directory ${dir}`, |
| 86 | "INIT_ERROR", |
| 87 | error instanceof Error ? error : new Error(String(error)) |
| 88 | ); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | private async ensureDirectories() { |
| 93 | this.logger.debug("Ensuring required directories exist"); |
| 94 | await Promise.all([ |
| 95 | this.ensureDirectory(this.folderPath), |
| 96 | this.ensureDirectory(this.catalogPath), |
nothing calls this directly
no outgoing calls
no test coverage detected