@tscircuit/parts-engine 0.0.25 → 0.0.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,2 +1,27 @@
1
1
  # parts-engine
2
- The tscircuit platform parts engine
2
+
3
+ The tscircuit platform parts engine.
4
+
5
+ ## Supplier engines
6
+
7
+ ```ts
8
+ import {
9
+ digikeyPartsEngine,
10
+ jlcPartsEngine,
11
+ DigiKeyPartsEngine,
12
+ mouserPartsEngine,
13
+ MouserPartsEngine,
14
+ } from "@tscircuit/parts-engine"
15
+ ```
16
+
17
+ `digikeyPartsEngine.findPart(...)` queries
18
+ `https://digikeysearch.tscircuit.com`, which provides the same category route
19
+ shape as jlcsearch while caching DigiKey Product Information V4 calls. Use
20
+ `new DigiKeyPartsEngine({ platformFetch, apiBaseUrl })` to inject a platform
21
+ fetch implementation or a test/self-hosted endpoint.
22
+
23
+ `mouserPartsEngine.findPart(...)` queries
24
+ `https://mousersearch.tscircuit.com`, which exposes the same category route
25
+ shape while caching Mouser Search API calls. Use
26
+ `new MouserPartsEngine({ platformFetch, apiBaseUrl })` to inject a platform
27
+ fetch implementation or a test/self-hosted endpoint.
package/dist/index.d.ts CHANGED
@@ -4374,4 +4374,74 @@ declare const getFetchWithEasyEdaProxy: ({ platformFetch: upstreamFetch, easyEda
4374
4374
 
4375
4375
  declare const jlcPartsEngine: JlcPcbPartsEngine;
4376
4376
 
4377
- export { type EasyEdaProxyConfig, type FetchPartCircuitJsonParams, JlcPcbPartsEngine, type JlcPcbPartsEngineOptions, type PlatformFetch, cache, getFetchWithEasyEdaProxy, jlcPartsEngine };
4377
+ type DigiKeyPartsEngineOptions = {
4378
+ platformFetch?: PlatformFetch;
4379
+ apiBaseUrl?: string;
4380
+ };
4381
+ type DigiKeySearchPart = {
4382
+ digikey_product_number: string;
4383
+ supplier_part_number?: string;
4384
+ mfr: string;
4385
+ manufacturer?: string;
4386
+ package?: string;
4387
+ description?: string;
4388
+ stock: number;
4389
+ price?: number;
4390
+ normally_stocking?: boolean;
4391
+ };
4392
+
4393
+ declare class DigiKeyPartsEngine implements PartsEngine {
4394
+ private readonly platformFetch;
4395
+ private readonly apiBaseUrl;
4396
+ constructor(options?: DigiKeyPartsEngineOptions);
4397
+ private getCategoryParts;
4398
+ private getKeywordParts;
4399
+ private toSupplierPartNumbers;
4400
+ findPart({ sourceComponent, footprinterString, }: Parameters<PartsEngine["findPart"]>[0]): Promise<{}>;
4401
+ }
4402
+
4403
+ declare const digikeyCache: Map<string, unknown>;
4404
+ declare const getDigiKeyPartsCached: (path: string, params: Record<string, string | number | boolean | undefined>, options?: {
4405
+ platformFetch?: PlatformFetch;
4406
+ apiBaseUrl?: string;
4407
+ }) => Promise<Record<string, DigiKeySearchPart[]>>;
4408
+ declare const withDigiKeyStockPreference: (parts: DigiKeySearchPart[] | undefined) => DigiKeySearchPart[];
4409
+
4410
+ declare const digikeyPartsEngine: DigiKeyPartsEngine;
4411
+
4412
+ type MouserPartsEngineOptions = {
4413
+ platformFetch?: PlatformFetch;
4414
+ apiBaseUrl?: string;
4415
+ };
4416
+ type MouserSearchPart = {
4417
+ mouser_product_number: string;
4418
+ supplier_part_number?: string;
4419
+ mfr: string;
4420
+ manufacturer?: string;
4421
+ package?: string;
4422
+ description?: string;
4423
+ stock: number;
4424
+ price?: number;
4425
+ normally_stocking?: boolean;
4426
+ };
4427
+
4428
+ declare class MouserPartsEngine implements PartsEngine {
4429
+ private readonly platformFetch;
4430
+ private readonly apiBaseUrl;
4431
+ constructor(options?: MouserPartsEngineOptions);
4432
+ private getCategoryParts;
4433
+ private getKeywordParts;
4434
+ private toSupplierPartNumbers;
4435
+ findPart({ sourceComponent, footprinterString, }: Parameters<PartsEngine["findPart"]>[0]): Promise<{}>;
4436
+ }
4437
+
4438
+ declare const mouserCache: Map<string, unknown>;
4439
+ declare const getMouserPartsCached: (path: string, params: Record<string, string | number | boolean | undefined>, options?: {
4440
+ platformFetch?: PlatformFetch;
4441
+ apiBaseUrl?: string;
4442
+ }) => Promise<Record<string, MouserSearchPart[]>>;
4443
+ declare const withMouserStockPreference: (parts: MouserSearchPart[] | undefined) => MouserSearchPart[];
4444
+
4445
+ declare const mouserPartsEngine: MouserPartsEngine;
4446
+
4447
+ export { DigiKeyPartsEngine, type DigiKeyPartsEngineOptions, type DigiKeySearchPart, type EasyEdaProxyConfig, type FetchPartCircuitJsonParams, JlcPcbPartsEngine, type JlcPcbPartsEngineOptions, MouserPartsEngine, type MouserPartsEngineOptions, type MouserSearchPart, type PlatformFetch, cache, digikeyCache, digikeyPartsEngine, getDigiKeyPartsCached, getFetchWithEasyEdaProxy, getMouserPartsCached, jlcPartsEngine, mouserCache, mouserPartsEngine, withDigiKeyStockPreference, withMouserStockPreference };
package/dist/index.js CHANGED
@@ -11545,10 +11545,380 @@ var JlcPcbPartsEngine = class {
11545
11545
 
11546
11546
  // lib/jlc-parts-engine/index.ts
11547
11547
  var jlcPartsEngine = new JlcPcbPartsEngine();
11548
+
11549
+ // lib/digikey-parts-engine/digikey-parts-cache.ts
11550
+ var digikeyCache = /* @__PURE__ */ new Map();
11551
+ var normalizeBaseUrl = (baseUrl) => baseUrl.replace(/\/$/, "");
11552
+ var getDigiKeyPartsCached = async (path, params, options = {}) => {
11553
+ const platformFetch = options.platformFetch ?? globalThis.fetch;
11554
+ const baseUrl = normalizeBaseUrl(
11555
+ options.apiBaseUrl ?? "https://digikeysearch.tscircuit.com"
11556
+ );
11557
+ const url = new URL(path, `${baseUrl}/`);
11558
+ for (const [name, value] of Object.entries(params)) {
11559
+ if (value !== void 0 && value !== "") {
11560
+ url.searchParams.set(name, String(value));
11561
+ }
11562
+ }
11563
+ url.searchParams.set("json", "true");
11564
+ const cacheKey = url.toString();
11565
+ const cached = digikeyCache.get(cacheKey);
11566
+ if (cached) return cached;
11567
+ const response = await platformFetch(url);
11568
+ if (!response.ok) {
11569
+ throw new Error(
11570
+ `DigiKey search failed (${response.status}): ${await response.text()}`
11571
+ );
11572
+ }
11573
+ const responseJson = await response.json();
11574
+ digikeyCache.set(cacheKey, responseJson);
11575
+ return responseJson;
11576
+ };
11577
+ var withDigiKeyStockPreference = (parts) => [...parts ?? []].sort(
11578
+ (a, b) => Number(b.normally_stocking ?? false) - Number(a.normally_stocking ?? false) || (b.stock ?? 0) - (a.stock ?? 0)
11579
+ );
11580
+
11581
+ // lib/digikey-parts-engine/DigiKeyPartsEngine.ts
11582
+ var DigiKeyPartsEngine = class {
11583
+ platformFetch;
11584
+ apiBaseUrl;
11585
+ constructor(options = {}) {
11586
+ this.platformFetch = options.platformFetch;
11587
+ this.apiBaseUrl = options.apiBaseUrl;
11588
+ this.findPart = this.findPart.bind(this);
11589
+ }
11590
+ async getCategoryParts(path, responseKey, params) {
11591
+ const response = await getDigiKeyPartsCached(path, params, {
11592
+ platformFetch: this.platformFetch,
11593
+ apiBaseUrl: this.apiBaseUrl
11594
+ });
11595
+ return response[responseKey] ?? [];
11596
+ }
11597
+ async getKeywordParts(query) {
11598
+ const response = await getDigiKeyPartsCached(
11599
+ "/api/search",
11600
+ { q: query, limit: 20 },
11601
+ {
11602
+ platformFetch: this.platformFetch,
11603
+ apiBaseUrl: this.apiBaseUrl
11604
+ }
11605
+ );
11606
+ return response.components ?? [];
11607
+ }
11608
+ toSupplierPartNumbers(parts) {
11609
+ return {
11610
+ digikey: withDigiKeyStockPreference(parts).map((part) => part.digikey_product_number).filter(Boolean).slice(0, 3)
11611
+ };
11612
+ }
11613
+ async findPart({
11614
+ sourceComponent,
11615
+ footprinterString
11616
+ }) {
11617
+ if (sourceComponent.type !== "source_component") return {};
11618
+ const packageName = getJlcpcbPackageName(footprinterString);
11619
+ if (sourceComponent.ftype === "simple_resistor") {
11620
+ return this.toSupplierPartNumbers(
11621
+ await this.getCategoryParts("/resistors/list", "resistors", {
11622
+ resistance: sourceComponent.resistance,
11623
+ package: packageName
11624
+ })
11625
+ );
11626
+ }
11627
+ if (sourceComponent.ftype === "simple_capacitor") {
11628
+ return this.toSupplierPartNumbers(
11629
+ await this.getCategoryParts("/capacitors/list", "capacitors", {
11630
+ capacitance: sourceComponent.capacitance,
11631
+ package: packageName
11632
+ })
11633
+ );
11634
+ }
11635
+ if (sourceComponent.ftype === "simple_pin_header") {
11636
+ return this.toSupplierPartNumbers(
11637
+ await this.getCategoryParts(
11638
+ "/headers/list",
11639
+ "headers",
11640
+ getPinHeaderSearchParams(sourceComponent, footprinterString)
11641
+ )
11642
+ );
11643
+ }
11644
+ if (sourceComponent.ftype === "simple_potentiometer") {
11645
+ return this.toSupplierPartNumbers(
11646
+ await this.getCategoryParts("/potentiometers/list", "potentiometers", {
11647
+ resistance: sourceComponent.max_resistance,
11648
+ package: packageName
11649
+ })
11650
+ );
11651
+ }
11652
+ if (sourceComponent.ftype === "simple_diode") {
11653
+ return this.toSupplierPartNumbers(
11654
+ await this.getCategoryParts("/diodes/list", "diodes", {
11655
+ package: packageName
11656
+ })
11657
+ );
11658
+ }
11659
+ if (sourceComponent.ftype === "simple_transistor") {
11660
+ return this.toSupplierPartNumbers(
11661
+ await this.getCategoryParts(
11662
+ "/bjt_transistors/list",
11663
+ "bjt_transistors",
11664
+ { package: packageName }
11665
+ )
11666
+ );
11667
+ }
11668
+ if (sourceComponent.ftype === "simple_mosfet") {
11669
+ return this.toSupplierPartNumbers(
11670
+ await this.getCategoryParts("/mosfets/list", "mosfets", {
11671
+ package: packageName,
11672
+ channel_type: sourceComponent.channel_type,
11673
+ mosfet_mode: sourceComponent.mosfet_mode
11674
+ })
11675
+ );
11676
+ }
11677
+ if (sourceComponent.ftype === "simple_switch") {
11678
+ return this.toSupplierPartNumbers(
11679
+ await this.getCategoryParts("/switches/list", "switches", {
11680
+ package: packageName
11681
+ })
11682
+ );
11683
+ }
11684
+ if (sourceComponent.ftype === "simple_led") {
11685
+ return this.toSupplierPartNumbers(
11686
+ await this.getCategoryParts("/leds/list", "leds", {
11687
+ package: packageName
11688
+ })
11689
+ );
11690
+ }
11691
+ if (sourceComponent.ftype === "simple_fuse") {
11692
+ return this.toSupplierPartNumbers(
11693
+ await this.getCategoryParts("/fuses/list", "fuses", {
11694
+ package: packageName
11695
+ })
11696
+ );
11697
+ }
11698
+ if (sourceComponent.ftype === "simple_connector" && sourceComponent.standard === "usb_c") {
11699
+ return this.toSupplierPartNumbers(
11700
+ await this.getCategoryParts(
11701
+ "/usb_c_connectors/list",
11702
+ "usb_c_connectors",
11703
+ { package: packageName }
11704
+ )
11705
+ );
11706
+ }
11707
+ const keywordByFtype = {
11708
+ simple_chip: "integrated circuit",
11709
+ simple_power_source: "power supply",
11710
+ simple_inductor: "inductor",
11711
+ simple_crystal: "crystal",
11712
+ simple_resonator: "resonator"
11713
+ };
11714
+ const keyword = keywordByFtype[sourceComponent.ftype];
11715
+ if (keyword) {
11716
+ return this.toSupplierPartNumbers(
11717
+ await this.getKeywordParts(
11718
+ [keyword, packageName].filter(Boolean).join(" ")
11719
+ )
11720
+ );
11721
+ }
11722
+ return {};
11723
+ }
11724
+ };
11725
+
11726
+ // lib/digikey-parts-engine/index.ts
11727
+ var digikeyPartsEngine = new DigiKeyPartsEngine();
11728
+
11729
+ // lib/mouser-parts-engine/mouser-parts-cache.ts
11730
+ var mouserCache = /* @__PURE__ */ new Map();
11731
+ var normalizeBaseUrl2 = (baseUrl) => baseUrl.replace(/\/$/, "");
11732
+ var getMouserPartsCached = async (path, params, options = {}) => {
11733
+ const platformFetch = options.platformFetch ?? globalThis.fetch;
11734
+ const baseUrl = normalizeBaseUrl2(
11735
+ options.apiBaseUrl ?? "https://mousersearch.tscircuit.com"
11736
+ );
11737
+ const url = new URL(path, `${baseUrl}/`);
11738
+ for (const [name, value] of Object.entries(params)) {
11739
+ if (value !== void 0 && value !== "") {
11740
+ url.searchParams.set(name, String(value));
11741
+ }
11742
+ }
11743
+ url.searchParams.set("json", "true");
11744
+ const cacheKey = url.toString();
11745
+ const cached = mouserCache.get(cacheKey);
11746
+ if (cached) return cached;
11747
+ const response = await platformFetch(url);
11748
+ if (!response.ok) {
11749
+ throw new Error(
11750
+ `Mouser search failed (${response.status}): ${await response.text()}`
11751
+ );
11752
+ }
11753
+ const responseJson = await response.json();
11754
+ mouserCache.set(cacheKey, responseJson);
11755
+ return responseJson;
11756
+ };
11757
+ var withMouserStockPreference = (parts) => [...parts ?? []].sort(
11758
+ (a, b) => Number(b.normally_stocking ?? false) - Number(a.normally_stocking ?? false) || (b.stock ?? 0) - (a.stock ?? 0)
11759
+ );
11760
+
11761
+ // lib/mouser-parts-engine/MouserPartsEngine.ts
11762
+ var MouserPartsEngine = class {
11763
+ platformFetch;
11764
+ apiBaseUrl;
11765
+ constructor(options = {}) {
11766
+ this.platformFetch = options.platformFetch;
11767
+ this.apiBaseUrl = options.apiBaseUrl;
11768
+ this.findPart = this.findPart.bind(this);
11769
+ }
11770
+ async getCategoryParts(path, responseKey, params) {
11771
+ const response = await getMouserPartsCached(path, params, {
11772
+ platformFetch: this.platformFetch,
11773
+ apiBaseUrl: this.apiBaseUrl
11774
+ });
11775
+ return response[responseKey] ?? [];
11776
+ }
11777
+ async getKeywordParts(query) {
11778
+ const response = await getMouserPartsCached(
11779
+ "/api/search",
11780
+ { q: query, limit: 20 },
11781
+ {
11782
+ platformFetch: this.platformFetch,
11783
+ apiBaseUrl: this.apiBaseUrl
11784
+ }
11785
+ );
11786
+ return response.components ?? [];
11787
+ }
11788
+ toSupplierPartNumbers(parts) {
11789
+ return {
11790
+ mouser: withMouserStockPreference(parts).map((part) => part.mouser_product_number).filter(Boolean).slice(0, 3)
11791
+ };
11792
+ }
11793
+ async findPart({
11794
+ sourceComponent,
11795
+ footprinterString
11796
+ }) {
11797
+ if (sourceComponent.type !== "source_component") return {};
11798
+ const packageName = getJlcpcbPackageName(footprinterString);
11799
+ if (sourceComponent.ftype === "simple_resistor") {
11800
+ return this.toSupplierPartNumbers(
11801
+ await this.getCategoryParts("/resistors/list", "resistors", {
11802
+ resistance: sourceComponent.resistance,
11803
+ package: packageName
11804
+ })
11805
+ );
11806
+ }
11807
+ if (sourceComponent.ftype === "simple_capacitor") {
11808
+ return this.toSupplierPartNumbers(
11809
+ await this.getCategoryParts("/capacitors/list", "capacitors", {
11810
+ capacitance: sourceComponent.capacitance,
11811
+ package: packageName
11812
+ })
11813
+ );
11814
+ }
11815
+ if (sourceComponent.ftype === "simple_pin_header") {
11816
+ return this.toSupplierPartNumbers(
11817
+ await this.getCategoryParts(
11818
+ "/headers/list",
11819
+ "headers",
11820
+ getPinHeaderSearchParams(sourceComponent, footprinterString)
11821
+ )
11822
+ );
11823
+ }
11824
+ if (sourceComponent.ftype === "simple_potentiometer") {
11825
+ return this.toSupplierPartNumbers(
11826
+ await this.getCategoryParts("/potentiometers/list", "potentiometers", {
11827
+ resistance: sourceComponent.max_resistance,
11828
+ package: packageName
11829
+ })
11830
+ );
11831
+ }
11832
+ if (sourceComponent.ftype === "simple_diode") {
11833
+ return this.toSupplierPartNumbers(
11834
+ await this.getCategoryParts("/diodes/list", "diodes", {
11835
+ package: packageName
11836
+ })
11837
+ );
11838
+ }
11839
+ if (sourceComponent.ftype === "simple_transistor") {
11840
+ return this.toSupplierPartNumbers(
11841
+ await this.getCategoryParts(
11842
+ "/bjt_transistors/list",
11843
+ "bjt_transistors",
11844
+ { package: packageName }
11845
+ )
11846
+ );
11847
+ }
11848
+ if (sourceComponent.ftype === "simple_mosfet") {
11849
+ return this.toSupplierPartNumbers(
11850
+ await this.getCategoryParts("/mosfets/list", "mosfets", {
11851
+ package: packageName,
11852
+ channel_type: sourceComponent.channel_type,
11853
+ mosfet_mode: sourceComponent.mosfet_mode
11854
+ })
11855
+ );
11856
+ }
11857
+ if (sourceComponent.ftype === "simple_switch") {
11858
+ return this.toSupplierPartNumbers(
11859
+ await this.getCategoryParts("/switches/list", "switches", {
11860
+ package: packageName
11861
+ })
11862
+ );
11863
+ }
11864
+ if (sourceComponent.ftype === "simple_led") {
11865
+ return this.toSupplierPartNumbers(
11866
+ await this.getCategoryParts("/leds/list", "leds", {
11867
+ package: packageName
11868
+ })
11869
+ );
11870
+ }
11871
+ if (sourceComponent.ftype === "simple_fuse") {
11872
+ return this.toSupplierPartNumbers(
11873
+ await this.getCategoryParts("/fuses/list", "fuses", {
11874
+ package: packageName
11875
+ })
11876
+ );
11877
+ }
11878
+ if (sourceComponent.ftype === "simple_connector" && sourceComponent.standard === "usb_c") {
11879
+ return this.toSupplierPartNumbers(
11880
+ await this.getCategoryParts(
11881
+ "/usb_c_connectors/list",
11882
+ "usb_c_connectors",
11883
+ { package: packageName }
11884
+ )
11885
+ );
11886
+ }
11887
+ const keywordByFtype = {
11888
+ simple_chip: "integrated circuit",
11889
+ simple_power_source: "power supply",
11890
+ simple_inductor: "inductor",
11891
+ simple_crystal: "crystal",
11892
+ simple_resonator: "resonator"
11893
+ };
11894
+ const keyword = keywordByFtype[sourceComponent.ftype];
11895
+ if (keyword) {
11896
+ return this.toSupplierPartNumbers(
11897
+ await this.getKeywordParts(
11898
+ [keyword, packageName].filter(Boolean).join(" ")
11899
+ )
11900
+ );
11901
+ }
11902
+ return {};
11903
+ }
11904
+ };
11905
+
11906
+ // lib/mouser-parts-engine/index.ts
11907
+ var mouserPartsEngine = new MouserPartsEngine();
11548
11908
  export {
11909
+ DigiKeyPartsEngine,
11549
11910
  JlcPcbPartsEngine,
11911
+ MouserPartsEngine,
11550
11912
  cache,
11913
+ digikeyCache,
11914
+ digikeyPartsEngine,
11915
+ getDigiKeyPartsCached,
11551
11916
  getFetchWithEasyEdaProxy,
11552
- jlcPartsEngine
11917
+ getMouserPartsCached,
11918
+ jlcPartsEngine,
11919
+ mouserCache,
11920
+ mouserPartsEngine,
11921
+ withDigiKeyStockPreference,
11922
+ withMouserStockPreference
11553
11923
  };
11554
11924
  //# sourceMappingURL=index.js.map