discogs-marketplace-api-nodejs 1.16.1 → 1.16.3

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.
@@ -64,22 +64,16 @@ async function scrape({ currencies, genres, formats, formatDescriptions, conditi
64
64
  ].filter(([, p]) => !!p)).toString(),
65
65
  ].join('?');
66
66
  const browserPage = await browserContext.newPage();
67
- const response = await browserPage.request.get(urlGenerated, {
68
- headers: {
69
- 'User-Agent': 'Discogs',
70
- 'Content-Type': 'application/json',
71
- },
72
- });
73
- if (!response.ok()) {
74
- const message = response.headers()['content-type']?.includes('application/json')
67
+ const response = await browserPage.goto(urlGenerated);
68
+ if (!response?.ok()) {
69
+ const message = response?.headers()['content-type']?.includes('application/json')
75
70
  ? (await response.json())?.detail
76
71
  : '';
77
- await browserPage.close();
78
72
  // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
79
- throw new Error(message || `An error ${response.status()} occurred.`);
73
+ throw new Error(message || `An error ${response?.status() ?? '?'} occurred.`);
80
74
  }
81
75
  const result = (await response.json());
82
- const detailsResponse = await browserPage.request.post('https://www.discogs.com/graphql', {
76
+ const detailsResponse = await browserContext.request.post('https://www.discogs.com/graphql', {
83
77
  headers: {
84
78
  'User-Agent': 'Discogs',
85
79
  'Content-Type': 'application/json',
@@ -103,7 +97,6 @@ async function scrape({ currencies, genres, formats, formatDescriptions, conditi
103
97
  throw new Error(`An error ${detailsResponse.status()} occurred.`);
104
98
  }
105
99
  const detailsResult = (await detailsResponse.json());
106
- await browserPage.close();
107
100
  const details = Object.fromEntries(detailsResult.data?.releases?.map(release => {
108
101
  if (!release?.discogsId) {
109
102
  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.1",
3
+ "version": "1.16.3",
4
4
  "description": "Another (better ?) NodeJs library to fetch data from Discogs marketplace",
5
5
  "keywords": [
6
6
  "discogs",