Auf "Neues Set" reicht die Setnummer von der Packung: Name, Jahr, Teile, Minifiguren und Thema kommen aus der API, das Bild wird lokal gespeichert und nicht nur verlinkt. Hersteller und Thema werden bei Bedarf angelegt, ein Oberthema inklusive. Alle Aufrufe laufen serverseitig; der Browser spricht nie mit rebrickable.com. Ohne REBRICKABLE_API_KEY bleibt der Import sichtbar, aber abgeschaltet, mit dem Hinweis, was zu tun ist - von Hand anlegen geht unveraendert. Rebrickable liefert keine Anleitungsdateien. Statt das zu verschleiern, legt der Import einen externen Link auf die LEGO-Serviceseite zur Setnummer an. Nebenbei: src/env.ts behandelt leere Werte jetzt wie nicht gesetzt - ein "BASE_URL=" in der .env fuehrte sonst zum Start-Abbruch mit "Invalid URL" statt zum Standardwert. 14 neue Unit-Tests decken Antwortverarbeitung, Setnummern-Varianten und alle Fehlerfaelle der API ab (404, 401, 429, Netzwerk). Refs Phase 2 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
33 lines
1.1 KiB
TypeScript
33 lines
1.1 KiB
TypeScript
/**
|
|
* Set-number handling for the Lego import.
|
|
*
|
|
* Deliberately free of server-only imports so it can be unit tested and, if
|
|
* ever needed, used on the client.
|
|
*/
|
|
|
|
/**
|
|
* Rebrickable numbers a set's variant explicitly: the box says "60398", the
|
|
* API knows "60398-1". People type what is printed on the box, so the suffix
|
|
* is added when it is missing.
|
|
*/
|
|
export function normalizeSetNumber(input: string): string {
|
|
const trimmed = input.trim().toUpperCase();
|
|
return /-\d+$/.test(trimmed) ? trimmed : `${trimmed}-1`;
|
|
}
|
|
|
|
/** The number as it should be stored and shown: without the "-1" variant. */
|
|
export function displaySetNumber(setNum: string): string {
|
|
return setNum.trim().replace(/-1$/, '');
|
|
}
|
|
|
|
/**
|
|
* Lego publishes building instructions per set number. Rebrickable's API does
|
|
* not expose instruction files, so the import links to Lego's own service page
|
|
* rather than pretending to have the PDF.
|
|
*/
|
|
export function legoInstructionsUrl(setNumber: string): string {
|
|
return `https://www.lego.com/de-de/service/building-instructions/${encodeURIComponent(
|
|
displaySetNumber(setNumber),
|
|
)}`;
|
|
}
|