Skip to content

Commit

Permalink
feat: extract query_string from window.location to SentryRequestBody
Browse files Browse the repository at this point in the history
* chore: extend SentryRequestBody.request with query_string field

This change can be useful when we divide url for two parts - url and query params (or search params)

* feat: extract query_string from window.location to SentryRequestBody

---------

Co-authored-by: Edem <[email protected]>
  • Loading branch information
ereshidov and Edem authored May 5, 2024
1 parent 2d3a9fc commit 721312b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
13 changes: 13 additions & 0 deletions libs/browser/src/lib/services/browser-micro-sentry-client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,13 @@ describe('BrowserMicroSentryClient', () => {
});

it('Set data is in data to send', () => {
Object.defineProperty(window, 'location', {
value: new URL(
'https://example.com?env=development&auth=123&code=3212'
),
configurable: true,
});

client.report({ message: 'Error', name: 'Error', stack: '' });

expect(sendSpy).toHaveBeenCalledWith({
Expand All @@ -356,6 +363,7 @@ describe('BrowserMicroSentryClient', () => {
'User-Agent': expect.any(String),
},
url: expect.any(String),
query_string: 'env=development&auth=123&code=3212',
},
sdk: { name: 'micro-sentry.javascript.browser', version: '0.0.0' },
extra: {
Expand All @@ -368,6 +376,10 @@ describe('BrowserMicroSentryClient', () => {
});

it('Message has correct fields and log level', () => {
Object.defineProperty(window, 'location', {
value: '/some/path?env=development&auth=123&code=3212',
configurable: true,
});
client.captureMessage('Message', Severity.debug);

expect(sendSpy).toHaveBeenCalledWith({
Expand All @@ -380,6 +392,7 @@ describe('BrowserMicroSentryClient', () => {
'User-Agent': expect.any(String),
},
url: expect.any(String),
query_string: 'env=development&auth=123&code=3212',
},
sdk: { name: 'micro-sentry.javascript.browser', version: '0.0.0' },
extra: {
Expand Down
26 changes: 25 additions & 1 deletion libs/browser/src/lib/services/browser-micro-sentry-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
SentryRequestBody,
Severity,
Tags,
QueryString,
} from '@micro-sentry/core';
import { State } from '../models/state';
import { MicroSentryPlugin } from '../models/plugin';
Expand Down Expand Up @@ -166,10 +167,33 @@ export class BrowserMicroSentryClient extends MicroSentryClient {
);
}

extractQueryString(originalUrl: string): QueryString | undefined {
let url = originalUrl;

if (!url) {
return undefined;
}

if (url.startsWith('/')) {
url = `http://prefix${url}`;
}

try {
const queryString = new URL(url).search.slice(1);

return queryString.length ? queryString : undefined;
} catch (e) {
return undefined;
}
}

protected override getRequestBlank(): SentryRequestBody {
const url = this.window.location.toString();

return {
request: {
url: this.window.location.toString(),
url,
query_string: this.extractQueryString(url),
headers: {
'User-Agent': this.window.navigator.userAgent,
},
Expand Down
6 changes: 6 additions & 0 deletions libs/core/src/lib/models/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export type Tags = Partial<{ [key: string]: string }>;

export type Extras = Partial<{ [key: string]: unknown }>;

export type QueryString =
| string
| { [key: string]: string }
| Array<[string, string]>;

export interface User {
id?: string;
ip_address?: string;
Expand All @@ -39,6 +44,7 @@ export interface SentryRequestBody {
timestamp: number;
event_id: string;
request?: {
query_string?: QueryString;
url?: string;
headers?: {
'User-Agent': string;
Expand Down

0 comments on commit 721312b

Please sign in to comment.