Skip to content

Conversation

@jp-knj
Copy link
Member

@jp-knj jp-knj commented May 6, 2025

Changes

  • remove todo comment

  • Renamed helper
    get<T>getJson<T> to clarify that the helper always returns parsed JSON.

  • Timeout & abort support
    Adds an AbortController with a 10 s default timeout to prevent hanging requests.

  • Richer error messages

    • Distinguishes HTTP errors (GET /api … → 503) from network/abort errors (failed: The user aborted a request).
    • Includes status text for quicker debugging.
  • Less boilerplate
    Helper now returns response.json() directly, so the four public API functions no longer need their own inline callbacks.

  • No functional change to callers
    Signatures of getProducts(), getProduct(), getUser(), and getCart() remain identical; only internal implementation is updated.

  • Changeset included
    pnpm exec changeset this is purely internal refactor. not add.

Testing

  • Unit: pnpm test — all existing tests pass.
  • Manual:
    1. Started dev server, loaded /products page – data still renders.
    2. Throttled network in DevTools → request now aborts at 10 s with descriptive console error.
    3. Verified addToUserCart() still posts successfully.

No new automated tests added because the helper is already covered by higher-level API tests; timeout behaviour is best validated manually.

Docs

No public-facing API surface changed, so docs update not required.

/cc @withastro/maintainers-docs for visibility, but I believe no docs edits are needed.

@changeset-bot
Copy link

changeset-bot bot commented May 6, 2025

⚠️ No Changeset found

Latest commit: 783acf3

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions github-actions bot added the pkg: example Related to an example package (scope) label May 6, 2025
@jp-knj jp-knj marked this pull request as ready for review May 6, 2025 07:38
@jp-knj jp-knj marked this pull request as draft May 6, 2025 07:38
@jp-knj jp-knj force-pushed the remove-todo-comment branch from 9a5b92f to 93709f7 Compare May 6, 2025 07:49
@jp-knj jp-knj marked this pull request as ready for review May 6, 2025 08:42
Copy link
Member

@ematipico ematipico left a comment

Choose a reason for hiding this comment

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

Can you please update the example to show the usage of the new parameter timeout? This is an example, so we need to show the usage of things that we write

@jp-knj jp-knj force-pushed the remove-todo-comment branch 3 times, most recently from 501838b to f234636 Compare May 7, 2025 11:58
@jp-knj
Copy link
Member Author

jp-knj commented May 7, 2025

@ematipico
Sorry for the delay—just pushed an update
Please review again,

that highlights the new timeout parameter in real calls.

/********************************************************************
 * example.ts – showing the new `timeout` option in action
 *******************************************************************/

// Assume getJSON + helpers are imported from your library:
//
//   import { getProducts, getProduct, addToUserCart } from './api';

async function demo(req: Request) {
  try {
    // 1) Short-fuse fetch: if your catalog service is slow, fail fast.
    const products = await getProducts(req, { timeout: 3_000 });
    console.log('products loaded in ≤3 s:', products.length);

    // 2) Normal fetch – keep the default 10 s timeout.
    const game = await getProduct(req, 42);
    console.log('single product:', game);

    // 3) POST helper also accepts timeout (and signal, base, …).
    await addToUserCart(42, 'Space Adventure', { timeout: 5_000 });
    console.log('cart updated!');
  } catch (err) {
    console.error('Request aborted or failed:', err);
  }
}
Call Timeout behaviour
getProducts(req, { timeout: 3000 }) Aborts after 3 s if the list API hasn’t replied.
getProduct(req, 42) Uses the helper’s default 10 s timeout.
addToUserCart(…, { timeout: 5000 }) Aborts the POST after 5 s if the cart API hasn’t replied.

This shows exactly how consumers pass the new option while keeping the helper’s surface minimal: just an options object whose timeout key is optional.

Copy link
Member

@florian-lefebvre florian-lefebvre left a comment

Choose a reason for hiding this comment

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

Can you remove the changesets? Changesets are only for modifications of packages, not examples

@jp-knj jp-knj force-pushed the remove-todo-comment branch 2 times, most recently from 7c08617 to f234636 Compare May 7, 2025 13:08
@jp-knj
Copy link
Member Author

jp-knj commented May 7, 2025

@florian-lefebvre

Can you remove the changesets? Changesets are only for modifications of packages, not examples

Sure, updated it.

Copy link
Member

@florian-lefebvre florian-lefebvre left a comment

Choose a reason for hiding this comment

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

Thanks for removing one changeset! There's another one to remove too

incomingReq: Request,
endpoint: string,
cb: (response: Response) => Promise<T>
timeout = 10_000
Copy link
Member

Choose a reason for hiding this comment

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

Since the timeout isn't used, i think it could be worth removing it altogether from the example. We try to keep examples minimal and as a starting point. So that probably means removing the abort controller too

Copy link
Member Author

Choose a reason for hiding this comment

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

Got it, Just a moment.

Copy link
Member Author

Choose a reason for hiding this comment

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

@florian-lefebvre
I'm done, just keep simple. is it still Okay?

@jp-knj jp-knj force-pushed the remove-todo-comment branch from 58950e1 to e4e21ac Compare May 7, 2025 14:33
@jp-knj jp-knj force-pushed the remove-todo-comment branch from e4e21ac to 901ba85 Compare May 7, 2025 14:36
Copy link
Member

@florian-lefebvre florian-lefebvre left a comment

Choose a reason for hiding this comment

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

Thanks!

@florian-lefebvre florian-lefebvre merged commit 7246cde into withastro:main May 7, 2025
15 checks passed
openscript pushed a commit to openscript/astro that referenced this pull request Sep 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: example Related to an example package (scope)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants