|
| 1 | +interface MockResponse { |
| 2 | + status: number; |
| 3 | + body: any; |
| 4 | +} |
| 5 | + |
| 6 | +interface MockEndpoint { |
| 7 | + method: string; |
| 8 | + path: string; |
| 9 | + response: MockResponse; |
| 10 | +} |
| 11 | + |
| 12 | +interface OpenAPI { |
| 13 | + openapi: string; |
| 14 | + info: { |
| 15 | + title: string; |
| 16 | + version: string; |
| 17 | + }; |
| 18 | + paths: Record<string, any>; |
| 19 | +} |
| 20 | + |
| 21 | +export class DocGenerator { |
| 22 | + private mocks: MockEndpoint[] = []; |
| 23 | + |
| 24 | + private filename: string; |
| 25 | + |
| 26 | + constructor(filename = 'openapi.json') { |
| 27 | + this.filename = filename; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Adiciona um novo mock e automaticamente atualiza o JSON de documentação |
| 32 | + * @param method - Método HTTP (GET, POST, etc.) |
| 33 | + * @param path - Endpoint da API |
| 34 | + * @param status - Código de status HTTP da resposta |
| 35 | + * @param body - Corpo da resposta |
| 36 | + */ |
| 37 | + public addMock( |
| 38 | + method: string, |
| 39 | + path: string, |
| 40 | + status: number, |
| 41 | + body: any, |
| 42 | + ): void { |
| 43 | + // Verifica se já existe um mock com o mesmo método e path para evitar duplicação |
| 44 | + const existingMockIndex = this.mocks.findIndex( |
| 45 | + mock => mock.method === method.toUpperCase() && mock.path === path, |
| 46 | + ); |
| 47 | + |
| 48 | + if (existingMockIndex !== -1) { |
| 49 | + // Se já existe, atualiza o mock existente |
| 50 | + this.mocks[existingMockIndex] = { |
| 51 | + method: method.toUpperCase(), |
| 52 | + path, |
| 53 | + response: { status, body }, |
| 54 | + }; |
| 55 | + } else { |
| 56 | + // Caso contrário, adiciona um novo mock |
| 57 | + this.mocks.push({ |
| 58 | + method: method.toUpperCase(), |
| 59 | + path, |
| 60 | + response: { status, body }, |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + // Regenera a documentação automaticamente |
| 65 | + this.generateOpenApiJson(); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Gera o JSON de documentação OpenAPI e salva no arquivo definido no construtor |
| 70 | + */ |
| 71 | + private generateOpenApiJson(): void { |
| 72 | + const openAPI: OpenAPI = { |
| 73 | + openapi: '3.0.0', |
| 74 | + info: { |
| 75 | + title: 'Mocked API', |
| 76 | + version: '1.0.0', |
| 77 | + }, |
| 78 | + paths: {}, |
| 79 | + }; |
| 80 | + |
| 81 | + // Construindo os endpoints no formato OpenAPI |
| 82 | + this.mocks.forEach(({ method, path, response }) => { |
| 83 | + if (!openAPI.paths[path]) { |
| 84 | + openAPI.paths[path] = {}; |
| 85 | + } |
| 86 | + |
| 87 | + openAPI.paths[path][method.toLowerCase()] = { |
| 88 | + summary: `Mock response for ${method} ${path}`, |
| 89 | + responses: { |
| 90 | + [response.status]: { |
| 91 | + description: `Mock response for ${method} ${path}`, |
| 92 | + content: { |
| 93 | + 'application/json': response.body, |
| 94 | + }, |
| 95 | + }, |
| 96 | + }, |
| 97 | + }; |
| 98 | + }); |
| 99 | + |
| 100 | + console.log('\n\n## OpenAPI JSON ##'); |
| 101 | + console.log(JSON.stringify(openAPI, null, 2)); |
| 102 | + console.log('\n\n'); |
| 103 | + } |
| 104 | +} |
0 commit comments