Skip to content

Commit f778e13

Browse files
committed
refactor(core): Move hasProp
Signed-off-by: Kaung Zin Hein <[email protected]>
1 parent 8ea71ba commit f778e13

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

packages/browser/src/integrations/graphqlClient.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
SEMANTIC_ATTRIBUTE_SENTRY_OP,
66
SEMANTIC_ATTRIBUTE_URL_FULL,
77
defineIntegration,
8+
hasProp,
89
spanToJSON,
910
} from '@sentry/core';
1011
import type { Client, IntegrationFn } from '@sentry/core';
@@ -136,11 +137,6 @@ export function getRequestPayloadXhrOrFetch(hint: XhrHint | FetchHint): string |
136137
return body;
137138
}
138139

139-
// Duplicate from deprecated @sentry-utils/src/instrument/fetch.ts
140-
function hasProp<T extends string>(obj: unknown, prop: T): obj is Record<string, string> {
141-
return !!obj && typeof obj === 'object' && !!(obj as Record<string, string>)[prop];
142-
}
143-
144140
/**
145141
* Parses the fetch arguments to extract the request payload.
146142
* Exported for tests only.

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ export {
9696
extractQueryParamsFromUrl,
9797
headersToDict,
9898
} from './utils/request';
99+
export { hasProp } from './utils/hasProp';
99100
export { DEFAULT_ENVIRONMENT } from './constants';
100101
export { addBreadcrumb } from './breadcrumbs';
101102
export { functionToStringIntegration } from './integrations/functiontostring';

packages/core/src/utils/hasProp.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* A more comprehensive key property check.
3+
*/
4+
export function hasProp<T extends string>(obj: unknown, prop: T): obj is Record<string, unknown> {
5+
return !!obj && typeof obj === 'object' && !!(obj as Record<string, unknown>)[prop];
6+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { hasProp } from '../../../src/utils/hasProp';
2+
3+
describe('hasProp', () => {
4+
it('should return true if the object has the provided property', () => {
5+
const obj = { a: 1 };
6+
const result = hasProp(obj, 'a');
7+
expect(result).toBe(true);
8+
});
9+
10+
it('should return false if the object does not have the provided property', () => {
11+
const obj = { a: 1 };
12+
const result = hasProp(obj, 'b');
13+
expect(result).toBe(false);
14+
});
15+
16+
it('should return false if the object is null', () => {
17+
const obj = null;
18+
const result = hasProp(obj, 'a');
19+
expect(result).toBe(false);
20+
});
21+
22+
it('should return false if the object is undefined', () => {
23+
const obj = undefined;
24+
const result = hasProp(obj, 'a');
25+
expect(result).toBe(false);
26+
});
27+
});

0 commit comments

Comments
 (0)