Skip to content

Commit f1e1b48

Browse files
committed
feat: Refactor api generation
1 parent 1ef0eb2 commit f1e1b48

File tree

11 files changed

+650504
-5357677
lines changed

11 files changed

+650504
-5357677
lines changed

src/components/ApiDocs/ReferencePage.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import { Fragment } from 'react';
22

33
import { FunctionReference } from './FunctionReference';
44
import { Divider, View, Flex } from '@aws-amplify/ui-react';
5-
import { API_CATEGORIES, API_SUB_CATEGORIES } from '@/data/api-categories.mjs';
5+
import { packageCategories } from '@/data/api-categories.mjs';
66
import references from '@/directory/apiReferences/amplify-js.json';
77
import { MDXHeading } from '../MDXComponents';
88

9+
const { API_CATEGORIES, API_SUB_CATEGORIES } = packageCategories['amplify-js'];
10+
911
export const ReferencePage = ({ category }) => {
1012
category = API_CATEGORIES[category] || API_SUB_CATEGORIES[category];
1113
const cat = references['categories'].find(

src/data/api-categories.mjs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
11
// mapping of api categories coming in from libraries to the associated categories in the docs
2-
export const API_CATEGORIES = {
3-
auth: 'auth',
4-
storage: 'storage'
5-
};
6-
7-
export const API_SUB_CATEGORIES = {
8-
analytics: 'analytics',
9-
'rest-api': 'api',
10-
'in-app-messaging': 'in-app-messaging'
2+
export const packageCategories = {
3+
'amplify-js': {
4+
ROOT_PACKAGE: 'aws-amplify',
5+
API_CATEGORIES: {
6+
auth: 'auth',
7+
storage: 'storage'
8+
},
9+
API_SUB_CATEGORIES: {
10+
analytics: 'analytics',
11+
'rest-api': 'api',
12+
'in-app-messaging': 'in-app-messaging'
13+
}
14+
},
15+
'amplify-data': {
16+
API_CATEGORIES: {
17+
auth: 'auth',
18+
storage: 'storage'
19+
},
20+
API_SUB_CATEGORIES: {
21+
analytics: 'analytics',
22+
'rest-api': 'api',
23+
'in-app-messaging': 'in-app-messaging'
24+
}
25+
}
1126
};

src/data/process-typedoc.mjs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
export const processReferences = (references, rootPackage) => {
2+
// build flat object for easier faster lookups
3+
const flatReferences = {};
4+
5+
const recursivelyPopulateFlatObject = (referenceObject) => {
6+
if (!referenceObject) return;
7+
if (referenceObject['id']) {
8+
const copy = recursivelyStripObject(structuredClone(referenceObject));
9+
flatReferences[referenceObject['id']] = copy;
10+
}
11+
12+
for (let key in referenceObject) {
13+
if (referenceObject.hasOwnProperty(key)) {
14+
if (Array.isArray(referenceObject[key])) {
15+
referenceObject[key].forEach((child) => {
16+
recursivelyPopulateFlatObject(child);
17+
});
18+
} else if (
19+
typeof referenceObject[key] === 'object' &&
20+
referenceObject[key] !== null
21+
) {
22+
recursivelyPopulateFlatObject(referenceObject[key]);
23+
}
24+
}
25+
}
26+
};
27+
28+
const recursivelyStripObject = (referenceObject) => {
29+
for (let key in referenceObject) {
30+
if (referenceObject.hasOwnProperty(key)) {
31+
if (Array.isArray(referenceObject[key])) {
32+
referenceObject[key] = referenceObject[key].map((child) => {
33+
return child.id || child;
34+
});
35+
} else if (
36+
typeof referenceObject[key] === 'object' &&
37+
referenceObject[key] !== null
38+
) {
39+
recursivelyStripObject(referenceObject[key]);
40+
}
41+
}
42+
}
43+
return referenceObject;
44+
};
45+
46+
const isFunctionObject = (obj) => {
47+
return obj.kind === 64 && obj.variant === 'declaration';
48+
};
49+
recursivelyPopulateFlatObject(references);
50+
51+
const rootId = Object.keys(flatReferences).find(
52+
(id) => flatReferences[id].name == rootPackage
53+
);
54+
55+
flatReferences['categories'] = flatReferences[rootId].children.map(
56+
(catId) => {
57+
const cat = structuredClone(flatReferences[catId]);
58+
if (cat.children && Array.isArray(cat.children)) {
59+
cat.children = cat.children
60+
.map((childId) => flatReferences[childId])
61+
.filter((child) => {
62+
return isFunctionObject(child);
63+
});
64+
}
65+
return cat;
66+
}
67+
);
68+
69+
return flatReferences;
70+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"categories": []
3+
}

0 commit comments

Comments
 (0)