Skip to content

fix: avoid duplicate region prefix in getApiEndpoint for regional API URLs#2941

Merged
HarshMN2345 merged 1 commit intomainfrom
fix/api-endpoint-double-region-subdomain
Mar 29, 2026
Merged

fix: avoid duplicate region prefix in getApiEndpoint for regional API URLs#2941
HarshMN2345 merged 1 commit intomainfrom
fix/api-endpoint-double-region-subdomain

Conversation

@HarshMN2345
Copy link
Copy Markdown
Member

@HarshMN2345 HarshMN2345 commented Mar 29, 2026

What does this PR do?

(Provide a description of what this PR does.)

Test Plan

(Write your test plan here. If you changed any code, please provide us with clear instructions on how you verified your changes work.)

Related PRs and Issues

(If this PR is related to any other PR or resolves any issue or related to any issue link all related PR and issues here.)

Have you read the Contributing Guidelines on issues?

(Write your answer here.)

Summary by CodeRabbit

  • Bug Fixes
    • Fixed an issue where duplicate region identifiers were being added to API endpoint URLs, which could have caused connection failures.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 29, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 5ac7f9a0-e750-4c35-af1e-1292587e1a5e

📥 Commits

Reviewing files that changed from the base of the PR and between 089ca52 and d2e7e26.

📒 Files selected for processing (1)
  • src/lib/stores/sdk.ts

Walkthrough

The getApiEndpoint(region?: string) function in src/lib/stores/sdk.ts has been modified to prevent duplicate subdomain segments in constructed API endpoint URLs. The subdomain variable is now declared with let instead of const, and a conditional check has been added to clear the subdomain value when it is already present at the start of the parsed hostname. This prevents the subdomain from being redundantly prepended to the base URL.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: avoid duplicate region prefix in getApiEndpoint for regional API URLs' directly and clearly describes the main change: preventing duplicate region prefixes in the getApiEndpoint function.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/api-endpoint-double-region-subdomain

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Mar 29, 2026

Greptile Summary

This PR adds a guard in getApiEndpoint to prevent duplicate region subdomain prepending when VARS.APPWRITE_ENDPOINT is already set to a regionalized URL (e.g., fra.cloud.appwrite.io). Without the fix, calling getApiEndpoint('fra') with such an endpoint would produce fra.fra.cloud.appwrite.io/v1.

Key changes:

  • subdomain declaration changed from const to let to allow conditional clearing.
  • A startsWith check clears subdomain when the configured endpoint hostname already begins with the region prefix, avoiding double-prepending.

Concerns:

  • The fix is correct for the same-region case (e.g., endpoint is fra.cloud.appwrite.io, region is fra), but a cross-region call (e.g., endpoint is fra.cloud.appwrite.io, region is nyc) would still produce a malformed URL (nyc.fra.cloud.appwrite.io/v1). Whether this scenario is reachable in production depends on configuration, but should be documented or handled.
  • The PR description, test plan, and related issue fields are all blank, making it hard to understand the original bug, confirm the fix is complete, or verify the change was tested.

Confidence Score: 4/5

Safe for the primary same-region duplicate-prefix scenario, but the cross-region edge case with a pre-regionalized endpoint is unaddressed and there are no tests.

The fix correctly resolves the described same-region double-prefix bug. However, it is incomplete for the cross-region mismatch case, and the complete lack of PR description or test plan makes it hard to verify the full scope of the intended fix.

src/lib/stores/sdk.ts — the only changed file; the new guard needs to handle cross-region mismatches and ideally be covered by tests.

Important Files Changed

Filename Overview
src/lib/stores/sdk.ts Adds a guard in getApiEndpoint to skip prepending a region subdomain when the base hostname already starts with it, preventing double-prefixed URLs like fra.fra.cloud.appwrite.io/v1. Logic is correct for the common case but has edge cases and no tests.

Reviews (1): Last reviewed commit: "avoid duplicate region prefix in getApiE..." | Re-trigger Greptile

Comment on lines +57 to 62
let subdomain = isMultiRegionSupported(url) ? getSubdomain(region) : '';
if (subdomain && hostname.startsWith(subdomain)) {
subdomain = '';
}

return `${protocol}//${subdomain}${hostname}/v1`;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 No tests added for the new guard

The getApiEndpoint function is called in many places across the codebase, yet there are no unit tests for this particular fix. A test covering at least the two key scenarios would make the intention clear and prevent regressions:

  1. APPWRITE_ENDPOINT = "https://fra.cloud.appwrite.io" + getApiEndpoint('fra') → should return https://fra.cloud.appwrite.io/v1 (no duplicate prefix)
  2. APPWRITE_ENDPOINT = "https://cloud.appwrite.io" + getApiEndpoint('fra') → should return https://fra.cloud.appwrite.io/v1 (prefix added normally)

Without tests, it is difficult to verify the fix is correct and to protect against future regressions.

@HarshMN2345 HarshMN2345 merged commit 00d9765 into main Mar 29, 2026
5 checks passed
@HarshMN2345 HarshMN2345 deleted the fix/api-endpoint-double-region-subdomain branch March 29, 2026 10:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants