1
1
import { isNullOrBlank } from "../utils"
2
- import { ICollections , IIdentify , IProfile , IReleases , IReleaseSet , VinylAPIImageMap } from "./interface"
2
+ import {
3
+ ICollections ,
4
+ IIdentify ,
5
+ IProfile ,
6
+ IReleases ,
7
+ IReleaseSet ,
8
+ VinylAPIImageMap ,
9
+ VinylAPIImageRecord ,
10
+ } from "./interface"
3
11
4
12
const API_URL = "https://api.discogs.com"
5
13
@@ -36,11 +44,8 @@ export const getCollectionAndWants = async (
36
44
onProgress ?: ( page : number , pages : number ) => void
37
45
) : Promise < IReleaseSet > => {
38
46
const vinylURL = import . meta. env . VITE_VINYL_API_URL
39
-
40
- const fetchReleases = async (
41
- url : string ,
42
- releaseType : 'collection' | 'wants'
43
- ) : Promise < IReleases [ ] > => {
47
+
48
+ const fetchReleases = async ( url : string , releaseType : "collection" | "wants" ) : Promise < IReleases [ ] > => {
44
49
let allReleases : IReleases [ ] = [ ]
45
50
while ( url ) {
46
51
const response = await fetch ( url , {
@@ -66,21 +71,27 @@ export const getCollectionAndWants = async (
66
71
} ,
67
72
body : JSON . stringify (
68
73
// @ts -expect-error Cheating a bit - converting the reference to keep the same models.
69
- ( releaseType === 'wants' ? data . wants : data . releases ) . map ( ( item ) => item . basic_information . id ) ?? [ ]
74
+ ( releaseType === "wants" ? data . wants : data . releases ) . map (
75
+ // @ts -expect-error Cheating a bit - converting the reference to keep the same models.
76
+ ( item ) => item . basic_information . id
77
+ ) ?? [ ]
70
78
) ,
71
79
} )
72
80
73
81
if ( secondaryResponse . ok ) {
74
82
const imageData = await secondaryResponse . json ( )
75
83
76
- imageMap = imageData . available . reduce ( ( acc : Record < number , VinylAPIImageMap > , record : any ) => {
77
- acc [ record . recordID ] = {
78
- image : record . image ,
79
- imageHigh : record . imageHigh ,
80
- barcode : record . barcode . replace ( / \D / g, '' ) ?? undefined ,
81
- }
82
- return acc
83
- } , { } )
84
+ imageMap = imageData . available . reduce (
85
+ ( acc : Record < number , VinylAPIImageMap > , record : VinylAPIImageRecord ) => {
86
+ acc [ record . recordID ] = {
87
+ image : record . image ,
88
+ imageHigh : record . imageHigh ,
89
+ barcode : record . barcode ?. replace ( / \D / g, "" ) ?? undefined ,
90
+ }
91
+ return acc
92
+ } ,
93
+ { }
94
+ )
84
95
} else {
85
96
console . warn ( "Vinyl API response was not ok, skipping." )
86
97
}
@@ -90,7 +101,7 @@ export const getCollectionAndWants = async (
90
101
}
91
102
92
103
// @ts -expect-error Cheating a bit - converting the reference to keep the same models.
93
- const releasesExtraData = ( releaseType === ' wants' ? data . wants : data . releases ) . map ( ( release ) => {
104
+ const releasesExtraData = ( releaseType === " wants" ? data . wants : data . releases ) . map ( ( release ) => {
94
105
const imageRecord = imageMap [ release . basic_information . id ] || {
95
106
image : null ,
96
107
imageHigh : null ,
@@ -122,8 +133,8 @@ export const getCollectionAndWants = async (
122
133
123
134
// Fetch wants and collection in parallel
124
135
const [ wantsReleases , collectionReleases ] = await Promise . all ( [
125
- fetchReleases ( `${ API_URL } /users/${ username } /wants?per_page=100` , ' wants' ) ,
126
- fetchReleases ( `${ API_URL } /users/${ username } /collection/folders/0/releases?per_page=100` , ' collection' ) ,
136
+ fetchReleases ( `${ API_URL } /users/${ username } /wants?per_page=100` , " wants" ) ,
137
+ fetchReleases ( `${ API_URL } /users/${ username } /collection/folders/0/releases?per_page=100` , " collection" ) ,
127
138
] )
128
139
129
140
return {
0 commit comments