@stainless-api/docs 0.1.0-beta.125 → 0.1.0-beta.127

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.
@@ -0,0 +1,130 @@
1
+ import type { Dirent } from 'fs';
2
+ import { describe, expect, it, vi, beforeEach } from 'vitest';
3
+ import { join } from 'path';
4
+
5
+ vi.mock('fs/promises', () => ({
6
+ readdir: vi.fn(),
7
+ readFile: vi.fn(),
8
+ }));
9
+
10
+ import { readdir, readFile } from 'fs/promises';
11
+ import { getProsePages } from './getProsePages';
12
+
13
+ const mockedReaddir = vi.mocked(readdir);
14
+ const mockedReadFile = vi.mocked(readFile);
15
+
16
+ function fakeDirent(parentPath: string, name: string, isFile = true): Dirent {
17
+ return {
18
+ name,
19
+ parentPath,
20
+ path: parentPath,
21
+ isFile: () => isFile,
22
+ isDirectory: () => !isFile,
23
+ isBlockDevice: () => false,
24
+ isCharacterDevice: () => false,
25
+ isFIFO: () => false,
26
+ isSocket: () => false,
27
+ isSymbolicLink: () => false,
28
+ } as Dirent;
29
+ }
30
+
31
+ function stubManifest(apiReferencePages: string[]) {
32
+ mockedReadFile.mockResolvedValue(JSON.stringify({ astroBase: '/', apiReferencePages }));
33
+ }
34
+
35
+ function stubReaddir(dirents: Dirent[]) {
36
+ // readdir has many overloads; cast to match the withFileTypes variant
37
+ mockedReaddir.mockResolvedValue(dirents as unknown as Awaited<ReturnType<typeof readdir>>);
38
+ }
39
+
40
+ describe('getProsePages', () => {
41
+ beforeEach(() => {
42
+ vi.resetAllMocks();
43
+ // Default: no manifest
44
+ mockedReadFile.mockRejectedValue(new Error('ENOENT'));
45
+ });
46
+
47
+ it('does not return API reference pages as prose when basePath is ""', async () => {
48
+ stubManifest(['resources/users/index.html', 'resources/users/methods/create/index.html']);
49
+ stubReaddir([
50
+ fakeDirent('/out/guides', 'intro.html'),
51
+ fakeDirent('/out/resources/users', 'index.html'),
52
+ fakeDirent('/out/resources/users/methods/create', 'index.html'),
53
+ ]);
54
+
55
+ const result = await getProsePages({ outputBasePath: '/out' });
56
+
57
+ // resources/users/** are API reference pages, not prose
58
+ expect(result).not.toContainEqual(join('/out/resources/users', 'index.html'));
59
+ expect(result).not.toContainEqual(join('/out/resources/users/methods/create', 'index.html'));
60
+ expect(result).toContainEqual(join('/out/guides', 'intro.html'));
61
+ });
62
+
63
+ it('returns all HTML files when no manifest exists', async () => {
64
+ stubReaddir([fakeDirent('/out', 'index.html'), fakeDirent('/out/guides', 'intro.html')]);
65
+
66
+ const result = await getProsePages({ outputBasePath: '/out' });
67
+
68
+ expect(result).toEqual([join('/out', 'index.html'), join('/out/guides', 'intro.html')]);
69
+ });
70
+
71
+ it('returns all HTML files when manifest has empty apiReferencePages', async () => {
72
+ stubManifest([]);
73
+ stubReaddir([fakeDirent('/out', 'index.html'), fakeDirent('/out/guides', 'intro.html')]);
74
+
75
+ const result = await getProsePages({ outputBasePath: '/out' });
76
+
77
+ expect(result).toEqual([join('/out', 'index.html'), join('/out/guides', 'intro.html')]);
78
+ });
79
+
80
+ it('excludes files listed in the manifest', async () => {
81
+ stubManifest(['api/resources/users/index.html', 'api/resources/users/methods/create/index.html']);
82
+ stubReaddir([
83
+ fakeDirent('/out', 'index.html'),
84
+ fakeDirent('/out/guides', 'intro.html'),
85
+ fakeDirent('/out/api/resources/users', 'index.html'),
86
+ fakeDirent('/out/api/resources/users/methods/create', 'index.html'),
87
+ ]);
88
+
89
+ const result = await getProsePages({ outputBasePath: '/out' });
90
+
91
+ expect(result).toEqual([join('/out', 'index.html'), join('/out/guides', 'intro.html')]);
92
+ });
93
+
94
+ it('excludes non-HTML files', async () => {
95
+ stubReaddir([
96
+ fakeDirent('/out', 'index.html'),
97
+ fakeDirent('/out', 'style.css'),
98
+ fakeDirent('/out', 'app.js'),
99
+ ]);
100
+
101
+ const result = await getProsePages({ outputBasePath: '/out' });
102
+
103
+ expect(result).toEqual([join('/out', 'index.html')]);
104
+ });
105
+
106
+ it('excludes directories', async () => {
107
+ stubReaddir([fakeDirent('/out', 'index.html'), fakeDirent('/out', 'guides', false)]);
108
+
109
+ const result = await getProsePages({ outputBasePath: '/out' });
110
+
111
+ expect(result).toEqual([join('/out', 'index.html')]);
112
+ });
113
+
114
+ it('returns empty when all HTML files are in the manifest', async () => {
115
+ stubManifest(['index.html', 'resources/users/index.html']);
116
+ stubReaddir([fakeDirent('/out', 'index.html'), fakeDirent('/out/resources/users', 'index.html')]);
117
+
118
+ const result = await getProsePages({ outputBasePath: '/out' });
119
+
120
+ expect(result).toEqual([]);
121
+ });
122
+
123
+ it('returns empty when output has no HTML files', async () => {
124
+ stubReaddir([fakeDirent('/out', 'style.css')]);
125
+
126
+ const result = await getProsePages({ outputBasePath: '/out' });
127
+
128
+ expect(result).toEqual([]);
129
+ });
130
+ });
@@ -1,6 +1,8 @@
1
- import { readdir } from 'fs/promises';
1
+ import { readdir, readFile } from 'fs/promises';
2
2
  import { join, relative } from 'path';
