This commit is contained in:
stuzer05
2025-01-26 21:51:32 +02:00
parent 2a5446fc02
commit ef119b4085
+40 -3
View File
@@ -9,17 +9,54 @@ interface ResponseCollections {
members: never[]; // Assuming members can be of any type, adjust as necessary
name: string;
ownerId: number;
parent: null | never; // Assuming parent can be of any type or null, adjust as necessary
parent: null | {
id: number;
name: string;
};
parentId: null | number; // Assuming parentId can be null or a number
updatedAt: string;
}
function buildFullPath(
collection: ResponseCollections,
collectionsMap: Map<number, ResponseCollections>
): string {
const paths: string[] = [collection.name];
let currentParent = collection.parent;
while (currentParent) {
paths.unshift(currentParent.name);
const parentCollection = collectionsMap.get(currentParent.id);
currentParent = parentCollection?.parent || null;
}
return paths.join(' > ');
}
export async function getCollections(baseUrl: string, apiKey: string) {
const url = `${baseUrl}/api/v1/collections`;
return await axios.get<{ response: ResponseCollections[] }>(url, {
const response = await axios.get<{ response: ResponseCollections[] }>(url, {
headers: {
Authorization: `Bearer ${apiKey}`,
},
});
}
// Create a map for quick lookups
const collectionsMap = new Map(
response.data.response.map(collection => [collection.id, collection])
);
// Format the collection names with full parent structure
const formattedCollections = response.data.response.map(collection => ({
...collection,
name: buildFullPath(collection, collectionsMap)
}));
return {
...response,
data: {
response: formattedCollections
}
};
}