@postfetch/core 0.13.2 → 0.13.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.
package/dist/index.js CHANGED
@@ -1642,14 +1642,17 @@ async function appToken(net) {
1642
1642
  return token;
1643
1643
  }
1644
1644
  async function fetchPost(net, token, id) {
1645
- const response = await net(`https://oauth.reddit.com/comments/${id}?raw_json=1&limit=1`, {
1646
- headers: { authorization: `Bearer ${token}`, "user-agent": browserUserAgent() }
1647
- });
1645
+ const headers = { authorization: `Bearer ${token}`, "user-agent": browserUserAgent() };
1646
+ let response = await net(`https://oauth.reddit.com/comments/${id}?raw_json=1&limit=1`, { headers });
1647
+ if (response.status === 403) {
1648
+ await response.body?.cancel();
1649
+ response = await net(`https://oauth.reddit.com/by_id/t3_${id}?raw_json=1`, { headers });
1650
+ }
1648
1651
  if (!response.ok) {
1649
1652
  throw new Error(`Reddit post failed: ${response.status}`);
1650
1653
  }
1651
1654
  const payload = await response.json().catch(() => null);
1652
- const listing = Array.isArray(payload) ? payload[0] : null;
1655
+ const listing = Array.isArray(payload) ? payload[0] : payload;
1653
1656
  const children = object(listing) && object(listing.data) && Array.isArray(listing.data.children) ? listing.data.children : [];
1654
1657
  const first = children[0];
1655
1658
  const post = object(first) && object(first.data) ? first.data : null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@postfetch/core",
3
- "version": "0.13.2",
3
+ "version": "0.13.3",
4
4
  "description": "Zero-dependency typed core that turns Facebook, Instagram, LinkedIn, Pinterest, Reddit, SoundCloud, Spotify, TikTok, X and YouTube post URLs into media files.",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/src/reddit.ts CHANGED
@@ -100,14 +100,19 @@ async function appToken(net: Net): Promise<string> {
100
100
  }
101
101
 
102
102
  async function fetchPost(net: Net, token: string, id: string): Promise<Json> {
103
- const response = await net(`https://oauth.reddit.com/comments/${id}?raw_json=1&limit=1`, {
104
- headers: { authorization: `Bearer ${token}`, "user-agent": browserUserAgent() },
105
- });
103
+ const headers = { authorization: `Bearer ${token}`, "user-agent": browserUserAgent() };
104
+ let response = await net(`https://oauth.reddit.com/comments/${id}?raw_json=1&limit=1`, { headers });
105
+ // Reddit can block the comments route while still allowing direct post
106
+ // listings with the same token. This route returns the post, not its replies.
107
+ if (response.status === 403) {
108
+ await response.body?.cancel();
109
+ response = await net(`https://oauth.reddit.com/by_id/t3_${id}?raw_json=1`, { headers });
110
+ }
106
111
  if (!response.ok) {
107
112
  throw new Error(`Reddit post failed: ${response.status}`);
108
113
  }
109
114
  const payload = await response.json().catch(() => null);
110
- const listing = Array.isArray(payload) ? payload[0] : null;
115
+ const listing = Array.isArray(payload) ? payload[0] : payload;
111
116
  const children = object(listing) && object(listing.data) && Array.isArray(listing.data.children) ? listing.data.children : [];
112
117
  const first = children[0];
113
118
  const post = object(first) && object(first.data) ? first.data : null;