Skip to content

fix: normalize regional API base URL and prevent invalid multi-region…#2942

Merged
HarshMN2345 merged 3 commits intomainfrom
fix/api-endpoint-double-region-subdomain
Apr 16, 2026
Merged

fix: normalize regional API base URL and prevent invalid multi-region…#2942
HarshMN2345 merged 3 commits intomainfrom
fix/api-endpoint-double-region-subdomain

Conversation

@HarshMN2345
Copy link
Copy Markdown
Member

@HarshMN2345 HarshMN2345 commented Mar 29, 2026

… hosts

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

  • Refactor
    • Enhanced API endpoint construction logic to improve support for multi-region configurations. Consolidated regional endpoint formatting into centralized utilities for better maintainability and consistency across the application.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 29, 2026

Walkthrough

A new utility module src/lib/helpers/apiEndpoint.ts is introduced to centralize regional API endpoint construction logic. The module exports three functions: stripLeadingRegionSubdomain() removes known region prefixes from hostnames, getRegionSubdomain() maps region identifiers to DNS subdomain prefixes, and buildRegionalV1Endpoint() constructs complete regional v1 endpoint URLs. The existing getApiEndpoint() function in src/lib/stores/sdk.ts is refactored to delegate endpoint construction to buildRegionalV1Endpoint(), removing inline logic and the local getSubdomain() helper.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 60.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 clearly summarizes the main change: normalizing regional API base URLs and preventing invalid multi-region configurations through the introduction of shared endpoint-building utilities.

✏️ 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.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
src/lib/helpers/apiEndpoint.ts (1)

71-85: Add focused tests for endpoint normalization edge cases.

Please add helper-level tests for: repeated prefixes, hostnames with ports, unknown region values, and isMultiRegion=false passthrough behavior to lock this logic down.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/lib/helpers/apiEndpoint.ts` around lines 71 - 85, Add focused unit tests
that exercise buildRegionalV1Endpoint and its helpers
(stripLeadingRegionSubdomain, getRegionSubdomain): 1) verify repeated region
prefixes are normalized (e.g., "us-us.example.com" -> single "us."), 2) ensure
hostnames with ports (e.g., "example.com:3000") preserve the port and produce
correct "/v1" path, 3) cover unknown/undefined region values to confirm
getRegionSubdomain returns the expected fallback and the endpoint remains valid,
and 4) assert that when isMultiRegion=false the function returns the hostname
unchanged (passthrough) with protocol and "/v1". Include assertions for both
protocol variants and edge-case inputs to lock down normalization behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/lib/helpers/apiEndpoint.ts`:
- Around line 71-85: Add focused unit tests that exercise
buildRegionalV1Endpoint and its helpers (stripLeadingRegionSubdomain,
getRegionSubdomain): 1) verify repeated region prefixes are normalized (e.g.,
"us-us.example.com" -> single "us."), 2) ensure hostnames with ports (e.g.,
"example.com:3000") preserve the port and produce correct "/v1" path, 3) cover
unknown/undefined region values to confirm getRegionSubdomain returns the
expected fallback and the endpoint remains valid, and 4) assert that when
isMultiRegion=false the function returns the hostname unchanged (passthrough)
with protocol and "/v1". Include assertions for both protocol variants and
edge-case inputs to lock down normalization behavior.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: dae0f278-bde9-41db-89eb-598cb928dce8

📥 Commits

Reviewing files that changed from the base of the PR and between 00d9765 and 92ad5d0.

📒 Files selected for processing (2)
  • src/lib/helpers/apiEndpoint.ts
  • src/lib/stores/sdk.ts

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Mar 29, 2026

Greptile Summary

This PR extracts regional API URL construction from sdk.ts into a dedicated src/lib/helpers/apiEndpoint.ts helper, and fixes a real bug where switching regions compounded prefixes (e.g. nyc.fra.cloud.appwrite.io instead of nyc.cloud.appwrite.io). The new buildRegionalV1Endpoint correctly guards the "no region" case with an early return, so a multi-region hostname is never accidentally stripped when region is undefined.

Confidence Score: 5/5

Safe to merge — refactoring is functionally correct and fixes a real region-switching bug

All previously raised P1 concerns (region stripped on undefined, missing guard) are addressed in the current code via the early !subdomain return. No new P0/P1 issues found. Remaining gap (no test file) was already noted in prior review threads and is a P2.

src/lib/helpers/apiEndpoint.ts — no accompanying test file

Important Files Changed

Filename Overview
src/lib/helpers/apiEndpoint.ts New helper encapsulating regional URL construction; logic is correct but ships without a test file, and the aggressive prefix-stripping loop could obscure edge cases
src/lib/stores/sdk.ts Cleanly delegates to the new helper; no functional regressions introduced

Reviews (3): Last reviewed commit: "addressed commet" | Re-trigger Greptile

Comment thread src/lib/helpers/apiEndpoint.ts
Comment thread src/lib/helpers/apiEndpoint.ts
@HarshMN2345 HarshMN2345 merged commit 05ff668 into main Apr 16, 2026
4 checks passed
@HarshMN2345 HarshMN2345 deleted the fix/api-endpoint-double-region-subdomain branch April 16, 2026 13:05
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