Skip to content

fix: 🐞Update postForId to use correct Substack API endpoint - #89

Merged
jakub-k-slys merged 2 commits into
mainfrom
copilot/fix-88
Jul 8, 2025
Merged

fix: 🐞Update postForId to use correct Substack API endpoint#89
jakub-k-slys merged 2 commits into
mainfrom
copilot/fix-88

Conversation

Copilot AI commented Jul 8, 2025

Copy link
Copy Markdown
Contributor

Problem

The postForId(postId) method was using an incorrect Substack API endpoint, causing requests to fail with 404 errors or return invalid responses.

Current (incorrect) endpoint: /api/v1/posts/{id}
Correct endpoint: /api/v1/posts/by-id/{id}

Changes

  • Fixed endpoint URL in SubstackClient.postForId() method
  • Added error handling with descriptive error messages for missing posts
  • Updated unit tests to validate the correct endpoint is called
  • Added test coverage for error scenarios

Example

// This now works correctly with the proper Substack API endpoint
const post = await client.postForId('164064938');
console.log(`Title: ${post.title}`);
console.log(`Author: ${post.author.name}`);

Verification

  • ✅ All existing tests pass
  • ✅ New error handling test added
  • ✅ Endpoint validated against Substack API documentation
  • ✅ No breaking changes to public API

Fixes #88.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • test.substack.com
    • Triggering command: /usr/local/bin/node /home/REDACTED/work/substack-api/substack-api/node_modules/jest-worker/build/processChild.js (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…id/{id})

Co-authored-by: jakub-k-slys <29287862+jakub-k-slys@users.noreply.github.com>
Copilot AI changed the title [WIP] 🐞 Bug: getPostById Uses Incorrect Endpoint 🐞 Fix: Update postForId to use correct Substack API endpoint Jul 8, 2025
Copilot AI requested a review from jakub-k-slys July 8, 2025 12:59
@jakub-k-slys jakub-k-slys changed the title 🐞 Fix: Update postForId to use correct Substack API endpoint fix: 🐞Update postForId to use correct Substack API endpoint Jul 8, 2025
@jakub-k-slys
jakub-k-slys marked this pull request as ready for review July 8, 2025 13:29
Copilot AI review requested due to automatic review settings July 8, 2025 13:29
@jakub-k-slys
jakub-k-slys merged commit 52f244b into main Jul 8, 2025
20 of 25 checks passed
@jakub-k-slys
jakub-k-slys deleted the copilot/fix-88 branch July 8, 2025 13:29
@codecov

codecov Bot commented Jul 8, 2025

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.68%. Comparing base (6944267) to head (cce9af9).
⚠️ Report is 164 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #89      +/-   ##
==========================================
+ Coverage   92.62%   92.68%   +0.05%     
==========================================
  Files           9        9              
  Lines         244      246       +2     
  Branches       43       43              
==========================================
+ Hits          226      228       +2     
  Misses          1        1              
  Partials       17       17              
Files with missing lines Coverage Δ
src/substack-client.ts 93.54% <100.00%> (+0.21%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes the postForId method to call the correct Substack API endpoint, adds error handling for missing posts, and updates unit tests to cover both success and error cases.

  • Updated the endpoint URL in SubstackClient.postForId()
  • Wrapped the HTTP call in a try/catch with a descriptive error message
  • Adjusted unit tests to assert the new endpoint and error behavior

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
tests/unit/substack-client.test.ts Updated expected URL in test and added an error scenario test
src/substack-client.ts Changed endpoint path and added error handling logic

Comment thread src/substack-client.ts
const post = await this.httpClient.get<SubstackPost>(`/api/v1/posts/by-id/${id}`)
return new Post(post, this.httpClient)
} catch (error) {
throw new Error(`Post with ID ${id} not found: ${(error as Error).message}`)

Copilot AI Jul 8, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Catching all errors unconditionally may mask non-404 issues (e.g., network or permission errors). Consider inspecting the error (e.g., checking error.response?.status) and rethrowing unexpected errors so only genuine "not found" cases are handled here.

Suggested change
throw new Error(`Post with ID ${id} not found: ${(error as Error).message}`)
if (error.response?.status === 404) {
throw new Error(`Post with ID ${id} not found`);
}
throw error; // Rethrow the original error for non-404 cases

Copilot uses AI. Check for mistakes.
Comment thread src/substack-client.ts
const post = await this.httpClient.get<SubstackPost>(`/api/v1/posts/by-id/${id}`)
return new Post(post, this.httpClient)
} catch (error) {
throw new Error(`Post with ID ${id} not found: ${(error as Error).message}`)

Copilot AI Jul 8, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throwing a new Error here discards the original stack trace. Consider using the cause option (Node.js >=16.9) like throw new Error(msg, { cause: error as Error }) to preserve the original error details.

Suggested change
throw new Error(`Post with ID ${id} not found: ${(error as Error).message}`)
throw new Error(`Post with ID ${id} not found: ${(error as Error).message}`, { cause: error as Error })

Copilot uses AI. Check for mistakes.
jakub-k-slys pushed a commit that referenced this pull request Jul 8, 2025
## [0.15.1](0.15.0...0.15.1) (2025-07-08)

### Bug Fixes

* 🐞 Fix getNoteById endpoint and response transformation ([#87](#87)) ([a02ac92](a02ac92))
* 🐞Update postForId to use correct Substack API endpoint ([#89](#89)) ([52f244b](52f244b))
@jakub-k-slys

Copy link
Copy Markdown
Owner

🎉 This PR is included in version 0.15.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐞 Bug: getPostById Uses Incorrect Endpoint

3 participants