From 48c5eaeb40cb3e358c746e4a31050cd15755b0de Mon Sep 17 00:00:00 2001 From: grabowski Date: Wed, 1 Jul 2026 13:01:05 +0700 Subject: [PATCH] Store LCSC number per part; use Key Attributes as part name Adds a nullable lcsc_number column to parts so each part records its LCSC code for reordering -- surfaced as an "LCSC #" field on the add/edit forms (autofilled by the lookup) and as a click-through link to the LCSC product page on the detail view. The autofilled Name now comes from LCSC's human-readable "Key Attributes" summary (e.g. "RES 10kohm +/-1% 100mW 0603") scraped from the product page's SSR data, falling back to the manufacturer name if that scrape returns nothing. Also normalizes lowercase LCSC codes to uppercase so "c25804" resolves instead of dead-ending on a 404. --- src/lib/server/db/schema.ts | 1 + src/lib/server/digikeyLookup.ts | 1 + src/lib/server/lcscLookup.ts | 36 +++++++++++++++++-- src/lib/server/partsLookup.ts | 1 + src/routes/(app)/parts/[id]/+page.server.ts | 1 + src/routes/(app)/parts/[id]/+page.svelte | 9 +++++ .../(app)/parts/[id]/edit/+page.server.ts | 2 ++ src/routes/(app)/parts/[id]/edit/+page.svelte | 8 +++++ src/routes/(app)/parts/new/+page.server.ts | 2 ++ src/routes/(app)/parts/new/+page.svelte | 8 +++++ 10 files changed, 66 insertions(+), 3 deletions(-) diff --git a/src/lib/server/db/schema.ts b/src/lib/server/db/schema.ts index 10ade18..c01ef60 100644 --- a/src/lib/server/db/schema.ts +++ b/src/lib/server/db/schema.ts @@ -383,6 +383,7 @@ export const parts = pgTable( name: text('name'), category: text('category').notNull(), mpn: text('mpn'), + lcscNumber: text('lcsc_number'), value: text('value'), valueNumeric: doublePrecision('value_numeric'), voltage: text('voltage'), diff --git a/src/lib/server/digikeyLookup.ts b/src/lib/server/digikeyLookup.ts index 7711851..0573cdf 100644 --- a/src/lib/server/digikeyLookup.ts +++ b/src/lib/server/digikeyLookup.ts @@ -60,6 +60,7 @@ export async function lookupDigikeyPart(pn: string): Promise { +// Scrapes LCSC's own product page for the human-readable "Key Attributes" summary. +// This reads a third-party page's internal Next.js SSR blob (not a documented API), so it may break silently — always returns null on any failure, never throws. +async function fetchKeyAttributes(code: string): Promise { + try { + const res = await fetch(`https://www.lcsc.com/product-detail/${code}.html`, { + headers: { 'User-Agent': USER_AGENT, Accept: 'text/html' }, + signal: AbortSignal.timeout(8000) + }); + if (!res.ok) return null; + + const html = await res.text(); + const match = html.match(/