-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathrrweb.ts
More file actions
80 lines (67 loc) · 1.8 KB
/
rrweb.ts
File metadata and controls
80 lines (67 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { EventType } from '@rrweb/types'
import { z } from 'zod'
import { BrowserReplayEvent } from './schema'
const RecordingEndEventSchema = z.object({
tag: z.literal('recording-end'),
payload: z.object({}),
})
const PageStartEventSchema = z.object({
tag: z.literal('page-start'),
payload: z.object({
title: z.string(),
href: z.string(),
width: z.number(),
height: z.number(),
}),
})
const ActionBeginEventSchema = z.object({
tag: z.literal('action-begin'),
payload: z.object({
actionId: z.string(),
}),
})
const ActionEndEventSchema = z.object({
tag: z.literal('action-end'),
payload: z.object({
actionId: z.string(),
}),
})
const CustomReplayEventSchema = z.discriminatedUnion('tag', [
RecordingEndEventSchema,
PageStartEventSchema,
ActionBeginEventSchema,
ActionEndEventSchema,
])
const RrwebCustomEventSchema = z.object({
type: z.nativeEnum(EventType),
data: CustomReplayEventSchema,
timestamp: z.number(),
})
export type RecordingEndEvent = z.infer<typeof RecordingEndEventSchema>
export type PageStartEvent = z.infer<typeof PageStartEventSchema>
export type CustomReplayEvent = z.infer<typeof CustomReplayEventSchema>
type CustomReplayEventMap = {
[P in CustomReplayEvent['tag']]: Extract<CustomReplayEvent, { tag: P }>
}
export function parseReplayEvent(event: unknown) {
return RrwebCustomEventSchema.parse(event)
}
interface CreateReplayEventOptions<T extends keyof CustomReplayEventMap> {
tag: T
payload: CustomReplayEventMap[T]['payload']
timestamp?: number
}
export function createReplayEvent<T extends keyof CustomReplayEventMap>({
tag,
payload,
timestamp,
}: CreateReplayEventOptions<T>): BrowserReplayEvent {
return {
type: EventType.Custom,
timestamp: timestamp ?? Date.now(),
data: {
tag,
payload,
},
}
}