Skip to content

Commit d097bef

Browse files
committed
fix(web): correct UnifiedIndexEntry type usage in openalex-data-plugin
- Add ExtendedIndexEntry interface to extend UnifiedIndexEntry with metadata - Fix indexEntryToUnified to return ExtendedIndexEntry instead of invalid type - Update function signatures to use Record<string, ExtendedIndexEntry> - Remove invalid $ref property from UnifiedIndexEntry usage - Fixes type errors: property 'lastModified' and 'contentHash' did not exist on UnifiedIndexEntry
1 parent 616f8d0 commit d097bef

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

apps/web/src/build-plugins/openalex-data-plugin.ts

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,27 @@ interface UnifiedIndex {
6464
[key: string]: UnifiedIndexEntry;
6565
}
6666

67+
// Extended index entry with metadata for build plugin
68+
interface ExtendedIndexEntry extends UnifiedIndexEntry {
69+
lastModified?: string;
70+
contentHash?: string;
71+
}
72+
6773
// Zod schemas for type validation
6874
const IndexEntrySchema = z.object({
6975
lastModified: z.string().optional(),
7076
contentHash: z.string().optional(),
7177
});
7278

73-
// Helper function to convert IndexEntry to UnifiedIndexEntry
74-
const indexEntryToUnified = (indexEntry: IndexEntry, $ref?: string): UnifiedIndexEntry => ({
75-
$ref: $ref || "",
76-
lastModified: indexEntry.lastModified || "",
77-
contentHash: indexEntry.contentHash || "",
78-
});
79+
// Helper function to convert IndexEntry to ExtendedIndexEntry
80+
const indexEntryToUnified = (indexEntry: IndexEntry, $ref?: string): ExtendedIndexEntry => {
81+
const result: ExtendedIndexEntry = {
82+
type: "file",
83+
lastModified: indexEntry.lastModified,
84+
contentHash: indexEntry.contentHash,
85+
};
86+
return result;
87+
};
7988

8089
const QueryParamsSchema = z.record(z.string(), z.unknown());
8190

@@ -680,7 +689,7 @@ export const openalexDataPlugin = (): Plugin => ({
680689
});
681690

682691
// Create new index without removed keys and with updated redirected keys
683-
const updatedIndex: UnifiedIndex = {};
692+
const updatedIndex: Record<string, ExtendedIndexEntry> = {};
684693

