Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Refactor api generation #8229

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/update_references.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
run: |
git checkout -b ${{ env.BRANCH_NAME }}
curl -L -o ${{ vars.REF_LOC }} ${{ vars.REMOTE_REF }}
node tasks/clean-references.mjs
node tasks/clean-references.mjs -- -p amplify-js
git add ${{ vars.REF_LOC }} ${{ vars.CLEAN_LOC }}
git commit -m "updating references"
git push -u origin ${{ env.BRANCH_NAME }}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
"prepare": "husky install",
"analyze": "ANALYZE=true yarn next-build",
"prebuild": "node src/directory/generateDirectory.mjs && node src/directory/generateFlatDirectory.mjs",
"lint": "next lint"
"lint": "next lint",
"clean-references": "node tasks/clean-references.mjs"
}
}
2 changes: 1 addition & 1 deletion src/components/ApiDocs/FunctionReturn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { View } from '@aws-amplify/ui-react';
import { MDXHeading } from '../MDXComponents';
import { Promise } from './display/Promise';
import { ApiComment } from './ApiComment';
import references from '@/directory/apiReferences.json';
import references from '@/directory/apiReferences/amplify-js.json';
import { ParameterType } from './display';

export const FunctionReturn = ({ functionReturn, sigName }) => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/ApiDocs/FunctionSignature.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ApiComment } from './ApiComment';
import { Parameters } from './Parameters';
import { Throws } from './Throws';
import { FunctionReturn } from './FunctionReturn';
import references from '@/directory/apiReferences.json';
import references from '@/directory/apiReferences/amplify-js.json';

