myex-cli
Version:
Opinionated Express.js framework with CLI tools
228 lines (224 loc) • 6.13 kB
JavaScript
import swaggerJsDoc from 'swagger-jsdoc';
import { fileURLToPath } from 'url';
import path from 'path';
// ES module fix for __dirname
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Swagger definition
const swaggerOptions = {
definition: {
openapi: '3.0.0',
info: {
title: 'MYX API Documentation',
version: '1.0.0',
description: 'API documentation for the MYX Express.js starter project',
contact: {
name: 'API Support',
url: 'https://github.com/satechmedia/myx',
email: 'satechmedia@gmail.com',
},
license: {
name: 'MIT',
url: 'https://opensource.org/licenses/MIT',
},
},
servers: [
{
url: 'http://localhost:3000',
description: 'Development server',
},
{
url: 'https://api.myx.com',
description: 'Production server',
},
],
components: {
securitySchemes: {
BearerAuth: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
},
},
schemas: {
User: {
type: 'object',
required: ['name', 'email', 'password'],
properties: {
id: {
type: 'string',
description: 'User ID',
example: '60d21b4667d0d8992e610c85',
},
name: {
type: 'string',
description: 'User name',
example: 'John Doe',
},
email: {
type: 'string',
description: 'User email',
format: 'email',
example: 'john@example.com',
},
password: {
type: 'string',
description: 'User password (hashed, not returned in responses)',
example: 'password123',
},
role: {
type: 'string',
description: 'User role',
enum: ['user', 'admin'],
example: 'user',
},
createdAt: {
type: 'string',
format: 'date-time',
description: 'Creation timestamp',
},
updatedAt: {
type: 'string',
format: 'date-time',
description: 'Last update timestamp',
},
},
},
Error: {
type: 'object',
properties: {
status: {
type: 'string',
example: 'error',
},
statusCode: {
type: 'integer',
example: 400,
},
message: {
type: 'string',
example: 'Invalid input data',
},
},
},
SuccessResponse: {
type: 'object',
properties: {
status: {
type: 'string',
example: 'success',
},
message: {
type: 'string',
example: 'Operation completed successfully',
},
data: {
type: 'object',
example: {},
},
},
},
},
responses: {
UnauthorizedError: {
description: 'Authentication is required to access the resource',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/Error',
},
example: {
status: 'error',
statusCode: 401,
message: 'Authentication required',
},
},
},
},
ForbiddenError: {
description: 'Client is authenticated but does not have permission to access the resource',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/Error',
},
example: {
status: 'error',
statusCode: 403,
message: 'Access denied: Insufficient permissions',
},
},
},
},
NotFoundError: {
description: 'The specified resource was not found',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/Error',
},
example: {
status: 'error',
statusCode: 404,
message: 'Resource not found',
},
},
},
},
ValidationError: {
description: 'Validation error occurred',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/Error',
},
example: {
status: 'error',
statusCode: 400,
message: 'Validation failed: Email is required',
},
},
},
},
ServerError: {
description: 'Internal server error',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/Error',
},
example: {
status: 'error',
statusCode: 500,
message: 'Internal server error',
},
},
},
},
},
},
tags: [
{
name: 'Authentication',
description: 'Authentication related endpoints',
},
{
name: 'Users',
description: 'User management endpoints',
},
{
name: 'System',
description: 'System related endpoints',
},
],
},
apis: [
// Include routes and models for documentation
path.join(__dirname, '../routes/*.js'),
path.join(__dirname, '../models/*.js'),
path.join(__dirname, '../controllers/*.js'),
],
};
// Initialize swagger-jsdoc
const swaggerSpec = swaggerJsDoc(swaggerOptions);
export default swaggerSpec;