3
3
 
4
+ const MANIFEST_PATH = '_stainless/stl-manifest.json';
5
+
4
6
  /**
5
7
  * Get all prose pages after a build, by reading all HTML files from the
6
8
  * given output directory, and not from assets.
@@ -10,14 +12,11 @@ import { join, relative } from 'path';
10
12
  * Other astro integrations may hijack the "[...slug]" entrypoint, and any files
11
13
  * previously in the [...slug] asset map entry would be lost (this is where starlight stores
12
14
  * its prose HTML files).
15
+ *
16
+ * API reference pages are excluded using the manifest written by the API
17
+ * reference plugin at build time, rather than guessing based on path prefix.
13
18
  */
14
- export async function getProsePages({
15
- apiReferenceBasePath,
16
- outputBasePath,
17
- }: {
18
- apiReferenceBasePath: string | null;
19
- outputBasePath: string;
20
- }): Promise<string[]> {
19
+ export async function getProsePages({ outputBasePath }: { outputBasePath: string }): Promise<string[]> {
21
20
  const allFiles = await readdir(outputBasePath, {
22
21
  recursive: true,
23
22
  withFileTypes: true,
@@ -27,15 +26,22 @@ export async function getProsePages({
27
26
  .filter((file) => file.isFile() && file.name.endsWith('.html'))
28
27
  .map((file) => join(file.parentPath, file.name));
29
28
 
30
- if (!apiReferenceBasePath) return htmlFiles;
31
- // Normalize by removing leading/trailing slashes from apiReferenceBasePath
32
- const normalizedApiPath = apiReferenceBasePath.replace(/^\/+/g, '').replace(/\/+$/g, '');
33
- const pagesToRender = htmlFiles.filter((absPath) => {
29
+ const apiReferencePages = await readApiReferencePagesFromManifest(outputBasePath);
30
+ if (apiReferencePages.size === 0) return htmlFiles;
31
+
32
+ return htmlFiles.filter((absPath) => {
34
33
  const relPath = relative(outputBasePath, absPath);
35
- // Filter out API reference pages
36
- if (relPath === normalizedApiPath || relPath.startsWith(`${normalizedApiPath}/`)) return false;
37
- return true;
34
+ return !apiReferencePages.has(relPath);
38
35
  });
36
+ }
39
37
 
40
- return pagesToRender;
38
+ async function readApiReferencePagesFromManifest(outputBasePath: string): Promise<Set<string>> {
39
+ try {
40
+ const raw = await readFile(join(outputBasePath, MANIFEST_PATH), 'utf-8');
41
+ const manifest = JSON.parse(raw) as { apiReferencePages?: string[] };
42
+ const pages = manifest.apiReferencePages ?? [];
43
+ return new Set(pages);
44
+ } catch {
45
+ return new Set();
46
+ }
41
47
  }
package/stl-docs/index.ts CHANGED
@@ -303,17 +303,17 @@ export function stainlessDocs(config: StainlessDocsUserConfig): AstroIntegration
303
303
  stainlessDocsIntegration(normalizedConfig, apiReferenceBasePath),
304
304
  conditionalIntegration({
305
305
  condition: !config.experimental?.disableProseMarkdownRendering,
306
- integration: stainlessDocsMarkdownRenderer({ apiReferenceBasePath }),
306
+ integration: stainlessDocsMarkdownRenderer(),
307
307
  reason: 'disabled by experimental config "disableProseMarkdownRendering"',
308
308
  }),
309
309
  conditionalIntegration({
310
310
  condition: !config.experimental?.disableStainlessProseIndexing,
311
- integration: stainlessDocsAlgoliaProseIndexing({ apiReferenceBasePath }),
311
+ integration: stainlessDocsAlgoliaProseIndexing(),
312
312
  reason: 'disabled by experimental config "disableStainlessProseIndexing"',
313
313
  }),
314
314
  conditionalIntegration({
315
315
  condition: !config.experimental?.disableStainlessProseIndexing,
316
- integration: stainlessDocsVectorProseIndexing(normalizedConfig, apiReferenceBasePath),
316
+ integration: stainlessDocsVectorProseIndexing(normalizedConfig),
317
317
  reason: 'disabled by experimental config "disableStainlessProseIndexing"',
318
318
  }),
319
319
  ];
@@ -304,10 +304,7 @@ async function syncProseDocuments(opts: {
304
304
 
305
305
  // ─── Astro integration ──────────────────────────────────────────────
306
306
 
307
- export function stainlessDocsVectorProseIndexing(
308
- config: NormalizedStainlessDocsConfig,
309
- apiReferenceBasePath: string | null,
310
- ): AstroIntegration {
307
+ export function stainlessDocsVectorProseIndexing(config: NormalizedStainlessDocsConfig): AstroIntegration {
311
308
  return {
312
309
  name: 'stl-docs-prose-indexing',
313
310
  hooks: {
@@ -331,7 +328,7 @@ export function stainlessDocsVectorProseIndexing(
331
328
  return;
332
329
  }
333
330
 
334
- const pages = await getProsePages({ apiReferenceBasePath, outputBasePath });
331
+ const pages = await getProsePages({ outputBasePath });
335
332
  if (pages.length === 0) {
336
333
  logger.info('No prose pages found to index for vector search');
337
334
  return;
@@ -6,11 +6,7 @@ import { getSharedLogger } from '../../shared/getSharedLogger';
6
6
  import { bold } from '../../shared/terminalUtils';
7
7
  import { getProsePages } from '../../shared/getProsePages';
8
8
 
9
- export function stainlessDocsMarkdownRenderer({
10
- apiReferenceBasePath,
11
- }: {
12
- apiReferenceBasePath: string | null;
13
- }): AstroIntegration {
9
+ export function stainlessDocsMarkdownRenderer(): AstroIntegration {
14
10
  return {
15
11
  name: 'stl-docs-md',
16
12
  hooks: {
@@ -23,7 +19,7 @@ export function stainlessDocsMarkdownRenderer({
23
19
  'astro:build:done': async ({ logger: localLogger, dir }) => {
24
20
  const logger = getSharedLogger({ fallback: localLogger });
25
21
  const outputBasePath = dir.pathname;
26
- const pagesToRender = await getProsePages({ apiReferenceBasePath, outputBasePath });
22
+ const pagesToRender = await getProsePages({ outputBasePath });
27
23
 
28
24
  logger.info(bold(`Building ${pagesToRender.length} Markdown pages for prose content`));
29
25
 
@@ -174,11 +174,7 @@ export function* indexHTML(
174
174
  }
175
175
  }
176
176
 
177
- export function stainlessDocsAlgoliaProseIndexing({
178
- apiReferenceBasePath,
179
- }: {
180
- apiReferenceBasePath: string | null;
181
- }): AstroIntegration {
177
+ export function stainlessDocsAlgoliaProseIndexing(): AstroIntegration {
182
178
  return {
183
179
  name: 'stl-docs-prose-indexing',
184
180
  hooks: {
@@ -197,7 +193,7 @@ export function stainlessDocsAlgoliaProseIndexing({
197
193
  return;
198
194
  }
199
195
 
200
- const pagesToRender = await getProsePages({ apiReferenceBasePath, outputBasePath });
196
+ const pagesToRender = await getProsePages({ outputBasePath });
201
197
  logger.info(bold(`Indexing ${pagesToRender.length} prose pages for algolia search`));
202
198
 
203
199
  const objects = [];