685694
for (const [key, metadata] of Object.entries(index)) {
686695
// Skip keys marked for removal
@@ -695,15 +704,7 @@ export const openalexDataPlugin = (): Plugin => ({
695704
if (redirectUpdate) {
696705
// Use new key with updated metadata
697706
const fixedMetadata = { ...redirectUpdate.metadata };
698-
// Fix $ref if it exists and needs updating
699-
let $ref: string | undefined;
700-
if (
701-
"$ref" in fixedMetadata &&
702-
typeof fixedMetadata.$ref === "string"
703-
) {
704-
$ref = `./${urlToEncodedKey(redirectUpdate.newKey)}.json`;
705-
}
706-
updatedIndex[redirectUpdate.newKey] = indexEntryToUnified(fixedMetadata, $ref);
707+
updatedIndex[redirectUpdate.newKey] = indexEntryToUnified(fixedMetadata);
707708
} else {
708709
// Keep existing key
709710
updatedIndex[key] = indexEntryToUnified(metadata);
@@ -766,7 +767,7 @@ export const openalexDataPlugin = (): Plugin => ({
766767
* @param dataPath
767768
* @param entityType
768769
*/
769-
const loadUnifiedIndex = async (dataPath: string, entityType: string): Promise<UnifiedIndex> => {
770+
const loadUnifiedIndex = async (dataPath: string, entityType: string): Promise<Record<string, ExtendedIndexEntry>> => {
770771
const indexPath = join(dataPath, entityType, "index.json");
771772

772773
try {
@@ -777,7 +778,7 @@ const loadUnifiedIndex = async (dataPath: string, entityType: string): Promise<U
777778
const requestsWrapper = RequestsWrapperSchema.safeParse(parsed);
778779
if (requestsWrapper.success) {
779780
// Clean and normalize existing unified format from requests
780-
const cleaned: UnifiedIndex = {};
781+
const cleaned: Record<string, ExtendedIndexEntry> = {};
781782
for (const [key, entry] of Object.entries(
782783
requestsWrapper.data.requests,
783784
)) {
@@ -810,10 +811,10 @@ const loadUnifiedIndex = async (dataPath: string, entityType: string): Promise<U
810811
cleanEntry.lastModified >
811812
(cleaned[canonicalKey].lastModified ?? ""))
812813
) {
813-
cleaned[canonicalKey] = indexEntryToUnified(cleanEntry, `./${urlToEncodedKey(key)}.json`);
814+
cleaned[canonicalKey] = indexEntryToUnified(cleanEntry);
814815
}
815816
} else {
816-
cleaned[canonicalKey] = indexEntryToUnified(cleanEntry, `./${urlToEncodedKey(key)}.json`);
817+
cleaned[canonicalKey] = indexEntryToUnified(cleanEntry);
817818
}
818819
}
819820
// Skip query entries - they will be handled by the separate query index
@@ -829,7 +830,7 @@ const loadUnifiedIndex = async (dataPath: string, entityType: string): Promise<U
829830
"Converting flat index format to requests wrapper format",
830831
);
831832
// Clean and normalize existing unified format from flat structure
832-
const cleaned: UnifiedIndex = {};
833+
const cleaned: Record<string, ExtendedIndexEntry> = {};
833834
for (const [key, entry] of Object.entries(flatIndex.data)) {
834835
// Parse the key and get its canonical form
835836
const parsedKey = parseIndexKey(key);
@@ -855,10 +856,10 @@ const loadUnifiedIndex = async (dataPath: string, entityType: string): Promise<U
855856
cleanEntry.lastModified >
856857
(cleaned[canonicalKey].lastModified ?? ""))
857858
) {
858-
cleaned[canonicalKey] = indexEntryToUnified(cleanEntry, `./${urlToEncodedKey(key)}.json`);
859+
cleaned[canonicalKey] = indexEntryToUnified(cleanEntry);
859860
}
860861
} else {
861-
cleaned[canonicalKey] = indexEntryToUnified(cleanEntry, `./${urlToEncodedKey(key)}.json`);
862+
cleaned[canonicalKey] = indexEntryToUnified(cleanEntry);
862863
}
863864
}
864865
}
@@ -1021,7 +1022,7 @@ const generateCanonicalQueryKeyFromEntry = (entry: unknown, entityType: string):
10211022
* @param entityType
10221023
* @param index
10231024
*/
1024-
const seedMissingData = async (dataPath: string, entityType: string, index: UnifiedIndex): Promise<{
1025+
const seedMissingData = async (dataPath: string, entityType: string, index: Record<string, ExtendedIndexEntry>): Promise<{
10251026
keysToRemove: Set<string>;
10261027
redirectUpdates: Array<{
10271028
oldKey: string;
@@ -1341,7 +1342,7 @@ const generateFilenameFromParsedKey = (parsed: ParsedKey): string | null => {
13411342
* @param entityType
13421343
* @param index
13431344
*/
1344-
const updateUnifiedIndex = async (dataPath: string, entityType: string, index: UnifiedIndex): Promise<UnifiedIndex> => {
1345+
const updateUnifiedIndex = async (dataPath: string, entityType: string, index: Record<string, ExtendedIndexEntry>): Promise<Record<string, ExtendedIndexEntry>> => {
13451346
// Scan entity files
13461347
const entityDir = join(dataPath, entityType);
13471348
try {
@@ -1401,8 +1402,8 @@ const updateUnifiedIndex = async (dataPath: string, entityType: string, index: U
14011402
// If we can't parse, assume it's an entity file
14021403
}
14031404

1404-
const metadata: UnifiedIndexEntry = {
1405-
$ref: `file://${filePath}`,
1405+
const metadata: ExtendedIndexEntry = {
1406+
type: "file",
14061407
lastModified: fileStat.mtime.toISOString(),
14071408
contentHash,
14081409
};
@@ -1786,7 +1787,7 @@ const deduplicateIndexEntries = (index: UnifiedIndex, entityType: string): Unifi
17861787
* @param entityType
17871788
* @param index
17881789
*/
1789-
const saveUnifiedIndex = async (dataPath: string, entityType: string, index: UnifiedIndex) => {
1790+
const saveUnifiedIndex = async (dataPath: string, entityType: string, index: Record<string, ExtendedIndexEntry>) => {
17901791
const indexPath = join(dataPath, entityType, "index.json");
17911792

17921793
try {

0 commit comments

Comments
 (0)