hasancode-api-docs 1.0.16 → 1.0.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,7 +2,7 @@
2
2
  <html lang="en">
3
3
  <head>
4
4
  <meta charset="UTF-8" />
5
- <link rel="icon" type="image/svg+xml" href="./logo.png" />
5
+ <link rel="icon" type="image/svg+xml" href="/api-docs/logo.png" />
6
6
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7
7
 
8
8
  <link rel="preconnect" href="https://fonts.googleapis.com" />
@@ -13,9 +13,9 @@
13
13
  />
14
14
 
15
15
  <title>API Doc</title>
16
- <script type="module" crossorigin src="./assets/index-DA78l2zr.js"></script>
17
- <link rel="modulepreload" crossorigin href="./assets/vendor-Cy83rCXF.js">
18
- <link rel="stylesheet" crossorigin href="./assets/index-BddYm5rR.css">
16
+ <script type="module" crossorigin src="/api-docs/assets/index-CAGuZ--I.js"></script>
17
+ <link rel="modulepreload" crossorigin href="/api-docs/assets/vendor-r6CGHiWc.js">
18
+ <link rel="stylesheet" crossorigin href="/api-docs/assets/index-BddYm5rR.css">
19
19
  </head>
20
20
  <body>
21
21
  <div id="root"></div>
@@ -78,7 +78,6 @@ class ApiDoc {
78
78
  path.join(__dirname, "../../client/dist"),
79
79
  path.join(__dirname, "../client/dist"),
80
80
  path.join(__dirname, "../../../client/dist"),
81
- // Fallback for some npm structures
82
81
  path.join(process.cwd(), "node_modules/hasancode-api-docs/client/dist"),
83
82
  ];
84
83
  for (const testPath of possiblePaths) {
@@ -202,31 +201,20 @@ class ApiDoc {
202
201
  */
203
202
  middleware() {
204
203
  const router = (0, express_1.Router)();
205
- // 1. Direct redirect to overview from the root documentation path
206
- // Using 302 (Found) to avoid browser-cached permanent redirects (301)
207
- router.get([this.DOCS_PATH, `${this.DOCS_PATH}/`], (req, res) => {
208
- const cleanBase = (req.baseUrl + this.DOCS_PATH).replace(/\/+$/, "");
209
- res.redirect(302, `${cleanBase}/overview`);
210
- });
211
- // 2. Serve config.json
212
- router.get(`${this.DOCS_PATH}/config.json`, this.serveConfig.bind(this));
213
- // 3. Serve static assets with explicit MIME types
214
- router.use(`${this.DOCS_PATH}/assets`, (req, res, next) => {
215
- const ext = path.extname(req.path).toLowerCase();
216
- if (ext === ".js") {
217
- res.setHeader("Content-Type", "application/javascript; charset=utf-8");
218
- }
219
- else if (ext === ".css") {
220
- res.setHeader("Content-Type", "text/css; charset=utf-8");
221
- }
222
- next();
223
- }, (0, express_1.static)(path.join(this.clientPath, "assets"), {
204
+ router.use(`${this.DOCS_PATH}/assets`, (0, express_1.static)(path.join(this.clientPath, "assets"), {
224
205
  maxAge: "1d",
225
206
  etag: true,
226
- index: false,
227
207
  fallthrough: false,
208
+ setHeaders: (res, path) => {
209
+ if (path.endsWith(".js")) {
210
+ res.setHeader("Content-Type", "application/javascript");
211
+ }
212
+ else if (path.endsWith(".css")) {
213
+ res.setHeader("Content-Type", "text/css");
214
+ }
215
+ },
228
216
  }));
229
- // 4. Serve the React app
217
+ router.get(`${this.DOCS_PATH}/config.json`, this.serveConfig.bind(this));
230
218
  router.use(this.serveApp.bind(this));
231
219
  return router;
232
220
  }
@@ -253,54 +241,48 @@ class ApiDoc {
253
241
  const indexPath = path.join(this.clientPath, "index.html");
254
242
  if (fs.existsSync(indexPath)) {
255
243
  let html = fs.readFileSync(indexPath, "utf-8");
256
- // Calculate the absolute base path for the documentation
257
- // This makes the app portable even when mounted at a prefix
258
- const mountPath = req.baseUrl || "";
259
- const docsPath = this.DOCS_PATH.startsWith("/")
260
- ? this.DOCS_PATH
261
- : `/${this.DOCS_PATH}`;
262
- const fullPath = (mountPath + docsPath).replace(/\/+$/, "");
263
- const baseTag = `<base href="${fullPath}/">`;
264
- // Insert or replace base tag
265
- if (html.includes("<base ")) {
244
+ // Inject base tag with trailing slash for proper asset resolution
245
+ const baseTag = `<base href="${this.DOCS_PATH}/">`;
246
+ // Insert base tag right after <head> or before any other tags
247
+ if (html.includes("<base")) {
248
+ // Replace existing base tag
266
249
  html = html.replace(/<base[^>]*>/i, baseTag);
267
250
  }
268
251
  else {
252
+ // Insert new base tag
269
253
  html = html.replace(/<head>/i, `<head>${baseTag}`);
270
254
  }
271
- // Also ensure any absolute references in index.html are corrected
272
- html = html.replace(/\/api-docs\/assets\//g, "assets/");
273
- res.setHeader("Content-Type", "text/html; charset=utf-8");
255
+ res.setHeader("Content-Type", "text/html");
274
256
  res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
275
257
  res.send(html);
276
258
  }
277
259
  else {
278
260
  res.status(404).send(`
279
- <html>
280
- <head>
281
- <title>API Docs - Not Built</title>
282
- <style>
283
- body {
284
- font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
285
- max-width: 600px;
286
- margin: 100px auto;
287
- padding: 20px;
288
- text-align: center;
289
- }
290
- h1 { color: #e53e3e; }
291
- code {
292
- background: #f7fafc;
293
- padding: 2px 6px;
294
- border-radius: 4px;
295
- }
296
- </style>
297
- </head>
298
- <body>
299
- <h1>⚠️ Client Not Built</h1>
300
- <p>Please run <code>npm run build</code> first.</p>
301
- <p><small>Looking at: ${this.clientPath}</small></p>
302
- </body>
303
- </html>
261
+ <html>
262
+ <head>
263
+ <title>API Docs - Not Built</title>
264
+ <style>
265
+ body {
266
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
267
+ max-width: 600px;
268
+ margin: 100px auto;
269
+ padding: 20px;
270
+ text-align: center;
271
+ }
272
+ h1 { color: #e53e3e; }
273
+ code {
274
+ background: #f7fafc;
275
+ padding: 2px 6px;
276
+ border-radius: 4px;
277
+ }
278
+ </style>
279
+ </head>
280
+ <body>
281
+ <h1>⚠️ Client Not Built</h1>
282
+ <p>Please run <code>npm run build</code> first.</p>
283
+ <p><small>Looking at: ${this.clientPath}</small></p>
284
+ </body>
285
+ </html>
304
286
  `);
305
287
  }
306
288
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hasancode-api-docs",
3
- "version": "1.0.16",
3
+ "version": "1.0.17",
4
4
  "description": "A simple and easy to use API documentation generator for Express.js",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",