-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat: Add API class to help with endpoints #1459
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
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
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,97 @@ | ||
| import { DSNLike } from '@sentry/types'; | ||
| import { urlEncode } from '@sentry/utils/object'; | ||
| import { DSN } from './dsn'; | ||
|
|
||
| const SENTRY_API_VERSION = '7'; | ||
|
|
||
| /** Helper class to provide urls to different Sentry endpoints. */ | ||
| export class API { | ||
| /** The internally used DSN object. */ | ||
| private readonly dsnObject: DSN; | ||
| /** Create a new instance of API */ | ||
| public constructor(public dsn: DSNLike) { | ||
| this.dsnObject = new DSN(dsn); | ||
| } | ||
|
|
||
| /** Returns the DSN object. */ | ||
| public getDSN(): DSN { | ||
| return this.dsnObject; | ||
| } | ||
|
|
||
| /** Returns a string with auth headers in the url to the store endpoint. */ | ||
| public getStoreEndpoint(): string { | ||
| return `${this.getBaseUrl()}${this.getStoreEndpointPath()}`; | ||
| } | ||
|
|
||
| /** Returns the store endpoint with auth added in url encoded. */ | ||
| public getStoreEndpointWithUrlEncodedAuth(): string { | ||
| const dsn = this.dsnObject; | ||
| const auth = { | ||
| sentry_key: dsn.user, | ||
| sentry_version: SENTRY_API_VERSION, | ||
| }; | ||
| // Auth is intentionally sent as part of query string (NOT as custom HTTP header) | ||
| // to avoid preflight CORS requests | ||
| return `${this.getStoreEndpoint()}?${urlEncode(auth)}`; | ||
| } | ||
|
|
||
| /** Returns the base path of the url including the port. */ | ||
| private getBaseUrl(): string { | ||
| const dsn = this.dsnObject; | ||
| const protocol = dsn.protocol ? `${dsn.protocol}:` : ''; | ||
| const port = dsn.port ? `:${dsn.port}` : ''; | ||
| return `${protocol}//${dsn.host}${port}`; | ||
| } | ||
|
|
||
| /** Returns only the path component for the store endpoint. */ | ||
| public getStoreEndpointPath(): string { | ||
| const dsn = this.dsnObject; | ||
| return `${dsn.path ? `/${dsn.path}` : ''}/api/${dsn.projectId}/store/`; | ||
| } | ||
|
|
||
| /** Returns an object that can be used in request headers. */ | ||
| public getRequestHeaders(clientName: string, clientVersion: string): { [key: string]: string } { | ||
| const dsn = this.dsnObject; | ||
| const header = [`Sentry sentry_version=${SENTRY_API_VERSION}`]; | ||
| header.push(`sentry_timestamp=${new Date().getTime()}`); | ||
| header.push(`sentry_client=${clientName}/${clientVersion}`); | ||
| header.push(`sentry_key=${dsn.user}`); | ||
| return { | ||
| 'Content-Type': 'application/json', | ||
| 'X-Sentry-Auth': header.join(', '), | ||
| }; | ||
| } | ||
|
|
||
| /** Returns the url to the report dialog endpoint. */ | ||
| public getReportDialogEndpoint( | ||
| dialogOptions: { | ||
| [key: string]: any; | ||
| user?: { name?: string; email?: string }; | ||
| } = {}, | ||
| ): string { | ||
| const dsn = this.dsnObject; | ||
| const endpoint = `${this.getBaseUrl()}${dsn.path ? `/${dsn.path}` : ''}/api/embed/error-page/`; | ||
|
|
||
| const encodedOptions = []; | ||
| for (const key in dialogOptions) { | ||
| if (key === 'user') { | ||
| if (!dialogOptions.user) { | ||
| continue; | ||
| } | ||
| if (dialogOptions.user.name) { | ||
| encodedOptions.push(`name=${encodeURIComponent(dialogOptions.user.name)}`); | ||
| } | ||
| if (dialogOptions.user.email) { | ||
| encodedOptions.push(`email=${encodeURIComponent(dialogOptions.user.email)}`); | ||
| } | ||
| } else { | ||
| encodedOptions.push(`${encodeURIComponent(key)}=${encodeURIComponent(dialogOptions[key] as string)}`); | ||
| } | ||
| } | ||
| if (encodedOptions.length) { | ||
| return `${endpoint}?${encodedOptions.join('&')}`; | ||
| } | ||
|
|
||
| return endpoint; | ||
| } | ||
| } | ||
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,53 @@ | ||
| import { API } from '../../src/api'; | ||
| import { DSN } from '../../src/dsn'; | ||
|
|
||
| const dsnPublic = 'https://abc@sentry.io:1234/subpath/123'; | ||
|
|
||
| describe('API', () => { | ||
| test('getStoreEndpoint', () => { | ||
| expect(new API(dsnPublic).getStoreEndpointWithUrlEncodedAuth()).toEqual( | ||
| 'https://sentry.io:1234/subpath/api/123/store/?sentry_key=abc&sentry_version=7', | ||
| ); | ||
| expect(new API(dsnPublic).getStoreEndpoint()).toEqual('https://sentry.io:1234/subpath/api/123/store/'); | ||
| }); | ||
|
|
||
| test('getRequestHeaders', () => { | ||
| expect(new API(dsnPublic).getRequestHeaders('a', '1.0')).toMatchObject({ | ||
| 'Content-Type': 'application/json', | ||
| 'X-Sentry-Auth': expect.stringMatching( | ||
| /^Sentry sentry_version=\d, sentry_timestamp=\d+, sentry_client=a\/1\.0, sentry_key=abc$/, | ||
| ), | ||
| }); | ||
| }); | ||
| test('getReportDialogEndpoint', () => { | ||
| expect(new API(dsnPublic).getReportDialogEndpoint({})).toEqual( | ||
| 'https://sentry.io:1234/subpath/api/embed/error-page/', | ||
| ); | ||
| expect( | ||
| new API(dsnPublic).getReportDialogEndpoint({ | ||
| eventId: 'abc', | ||
| testy: '2', | ||
| }), | ||
| ).toEqual('https://sentry.io:1234/subpath/api/embed/error-page/?eventId=abc&testy=2'); | ||
|
|
||
| expect( | ||
| new API(dsnPublic).getReportDialogEndpoint({ | ||
| eventId: 'abc', | ||
| user: { | ||
| email: 'email', | ||
| name: 'yo', | ||
| }, | ||
| }), | ||
| ).toEqual('https://sentry.io:1234/subpath/api/embed/error-page/?eventId=abc&name=yo&email=email'); | ||
|
|
||
| expect( | ||
| new API(dsnPublic).getReportDialogEndpoint({ | ||
| eventId: 'abc', | ||
| user: undefined, | ||
| }), | ||
| ).toEqual('https://sentry.io:1234/subpath/api/embed/error-page/?eventId=abc'); | ||
| }); | ||
| test('getDSN', () => { | ||
| expect(new API(dsnPublic).getDSN()).toEqual(new DSN(dsnPublic)); | ||
| }); | ||
| }); |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here we use
sentry_clientand above we usedsentry_version. We should unify it somehow.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not really sure what you mean, we need
sentry_clientfor sending aPOST.