export const FunctionSignature = ({ sig }) => {
const sigObject = references[sig];
Expand Down
2 changes: 1 addition & 1 deletion src/components/ApiDocs/Parameters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { View } from '@aws-amplify/ui-react';
import { MDXHeading, MDXTable } from '../MDXComponents';
import { ApiComment } from './ApiComment';
import { ParameterType } from './display';
import references from '@/directory/apiReferences.json';
import references from '@/directory/apiReferences/amplify-js.json';

export const Parameters = ({ parameters, sigName }) => {
const paramObjects = parameters.map((id) => references[id]);
Expand Down
7 changes: 5 additions & 2 deletions src/components/ApiDocs/ReferencePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ import { Fragment } from 'react';

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

const { API_CATEGORIES, API_SUB_CATEGORIES } = packageCategories['amplify-js'];

export const ReferencePage = ({ category }) => {
category = API_CATEGORIES[category] || API_SUB_CATEGORIES[category];
const cat = references['categories'].find(
(catObject) => catObject.name === category
);
return (
<View className={'reference-page'}>
asd
{cat?.children?.map((child, idx) => (
<Fragment key={`reference-${idx}`}>
{idx !== 0 && <Divider marginTop={'medium'} />}
Expand Down
2 changes: 1 addition & 1 deletion src/components/ApiDocs/display/ApiModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { IconX } from '../../Icons';
import { ParameterType } from './ParameterType';
import { ApiComment } from '../ApiComment';
import { LinkDataType, TypeLinkInterface } from './TypeLink';
import references from '@/directory/apiReferences.json';
import references from '@/directory/apiReferences/amplify-js.json';
import { useRef } from 'react';

interface ApiModalInterface {
Expand Down
2 changes: 1 addition & 1 deletion src/components/ApiDocs/display/ParameterType.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { View } from '@aws-amplify/ui-react';
import { TypeLink } from './TypeLink';
import { LinkDataType } from './TypeLink';
import React from 'react';
import references from '@/directory/apiReferences.json';
import references from '@/directory/apiReferences/amplify-js.json';

interface typeDataType {
typeArguments?: LinkDataType[];
Expand Down
2 changes: 1 addition & 1 deletion src/components/ApiDocs/display/TypeLink.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useContext } from 'react';
import { TypeContext } from '@/components/ApiDocs/ApiModalProvider';
import { View } from '@aws-amplify/ui-react';
import references from '@/directory/apiReferences.json';
import references from '@/directory/apiReferences/amplify-js.json';

export interface LinkDataType {
name: string;
Expand Down
33 changes: 24 additions & 9 deletions src/data/api-categories.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
// mapping of api categories coming in from libraries to the associated categories in the docs
export const API_CATEGORIES = {
auth: 'auth',
storage: 'storage'
};

export const API_SUB_CATEGORIES = {
analytics: 'analytics',
'rest-api': 'api',
'in-app-messaging': 'in-app-messaging'
export const packageCategories = {
'amplify-js': {
ROOT_PACKAGE: 'aws-amplify',
API_CATEGORIES: {
auth: 'auth',
storage: 'storage'
},
API_SUB_CATEGORIES: {
analytics: 'analytics',
'rest-api': 'api',
'in-app-messaging': 'in-app-messaging'
}
},
'amplify-data': {
API_CATEGORIES: {
auth: 'auth',
storage: 'storage'
},
API_SUB_CATEGORIES: {
analytics: 'analytics',
'rest-api': 'api',
'in-app-messaging': 'in-app-messaging'
}
}
};
83 changes: 83 additions & 0 deletions src/data/process-typedoc.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { writeFileSync } from 'fs';

export const processReferences = (references, rootPackage) => {
// build flat object for easier faster lookups
const flatReferences = {};

const recursivelyPopulateFlatObject = (referenceObject) => {
if (!referenceObject) return;
if (referenceObject['id']) {
const copy = recursivelyStripObject(structuredClone(referenceObject));
flatReferences[referenceObject['id']] = copy;
}

for (let key in referenceObject) {
if (referenceObject.hasOwnProperty(key)) {
if (Array.isArray(referenceObject[key])) {
referenceObject[key].forEach((child) => {
recursivelyPopulateFlatObject(child);
});
} else if (
typeof referenceObject[key] === 'object' &&
referenceObject[key] !== null
) {
recursivelyPopulateFlatObject(referenceObject[key]);
}
}
}
};

const recursivelyStripObject = (referenceObject) => {
for (let key in referenceObject) {
if (referenceObject.hasOwnProperty(key)) {
if (Array.isArray(referenceObject[key])) {
referenceObject[key] = referenceObject[key].map((child) => {
return child.id || child;
});
} else if (
typeof referenceObject[key] === 'object' &&
referenceObject[key] !== null
) {
recursivelyStripObject(referenceObject[key]);
}
}
}
return referenceObject;
};

const isFunctionObject = (obj) => {
return obj.kind === 64 && obj.variant === 'declaration';
};
recursivelyPopulateFlatObject(references);

const rootId = Object.keys(flatReferences).find(
(id) => flatReferences[id].name == rootPackage
);

flatReferences['categories'] = flatReferences[rootId].children.map(
(catId) => {
const cat = structuredClone(flatReferences[catId]);
if (cat.children && Array.isArray(cat.children)) {
cat.children = cat.children
.map((childId) => flatReferences[childId])
.filter((child) => {
return isFunctionObject(child);
});
}
return cat;
}
);

try {
writeFileSync(
'./parsedJson.json',
JSON.stringify(flatReferences, null, 2),
'utf8'
);
console.log('Successfully saved parsed API information');
} catch (error) {
console.log('An error has occurred ', error);
}

return flatReferences;
};
3 changes: 3 additions & 0 deletions src/directory/apiReferences/amplify-data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"categories": []
}
74 changes: 74 additions & 0 deletions src/directory/apiReferences/amplify-js.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
"20": {
"id": 20,
"name": "analytics",
"variant": "declaration",
"kind": 2,
"flags": {}
},
"25": {
"id": 25,
"name": "api",
"variant": "declaration",
"kind": 2,
"flags": {}
},
"27": {
"id": 27,
"name": "auth",
"variant": "declaration",
"kind": 2,
"flags": {}
},
"32": {
"id": 32,
"name": "in-app-messaging",
"variant": "declaration",
"kind": 2,
"flags": {}
},
"46": {
"id": 46,
"name": "storage",
"variant": "declaration",
"kind": 2,
"flags": {}
},
"categories": [
{
"id": 27,
"name": "auth",
"variant": "declaration",
"kind": 2,
"flags": {}
},
{
"id": 46,
"name": "storage",
"variant": "declaration",
"kind": 2,
"flags": {}
},
{
"id": 20,
"name": "analytics",
"variant": "declaration",
"kind": 2,
"flags": {}
},
{
"id": 25,
"name": "api",
"variant": "declaration",
"kind": 2,
"flags": {}
},
{
"id": 32,
"name": "in-app-messaging",
"variant": "declaration",
"kind": 2,
"flags": {}
}
]
}
4 changes: 3 additions & 1 deletion src/directory/generateDirectory.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import JSON5 from 'json5';
import { directory } from './directory.mjs';
import { writeFile } from 'fs/promises';
import { getLastModifiedDate } from 'git-jiggy';
import { API_CATEGORIES, API_SUB_CATEGORIES } from '../data/api-categories.mjs';
import { packageCategories } from '../data/api-categories.mjs';

const { API_CATEGORIES, API_SUB_CATEGORIES } = packageCategories['amplify-js'];

// Set up the root path so that we can get the correct path from the current working directory
const rootPath = path.resolve(cwd(), 'src/pages');
Expand Down
Loading