feat: ensure swagger is accessible with or without build

This commit is contained in:
amhsirak
2024-12-11 15:16:45 +05:30
parent 2025d09e1e
commit 319f9fce24

View File

@@ -1,5 +1,16 @@
import swaggerJSDoc from 'swagger-jsdoc'; import swaggerJSDoc from 'swagger-jsdoc';
import path from 'path'; import path from 'path';
import fs from 'fs';
// Dynamically determine API file paths
const jsFiles = [path.join(__dirname, '../api/*.js')]
const tsFiles = [path.join(__dirname, '../api/*.ts')]
let apis = fs.existsSync(jsFiles[0]) ? jsFiles : tsFiles;
if (!apis) {
throw new Error('No valid API files found! Ensure either .js or .ts files exist in the ../api/ directory.');
}
const options = { const options = {
definition: { definition: {
@@ -7,7 +18,8 @@ const options = {
info: { info: {
title: 'Website to API', title: 'Website to API',
version: '1.0.0', version: '1.0.0',
description: 'Maxun lets you get the data your robot extracted and run robots via API. All you need to do is input the Maxun API key by clicking Authorize below.', description:
'Maxun lets you get the data your robot extracted and run robots via API. All you need to do is input the Maxun API key by clicking Authorize below.',
}, },
components: { components: {
securitySchemes: { securitySchemes: {
@@ -15,7 +27,8 @@ const options = {
type: 'apiKey', type: 'apiKey',
in: 'header', in: 'header',
name: 'x-api-key', name: 'x-api-key',
description: 'API key for authorization. You can find your API key in the "API Key" section on Maxun Dashboard.', description:
'API key for authorization. You can find your API key in the "API Key" section on Maxun Dashboard.',
}, },
}, },
}, },
@@ -25,7 +38,7 @@ const options = {
}, },
], ],
}, },
apis: process.env.NODE_ENV === 'production' ? [path.join(__dirname, '../api/*.js')] : [path.join(__dirname, '../api/*.ts')] apis,
}; };
const swaggerSpec = swaggerJSDoc(options); const swaggerSpec = swaggerJSDoc(options);