| 1 |
// h/t https://stackoverflow.com/a/59801602/93579 |
| 2 |
type ExcludeProps< T > = { [ k: string ]: any } & { [ K in keyof T ]?: never }; |
| 3 |
|
| 4 |
import { onCLS, onFCP, onINP, onLCP, onTTFB } from 'web-vitals'; |
| 5 |
import { |
| 6 |
onCLS as onCLSWithAttribution, |
| 7 |
onFCP as onFCPWithAttribution, |
| 8 |
onINP as onINPWithAttribution, |
| 9 |
onLCP as onLCPWithAttribution, |
| 10 |
onTTFB as onTTFBWithAttribution, |
| 11 |
} from 'web-vitals/attribution'; |
| 12 |
|
| 13 |
export interface ElementData { |
| 14 |
isLCP: boolean; |
| 15 |
isLCPCandidate: boolean; |
| 16 |
xpath: string; |
| 17 |
intersectionRatio: number; |
| 18 |
intersectionRect: DOMRectReadOnly; |
| 19 |
boundingClientRect: DOMRectReadOnly; |
| 20 |
} |
| 21 |
|
| 22 |
export type ExtendedElementData = ExcludeProps< ElementData >; |
| 23 |
|
| 24 |
export interface URLMetric { |
| 25 |
url: string; |
| 26 |
viewport: { |
| 27 |
width: number; |
| 28 |
height: number; |
| 29 |
}; |
| 30 |
elements: ElementData[]; |
| 31 |
} |
| 32 |
|
| 33 |
export type ExtendedRootData = ExcludeProps< URLMetric >; |
| 34 |
|
| 35 |
export interface URLMetricGroupStatus { |
| 36 |
minimumViewportWidth: number; |
| 37 |
maximumViewportWidth: number | null; |
| 38 |
complete: boolean; |
| 39 |
} |
| 40 |
|
| 41 |
export type OnTTFBFunction = typeof onTTFB; |
| 42 |
export type OnFCPFunction = typeof onFCP; |
| 43 |
export type OnLCPFunction = typeof onLCP; |
| 44 |
export type OnINPFunction = typeof onINP; |
| 45 |
export type OnCLSFunction = typeof onCLS; |
| 46 |
export type OnTTFBWithAttributionFunction = typeof onTTFBWithAttribution; |
| 47 |
export type OnFCPWithAttributionFunction = typeof onFCPWithAttribution; |
| 48 |
export type OnLCPWithAttributionFunction = typeof onLCPWithAttribution; |
| 49 |
export type OnINPWithAttributionFunction = typeof onINPWithAttribution; |
| 50 |
export type OnCLSWithAttributionFunction = typeof onCLSWithAttribution; |
| 51 |
|
| 52 |
export type LogFunction = ( ...message: any[] ) => void; |
| 53 |
|
| 54 |
export interface Logger { |
| 55 |
log: LogFunction; |
| 56 |
info: LogFunction; |
| 57 |
warn: LogFunction; |
| 58 |
error: LogFunction; |
| 59 |
} |
| 60 |
|
| 61 |
export type GetRootDataFunction = () => URLMetric; |
| 62 |
export type ExtendRootDataFunction = ( properties: ExtendedRootData ) => void; |
| 63 |
export type GetElementDataFunction = ( xpath: string ) => ElementData | null; |
| 64 |
export type ExtendElementDataFunction = ( |
| 65 |
xpath: string, |
| 66 |
properties: ExtendedElementData |
| 67 |
) => void; |
| 68 |
|
| 69 |
export type InitializeArgs = { |
| 70 |
readonly isDebug: boolean; |
| 71 |
readonly log: LogFunction; |
| 72 |
readonly info: LogFunction; |
| 73 |
readonly warn: LogFunction; |
| 74 |
readonly error: LogFunction; |
| 75 |
readonly onTTFB: OnTTFBFunction | OnTTFBWithAttributionFunction; |
| 76 |
readonly onFCP: OnFCPFunction | OnFCPWithAttributionFunction; |
| 77 |
readonly onLCP: OnLCPFunction | OnLCPWithAttributionFunction; |
| 78 |
readonly onINP: OnINPFunction | OnINPWithAttributionFunction; |
| 79 |
readonly onCLS: OnCLSFunction | OnCLSWithAttributionFunction; |
| 80 |
readonly getRootData: GetRootDataFunction; |
| 81 |
readonly extendRootData: ExtendRootDataFunction; |
| 82 |
readonly getElementData: GetElementDataFunction; |
| 83 |
readonly extendElementData: ExtendElementDataFunction; |
| 84 |
}; |
| 85 |
|
| 86 |
export type InitializeCallback = ( args: InitializeArgs ) => Promise< void >; |
| 87 |
|
| 88 |
export type FinalizeArgs = { |
| 89 |
readonly getRootData: GetRootDataFunction; |
| 90 |
readonly extendRootData: ExtendRootDataFunction; |
| 91 |
readonly getElementData: GetElementDataFunction; |
| 92 |
readonly extendElementData: ExtendElementDataFunction; |
| 93 |
readonly isDebug: boolean; |
| 94 |
readonly log: LogFunction; |
| 95 |
readonly info: LogFunction; |
| 96 |
readonly warn: LogFunction; |
| 97 |
readonly error: LogFunction; |
| 98 |
}; |
| 99 |
|
| 100 |
export type FinalizeCallback = ( args: FinalizeArgs ) => Promise< void >; |
| 101 |
|
| 102 |
export interface Extension { |
| 103 |
readonly name?: string; |
| 104 |
readonly initialize?: InitializeCallback; |
| 105 |
readonly finalize?: FinalizeCallback; |
| 106 |
} |
| 107 |
|