Configuration file handling and option processing for apidoc. The system supports multiple configuration sources and provides flexible project setup options.
class Reader {
constructor(app: App);
read(): ConfigurationData;
search(): ConfigurationData;
getHeaderFooter(config: ConfigurationData): HeaderFooterData;
findConfigFileInDir(filename: string, dir: string): Partial<ConfigurationData>;
findFileInSrc(filename: string): string;
}The Reader class handles discovery and parsing of configuration files from multiple sources.
Constructor:
app (App): Application context object containing options and loggerMethods:
read(): Reads configuration from specified config file or searches for config filessearch(): Searches for configuration files in source directoriesgetHeaderFooter(): Processes header and footer file referencesfindConfigFileInDir(): Searches for a specific config file in a directoryfindFileInSrc(): Finds a file in source directoriesinterface ConfigurationData {
name?: string;
version?: string;
description?: string;
title?: string;
url?: string;
sampleUrl?: string;
input?: string[];
output?: string;
template?: TemplateOptions;
header?: HeaderFooterConfig;
footer?: HeaderFooterConfig;
order?: string[];
}Properties:
name: Project name for documentationversion: Project versiondescription: Project descriptiontitle: Custom browser title for generated documentationurl: Base URL for the APIsampleUrl: URL for sample requests or false to disableinput: Array of input source directoriesoutput: Output directory for generated documentationtemplate: Template configuration optionsheader: Header section configurationfooter: Footer section configurationorder: Custom ordering for API groups and endpointsinterface HeaderFooterConfig {
title: string;
filename: string;
}
interface HeaderFooterData {
title: string;
content: string;
}HeaderFooterConfig:
title: Display title for the sectionfilename: Path to markdown file containing contentHeaderFooterData:
title: Section titlecontent: Processed HTML content from markdown fileinterface TemplateOptions {
showRequiredLabels?: boolean;
withCompare?: boolean;
withGenerator?: boolean;
aloneDisplay?: boolean;
}Properties:
showRequiredLabels: Show "required" labels on parameterswithCompare: Enable version comparison featureswithGenerator: Show generator informationaloneDisplay: Display mode for single API endpointsconst defaultConfig: ConfigurationData = {
name: 'Acme project',
version: '0.0.0',
description: 'REST Api'
};The configuration system searches for configuration in the following order:
When a config file is specified via the config option:
const { createDoc } = require('apidoc');
const result = createDoc({
config: './apidoc.json',
src: ['./src'],
dest: './docs'
});Configuration can be specified in the apidoc key of package.json:
{
"name": "my-api",
"version": "1.0.0",
"apidoc": {
"name": "My API Documentation",
"version": "1.0.0",
"description": "REST API for my application",
"title": "My API Docs",
"url": "https://api.example.com",
"sampleUrl": "https://api.example.com",
"template": {
"showRequiredLabels": true,
"withCompare": true
}
}
}Dedicated JSON configuration file:
{
"name": "My API Documentation",
"version": "1.0.0",
"description": "REST API for my application",
"title": "Custom API Browser Title",
"url": "https://api.example.com",
"sampleUrl": "https://api.example.com",
"input": ["src", "lib"],
"output": "documentation",
"header": {
"title": "Introduction",
"filename": "header.md"
},
"footer": {
"title": "Best Practices",
"filename": "footer.md"
},
"order": [
"User",
"Authentication",
"Posts"
],
"template": {
"showRequiredLabels": false,
"withCompare": true,
"withGenerator": true,
"aloneDisplay": false
}
}JavaScript configuration file for dynamic configuration:
module.exports = {
name: 'My API Documentation',
version: process.env.API_VERSION || '1.0.0',
description: 'REST API for my application',
title: 'My API Docs',
url: process.env.API_BASE_URL || 'https://api.example.com',
sampleUrl: process.env.API_BASE_URL || 'https://api.example.com',
input: ['src'],
output: 'docs',
template: {
showRequiredLabels: process.env.NODE_ENV === 'development',
withCompare: true,
withGenerator: true
}
};Note: The Reader class is internal to ApiDoc and not part of the public API. This example is for reference only.
const { Reader } = require('apidoc/lib/reader');
const app = {
options: {
config: './apidoc.json',
src: ['./src']
},
log: console,
markdownParser: null
};
const reader = new Reader(app);
const config = reader.read();
console.log(`Project: ${config.name} v${config.version}`);
console.log(`Description: ${config.description}`);const { Reader } = require('apidoc/lib/reader');
const app = {
options: {
src: ['./src', './lib']
},
log: console,
markdownParser: null
};
const reader = new Reader(app);
const config = reader.search();
if (config.name === 'Acme project') {
console.log('No configuration files found, using defaults');
} else {
console.log(`Found configuration: ${config.name}`);
}const { Reader } = require('apidoc/lib/reader');
const MarkdownIt = require('markdown-it');
const app = {
options: {
config: './apidoc.json'
},
log: console,
markdownParser: new MarkdownIt()
};
const reader = new Reader(app);
const config = reader.read();
// config.header and config.footer now contain processed HTML content
if (config.header) {
console.log(`Header title: ${config.header.title}`);
console.log(`Header content length: ${config.header.content.length}`);
}const { Reader } = require('apidoc/lib/reader');
const app = {
options: {
config: '/path/to/custom/config.js',
src: ['./src']
},
log: console,
markdownParser: null
};
const reader = new Reader(app);
const config = reader.read();While most configuration fields are optional, certain combinations are recommended:
name and version for proper project identificationinput or proper src option for source file locationoutput for documentation destinationAll file paths in configuration are resolved relative to:
const { Reader } = require('apidoc/lib/reader');
const app = {
options: {
config: './invalid-config.json'
},
log: console,
markdownParser: null
};
try {
const reader = new Reader(app);
const config = reader.read();
} catch (error) {
if (error.code === 'ENOENT') {
console.error('Configuration file not found');
} else if (error instanceof SyntaxError) {
console.error('Invalid JSON in configuration file');
} else {
console.error('Configuration error:', error.message);
}
}// apidoc.config.js
const env = process.env.NODE_ENV || 'development';
module.exports = {
name: 'My API',
version: process.env.npm_package_version,
description: 'API Documentation',
url: env === 'production'
? 'https://api.mycompany.com'
: 'http://localhost:3000',
sampleUrl: env === 'production'
? 'https://api.mycompany.com'
: 'http://localhost:3000',
template: {
withCompare: env === 'development',
withGenerator: true
}
};// apidoc.config.js for monorepo
const path = require('path');
module.exports = {
name: 'Microservice API',
version: require('./package.json').version,
input: [
path.join(__dirname, 'src'),
path.join(__dirname, '../shared/src')
],
output: path.join(__dirname, 'docs'),
header: {
title: 'API Overview',
filename: '../shared/docs/header.md'
}
};