discogs-marketplace-api-nodejs 1.16.2 → 1.16.4

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,44 +63,42 @@ 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 response = await browserContext.request.get(urlGenerated, {
67
- headers: {
68
- 'User-Agent': 'Discogs',
69
- 'Content-Type': 'application/json',
70
- },
71
- });
72
- if (!response.ok()) {
73
- const message = response.headers()['content-type']?.includes('application/json')
66
+ const browserPage = await browserContext.newPage();
67
+ const response = await browserPage.goto(urlGenerated);
68
+ if (!response?.ok()) {
69
+ const message = response?.headers()['content-type']?.includes('application/json')
74
70
  ? (await response.json())?.detail
75
71
  : '';
76
72
  // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
77
- throw new Error(message || `An error ${response.status()} occurred.`);
73
+ throw new Error(message || `An error ${response?.status() ?? '?'} occurred.`);
78
74
  }
79
75
  const result = (await response.json());
80
- const detailsResponse = await browserContext.request.post('https://www.discogs.com/graphql', {
81
- headers: {
82
- 'User-Agent': 'Discogs',
83
- 'Content-Type': 'application/json',
84
- },
85
- data: {
86
- // eslint-disable-next-line max-len
87
- query: 'query Releases($discogsIds: [Int], $first: Int) {\n releases(discogsIds: $discogsIds) {\n inWantlistCount\n inCollectionCount\n images(first: $first) {\n edges {\n node {\n thumbnail {\n webpUrl\n }\n }\n }\n }\n ratings(first: 1) {\n averageRating\n }\n discogsId\n }\n}',
88
- variables: {
89
- discogsIds: result.items?.map(z => z.release?.releaseId ?? 0).filter((x, i, self) => x && self.indexOf(x) === i),
90
- first: 1,
76
+ const { json: detailsResult, ...detailsResponse } = await browserPage.evaluate(async (discogsIds) => {
77
+ const res = await fetch('https://www.discogs.com/graphql', {
78
+ method: 'POST',
79
+ headers: {
80
+ 'User-Agent': 'Discogs',
81
+ 'Content-Type': 'application/json',
91
82
  },
92
- extensions: {
93
- persistedQuery: {
94
- version: 1,
95
- sha256Hash: 'ee9d48441023ebd1e6ca169e550581b45de216e88b40711acb6617e49e1bb0cb',
83
+ body: JSON.stringify({
84
+ // eslint-disable-next-line max-len
85
+ query: 'query Releases($discogsIds: [Int], $first: Int) {\n releases(discogsIds: $discogsIds) {\n inWantlistCount\n inCollectionCount\n images(first: $first) {\n edges {\n node {\n thumbnail {\n webpUrl\n }\n }\n }\n }\n ratings(first: 1) {\n averageRating\n }\n discogsId\n }\n}',
86
+ variables: { discogsIds, first: 1 },
87
+ extensions: {
88
+ persistedQuery: { version: 1, sha256Hash: 'ee9d48441023ebd1e6ca169e550581b45de216e88b40711acb6617e49e1bb0cb' },
96
89
  },
97
- },
98
- },
99
- });
100
- if (!detailsResponse.ok()) {
101
- throw new Error(`An error ${detailsResponse.status()} occurred.`);
90
+ }),
91
+ });
92
+ return {
93
+ ok: res.ok,
94
+ status: res.status,
95
+ json: (await res.json().catch(() => ({}))),
96
+ };
97
+ }, result.items?.map(z => z.release?.releaseId ?? 0).filter((x, i, self) => x && self.indexOf(x) === i));
98
+ await browserPage.close();
99
+ if (!detailsResponse.ok) {
100
+ throw new Error(`An error ${detailsResponse.status} occurred.`);
102
101
  }
103
- const detailsResult = (await detailsResponse.json());
104
102
  const details = Object.fromEntries(detailsResult.data?.releases?.map(release => {
105
103
  if (!release?.discogsId) {
106
104
  return [0, { imageUrl: null, want: 0, have: 0 }];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "discogs-marketplace-api-nodejs",
3
- "version": "1.16.2",
3
+ "version": "1.16.4",
4
4
  "description": "Another (better ?) NodeJs library to fetch data from Discogs marketplace",
5
5
  "keywords": [
6
6
  "discogs",