discogs-marketplace-api-nodejs 1.10.1 → 1.12.0

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.
Files changed (42) hide show
  1. package/README.MD +160 -133
  2. package/dist/data/{countries.data.d.ts → country.data.d.ts} +5 -2
  3. package/dist/data/{countries.data.js → country.data.js} +6 -6
  4. package/dist/data/{currencies.data.d.ts → currency.data.d.ts} +5 -2
  5. package/dist/data/{currencies.data.js → currency.data.js} +2 -2
  6. package/dist/get-search-params.d.ts +10 -0
  7. package/dist/get-search-params.js +122 -0
  8. package/dist/index.d.ts +13 -4
  9. package/dist/index.js +7 -2
  10. package/dist/interfaces/search-params.interface.d.ts +120 -0
  11. package/dist/interfaces/search-result.interface.d.ts +118 -0
  12. package/dist/scrape.d.ts +10 -0
  13. package/dist/scrape.js +126 -0
  14. package/dist/search.d.ts +11 -0
  15. package/dist/search.js +154 -0
  16. package/dist/types/condition.type.d.ts +1 -1
  17. package/dist/types/currency.type.d.ts +1 -1
  18. package/dist/types/format-description.type.d.ts +1 -1
  19. package/dist/types/format.type.d.ts +1 -1
  20. package/dist/types/from.type.d.ts +1 -1
  21. package/dist/types/genre.type.d.ts +1 -1
  22. package/dist/types/hours-range.type.d.ts +4 -0
  23. package/dist/types/lang.type.d.ts +1 -1
  24. package/dist/types/limit.type.d.ts +1 -1
  25. package/dist/types/search-type.type.d.ts +1 -1
  26. package/dist/types/sort.type.d.ts +4 -1
  27. package/dist/types/style.type.d.ts +1 -1
  28. package/package.json +19 -18
  29. package/dist/data/index.d.ts +0 -3
  30. package/dist/data/index.js +0 -10
  31. package/dist/interfaces/index.d.ts +0 -4
  32. package/dist/interfaces/input.interface.d.ts +0 -100
  33. package/dist/interfaces/output-error.interface.d.ts +0 -9
  34. package/dist/interfaces/output-success.interface.d.ts +0 -115
  35. package/dist/interfaces/output-success.interface.js +0 -2
  36. package/dist/marketplace.d.ts +0 -27
  37. package/dist/marketplace.js +0 -311
  38. package/dist/types/index.d.ts +0 -12
  39. package/dist/types/index.js +0 -2
  40. /package/dist/interfaces/{index.js → search-params.interface.js} +0 -0
  41. /package/dist/interfaces/{input.interface.js → search-result.interface.js} +0 -0
  42. /package/dist/{interfaces/output-error.interface.js → types/hours-range.type.js} +0 -0
package/README.MD CHANGED
@@ -1,212 +1,256 @@
1
- # Discogs Marketplace API NodeJS
1
+ # 🎧 Discogs Marketplace API NodeJS
2
2
 
