Skip to content

Commit

Permalink
revert delete generateAlgoliaRecords
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity committed Dec 6, 2024
1 parent 1620205 commit e659df9
Show file tree
Hide file tree
Showing 15 changed files with 809 additions and 382,179 deletions.
1 change: 1 addition & 0 deletions packages/fdr-sdk/src/api-definition/collect-type-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { unwrapObjectType, unwrapReference } from "./unwrap";
* We collect the true shape of the path here so that the frontend can determine how to render it.
*/
export type KeyPathItem =
| { type: "meta"; value: string; displayName: string | undefined }
| { type: "objectProperty"; key: string; optional: boolean | undefined }
| {
type: "undiscriminatedUnionVariant";
Expand Down
4 changes: 4 additions & 0 deletions packages/ui/fern-docs-search-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
"types": "./dist/algolia/index.d.ts",
"default": "./dist/algolia/index.js"
},
"./archive": {
"types": "./dist/algolia/records/archive/index.d.ts",
"default": "./dist/algolia/records/archive/index.js"
},
"./tasks": {
"types": "./dist/tasks/index.d.ts",
"default": "./dist/tasks/index.js"
Expand Down

This file was deleted.

145,202 changes: 0 additions & 145,202 deletions packages/ui/fern-docs-search-server/src/algolia/__test__/__snapshots__/hume.test.ts.snap

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,185 +1,186 @@
// // import { Algolia, ApiDefinition, FernNavigation } from "@fern-api/fdr-sdk";
// import * as ApiDefinition from "@fern-api/fdr-sdk/api-definition";
// import * as FernNavigation from "@fern-api/fdr-sdk/navigation";
// import { isNonNullish } from "@fern-api/ui-core-utils";
// import { AlgoliaRecord } from "../types";
// import { generateMarkdownRecords } from "./generate-markdown-records";
// import { generateEndpointFieldRecords, generateEndpointBaseRecord } from "./generateEndpointBaseRecords2";
// import { generateWebSocketFieldRecords, generateWebSocketRecord } from "./generateWebSocketRecords";
// import { generateWebhookFieldRecords, generateWebhookRecord } from "./generateWebhookRecords";

// interface GenerateAlgoliaRecordsOptions {
// // indexSegmentId: string;
// nodes: FernNavigation.RootNode;
// pages: Record<FernNavigation.PageId, string>;
// apis: Record<ApiDefinition.ApiDefinitionId, ApiDefinition.ApiDefinition>;
// isFieldRecordsEnabled: boolean;
// }

// export function generateAlgoliaRecords({
// // indexSegmentId,
// nodes,
// pages,
// apis,
// isFieldRecordsEnabled,
// }: GenerateAlgoliaRecordsOptions): AlgoliaRecord[] {
// const collector = FernNavigation.NodeCollector.collect(nodes);

// const indexablePageNodes = collector.indexablePageSlugs
// .map((slug) => collector.slugMap.get(slug))
// .filter(isNonNullish);

// if (collector.indexablePageSlugs.length !== indexablePageNodes.length) {
// // eslint-disable-next-line no-console
// console.warn(
// `Some indexable page nodes were not found: ${collector.indexablePageSlugs.filter(
// (slug) => !collector.slugMap.has(slug),
// )}`,
// );
// }

// const markdownNodes = indexablePageNodes.filter(FernNavigation.hasMarkdown);
// const apiLeafNodes = indexablePageNodes.filter(FernNavigation.isApiLeaf);

// const records: AlgoliaRecord[] = [];

// /**
// * Markdown nodes contain pages, sections with overview content, and changelog nodes
// * TODO: we probably want to show changelog nodes as a separate entity.
// */
// markdownNodes.forEach((node) => {
// const pageId = FernNavigation.getPageId(node);
// if (!pageId) {
// // eslint-disable-next-line no-console
// console.error(`Page node ${node.slug} has no page id`);
// return;
// }
// const markdown = pages[pageId];
// if (!markdown) {
// // eslint-disable-next-line no-console
// console.error(`Page node ${node.slug} has page id ${pageId} but no markdown`);
// return;
// }

// const parents = collector.getParents(node.id) ?? [];
// const breadcrumb = FernNavigation.utils.createBreadcrumb(parents);
// const versionNode = parents.find((n): n is FernNavigation.VersionNode => n.type === "version");
// const version: Algolia.AlgoliaRecordVersionV3 | undefined = versionNode
// ? { id: versionNode.versionId, slug: FernNavigation.V1.Slug(versionNode.pointsTo ?? versionNode.slug) }
// : undefined;

// records.push(
// ...generateMarkdownRecords({
// indexSegmentId,
// breadcrumb,
// node,
// version,
// markdown,
// }),
// );
// });

// apiLeafNodes.forEach((node) => {
// const apiDefinition = apis[node.apiDefinitionId];

// if (!apiDefinition) {
// // eslint-disable-next-line no-console
// console.error(
// `API leaf node ${node.slug} has api definition id ${node.apiDefinitionId} but no api definition`,
// );
// return;
// }

// const parents = collector.getParents(node.id) ?? [];
// const breadcrumb = FernNavigation.utils.createBreadcrumb(parents);
// const versionNode = parents.find((n): n is FernNavigation.VersionNode => n.type === "version");
// const version: Algolia.AlgoliaRecordVersionV3 | undefined = versionNode
// ? { id: versionNode.versionId, slug: FernNavigation.V1.Slug(versionNode.pointsTo ?? versionNode.slug) }
// : undefined;

// if (node.type === "endpoint") {
// const endpoint = apiDefinition.endpoints[node.endpointId];
// if (!endpoint) {
// // eslint-disable-next-line no-console
// console.error(`API leaf node ${node.slug} has endpoint id ${node.endpointId} but no endpoint`);
// return;
// }

// const endpointRecord = generateEndpointBaseRecord({
// indexSegmentId,
// node,
// breadcrumb,
// endpoint,
// version,
// });

// records.push(endpointRecord);

// if (isFieldRecordsEnabled) {
// records.push(
// ...generateEndpointFieldRecords({
// endpointRecord,
// endpoint,
// types: apiDefinition.types,
// }),
// );
// }
// } else if (node.type === "webSocket") {
// const channel = apiDefinition.websockets[node.webSocketId];
// if (!channel) {
// // eslint-disable-next-line no-console
// console.error(`API leaf node ${node.slug} has web socket id ${node.webSocketId} but no web socket`);
// return;
// }

// const channelRecord = generateWebSocketRecord({
// indexSegmentId,
// node,
// breadcrumb,
// channel,
// version,
// });

// records.push(channelRecord);

// if (isFieldRecordsEnabled) {
// records.push(
// ...generateWebSocketFieldRecords({
// channelRecord,
// channel,
// types: apiDefinition.types,
// }),
// );
// }
// } else if (node.type === "webhook") {
// const webhook = apiDefinition.webhooks[node.webhookId];
// if (!webhook) {
// // eslint-disable-next-line no-console
// console.error(`API leaf node ${node.slug} has web hook id ${node.webhookId} but no web hook`);
// return;
// }

// const webhookRecord = generateWebhookRecord({
// indexSegmentId,
// node,
// breadcrumb,
// webhook,
// version,
// });

// records.push(webhookRecord);

// if (isFieldRecordsEnabled) {
// records.push(
// ...generateWebhookFieldRecords({
// webhookRecord,
// webhook,
// types: apiDefinition.types,
// }),
// );
// }
// }
// });

// return records;
// }
import { Algolia, ApiDefinition, FernNavigation } from "@fern-api/fdr-sdk";
import { isNonNullish } from "@fern-api/ui-core-utils";
import { generateEndpointFieldRecords, generateEndpointRecord } from "./generateEndpointRecords";
import { generateMarkdownRecords } from "./generateMarkdownRecords";
import { generateWebSocketFieldRecords, generateWebSocketRecord } from "./generateWebSocketRecords";
import { generateWebhookFieldRecords, generateWebhookRecord } from "./generateWebhookRecords";
import { convertEndpointV4ToV3 } from "./v1-record-converter/convertRecords";

interface GenerateAlgoliaRecordsOptions {
indexSegmentId: Algolia.IndexSegmentId;
nodes: FernNavigation.RootNode;
pages: Record<FernNavigation.PageId, string>;
apis: Record<ApiDefinition.ApiDefinitionId, ApiDefinition.ApiDefinition>;
isFieldRecordsEnabled: boolean;
}

export function generateAlgoliaRecords({
indexSegmentId,
nodes,
pages,
apis,
isFieldRecordsEnabled,
}: GenerateAlgoliaRecordsOptions): Algolia.AlgoliaRecord[] {
const collector = FernNavigation.NodeCollector.collect(nodes);

const indexablePageNodes = collector.indexablePageSlugs
.map((slug) => collector.slugMap.get(slug))
.filter(isNonNullish);

if (collector.indexablePageSlugs.length !== indexablePageNodes.length) {
// eslint-disable-next-line no-console
console.warn(
`Some indexable page nodes were not found: ${collector.indexablePageSlugs.filter(
(slug) => !collector.slugMap.has(slug),
)}`,
);
}

const markdownNodes = indexablePageNodes.filter(FernNavigation.hasMarkdown);
const apiLeafNodes = indexablePageNodes.filter(FernNavigation.isApiLeaf);

const records: Algolia.AlgoliaRecord[] = [];

/**
* Markdown nodes contain pages, sections with overview content, and changelog nodes
* TODO: we probably want to show changelog nodes as a separate entity.
*/
markdownNodes.forEach((node) => {
const pageId = FernNavigation.getPageId(node);
if (!pageId) {
// eslint-disable-next-line no-console
console.error(`Page node ${node.slug} has no page id`);
return;
}
const markdown = pages[pageId];
if (!markdown) {
// eslint-disable-next-line no-console
console.error(`Page node ${node.slug} has page id ${pageId} but no markdown`);
return;
}

const parents = collector.getParents(node.id) ?? [];
const breadcrumb = FernNavigation.utils.createBreadcrumb(parents);
const versionNode = parents.find((n): n is FernNavigation.VersionNode => n.type === "version");
const version: Algolia.AlgoliaRecordVersionV3 | undefined = versionNode
? { id: versionNode.versionId, slug: FernNavigation.V1.Slug(versionNode.pointsTo ?? versionNode.slug) }
: undefined;

records.push(
...generateMarkdownRecords({
indexSegmentId,
breadcrumb,
node,
version,
markdown,
}),
);
});

apiLeafNodes.forEach((node) => {
const apiDefinition = apis[node.apiDefinitionId];

if (!apiDefinition) {
// eslint-disable-next-line no-console
console.error(
`API leaf node ${node.slug} has api definition id ${node.apiDefinitionId} but no api definition`,
);
return;
}

const parents = collector.getParents(node.id) ?? [];
const breadcrumb = FernNavigation.utils.createBreadcrumb(parents);
const versionNode = parents.find((n): n is FernNavigation.VersionNode => n.type === "version");
const version: Algolia.AlgoliaRecordVersionV3 | undefined = versionNode
? { id: versionNode.versionId, slug: FernNavigation.V1.Slug(versionNode.pointsTo ?? versionNode.slug) }
: undefined;

if (node.type === "endpoint") {
const endpoint = apiDefinition.endpoints[node.endpointId];
if (!endpoint) {
// eslint-disable-next-line no-console
console.error(`API leaf node ${node.slug} has endpoint id ${node.endpointId} but no endpoint`);
return;
}

const endpointRecord = generateEndpointRecord({
indexSegmentId,
node,
breadcrumb,
endpoint,
version,
});

// TODO: remove this once we've migrated to v4
const endpointRecordV3 = convertEndpointV4ToV3(endpointRecord, endpoint, apiDefinition.types);

records.push(endpointRecordV3);

if (isFieldRecordsEnabled) {
records.push(
...generateEndpointFieldRecords({
endpointRecord,
endpoint,
types: apiDefinition.types,
}),
);
}
} else if (node.type === "webSocket") {
const channel = apiDefinition.websockets[node.webSocketId];
if (!channel) {
// eslint-disable-next-line no-console
console.error(`API leaf node ${node.slug} has web socket id ${node.webSocketId} but no web socket`);
return;
}

const channelRecord = generateWebSocketRecord({
indexSegmentId,
node,
breadcrumb,
channel,
version,
});

records.push(channelRecord);

if (isFieldRecordsEnabled) {
records.push(
...generateWebSocketFieldRecords({
channelRecord,
channel,
types: apiDefinition.types,
}),
);
}
} else if (node.type === "webhook") {
const webhook = apiDefinition.webhooks[node.webhookId];
if (!webhook) {
// eslint-disable-next-line no-console
console.error(`API leaf node ${node.slug} has web hook id ${node.webhookId} but no web hook`);
return;
}

const webhookRecord = generateWebhookRecord({
indexSegmentId,
node,
breadcrumb,
webhook,
version,
});

records.push(webhookRecord);

if (isFieldRecordsEnabled) {
records.push(
...generateWebhookFieldRecords({
webhookRecord,
webhook,
types: apiDefinition.types,
}),
);
}
}
});

return records;
}
Loading

0 comments on commit e659df9

Please sign in to comment.