-
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 8 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
157 changes: 157 additions & 0 deletions
157
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,157 @@ | ||
| import { objectFromEntries } from './transform.js'; | ||
|
BioPhoton marked this conversation as resolved.
Outdated
|
||
| import type { | ||
| DevToolsColor, | ||
| DevToolsProperties, | ||
| EntryMeta, | ||
| MarkOptionsWithDevtools, | ||
| MarkerPayload, | ||
| MeasureOptionsWithDevtools, | ||
| TrackEntryPayload, | ||
| WithDevToolsPayload, | ||
| } from './user-timing-extensibility-api.type.js'; | ||
|
|
||
| const dataTypeTrackEntry = 'track-entry'; | ||
| const dataTypeMarker = 'marker'; | ||
|
|
||
| export function mergePropertiesWithOverwrite( | ||
| baseProperties: DevToolsProperties | undefined, | ||
| overrideProperties?: DevToolsProperties | undefined, | ||
| ) { | ||
| return Object.entries({ | ||
| ...objectFromEntries(baseProperties ?? []), | ||
| ...objectFromEntries(overrideProperties ?? []), | ||
| }) satisfies DevToolsProperties; | ||
|
BioPhoton marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| export function markerPayload(options?: Omit<MarkerPayload, 'dataType'>) { | ||
| return { | ||
| dataType: dataTypeMarker, | ||
| ...options, | ||
| } satisfies MarkerPayload; | ||
| } | ||
|
|
||
| export function trackEntryPayload( | ||
| options: Omit<TrackEntryPayload, 'dataType'>, | ||
| ) { | ||
| const { track, ...rest } = options; | ||
| return { | ||
| dataType: dataTypeTrackEntry, | ||
| track, | ||
| ...rest, | ||
| } satisfies TrackEntryPayload; | ||
| } | ||
|
|
||
| export function markerErrorPayload<T extends DevToolsColor>( | ||
| options?: Omit<MarkerPayload, 'dataType' | 'color'>, | ||
| ) { | ||
| 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; | ||
| }, | ||
| ) { | ||
| const { track, color = 'error' as const, ...restOptions } = options; | ||
| return { | ||
| dataType: dataTypeTrackEntry, | ||
| color, | ||
| track, | ||
| ...restOptions, | ||
| } satisfies TrackEntryPayload; | ||
| } | ||
|
|
||
| export function errorToDevToolsProperties(e: unknown) { | ||
| const name = e instanceof Error ? e.name : 'UnknownError'; | ||
| const message = e instanceof Error ? e.message : String(e); | ||
| return [ | ||
| ['Error Type' as const, name], | ||
| ['Error Message' as const, message], | ||
| ] satisfies DevToolsProperties; | ||
| } | ||
|
|
||
| export function errorToEntryMeta( | ||
| e: unknown, | ||
| options?: { | ||
| tooltipText?: string; | ||
| properties?: DevToolsProperties; | ||
| }, | ||
| ) { | ||
| const { properties, tooltipText } = options ?? {}; | ||
| const props = mergePropertiesWithOverwrite( | ||
| errorToDevToolsProperties(e), | ||
| properties, | ||
| ); | ||
| return { | ||
| properties: props, | ||
| ...(tooltipText ? { tooltipText } : {}), | ||
| } satisfies EntryMeta; | ||
| } | ||
|
|
||
| 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( | ||
| error: unknown, | ||
| detail?: Omit<MarkerPayload, 'color' | 'dataType'>, | ||
| ) { | ||
| const { properties, tooltipText } = detail ?? {}; | ||
| return { | ||
| dataType: dataTypeMarker, | ||
| color: 'error' as const, | ||
| ...errorToEntryMeta(error, { | ||
| properties, | ||
| tooltipText, | ||
| }), | ||
| } satisfies MarkerPayload; | ||
| } | ||
|
|
||
| /** | ||
| * asOptions wraps a DevTools payload into the `detail` property of User Timing entry options. | ||
| * | ||
| * @example | ||
| * profiler.mark('mark', asOptions({ | ||
| * dataType: 'marker', | ||
| * color: 'error', | ||
| * tooltipText: 'This is a marker', | ||
| * properties: [ | ||
| * ['str', 'This is a detail property'] | ||
| * ], | ||
| * })); | ||
| */ | ||
| export function asOptions<T extends MarkerPayload>( | ||
| devtools: T, | ||
| ): MarkOptionsWithDevtools<T>; | ||
| export function asOptions<T extends TrackEntryPayload>( | ||
| devtools: T, | ||
| ): MeasureOptionsWithDevtools<T>; | ||
| export function asOptions<T extends MarkerPayload | TrackEntryPayload>( | ||
| devtools?: T, | ||
|
BioPhoton marked this conversation as resolved.
Outdated
|
||
| ): { | ||
| detail?: WithDevToolsPayload<T>; | ||
| } { | ||
| return devtools ? { detail: { devtools } } : { detail: {} }; | ||
| } | ||
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.