3
3
  [![NPM Version](https://img.shields.io/npm/v/discogs-marketplace-api-nodejs?logo=npm&color=CB3837)](https://www.npmjs.com/package/discogs-marketplace-api-nodejs) ![GitHub Repo stars](https://img.shields.io/github/stars/KirianCaumes/Discogs-Marketplace-API-NodeJS?style=flat&logo=github&label=Stars&color=%23181717) [![GitHub License](https://img.shields.io/github/license/KirianCaumes/Discogs-Marketplace-API-NodeJS?logo=data%3Aimage%2Fsvg%2Bxml%3Bbase64%2CPD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iaXNvLTg4NTktMSI%2FPg0KPHN2ZyBmaWxsPSIjZmZmIiBhcmlhLWhpZGRlbj0idHJ1ZSIgaGVpZ2h0PSIxNiIgdmlld0JveD0iMCAwIDE2IDE2IiB2ZXJzaW9uPSIxLjEiIHdpZHRoPSIxNiIgZGF0YS12aWV3LWNvbXBvbmVudD0idHJ1ZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayIgPg0KICAgIDxwYXRoIGQ9Ik04Ljc1Ljc1VjJoLjk4NWMuMzA0IDAgLjYwMy4wOC44NjcuMjMxbDEuMjkuNzM2Yy4wMzguMDIyLjA4LjAzMy4xMjQuMDMzaDIuMjM0YS43NS43NSAwIDAgMSAwIDEuNWgtLjQyN2wyLjExMSA0LjY5MmEuNzUuNzUgMCAwIDEtLjE1NC44MzhsLS41My0uNTMuNTI5LjUzMS0uMDAxLjAwMi0uMDAyLjAwMi0uMDA2LjAwNi0uMDA2LjAwNS0uMDEuMDEtLjA0NS4wNGMtLjIxLjE3Ni0uNDQxLjMyNy0uNjg2LjQ1QzE0LjU1NiAxMC43OCAxMy44OCAxMSAxMyAxMWE0LjQ5OCA0LjQ5OCAwIDAgMS0yLjAyMy0uNDU0IDMuNTQ0IDMuNTQ0IDAgMCAxLS42ODYtLjQ1bC0uMDQ1LS4wNC0uMDE2LS4wMTUtLjAwNi0uMDA2LS4wMDQtLjAwNHYtLjAwMWEuNzUuNzUgMCAwIDEtLjE1NC0uODM4TDEyLjE3OCA0LjVoLS4xNjJjLS4zMDUgMC0uNjA0LS4wNzktLjg2OC0uMjMxbC0xLjI5LS43MzZhLjI0NS4yNDUgMCAwIDAtLjEyNC0uMDMzSDguNzVWMTNoMi41YS43NS43NSAwIDAgMSAwIDEuNWgtNi41YS43NS43NSAwIDAgMSAwLTEuNWgyLjVWMy41aC0uOTg0YS4yNDUuMjQ1IDAgMCAwLS4xMjQuMDMzbC0xLjI4OS43MzdjLS4yNjUuMTUtLjU2NC4yMy0uODY5LjIzaC0uMTYybDIuMTEyIDQuNjkyYS43NS43NSAwIDAgMS0uMTU0LjgzOGwtLjUzLS41My41MjkuNTMxLS4wMDEuMDAyLS4wMDIuMDAyLS4wMDYuMDA2LS4wMTYuMDE1LS4wNDUuMDRjLS4yMS4xNzYtLjQ0MS4zMjctLjY4Ni40NUM0LjU1NiAxMC43OCAzLjg4IDExIDMgMTFhNC40OTggNC40OTggMCAwIDEtMi4wMjMtLjQ1NCAzLjU0NCAzLjU0NCAwIDAgMS0uNjg2LS40NWwtLjA0NS0uMDQtLjAxNi0uMDE1LS4wMDYtLjAwNi0uMDA0LS4wMDR2LS4wMDFhLjc1Ljc1IDAgMCAxLS4xNTQtLjgzOEwyLjE3OCA0LjVIMS43NWEuNzUuNzUgMCAwIDEgMC0xLjVoMi4yMzRhLjI0OS4yNDkgMCAwIDAgLjEyNS0uMDMzbDEuMjg4LS43MzdjLjI2NS0uMTUuNTY0LS4yMy44NjktLjIzaC45ODRWLjc1YS43NS43NSAwIDAgMSAxLjUgMFptMi45NDUgOC40NzdjLjI4NS4xMzUuNzE4LjI3MyAxLjMwNS4yNzNzMS4wMi0uMTM4IDEuMzA1LS4yNzNMMTMgNi4zMjdabS0xMCAwYy4yODUuMTM1LjcxOC4yNzMgMS4zMDUuMjczczEuMDItLjEzOCAxLjMwNS0uMjczTDMgNi4zMjdaIj48L3BhdGg%2BDQo8L3N2Zz4%3D&logoColor=fff&label=License)](https://github.com/KirianCaumes/Discogs-Marketplace-API-NodeJS/blob/master/LICENSE) [![Support this project](https://img.shields.io/badge/Support%20this%20project-003087?logo=PayPal&logoColor=fff)](https://www.paypal.me/KirianCaumes)
4
4
 
5
- Another (better ?) NodeJs library to fetch data from Discogs marketplace. 💿
5
+ A modern TypeScript/Node.js client for scraping and retrieving Discogs Marketplace listings — flexible, type safe, and easy to use. 💿
6
+ > 💪 No Discogs API token required — this client scrapes public Marketplace data.
6
7
 
7
- ## 📂 Installation (with npm)
8
-
9
- Run the following command in your project:
8
+ ## 🚀 Installation
10
9
 
11
10
  ```sh
12
- npm i discogs-marketplace-api-nodejs
11
+ npm install discogs-marketplace-api-nodejs
13
12
  ```
14
13
 
15
- ## ▶️ Quick Start
16
-
17
- Import `DiscogsMarketplace` into your project:
14
+ ## ⚡ Quick Start
18
15
 
19
- ```js
20
- // With ECMAScript 6 and +
16
+ ```ts
21
17
  import { DiscogsMarketplace } from 'discogs-marketplace-api-nodejs'
22
18
 
23
- // With CommonJS
24
- const DiscogsMarketplace = require('discogs-marketplace-api-nodejs').DiscogsMarketplace
19
+ const result = await DiscogsMarketplace.search({
20
+ searchType: 'artist_id',
21
+ searchValue: 244819,
22
+ genre: 'Rock',
23
+ sort: 'price,asc',
24
+ limit: 50,
25
+ page: 1,
26
+ })
27
+
28
+ console.log(result.items)
25
29
  ```
26
30
 
27
- ## 🤔 Examples
31
+ ## 📚 Usage Examples
28
32
 
29
- - Get top 250 items, of the 2nd page, filtered by Rock, listed by newest, for a given artist
33
+ ### 1. Get 250 latest listings by a specific artist in the Rock genre, page 2
30
34
 
31
- ```js
35
+ ```ts
32
36
  // https://www.discogs.com/sell/list?sort=listed,desc&limit=250&artist_id=244819&page=2&genre=Rock
33
37
  const result = await DiscogsMarketplace.search({
34
- limit: 250,
35
- page: 2,
36
- genre: 'Rock',
37
- sort: 'Listed Newest',
38
38
  searchType: 'artist_id',
39
39
  searchValue: 244819,
40
+ genre: 'Rock',
41
+ sort: 'listed,desc',
42
+ limit: 250,
43
+ page: 2,
40
44
  })
41
45
  ```
42
46
 
43
- - Get top 50 items, of the 1st page, listed by price lowest, on a user wantlist
47
+ ### 2. Get listings of a user's wantlist, sorted by price, limit 50
44
48
 
45
- ```js
49
+ ```ts
46
50
  // https://www.discogs.com/sell/mywants?limit=50&user=Kirian_&sort=price,asc
47
51
  const result = await DiscogsMarketplace.search({
48
52
  limit: 50,
49
- page: 1,
50
- sort: 'Price Lowest',
53
+ sort: 'price,asc',
51
54
  searchType: 'user',
52
55
  searchValue: 'Kirian_',
53
56
  })
54
57
  ```
55
58
 
56
- - Get top 25 items, of the 1st page, listed by seller A-Z, on a release
59
+ ### 3. Get listings on a release, sorted by seller name
57
60
 
58
- ```js
59
- // https://www.discogs.com/sell/release/767931?sort=seller,asc&limit=25&page=1
61
+ ```ts
62
+ // https://www.discogs.com/sell/list?release_id=767931&sort=seller,asc&limit=25
63
+ // https://www.discogs.com/sell/release/767931?sort=seller,asc&limit=25
60
64
  const result = await DiscogsMarketplace.search({
61
- sort: 'Seller A-Z',
62
- page: 1,
63
65
  searchType: 'release_id',
64
66
  searchValue: '767931',
67
+ sort: 'seller,asc',
65
68
  })
66
69
  ```
67
70
 
68
- - Get top 100 items, of the 1st page, listed by newest, on a string search
71
+ ### 4. Get listings by a general search query (e.g., "in flames"), sorted by newest listings, limit 100
69
72
 
70
- ```js
73
+ ```ts
71
74
  // https://www.discogs.com/sell/list?sort=listed,desc&limit=100&q=in+flames&page=1
72
75
  const result = await DiscogsMarketplace.search({
73
- limit: 100,
74
- page: 1,
75
- sort: 'Listed Newest',
76
76
  searchType: 'q',
77
77
  searchValue: 'in flames',
78
+ sort: 'listed,desc',
79
+ limit: 100,
78
80
  })
79
81
  ```
80
82
 
81
- - Get top 25 items, of the 1st page, listed by newest, on a seller inventory
83
+ ### 5. Get newest listings on a seller inventory
82
84
 
83
- ```js
84
- // https://www.discogs.com/seller/Kirian_/profile?sort=listed,desc&limit=25&page=1
85
+ ```ts
86
+ // https://www.discogs.com/sell/list?sort=listed,desc&seller=Kirian_
87
+ // https://www.discogs.com/seller/Kirian_/profile?sort=listed,desc
85
88
  const result = await DiscogsMarketplace.search({
86
- sort: 'Listed Newest',
87
- page: 1,
89
+ sort: 'listed,desc',
88
90
  searchType: 'q',
89
- searchValue: '',
90
91
  seller: 'Kirian_',
91
92
  })
92
93
  ```
93
94
 
94
- - Get top 25 items, of the 1st page, listed by newest, on a user wantlist, on a seller inventory
95
+ ### 6. Get newest listings of a user's wantlist, on a seller inventory
95
96
 
96
- ```js
97
- // https://www.discogs.com/seller/Kirian_/mywants?sort=listed,desc&limit=25&user=Kirian_&page=1
97
+ ```ts
98
+ // https://www.discogs.com/sell/mywants?sort=listed,desc&seller=Kirian_&user=Kirian_
99
+ // https://www.discogs.com/seller/Kirian_/mywants?sort=listed,desc&user=Kirian_
98
100
  const result = await DiscogsMarketplace.search({
99
- sort: 'Listed Newest',
100
- page: 1,
101
+ sort: 'listed,desc',
101
102
  searchType: 'user',
102
103
  searchValue: 'Kirian_',
103
104
  seller: 'Kirian_',
104
105
  })
105
106
  ```
106
107
 
107
- ## 📃 Data format
108
+ ## 💁 Additional Features
109
+
110
+ ### 1. Reuse a Playwright `Page` instance for faster scraping
111
+
112
+ ```ts
113
+ const browser = await playwright.launch()
114
+ const browserContext = await browser.newContext()
115
+ const browserPage = await browserContext.newPage()
116
+
117
+ const result = await DiscogsMarketplace.search({ searchType: 'q'}, browserPage)
118
+
119
+ await browserContext.close()
120
+ await browser.close()
121
+ ```
122
+
123
+ ### 2. Extract correct parameters from URL
108
124
 
109
- You can provide parameters to `DiscogsMarketplace.search` function according to this interface:
125
+ ```ts
126
+ const searchParams = DiscogsMarketplace.getSearchParamsFromUrl('https://www.discogs.com/fr/sell/list?sort=listed,desc&limit=250&artist_id=244819&page=2&genre=Rock')
127
+ // {
128
+ // searchType: 'artist_id',
129
+ // searchValue: '244819',
130
+ // genre: 'Rock',
131
+ // sort: 'listed,desc',
132
+ // limit: 250,
133
+ // page: 2,
134
+ // lang: 'fr',
135
+ // }
136
+ ```
137
+
138
+ ## 🧩 API Reference
139
+
140
+ The `DiscogsMarketplace.search` function accepts parameters defined by the following interface:
110
141
 
111
142
  ```ts
112
- interface InputInterface {
143
+ interface SearchParams {
113
144
  /**
114
- * Type of elements to search.
115
- * Default to `q`.
116
- * | Name | Description |
117
- * |:---------: |:--------------------------:|
118
- * | q | Basic query search |
119
- * | master_id | Search in a master release |
120
- * | release_id | Search in a release |
121
- * | label_id | Search in a label |
122
- * | artist_id | Search in a artist |
123
- * | user | Search in user wantlist |
145
+ * The type of element to search.
146
+ * | Value | Description |
147
+ * |-------------|--------------------------------|
148
+ * | `q` | Basic query search |
149
+ * | `master_id` | Search within a master release |
150
+ * | `release_id`| Search within a release |
151
+ * | `label_id` | Search within a label |
152
+ * | `artist_id` | Search by artist |
153
+ * | `user` | Search a user's wantlist |
154
+ * @default 'q'
124
155
  */
125
- searchType: SearchTypeType
156
+ searchType: Search
126
157
  /**
127
- * Value to search corresponding to searchType
158
+ * The search value corresponding to the selected `searchType`.
128
159
  */
129
160
  searchValue?: string | number
130
161
  /**
131
- * Currency
162
+ * Currency code for price filtering.
163
+ * @example 'USD'
132
164
  */
133
- currency?: CurrencyType
165
+ currency?: Currency
134
166
  /**
135
- * Genre
167
+ * Genre filter.
168
+ * @example 'Rock'
136
169
  */
137
- genre?: GenreType
170
+ genre?: Genre
138
171
  /**
139
- * Styles
172
+ * List of styles.
173
+ * @example ['Death Metal', 'Heavy Metal']
140
174
  */
141
- style?: Array<StyleType>
175
+ style?: Array<Style>
142
176
  /**
143
- * Formats
177
+ * List of formats.
178
+ * @example ['Vinyl', 'CD']
144
179
  */
145
- format?: Array<FormatType>
180
+ format?: Array<Format>
146
181
  /**
147
- * Format descriptions
182
+ * List of format descriptions.
183
+ * @example ['Limited Edition', 'Numbered']
148
184
  */
149
- formatDescription?: Array<FormatDescriptionType>
185
+ formatDescription?: Array<FormatDescription>
150
186
  /**
151
- * Media conditions
187
+ * List of media conditions.
188
+ * @example ['Mint (M)', 'Very Good Plus (VG+)']
152
189
  */
153
- condition?: Array<ConditionType>
190
+ condition?: Array<Condition>
154
191
  /**
155
- * Year (Do not use it with `years`)
192
+ * Release year.
193
+ * Do not use together with `years`.
156
194
  */
157
195
  year?: number
158
196
  /**
159
- * Interval of years (Do not use it with `year`)
197
+ * Range of years.
198
+ * Do not use together with `year`.
160
199
  */
161
200
  years?: {
162
- /** Min */
201
+ /** Minimum year */
163
202
  min: number
164
- /** Max */
203
+ /** Maximum year */
165
204
  max: number
166
205
  }
167
206
  /**
168
- * Is audio sample ?
169
- */
170
- isAudioSample?: boolean
171
- /**
172
- * Is make an offer only ?
207
+ * If true, only listings that accept offers are returned.
208
+ * @default false
173
209
  */
174
210
  isMakeAnOfferOnly?: boolean
175
211
  /**
176
- * Expedition country
212
+ * Expedition country filter (2-letter country code).
213
+ * @example 'US'
177
214
  */
178
- from?: FromType
215
+ from?: From
179
216
  /**
180
- * Seller name
217
+ * Seller's username filter.
181
218
  */
182
219
  seller?: string
183
220
  /**
184
- * Sort elements by.
185
- * Default to `Listed Newest`.
221
+ * Hours range for wantlist searches.
222
+ * @example '0-24'
223
+ */
224
+ hoursRange?: HoursRange
225
+ /**
226
+ * Sort order for the results.
227
+ * @default 'listed,desc'
186
228
  */
187
- sort?: SortType
229
+ sort?: Sort
188
230
  /**
189
- * Limit of elements to search (25 | 50 | 100 | 250).
190
- * Default to `25`.
231
+ * Number of results to return per page.
232
+ * Allowed values: 25, 50, 100, 250.
233
+ * @default 25
191
234
  */
192
- limit?: LimitType
235
+ limit?: Limit
193
236
  /**
194
- * Page (Must be < 401 or discogs will return an error 404).
195
- * Default to `1`.
237
+ * Page number of results.
238
+ * Must be less than 401 to avoid Discogs 404 errors.
239
+ * @default 1
196
240
  */
197
241
  page?: number
198
242
  /**
199
- * Lang to use for Discogs.
200
- * Default to `en`.
243
+ * Language code for Discogs interface.
244
+ * @default 'en'
201
245
  */
202
- lang?: LangType
246
+ lang?: Lang
203
247
  }
204
248
  ```
205
249
 
206
- If success, it will return:
250
+ On success, it returns a result matching this interface:
207
251
 
208
252
  ```ts
209
- interface OutputSuccessInterface {
253
+ interface SearchResult {
210
254
  items: Array<{
211
255
  id: number
212
256
  title: {
@@ -239,12 +283,15 @@ interface OutputSuccessInterface {
239
283
  notes: number
240
284
  }
241
285
  price: {
242
- base: string
243
- shipping: string
286
+ /** @example '12.34 USD' */
287
+ base: `${number} ${CurrencyValues}` | ''
288
+ /** @example '5.67 USD' */
289
+ shipping: `${number} ${CurrencyValues}` | ''
244
290
  }
245
291
  country: {
246
292
  name: string
247
- code: string
293
+ /** @example 'US' */
294
+ code: CountryValues
248
295
  }
249
296
  community: {
250
297
  have: number
@@ -263,45 +310,25 @@ interface OutputSuccessInterface {
263
310
  total: number
264
311
  perPage: number
265
312
  }
266
- search: {
267
- value: string | number
268
- type: SearchTypeType
269
- }
270
313
  urlGenerated: string
271
314
  }
272
315
  ```
273
316
 
274
- If error, it will throw:
317
+ ## 🛠️ Development & Contribution
275
318
 
276
- ```ts
277
- interface OutputErrorInterface {
278
- message: string
279
- code: number
280
- }
281
- ```
282
-
283
- ## 💡 How to contribute
284
-
285
- There is a [devcontainer](https://code.visualstudio.com/docs/devcontainers/containers) on that project already configured, feel free to use it.
286
-
287
- Install dependencies with:
288
-
289
- ```sh
290
- npm install
291
- ```
292
-
293
- You can open a pull request with your new additions.
294
-
295
- ## 🐛 Known issues/problems
319
+ - Clone the repo and use the included devcontainer/VSCode setup.
320
+ - Run: `npm install`
321
+ - Add your contribution.
322
+ - Use `npm test` to run unit tests.
323
+ - Open a pull request with your new additions! 🎉
296
324
 
297
- ### Playwright
325
+ ## 👉 Known Issues
298
326
 
299
- Install system dependencies:
300
-
301
- ```sh
302
- sudo npx playwright install-deps chromium
303
- ```
327
+ - Ensure all Playwright requirements are installed (see [Install system dependencies](https://playwright.dev/docs/browsers#install-system-dependencies)).
328
+ - Requests hitting page >400 will result in a 404.
329
+ - Some search parameters might not be compatible with every type of queries.
330
+ - For other edge cases, feel free to open an [issue on GitHub](https://github.com/KirianCaumes/Discogs-Marketplace-API-NodeJS/issues).
304
331
 
305
- More information [here](https://playwright.dev/docs/browsers#install-system-dependencies).
332
+ ## 📃 License
306
333
 
307
- 👉 If you find another problem, feel free to open an issue.
334
+ Licensed under [MIT](https://github.com/KirianCaumes/Discogs-Marketplace-API-NodeJS/blob/master/LICENSE).
@@ -1,4 +1,4 @@
1
- declare const COUNTRIES: {
1
+ declare const Country: {
2
2
  readonly Azerbaidjan: "AZ";
3
3
  readonly Bolivia: "BO";
4
4
  readonly 'Bosnia-Herzegovina': "BA";
@@ -303,4 +303,7 @@ declare const COUNTRIES: {
303
303
  readonly Zambia: "ZM";
304
304
  readonly Zimbabwe: "ZW";
305
305
  };
306
- export default COUNTRIES;
306
+ export type CountryType = typeof Country;
307
+ export type CountryKeys = keyof CountryType;
308
+ export type CountryValues = CountryType[CountryKeys];
309
+ export default Country;
@@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
5
5
  * ISO name
6
6
  * {@link https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes}
7
7
  */
8
- const COUNTRIES_ISO = {
8
+ const CountryIso = {
9
9
  Afghanistan: 'AF',
10
10
  'Åland Islands': 'AX',
11
11
  Albania: 'AL',
@@ -268,7 +268,7 @@ const COUNTRIES_ISO = {
268
268
  * .filter(x => !Object.keys(COUNTRIES_ISO).includes(x))
269
269
  * .reduce((a,b) => ({ ...a, [b]: null }), {})
270
270
  */
271
- const COUNTRIES_DISCOGS = {
271
+ const CountryDiscogs = {
272
272
  Azerbaidjan: 'AZ',
273
273
  Bolivia: 'BO',
274
274
  'Bosnia-Herzegovina': 'BA',
@@ -324,8 +324,8 @@ const COUNTRIES_DISCOGS = {
324
324
  Yugoslavia: 'GR',
325
325
  Zaire: 'CD',
326
326
  };
327
- const COUNTRIES = {
328
- ...COUNTRIES_ISO,
329
- ...COUNTRIES_DISCOGS,
327
+ const Country = {
328
+ ...CountryIso,
329
+ ...CountryDiscogs,
330
330
  };
331
- exports.default = COUNTRIES;
331
+ exports.default = Country;
@@ -1,4 +1,4 @@
1
- declare const CURRENCIES: {
1
+ declare const Currency: {
2
2
  readonly $US: "USD";
3
3
  readonly US$: "USD";
4
4
  readonly USD: "USD";
@@ -30,4 +30,7 @@ declare const CURRENCIES: {
30
30
  readonly '\u00A3': "GBP";
31
31
  readonly '\u00A5': "JPY";
32
32
  };
33
- export default CURRENCIES;
33
+ export type CurrencyType = typeof Currency;
34
+ export type CurrencyKeys = keyof CurrencyType;
35
+ export type CurrencyValues = CurrencyType[CurrencyKeys];
36
+ export default Currency;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const CURRENCIES = {
3
+ const Currency = {
4
4
  $US: 'USD',
5
5
  US$: 'USD',
6
6
  USD: 'USD',
@@ -32,4 +32,4 @@ const CURRENCIES = {
32
32
  '£': 'GBP',
33
33
  '¥': 'JPY',
34
34
  };
35
- exports.default = CURRENCIES;
35
+ exports.default = Currency;
@@ -0,0 +1,10 @@
1
+ import type SearchParams from './interfaces/search-params.interface.js';
2
+ /**
3
+ * Get search parameters from a Discogs Marketplace URL
4
+ * @param url Discogs Marketplace URL
5
+ * @returns Search parameters
6
+ * @example
7
+ * const searchParams = getSearchParamsFromUrl('https://www.discogs.com/sell/list?sort=listed,desc&limit=250&artist_id=244819&page=2&genre=Rock')
8
+ * // { searchType: 'artist_id', searchValue: '244819', genre: 'Rock', sort: 'listed,desc', limit: 250, page: 2 }
9
+ */
10
+ export default function getSearchParamsFromUrl(url: string): SearchParams;
@@ -0,0 +1,122 @@
1
+ "use strict";
2
+ /* eslint-disable no-restricted-syntax */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.default = getSearchParamsFromUrl;
5
+ /**
6
+ * Safely parse an integer from a string
7
+ * @param value String to parse
8
+ * @returns Parsed integer or undefined if parsing fails
9
+ */
10
+ const safeParseInt = (value) => {
11
+ const parsed = parseInt(value ?? '', 10);
12
+ return isNaN(parsed) ? undefined : parsed;
13
+ };
14
+ /**
15
+ * Get search parameters from a Discogs Marketplace URL
16
+ * @param url Discogs Marketplace URL
17
+ * @returns Search parameters
18
+ * @example
19
+ * const searchParams = getSearchParamsFromUrl('https://www.discogs.com/sell/list?sort=listed,desc&limit=250&artist_id=244819&page=2&genre=Rock')
20
+ * // { searchType: 'artist_id', searchValue: '244819', genre: 'Rock', sort: 'listed,desc', limit: 250, page: 2 }
21
+ */
22
+ function getSearchParamsFromUrl(url) {
23
+ const { searchParams: urlSearchParams, pathname, host } = new URL(url);
24
+ if (!host.includes('discogs.com')) {
25
+ throw new Error(`Invalid host: ${host}. Expected 'www.discogs.com' or 'discogs.com'.`);
26
+ }
27
+ const searchParams = {
28
+ searchType: 'q', // Default search type
29
+ };
30
+ // Retrieve the search type and value
31
+ const searchType = ['q', 'master_id', 'release_id', 'label_id', 'artist_id', 'user'].find(type => urlSearchParams.has(type));
32
+ if (searchType) {
33
+ searchParams.searchType = searchType;
34
+ const searchValue = urlSearchParams.get(searchType);
35
+ if (searchValue) {
36
+ searchParams.searchValue = searchValue;
37
+ }
38
+ }
39
+ // Retrieve basic search values
40
+ for (const key of ['currency', 'genre', 'ships_from', 'seller', 'sort', 'hours_range']) {
41
+ const value = urlSearchParams.get(key);
42
+ if (value) {
43
+ switch (key) {
44
+ case 'ships_from':
45
+ searchParams.from = value;
46
+ break;
47
+ case 'hours_range':
48
+ searchParams.hoursRange = value;
49
+ break;
50
+ default:
51
+ searchParams[key] = value;
52
+ break;
53
+ }
54
+ }
55
+ }
56
+ // Retrieve number search values
57
+ for (const key of ['limit', 'page']) {
58
+ const value = safeParseInt(urlSearchParams.get(key));
59
+ if (value) {
60
+ switch (key) {
61
+ case 'limit':
62
+ searchParams.limit = value;
63
+ break;
64
+ default:
65
+ searchParams[key] = value;
66
+ break;
67
+ }
68
+ }
69
+ }
70
+ // Retrieve array search values
71
+ for (const key of ['style', 'format', 'format_desc', 'condition']) {
72
+ const values = urlSearchParams.getAll(key);
73
+ if (values.length) {
74
+ switch (key) {
75
+ case 'format_desc':
76
+ searchParams.formatDescription = values;
77
+ break;
78
+ default:
79
+ searchParams[key] = values;
80
+ break;
81
+ }
82
+ }
83
+ }
84
+ // Check if search is on offers only
85
+ if (urlSearchParams.get('offers') === '1') {
86
+ searchParams.isMakeAnOfferOnly = true;
87
+ }
88
+ // Handle year and years
89
+ const year = safeParseInt(urlSearchParams.get('year'));
90
+ const year1 = safeParseInt(urlSearchParams.get('year1'));
91
+ const year2 = safeParseInt(urlSearchParams.get('year2'));
92
+ if (year) {
93
+ searchParams.year = year;
94
+ }
95
+ else if (year1 && year2) {
96
+ searchParams.years = { min: year1, max: year2 };
97
+ }
98
+ // Retrieve the language from the URL path
99
+ const pathSegments = pathname.split('/').filter(x => x);
100
+ if (/^[a-z]{2}$/i.test(pathSegments[0] ?? '')) {
101
+ searchParams.lang = pathSegments[0]?.toLowerCase();
102
+ }
103
+ // Handle specific case for release ID in the URL path
104
+ const releaseIdMatch = /\/sell\/release\/(\d+)/.exec(url);
105
+ if (releaseIdMatch?.[1]) {
106
+ searchParams.searchType = 'release_id';
107
+ searchParams.searchValue = releaseIdMatch[1];
108
+ }
109
+ // Handle specific case for seller profile in the URL path
110
+ const sellerProfileMatch = /\/seller\/([^/]+)\/profile/.exec(url);
111
+ if (sellerProfileMatch?.[1]) {
112
+ searchParams.seller = sellerProfileMatch[1];
113
+ }
114
+ // Handle specific case for seller name on wantlist in the URL path
115
+ const sellerMyWantsMatch = /\/seller\/([^/]+)\/mywants/.exec(url);
116
+ if (sellerMyWantsMatch?.[1]) {
117
+ searchParams.searchType = 'user';
118
+ searchParams.searchValue = urlSearchParams.get('user') ?? '';
119
+ searchParams.seller = sellerMyWantsMatch[1];
120
+ }
121
+ return searchParams;
122
+ }
package/dist/index.d.ts CHANGED
@@ -1,4 +1,13 @@
1
- import DiscogsMarketplace from './marketplace';
2
- import type { InputInterface, OutputErrorInterface, OutputSuccessInterface } from './interfaces';
3
- export { DiscogsMarketplace };
4
- export type { InputInterface, OutputErrorInterface, OutputSuccessInterface };
1
+ import search from './search.js';
2
+ import getSearchParamsFromUrl from './get-search-params.js';
3
+ import type SearchParams from './interfaces/search-params.interface.js';
4
+ import type SearchResult from './interfaces/search-result.interface.js';
5
+ /**
6
+ * Discogs Marketplace API, tools for interacting with the Discogs Marketplace.
7
+ * @see {@link https://github.com/KirianCaumes/Discogs-Marketplace-API-NodeJS GitHub Repository}
8
+ */
9
+ export declare const DiscogsMarketplace: {
10
+ search: typeof search;
11
+ getSearchParamsFromUrl: typeof getSearchParamsFromUrl;
12
+ };
13
+ export type { SearchParams, SearchResult };