feat: endpoint to retriveve memeber info

This commit is contained in:
iCrawl
2023-04-09 14:21:45 +02:00
parent 79123fb260
commit 6412da4921
4 changed files with 85 additions and 39 deletions

View File

@@ -12,8 +12,9 @@ import type {
ApiPropertySignature,
ApiTypeAlias,
ApiVariable,
ApiFunction,
} from '@microsoft/api-extractor-model';
import { ApiItemKind, ApiModel, ApiFunction } from '@microsoft/api-extractor-model';
import { ApiItemKind, ApiModel } from '@microsoft/api-extractor-model';
import { notFound } from 'next/navigation';
import type { Metadata } from 'next/types';
import { fetchModelJSON } from '~/app/docAPI';
@@ -23,14 +24,10 @@ import { TypeAlias } from '~/components/model/TypeAlias';
import { Variable } from '~/components/model/Variable';
import { Enum } from '~/components/model/enum/Enum';
import { Function } from '~/components/model/function/Function';
import { OVERLOAD_SEPARATOR, PACKAGES } from '~/util/constants';
import { findMember, findMemberByKey } from '~/util/model';
export interface ItemRouteParams {
item: string;
package: string;
version: string;
}
import { OVERLOAD_SEPARATOR } from '~/util/constants';
import type { ItemRouteParams } from '~/util/fetchMember';
import { fetchMember } from '~/util/fetchMember';
import { findMember } from '~/util/model';
async function fetchHeadMember({ package: packageName, version, item }: ItemRouteParams): Promise<ApiItem | undefined> {
const modelJSON = await fetchModelJSON(packageName, version);
@@ -124,35 +121,6 @@ export async function generateStaticParams({ params: { package: packageName, ver
}));
}
async function fetchMember({ package: packageName, version: branchName = 'main', item }: ItemRouteParams) {
if (!PACKAGES.includes(packageName)) {
notFound();
}
const model = new ApiModel();
if (branchName === 'main') {
const modelJSONFiles = await Promise.all(PACKAGES.map(async (pkg) => fetchModelJSON(pkg, branchName)));
for (const modelJSONFile of modelJSONFiles) {
addPackageToModel(model, modelJSONFile);
}
} else {
const modelJSON = await fetchModelJSON(packageName, branchName);
addPackageToModel(model, modelJSON);
}
const [memberName, overloadIndex] = decodeURIComponent(item).split(OVERLOAD_SEPARATOR);
// eslint-disable-next-line prefer-const
let { containerKey, displayName: name } = findMember(model, packageName, memberName) ?? {};
if (name && overloadIndex && !Number.isNaN(Number.parseInt(overloadIndex, 10))) {
containerKey = ApiFunction.getContainerKey(name, Number.parseInt(overloadIndex, 10));
}
return memberName && containerKey ? findMemberByKey(model, packageName, containerKey) ?? null : null;
}
function Member({ member }: { member?: ApiItem }) {
switch (member?.kind) {
case 'Class':