All files / src/config swagger.ts

100% Statements 230/230
75% Branches 3/4
100% Functions 0/0
100% Lines 228/228

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 2292x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 24x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 24x  
// src/config/swagger.ts
/**
 * @file OpenAPI/Swagger configuration for API documentation.
 * Implements ADR-018: API Documentation Strategy.
 *
 * This file configures swagger-jsdoc to generate an OpenAPI 3.0 specification
 * from JSDoc annotations in route files. The specification is used by
 * swagger-ui-express to serve interactive API documentation.
 */
import swaggerJsdoc from 'swagger-jsdoc';
 
const options: swaggerJsdoc.Options = {
  definition: {
    openapi: '3.0.0',
    info: {
      title: 'Flyer Crawler API',
      version: '1.0.0',
      description:
        'API for the Flyer Crawler application - a platform for discovering grocery deals, managing recipes, and tracking budgets.',
      contact: {
        name: 'API Support',
      },
      license: {
        name: 'Private',
      },
    },
    servers: [
      {
        url: '/api',
        description: 'API server',
      },
    ],
    components: {
      securitySchemes: {
        bearerAuth: {
          type: 'http',
          scheme: 'bearer',
          bearerFormat: 'JWT',
          description: 'JWT token obtained from /auth/login or /auth/register',
        },
      },
      schemas: {
        // Standard success response wrapper (ADR-028)
        SuccessResponse: {
          type: 'object',
          properties: {
            success: {
              type: 'boolean',
              example: true,
            },
            data: {
              type: 'object',
              description: 'Response payload - structure varies by endpoint',
            },
          },
          required: ['success', 'data'],
        },
        // Standard error response wrapper (ADR-028)
        ErrorResponse: {
          type: 'object',
          properties: {
            success: {
              type: 'boolean',
              example: false,
            },
            error: {
              type: 'object',
              properties: {
                code: {
                  type: 'string',
                  description: 'Machine-readable error code',
                  example: 'VALIDATION_ERROR',
                },
                message: {
                  type: 'string',
                  description: 'Human-readable error message',
                  example: 'Invalid request parameters',
                },
              },
              required: ['code', 'message'],
            },
          },
          required: ['success', 'error'],
        },
        // Common service health status
        ServiceHealth: {
          type: 'object',
          properties: {
            status: {
              type: 'string',
              enum: ['healthy', 'degraded', 'unhealthy'],
            },
            latency: {
              type: 'number',
              description: 'Response time in milliseconds',
            },
            message: {
              type: 'string',
              description: 'Additional status information',
            },
            details: {
              type: 'object',
              description: 'Service-specific details',
            },
          },
          required: ['status'],
        },
        // Achievement schema
        Achievement: {
          type: 'object',
          properties: {
            achievement_id: {
              type: 'integer',
              example: 1,
            },
            name: {
              type: 'string',
              example: 'First-Upload',
            },
            description: {
              type: 'string',
              example: 'Upload your first flyer',
            },
            icon: {
              type: 'string',
              example: 'upload-cloud',
            },
            points_value: {
              type: 'integer',
              example: 25,
            },
            created_at: {
              type: 'string',
              format: 'date-time',
            },
          },
        },
        // User achievement (with achieved_at)
        UserAchievement: {
          allOf: [
            { $ref: '#/components/schemas/Achievement' },
            {
              type: 'object',
              properties: {
                user_id: {
                  type: 'string',
                  format: 'uuid',
                },
                achieved_at: {
                  type: 'string',
                  format: 'date-time',
                },
              },
            },
          ],
        },
        // Leaderboard entry
        LeaderboardUser: {
          type: 'object',
          properties: {
            user_id: {
              type: 'string',
              format: 'uuid',
            },
            full_name: {
              type: 'string',
              example: 'John Doe',
            },
            avatar_url: {
              type: 'string',
              nullable: true,
            },
            points: {
              type: 'integer',
              example: 150,
            },
            rank: {
              type: 'integer',
              example: 1,
            },
          },
        },
      },
    },
    tags: [
      {
        name: 'Health',
        description: 'Server health and readiness checks',
      },
      {
        name: 'Auth',
        description: 'Authentication and authorization',
      },
      {
        name: 'Users',
        description: 'User profile management',
      },
      {
        name: 'Achievements',
        description: 'Gamification and leaderboards',
      },
      {
        name: 'Flyers',
        description: 'Flyer uploads and retrieval',
      },
      {
        name: 'Recipes',
        description: 'Recipe management',
      },
      {
        name: 'Budgets',
        description: 'Budget tracking and analysis',
      },
      {
        name: 'Admin',
        description: 'Administrative operations (requires admin role)',
      },
      {
        name: 'System',
        description: 'System status and monitoring',
      },
    ],
  },
  // Path to the API routes files with JSDoc annotations
  apis: ['./src/routes/*.ts'],
};
 
export const swaggerSpec = swaggerJsdoc(options);