api-doc-ks
v0.0.4
Published
Framework-agnostic API documentation UI with password protection
Maintainers
Readme
api-document
Framework-agnostic API documentation UI with optional password protection.
- Works with Express, Fastify, Koa, Hono, or plain Node.js middleware style
- Reads endpoint schema from JSON files in your app
- Optional auth: if
secretis not set, password page is bypassed
Install
npm i api-doc-ksor
bun add api-doc-ksQuick Start (Express)
import express from 'express';
import path from 'path';
import { createApiDoc } from 'api-doc-ks';
const app = express();
const apiDoc = createApiDoc({
secret: process.env.API_DOCUMENT_SECRET,
title: 'My App API',
version: '1.0.0',
schemaDir: path.resolve('./src/api-document'),
basePath: '/api-doc',
});
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(apiDoc);
app.listen(3000, () => {
console.log('http://localhost:3000/api-doc');
});API
initApiDocument(options)
createApiDoc(options)
Create middleware handler and initialize config in one step.
interface ApiDocumentConfig {
secret?: string;
title?: string;
version?: string;
schemaDir?: string;
loadSchema?: () => Promise<{ endpoints?: unknown[]; errors?: Array<{ schemaPath: string; message: string }> }>;
basePath?: string;
}Options:
secretoptional passwordtitledocument title shown in UIversiondocument version shown in UIschemaDirpath to schema directory in your app- default:
src/api-document
- default:
loadSchemacustom loader function (if provided,schemaDiris ignored)basePathdefault mount path- default:
/api-document
- default:
createApiDoc(options?: ApiDocumentConfig): Handler;Auth Behavior
- If
secretis set:GET /<basePath>requires loginGET /<basePath>/datareturns401when not authenticated
- If
secretis not set or empty:- auth is disabled
- password page is not shown
- all api-doc routes are directly accessible
Routes
For basePath = /api-doc:
GET /api-docUI pagePOST /api-doc/loginlogin endpointPOST /api-doc/logoutlogout endpointGET /api-doc/script.jsfrontend scriptGET /api-doc/dataschema JSON payload
Schema Directory
By default, library scans this directory in your app:
src/api-documentIt reads all .json files recursively.
Example structure:
src/api-document
├── auth
│ └── login.json
└── users
├── get-users.json
└── create-user.jsonSchema JSON Example
{
"name": "Get Users",
"method": "GET",
"path": "/api/users",
"description": "Get user list",
"request": {
"headers": { "Authorization": "Bearer <token>" },
"query": { "page": 1, "limit": 20 }
},
"response": {
"status": "200",
"data": [{ "id": "usr_01", "name": "John Doe" }]
},
"responseError": [
{ "code": "401", "message": "Unauthorized" }
]
}TypeScript
Types are included in the package.
import { createApiDoc } from 'api-doc-ks';
import type { SchemaLoaderOptions, ApiDocumentData } from 'api-document/schema';Backward Compatibility
createApiDocumentHandleris still exported as an alias ofcreateApiDoc.
Notes
createApiDoc(...)returns a handler with its own captured config.- Ensure your app can read the schema directory path you configure.
