Skip to content

Commit

Permalink
fix: lexicographic sorting for AIA
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity committed Jun 10, 2024
1 parent 1c06eed commit 42762e6
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 103 deletions.
10 changes: 5 additions & 5 deletions packages/fdr-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,29 @@
"dayjs": "^1.11.11",
"fast-deep-equal": "^3.1.3",
"form-data": "4.0.0",
"formdata-node": "^6.0.3",
"js-base64": "3.7.7",
"node-fetch": "2.7.0",
"qs": "6.12.0",
"tinycolor2": "^1.6.0",
"title": "^3.5.3",
"url-join": "4.0.1",
"node-fetch": "2.7.0",
"formdata-node": "^6.0.3"
"url-join": "4.0.1"
},
"devDependencies": {
"@babel/core": "^7.18.9",
"@babel/preset-env": "^7.24.3",
"@babel/preset-typescript": "^7.24.1",
"@fern-api/eslint-config": "workspace:*",
"@fern-platform/configs": "workspace:*",
"@types/node-fetch": "2.6.9",
"@types/qs": "6.9.14",
"@types/tinycolor2": "^1.4.6",
"@types/title": "^3.4.3",
"@types/url-join": "4.0.1",
"eslint": "^8.56.0",
"prettier": "^3.2.4",
"typescript": "5.4.3",
"vitest": "^1.5.0",
"@types/node-fetch": "2.6.9"
"vitest": "^1.5.0"
},
"scripts": {
"compile": "tsc --build",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ export class ApiReferenceNavigationConverter {
baseSlug: string,
parentSlug: string,
idgen?: NodeIdGenerator,
lexicographic?: boolean,
) {
return new ApiReferenceNavigationConverter(
apiSection,
api,
baseSlug,
parentSlug,
idgen ?? new NodeIdGenerator(),
lexicographic,
).convert();
}

Expand All @@ -41,6 +43,7 @@ export class ApiReferenceNavigationConverter {
private baseSlug: string,
private apiDefinitionParentSlug: string,
idgen: NodeIdGenerator,
private lexicographic: boolean = false,
) {
this.apiDefinitionId = FernNavigation.ApiDefinitionId(api.id);
this.#holder = ApiDefinitionHolder.create(api);
Expand Down Expand Up @@ -229,7 +232,17 @@ export class ApiReferenceNavigationConverter {

this.#visitedSubpackages.add(subpackageId);

return this.mergeEndpointPairs(children);
const toRet = this.mergeEndpointPairs(children);

if (this.lexicographic) {
toRet.sort((a, b) => {
const aTitle = a.type === "endpointPair" ? a.nonStream.title : a.title;
const bTitle = b.type === "endpointPair" ? b.nonStream.title : b.title;
return aTitle.localeCompare(bTitle);
});
}

return toRet;
}

private convertApiNavigationItems(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ export class NavigationConfigConverter {
private config: DocsV1Read.NavigationConfig,
private apis: Record<string, APIV1Read.ApiDefinition>,
private basePath: string | undefined,
private lexicographic?: boolean,
) {}

public static convert(
config: DocsV1Read.NavigationConfig,
apis: Record<string, APIV1Read.ApiDefinition>,
basePath: string | undefined,
lexicographic?: boolean,
): FernNavigation.RootNode {
return new NavigationConfigConverter(config, apis, basePath).convert();
}
Expand Down Expand Up @@ -250,7 +252,14 @@ export class NavigationConfigConverter {
if (api == null) {
throw new Error(`API ${apiSection.api} not found}`);
}
return ApiReferenceNavigationConverter.convert(apiSection, api, baseSlug, parentSlug, this.#idgen);
return ApiReferenceNavigationConverter.convert(
apiSection,
api,
baseSlug,
parentSlug,
this.#idgen,
this.lexicographic,
);
},
changelog: (changelog) =>
ChangelogNavigationConverter.convert(changelog, baseSlug, parentSlug, this.#idgen),
Expand Down
4 changes: 2 additions & 2 deletions packages/fdr-sdk/src/navigation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ApiDefinitionHolder } from "./ApiDefinitionHolder";
import { NavigationConfigConverter } from "./converters/NavigationConfigConverter";
import { NodeCollector } from "./NodeCollector";

const convert = NavigationConfigConverter.convert;
// const convert = NavigationConfigConverter.convert;
const collectSlugs = NodeCollector.collect;
const createApiHolder = ApiDefinitionHolder.create;

Expand All @@ -11,4 +11,4 @@ export { ApiReferenceNavigationConverter } from "./converters/ApiReferenceNaviga
export * from "./generated/api";
export * from "./types";
export * as utils from "./utils";
export { ApiDefinitionHolder, collectSlugs, convert, createApiHolder, NavigationConfigConverter, NodeCollector };
export { ApiDefinitionHolder, collectSlugs, createApiHolder, NavigationConfigConverter, NodeCollector };
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@ export function convertLoadDocsForUrlResponse(response: DocsV2Read.LoadDocsForUr
response.definition.config.navigation,
hackReorderApis(response.definition.apis, response.baseUrl.domain),
response.baseUrl.basePath,
isLexicographicSortEnabled(response.baseUrl.domain),
);
}

function isLexicographicSortEnabled(domain: string): boolean {
// HACKHACK: This is a temporary solution to enable lexicographic sorting for AIA docs.
// Vercel's edge config UI is broken right now so we can't modify it there.
return domain.startsWith("aia.docs.buildwithfern.com");
}

function hackReorderApis(
apis: Record<string, APIV1Read.ApiDefinition>,
domain: string,
Expand Down
Loading

0 comments on commit 42762e6

Please sign in to comment.