Skip to content

Commit 3d7f92f

Browse files
authored
Merge branch 'next' into refactor/finance/simplify-account
2 parents b39747a + edc50b6 commit 3d7f92f

17 files changed

Lines changed: 253 additions & 184 deletions

File tree

cypress/e2e/api.cy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ describe('API Test', () => {
4343

4444
cy.request({
4545
method: 'HEAD',
46-
url: `/api/${link}`,
46+
url: link,
4747
failOnStatusCode: false,
4848
})
4949
.should(({ status }) => {

docs/.vitepress/api-pages.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Run 'pnpm run generate:api-docs' to update
33
export const apiPages = [
44
{ text: 'Overview', link: '/api/' },
5+
{ text: 'Faker', link: '/api/faker.html' },
56
{ text: 'Airline', link: '/api/airline.html' },
67
{ text: 'Animal', link: '/api/animal.html' },
78
{ text: 'Color', link: '/api/color.html' },
@@ -28,4 +29,5 @@ export const apiPages = [
2829
{ text: 'System', link: '/api/system.html' },
2930
{ text: 'Vehicle', link: '/api/vehicle.html' },
3031
{ text: 'Word', link: '/api/word.html' },
32+
{ text: 'Utilities', link: '/api/utils.html' },
3133
];

docs/.vitepress/components/api-docs/method.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
export interface Method {
22
readonly name: string;
3-
readonly title: string;
43
readonly description: string; // HTML
54
readonly parameters: MethodParameter[];
65
readonly returns: string;

docs/.vitepress/components/api-docs/method.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ const props = defineProps<{ method: Method }>();
88
99
function seeAlsoToUrl(see: string): string {
1010
const [, module, method] = see.replace(/\(.*/, '').split('\.');
11+
if (!method) {
12+
return 'faker.html#' + slugify(module);
13+
}
1114
return module + '.html#' + slugify(method);
1215
}
1316
</script>

docs/api/ApiIndex.vue

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,11 @@ onUnmounted(() => window.removeEventListener('keydown', apiSearchFocusHandler));
9797
<div class="api-groups">
9898
<div v-for="item of section.items" :key="item.text" class="api-group">
9999
<h3>
100-
<a :href="item.link + '.html'">{{ item.text }}</a>
100+
<a :href="item.link">{{ item.text }}</a>
101101
</h3>
102102
<ul>
103103
<li v-for="h of item.headers" :key="h.anchor">
104-
<a :href="item.link + '.html#' + slugify(h.anchor)">{{
105-
h.text
106-
}}</a>
104+
<a :href="item.link + '#' + slugify(h.anchor)">{{ h.text }}</a>
107105
</li>
108106
</ul>
109107
</div>

scripts/apidoc.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import { faker } from '../src';
21
import { generate } from './apidoc/generate';
32
import { initMarkdownRenderer } from './apidoc/signature';
43

54
async function build(): Promise<void> {
65
await initMarkdownRenderer();
7-
faker.setDefaultRefDate(Date.UTC(2023, 0, 1));
86
await generate();
97
}
108

scripts/apidoc/apiDocsWriter.ts

Lines changed: 71 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ import { writeFileSync } from 'node:fs';
22
import { resolve } from 'node:path';
33
import type { ProjectReflection } from 'typedoc';
44
import type { Method } from '../../docs/.vitepress/components/api-docs/method';
5-
import type { APIGroup, APIItem } from '../../docs/api/api-types';
5+
import type { APIGroup } from '../../docs/api/api-types';
66
import { formatMarkdown, formatTypescript } from './format';
7+
import { extractSourceBaseUrl } from './typedoc';
8+
import type { DocsApiDiffIndex, ModuleSummary, Page } from './utils';
79
import {
8-
extractModuleName,
9-
extractSourceBaseUrl,
10-
selectApiMethods,
11-
selectApiModules,
12-
} from './typedoc';
13-
import type { DocsApiDiffIndex, PageIndex } from './utils';
14-
import { pathDocsDiffIndexFile, pathDocsDir, pathOutputDir } from './utils';
10+
diffHash,
11+
methodDiffHash,
12+
pathDocsDiffIndexFile,
13+
pathDocsDir,
14+
pathOutputDir,
15+
} from './utils';
1516

1617
const pathDocsApiPages = resolve(pathDocsDir, '.vitepress', 'api-pages.ts');
1718
const pathDocsApiSearchIndex = resolve(
@@ -29,6 +30,52 @@ editLink: false
2930
3031
`;
3132

33+
/**
34+
* Writes the api docs for the given modules.
35+
*
36+
* @param moduleName The name of the module to write the docs for.
37+
* @param lowerModuleName The lowercase name of the module.
38+
* @param comment The module comments.
39+
* @param deprecated The deprecation message.
40+
* @param methods The methods of the module.
41+
*/
42+
export function writeApiDocsModule(
43+
moduleName: string,
44+
lowerModuleName: string,
45+
comment: string,
46+
deprecated: string | undefined,
47+
methods: Method[]
48+
): ModuleSummary {
49+
writeApiDocsModulePage(
50+
moduleName,
51+
lowerModuleName,
52+
comment,
53+
deprecated,
54+
methods
55+
);
56+
writeApiDocsModuleData(lowerModuleName, methods);
57+
58+
return {
59+
text: moduleName,
60+
link: `/api/${lowerModuleName}.html`,
61+
methods,
62+
diff: methods.reduce(
63+
(data, method) => ({
64+
...data,
65+
[method.name]: methodDiffHash(method),
66+
}),
67+
{
68+
moduleHash: diffHash({
69+
name: moduleName,
70+
field: lowerModuleName,
71+
deprecated,
72+
comment,
73+
}),
74+
}
75+
),
76+
};
77+
}
78+
3279
/**
3380
* Writes the api page for the given module to the correct location.
3481
*
@@ -37,7 +84,7 @@ editLink: false
3784
* @param comment The module comments.
3885
* @param methods The methods of the module.
3986
*/
40-
export function writeApiDocsModulePage(
87+
function writeApiDocsModulePage(
4188
moduleName: string,
4289
lowerModuleName: string,
4390
comment: string,
@@ -94,7 +141,7 @@ export function writeApiDocsModulePage(
94141
* @param lowerModuleName The lowercase name of the module.
95142
* @param methods The methods data to save.
96143
*/
97-
export function writeApiDocsData(
144+
function writeApiDocsModuleData(
98145
lowerModuleName: string,
99146
methods: Method[]
100147
): void {
@@ -116,10 +163,9 @@ export function writeApiDocsData(
116163
*
117164
* @param pages The pages to write into the index.
118165
*/
119-
export function writeApiPagesIndex(pages: PageIndex): void {
166+
export function writeApiPagesIndex(pages: Page[]): void {
120167
// Write api-pages.ts
121168
console.log('Updating api-pages.ts');
122-
pages.sort((a, b) => a.text.localeCompare(b.text));
123169
pages.splice(0, 0, { text: 'Overview', link: '/api/' });
124170
let apiPagesContent = `
125171
// This file is automatically generated.
@@ -146,33 +192,20 @@ export function writeApiDiffIndex(diffIndex: DocsApiDiffIndex): void {
146192
*
147193
* @param project The typedoc project.
148194
*/
149-
export function writeApiSearchIndex(project: ProjectReflection): void {
150-
const apiIndex: APIGroup[] = [];
151-
152-
const moduleApiSection: APIGroup = {
153-
text: 'Module API',
154-
items: [],
155-
};
156-
157-
apiIndex.push(moduleApiSection);
158-
159-
const apiModules = selectApiModules(project);
160-
161-
moduleApiSection.items = apiModules
162-
.map((module) => {
163-
const moduleName = extractModuleName(module);
164-
const apiSection: APIItem = {
165-
text: moduleName,
166-
link: moduleName.toLowerCase(),
167-
headers: selectApiMethods(module).map((child) => ({
168-
anchor: child.name,
169-
text: child.name,
195+
export function writeApiSearchIndex(pages: ModuleSummary[]): void {
196+
const apiIndex: APIGroup[] = [
197+
{
198+
text: 'Module API',
199+
items: pages.map((module) => ({
200+
text: module.text,
201+
link: module.link,
202+
headers: module.methods.map((method) => ({
203+
anchor: method.name,
204+
text: method.name,
170205
})),
171-
};
172-
173-
return apiSection;
174-
})
175-
.sort((a, b) => a.text.localeCompare(b.text));
206+
})),
207+
},
208+
];
176209

177210
writeFileSync(pathDocsApiSearchIndex, JSON.stringify(apiIndex));
178211
}

scripts/apidoc/fakerClass.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import type { DeclarationReflection, ProjectReflection } from 'typedoc';
2+
import { ReflectionKind } from 'typedoc';
3+
import type { Method } from '../../docs/.vitepress/components/api-docs/method';
4+
import { writeApiDocsModule } from './apiDocsWriter';
5+
import { processModuleMethods } from './moduleMethods';
6+
import { analyzeSignature, toBlock } from './signature';
7+
import { selectApiSignature } from './typedoc';
8+
import type { ModuleSummary } from './utils';
9+
10+
export function processFakerClass(project: ProjectReflection): ModuleSummary {
11+
const fakerClass = project
12+
.getChildrenByKind(ReflectionKind.Class)
13+
.filter((clazz) => clazz.name === 'Faker')[0];
14+
15+
if (!fakerClass) {
16+
throw new Error('Faker class not found');
17+
}
18+
19+
return processClass(fakerClass);
20+
}
21+
22+
function processClass(fakerClass: DeclarationReflection): ModuleSummary {
23+
console.log(`Processing Faker class`);
24+
const comment = toBlock(fakerClass.comment);
25+
const methods: Method[] = [];
26+
27+
console.debug(`- constructor`);
28+
methods.push(processConstructor(fakerClass));
29+
30+
methods.push(...processModuleMethods(fakerClass, 'faker.'));
31+
32+
return writeApiDocsModule('Faker', 'faker', comment, undefined, methods);
33+
}
34+
35+
function processConstructor(fakerClass: DeclarationReflection): Method {
36+
const constructor = fakerClass.getChildrenByKind(
37+
ReflectionKind.Constructor
38+
)[0];
39+
40+
const signature = selectApiSignature(constructor);
41+
42+
const method = analyzeSignature(signature, '', 'new Faker');
43+
44+
return {
45+
...method,
46+
name: 'constructor',
47+
};
48+
}

scripts/apidoc/fakerUtilities.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import type { DeclarationReflection, ProjectReflection } from 'typedoc';
2+
import { ReflectionKind } from 'typedoc';
3+
import type { Method } from '../../docs/.vitepress/components/api-docs/method';
4+
import { writeApiDocsModule } from './apiDocsWriter';
5+
import { processMethods } from './moduleMethods';
6+
import { selectApiSignature } from './typedoc';
7+
import type { ModuleSummary } from './utils';
8+
9+
export function processFakerUtilities(
10+
project: ProjectReflection
11+
): ModuleSummary {
12+
const fakerUtilities = project
13+
.getChildrenByKind(ReflectionKind.Function)
14+
.filter((method) => !method.flags.isPrivate);
15+
16+
return processUtilities(fakerUtilities);
17+
}
18+
19+
function processUtilities(
20+
fakerUtilities: DeclarationReflection[]
21+
): ModuleSummary {
22+
console.log(`Processing Faker Utilities`);
23+
const comment = 'A list of all the utilities available in Faker.js.';
24+
25+
const methods: Method[] = processMethods(
26+
Object.fromEntries(
27+
fakerUtilities.map((method) => [method.name, selectApiSignature(method)])
28+
)
29+
);
30+
31+
return writeApiDocsModule('Utilities', 'utils', comment, undefined, methods);
32+
}

scripts/apidoc/generate.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import {
55
writeApiSearchIndex,
66
writeSourceBaseUrl,
77
} from './apiDocsWriter';
8-
import { processModuleMethods } from './moduleMethods';
8+
import { processFakerClass } from './fakerClass';
9+
import { processFakerUtilities } from './fakerUtilities';
10+
import { processModules } from './moduleMethods';
911
import { loadProject } from './typedoc';
1012
import { pathOutputDir } from './utils';
1113

@@ -20,12 +22,16 @@ export async function generate(): Promise<void> {
2022
// Useful for manually analyzing the content
2123
await app.generateJson(project, pathOutputJson);
2224

23-
const modules = processModuleMethods(project);
24-
writeApiPagesIndex(modules.map(({ text, link }) => ({ text, link })));
25+
const pages = [
26+
processFakerClass(project),
27+
...processModules(project).sort((a, b) => a.text.localeCompare(b.text)),
28+
processFakerUtilities(project),
29+
];
30+
writeApiPagesIndex(pages.map(({ text, link }) => ({ text, link })));
2531
writeApiDiffIndex(
26-
modules.reduce((data, { text, diff }) => ({ ...data, [text]: diff }), {})
32+
pages.reduce((data, { text, diff }) => ({ ...data, [text]: diff }), {})
2733
);
34+
writeApiSearchIndex(pages);
2835

29-
writeApiSearchIndex(project);
3036
writeSourceBaseUrl(project);
3137
}

0 commit comments

Comments
 (0)