Repository navigation
feat(api-client)!: Read the API base path from the spec's server URL #148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+333
−152
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /** | ||
| * Reads the API base path, such as `/api/0`, from the spec's server URL and | ||
| * leaves every operation path relative to it. | ||
| * | ||
| * The spec declares it in `servers` (`https://{region}.sentry.io/api/0`), and | ||
| * its paths are relative to it. The client takes the Sentry origin as | ||
| * `baseUrl` and adds the base path itself, so the servers keep only their | ||
| * origin. | ||
| */ | ||
|
|
||
| function trimTrailingSlashes(path) { | ||
| let end = path.length; | ||
| while (end > 0 && path[end - 1] === "/") end -= 1; | ||
| return path.slice(0, end); | ||
| } | ||
|
|
||
| function splitServerUrl(url) { | ||
| if (!URL.canParse(url)) throw new Error(`Unsupported server URL: ${url}`); | ||
| const { origin, pathname } = new URL(url); | ||
| return { origin, path: trimTrailingSlashes(pathname) }; | ||
| } | ||
|
sentry-junior[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| // TODO: Remove once Sentry's spec declares the base path in `servers`. | ||
| // Until then, every path starts with the same `/api/<version>/`. | ||
| function legacyBasePath(paths) { | ||
| const prefixes = new Set(Object.keys(paths).map(path => { | ||
| const [root, api, version, ...rest] = path.split("/"); | ||
| return root === "" && api === "api" && version && rest.length > 0 ? `/api/${version}` : undefined; | ||
| })); | ||
| const [prefix] = prefixes; | ||
| if (prefixes.size !== 1 || prefix === undefined) { | ||
| throw new Error("The server URL has no base path, and the paths do not share one"); | ||
| } | ||
| return prefix; | ||
| } | ||
|
|
||
| /** | ||
| * Make `spec`'s paths relative to its API base path, reduce its server URLs | ||
| * to their origins, and return the base path. A spec without servers, such | ||
| * as a test fixture, is left unchanged and has no base path. | ||
| * @param {{servers?: {url: string}[], paths: Record<string, object>}} spec | ||
| * @returns {string} The base path, such as `/api/0`, or `""` | ||
| */ | ||
| export function splitApiBasePath(spec) { | ||
| if (!spec.servers?.length) return ""; | ||
| let basePath = splitServerUrl(spec.servers[0].url).path; | ||
| if (basePath === "") { | ||
| basePath = legacyBasePath(spec.paths); | ||
| spec.paths = Object.fromEntries( | ||
| Object.entries(spec.paths).map(([path, item]) => [path.slice(basePath.length), item]), | ||
| ); | ||
| } | ||
|
|
||
| // An operation can override the servers, but not move off the base path. | ||
| const toOrigins = servers => servers.map(server => { | ||
| const { origin, path } = splitServerUrl(server.url); | ||
| if (path !== "" && path !== basePath) { | ||
| throw new Error(`Server ${server.url} is not under the API base path ${basePath}`); | ||
| } | ||
| return { ...server, url: origin }; | ||
| }); | ||
| spec.servers = toOrigins(spec.servers); | ||
| for (const pathItem of Object.values(spec.paths)) { | ||
| for (const operation of Object.values(pathItem)) { | ||
| if (operation?.servers) operation.servers = toOrigins(operation.servers); | ||
| } | ||
| } | ||
| return basePath; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.