-
Notifications
You must be signed in to change notification settings - Fork 15
refactor: add extensibility API helper #1207
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
BioPhoton
merged 13 commits into
main
from
feat/utils/user-timing-extensibility-api.types.ts
Jan 9, 2026
Merged
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1fa6d3d
refactor: add extensibility API helper
44e22df
refactor: add type tests
b979e1d
refactor: remove unused type
a4ef406
refactor: rename tests file
24b5959
refactor: wip
11fa63e
Merge branch 'refs/heads/main' into feat/utils/user-timing-extensibil…
a9088cd
refactor: wip
815a61e
refactor: fix lint
1514523
Update packages/utils/src/lib/user-timing-extensibility-api-utils.ts
BioPhoton ebf934a
refactor: adjust types
0f61e62
refactor: revert
4a42270
refactor: fix lint
26358b6
refactor: fix lint
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
166 changes: 166 additions & 0 deletions
166
packages/utils/src/lib/user-timing-extensibility-api-utils.ts
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,166 @@ | ||
| import type { | ||
| DevToolsColor, | ||
| DevToolsProperties, | ||
| EntryMeta, | ||
| MarkOptionsWithDevtools, | ||
| MarkerPayload, | ||
| MeasureOptionsWithDevtools, | ||
| TrackEntryPayload, | ||
| } from './user-timing-extensibility-api.type.js'; | ||
|
|
||
| const dataTypeTrackEntry = 'track-entry'; | ||
| const dataTypeMarker = 'marker'; | ||
|
|
||
| export function objToPropertiesPayload( | ||
| object: Record<string, string | number | boolean | object | undefined>, | ||
| ): DevToolsProperties { | ||
| return Object.entries(object); | ||
|
BioPhoton marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| export function mergePropertiesWithOverwrite( | ||
| baseProperties: DevToolsProperties | undefined, | ||
| overrideProperties?: DevToolsProperties | undefined, | ||
| ): DevToolsProperties { | ||
| return objToPropertiesPayload({ | ||
| ...Object.fromEntries( | ||
| (baseProperties ?? []).map(([key, value]) => [key, String(value)]), | ||
| ), | ||
| ...Object.fromEntries( | ||
| (overrideProperties ?? []).map(([key, value]) => [key, String(value)]), | ||
|
BioPhoton marked this conversation as resolved.
Outdated
|
||
| ), | ||
| }); | ||
| } | ||
|
|
||
| export function markerPayload( | ||
| options?: Omit<MarkerPayload, 'dataType'>, | ||
| ): MarkerPayload { | ||
| return { | ||
| dataType: dataTypeMarker, | ||
| ...options, | ||
| }; | ||
| } | ||
|
|
||
| export function trackEntryPayload( | ||
| options: Omit<TrackEntryPayload, 'dataType'>, | ||
| ): TrackEntryPayload { | ||
| const { track, ...rest } = options; | ||
| return { | ||
| dataType: dataTypeTrackEntry, | ||
| track, | ||
| ...rest, | ||
| }; | ||
| } | ||
|
|
||
| export function markerErrorPayload<T extends DevToolsColor>( | ||
| options?: Omit<MarkerPayload, 'dataType' | 'color'>, | ||
| ): MarkerPayload { | ||
| return { | ||
| dataType: dataTypeMarker, | ||
| color: 'error' as T, | ||
| ...options, | ||
| } satisfies MarkerPayload; | ||
| } | ||
|
|
||
| export function trackEntryErrorPayload< | ||
| T extends string, | ||
| C extends DevToolsColor, | ||
| >( | ||
| options: Omit<TrackEntryPayload, 'color' | 'dataType'> & { | ||
| track: T; | ||
| color?: C; | ||
| }, | ||
| ): TrackEntryPayload { | ||
| const { track, color = 'error', ...restOptions } = options; | ||
| return { | ||
| dataType: dataTypeTrackEntry, | ||
| color, | ||
| track, | ||
| ...restOptions, | ||
| } satisfies TrackEntryPayload; | ||
| } | ||
|
|
||
| export function errorToDevToolsProperties(e: unknown): DevToolsProperties { | ||
| const name = e instanceof Error ? e.name : 'UnknownError'; | ||
| const message = e instanceof Error ? e.message : String(e); | ||
| return [ | ||
| ['Error Type', name], | ||
| ['Error Message', message], | ||
| ]; | ||
| } | ||
|
|
||
| export function errorToEntryMeta( | ||
| e: unknown, | ||
| options?: { | ||
| tooltipText?: string; | ||
| properties?: DevToolsProperties; | ||
| }, | ||
| ): EntryMeta { | ||
| const { properties, tooltipText } = options ?? {}; | ||
| const props = mergePropertiesWithOverwrite( | ||
| errorToDevToolsProperties(e), | ||
| properties, | ||
| ); | ||
| return { | ||
| properties: props, | ||
| ...(tooltipText ? { tooltipText } : {}), | ||
| }; | ||
| } | ||
|
|
||
| export function errorToTrackEntryPayload<T extends string>( | ||
| error: unknown, | ||
| detail: Omit<TrackEntryPayload, 'color' | 'dataType'> & { | ||
| track: T; | ||
| }, | ||
| ) { | ||
| const { properties, tooltipText, ...trackPayload } = detail; | ||
| return { | ||
| dataType: dataTypeTrackEntry, | ||
| color: 'error' as const, | ||
| ...trackPayload, | ||
| ...errorToEntryMeta(error, { | ||
| properties, | ||
| tooltipText, | ||
| }), | ||
| } satisfies TrackEntryPayload; | ||
| } | ||
|
|
||
| export function errorToMarkerPayload<T extends DevToolsColor>( | ||
| error: unknown, | ||
| detail?: Omit<MarkerPayload, 'color' | 'dataType'>, | ||
| ): MarkerPayload { | ||
| const { properties, tooltipText } = detail ?? {}; | ||
| return { | ||
| dataType: dataTypeMarker, | ||
| color: 'error' as T, | ||
| ...errorToEntryMeta(error, { | ||
| properties, | ||
| tooltipText, | ||
| }), | ||
| } satisfies MarkerPayload; | ||
| } | ||
|
|
||
| /** | ||
| * | ||
| * @example | ||
| * profiler.mark('mark', asOptions({ | ||
| * properties: [ | ||
| * ['str', 'This is a detail property'], | ||
| * ['num', 42], | ||
| * ['object', { str: '42', num: 42 }], | ||
| * ['array', [42, 42, 42]], | ||
| * ], | ||
| * })); | ||
| */ | ||
| export function asOptions( | ||
| devtools: MarkerPayload, | ||
| ): Pick<MarkOptionsWithDevtools, 'detail'>; | ||
| export function asOptions( | ||
| devtools: TrackEntryPayload, | ||
| ): Pick<MeasureOptionsWithDevtools, 'detail'>; | ||
| export function asOptions( | ||
| devtools: MarkerPayload | TrackEntryPayload, | ||
| ): | ||
| | Pick<MarkOptionsWithDevtools, 'detail'> | ||
| | Pick<MeasureOptionsWithDevtools, 'detail'> { | ||
| return devtools ? { detail: { devtools } } : { detail: {} }; | ||
| } | ||
|
BioPhoton marked this conversation as resolved.
Outdated
|
||
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.