Skip to content

feat(api-client): Export API_PREFIX from @sentry/api/client - #147

Closed
sentry-junior[bot] wants to merge 1 commit into
mainfrom
feat/export-api-prefix
Closed

sentry-junior[bot] wants to merge 1 commit into
mainfrom
feat/export-api-prefix

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Requested by @BYK in getsentry/toolkit#2229 (comment).

@sentry/api/client now exports API_PREFIX ("/api/0"). Hosts such as toolkit can import it instead of keeping their own copy; toolkit has three today.

The client itself spelled the prefix out in four places, and all four now use the export:

  • buildSentryApiUrl
  • parseApiPathScope
  • the SDK request check in core.ts
  • cache invalidation

Sentry's spec has no separate field for the prefix: servers is https://{region}.sentry.io, and every path starts with /api/0. So a test checks that every path in openapi-derefed.json still starts with API_PREFIX. If the spec ever changes, that test fails instead of the client drifting.

import { API_PREFIX } from "@sentry/api/client";
// "/api/0"

via @BYK.

--

View Session [Sentry]

Sentry's OpenAPI spec starts every path with /api/0, and the client
spelled that prefix out in four places. Export it once from
api-path-scope.ts and use it in buildSentryApiUrl, parseApiPathScope,
the SDK request check in core.ts, and cache invalidation, so hosts such
as toolkit can import it rather than keep their own copy. A test checks
that every path in the spec still starts with it.

Co-Authored-By: BYK <126780+BYK@users.noreply.github.com>
@sentry-junior
sentry-junior Bot requested review from BYK and betegon as code owners November 13, 2026 09:50
BYK added a commit that referenced this pull request Nov 13, 2026
…148)

**Merge order:**
[#148](#148)
→
[getsentry/sentry-docs#20787](getsentry/sentry-docs#20787)
→
[getsentry/sentry#131065](getsentry/sentry#131065)
→ the getsentry/toolkit bump, after `@sentry/api` 2.0.0 is published.
Docs has to merge before sentry#131065. When that merges, the spec sync
commits the new spec to sentry-api-schema, which auto-releases
`@sentry/api` and auto-bumps the schema in sentry-docs. Without
sentry-docs#20787 merged first, the docs would drop `/api/0` from paths
and curl examples. This PR supersedes #147, which hard-coded
`API_PREFIX`.

Per @BYK, the server decides the base path. `/api/0` comes from the
spec's server URL. It's exported as `API_BASE_PATH`, and every path in
the package is relative to it. Nothing here hard-codes `0`. A future
`/api/1` spec would produce `API_BASE_PATH = "/api/1"` with no code
change.

**Design**
- `lib/api-base-path.mjs` reads the base path from the path of
`servers[0].url`, makes `paths` relative, and trims server URLs to their
origin. That includes the per-operation override on `listSeerModels`.
- **Until the sentry PR lands,** the base path is the `/api/<version>`
prefix that every path shares. It's marked TODO. Both spec shapes build
the same `src/`; I checked this with `diff -r`. So the sentry change
only syncs a byte-identical package through the bot.
- **`baseUrl` stays the Sentry origin.** That's what users have,
including self-hosted URLs and `links.regionUrl`. The patched `buildUrl`
builds the request URL as origin + `API_BASE_PATH` + operation path.
- **Exported** from `@sentry/api`, `@sentry/api/client` and
`@sentry/api/operations`.
- **The four `/api/0` copies are gone:** in `core.ts`,
`buildSentryApiUrl`, `parseApiPathScope` and cache invalidation. So are
the copies in the idempotent matcher, `normalize-spec` and
`source-analysis`. Matching still finds the base path anywhere in the
URL, as before.

**Before / After**
```ts
// @sentry/api/operations
{ functionName: "listOrganizations", path: "/api/0/organizations/" }
{ functionName: "listOrganizations", path: "/organizations/" }

import { API_BASE_PATH } from "@sentry/api"; // "/api/0", from servers[0].url

// raw requests: url is relative to API_BASE_PATH
sentry.client.prepareRequest({ method: "GET", url: "/api/0/organizations/" }); // before
sentry.client.prepareRequest({ method: "GET", url: "/organizations/" });       // after
// both send https://sentry.io/api/0/organizations/
```

**Breaking: major bump to 2.0.0**
- `operations[].path` and `IdempotentOperation.path` are relative.
Build-time lists derived from the catalog must be regenerated, such as
toolkit's `idempotent-operations.json`.
- Raw `client.request`, `client.get` and `prepareRequest` take relative
`url`s. An old `/api/0/...` URL now gets a doubled base path.
- Generated function names, types and the request URLs of typed
operations don't change: `sentry-client.gen.ts` is byte-identical.

Co-Authored-By: BYK <126780+BYK@users.noreply.github.com>

<!-- junior-request-attribution:start -->
via @BYK.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:agent%3A640c5343d03e547097aa9b8a711daaf5 -->

--

[View
Session](https://junior-prod.sentry.dev/conversations/agent%3A640c5343d03e547097aa9b8a711daaf5)
[[Sentry]](https://sentry.sentry.io/explore/conversations/agent%3A640c5343d03e547097aa9b8a711daaf5/?project=4510944073809921)

<!-- junior-session-footer:end -->

---------

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: BYK <126780+BYK@users.noreply.github.com>
@BYK

BYK commented Nov 13, 2026

Copy link
Copy Markdown
Member

Is this still relevant after that other breaking change??

@sentry-junior

sentry-junior Bot commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

No, #148 supersedes this. main now exports API_BASE_PATH ("/api/0") from @sentry/api, @sentry/api/client and @sentry/api/operations, and generates it from the spec at build time. It also switches every call site this PR touched (api-path.ts, api-path-scope.ts, core.ts, response-cache/invalidation.ts), plus idempotent-operations.ts. Rebasing this PR would only add a second constant for the same value, so I'd close it unmerged.

@sentry-junior

sentry-junior Bot commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #148, which exports API_BASE_PATH generated from the spec. Closing unmerged.

@sentry-junior sentry-junior Bot closed this Nov 13, 2026
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.

1 participant