discogs-marketplace-api-nodejs 1.16.0 → 1.16.2

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.
@@ -63,28 +63,24 @@ async function scrape({ currencies, genres, formats, formatDescriptions, conditi
63
63
  ['priceRangeHigh', priceRange?.max.toString() ?? ''],
64
64
  ].filter(([, p]) => !!p)).toString(),
65
65
  ].join('?');
66
- const browserPage = await browserContext.newPage();
67
- const response = await browserPage.request.get(urlGenerated, {
66
+ const response = await browserContext.request.get(urlGenerated, {
68
67
  headers: {
69
68
  'User-Agent': 'Discogs',
70
69
  'Content-Type': 'application/json',
71
- Referrer: 'https://discogs.com',
72
70
  },
73
71
  });
74
72
  if (!response.ok()) {
75
73
  const message = response.headers()['content-type']?.includes('application/json')
76
74
  ? (await response.json())?.detail
77
75
  : '';
78
- await browserPage.close();
79
76
  // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
80
77
  throw new Error(message || `An error ${response.status()} occurred.`);
81
78
  }
82
79
  const result = (await response.json());
83
- const detailsResponse = await browserPage.request.post('https://www.discogs.com/graphql', {
80
+ const detailsResponse = await browserContext.request.post('https://www.discogs.com/graphql', {
84
81
  headers: {
85
82
  'User-Agent': 'Discogs',
86
83
  'Content-Type': 'application/json',
87
- Referrer: 'https://discogs.com',
88
84
  },
89
85
  data: {
90
86
  // eslint-disable-next-line max-len
@@ -105,7 +101,6 @@ async function scrape({ currencies, genres, formats, formatDescriptions, conditi
105
101
  throw new Error(`An error ${detailsResponse.status()} occurred.`);
106
102
  }
107
103
  const detailsResult = (await detailsResponse.json());
108
- await browserPage.close();
109
104
  const details = Object.fromEntries(detailsResult.data?.releases?.map(release => {
110
105
  if (!release?.discogsId) {
111
106
  return [0, { imageUrl: null, want: 0, have: 0 }];
package/dist/search.d.ts CHANGED
@@ -1,11 +1,13 @@
1
1
  import type { SearchParams } from './interfaces/search-params.interface.js';
2
2
  import type SearchResult from './interfaces/search-result.interface.js';
3
+ import type { Browser } from 'playwright-chromium';
3
4
  export declare const DEFAULT_LIMIT = 25;
4
5
  export declare const DEFAULT_PAGE = 1;
5
6
  export declare const DEFAULT_SORT = "listed,desc";
6
7
  /**
7
8
  * Performs a search on the Discogs marketplace using the provided parameters.
8
- * @param searchParams Search parameters
9
- * @returns A promise that resolves to the search results, including items found, pagination details, and the generated URL.
9
+ * @param searchParams - The search parameters, including API type, query, pagination, and filters.
10
+ * @param browserInstance - Optional Playwright browser instance. If provided, you must manage its lifecycle.
11
+ * @returns A promise that resolves to the search results, including items, pagination info, and the generated URL.
10
12
  */
11
- export default function search(searchParams: SearchParams): Promise<SearchResult>;
13
+ export default function search(searchParams: SearchParams, browserInstance?: Browser): Promise<SearchResult>;
package/dist/search.js CHANGED
@@ -15,16 +15,20 @@ exports.DEFAULT_PAGE = 1;
15
15
  exports.DEFAULT_SORT = 'listed,desc';
16
16
  /**
17
17
  * Performs a search on the Discogs marketplace using the provided parameters.
18
- * @param searchParams Search parameters
19
- * @returns A promise that resolves to the search results, including items found, pagination details, and the generated URL.
18
+ * @param searchParams - The search parameters, including API type, query, pagination, and filters.
19
+ * @param browserInstance - Optional Playwright browser instance. If provided, you must manage its lifecycle.
20
+ * @returns A promise that resolves to the search results, including items, pagination info, and the generated URL.
20
21
  */
21
- async function search(searchParams) {
22
- playwright_extra_1.chromium.use((0, puppeteer_extra_plugin_stealth_1.default)());
23
- const browser = await playwright_extra_1.chromium.launch({
24
- headless: true,
25
- chromiumSandbox: false,
26
- args: ['--no-sandbox', '--disable-setuid-sandbox'],
27
- });
22
+ async function search(searchParams, browserInstance) {
23
+ const browser = browserInstance ??
24
+ (await (() => {
25
+ playwright_extra_1.chromium.use((0, puppeteer_extra_plugin_stealth_1.default)());
26
+ return playwright_extra_1.chromium.launch({
27
+ headless: true,
28
+ chromiumSandbox: false,
29
+ args: ['--no-sandbox', '--disable-setuid-sandbox'],
30
+ });
31
+ })());
28
32
  const browserContext = await browser.newContext({
29
33
  javaScriptEnabled: false,
30
34
  extraHTTPHeaders: searchParams.api === 'legacy' && !(searchParams.seller && searchParams.user)
@@ -43,8 +47,6 @@ async function search(searchParams) {
43
47
  }
44
48
  return (0, legacy_scraper_1.default)(searchParams, browserContext);
45
49
  })();
46
- await browserContext.close();
47
- await browser.close();
48
50
  return {
49
51
  items,
50
52
  result: {
@@ -58,9 +60,10 @@ async function search(searchParams) {
58
60
  urlGenerated,
59
61
  };
60
62
  }
61
- catch (error) {
63
+ finally {
62
64
  await browserContext.close();
63
- await browser.close();
64
- throw error;
65
+ if (!browserInstance) {
66
+ await browser.close();
67
+ }
65
68
  }
66
69
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "discogs-marketplace-api-nodejs",
3
- "version": "1.16.0",
3
+ "version": "1.16.2",
4
4
  "description": "Another (better ?) NodeJs library to fetch data from Discogs marketplace",
5
5
  "keywords": [
6
6
  "discogs",