-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathcopyEvent.ts
More file actions
94 lines (86 loc) · 3.04 KB
/
Copy pathcopyEvent.ts
File metadata and controls
94 lines (86 loc) · 3.04 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import type { EndpointBuilder } from '@datadog/js-core/transport'
import { createEndpointBuilder } from '@datadog/js-core/transport'
import { copy } from '../../../copy'
import type { SdkInfos } from '../../../hooks/useSdkInfos'
import type { SdkEvent } from '../../../sdkEvent'
import { getEventSource, EventSource } from '../../../sdkEvent'
export function canCopyEvent(sdkInfos: SdkInfos | undefined, event: SdkEvent): sdkInfos is SdkInfos {
return Boolean(sdkInfos && getIntakeUrlForEvent(sdkInfos, event))
}
/**
* Copy the event as a curl command to the clipboard.
*
* This function is "naive" in the sense that it does not reflect the actual request done by the
* SDK:
*
* * Request payloads are sometimes compressed, and we don't compress them here.
*
* * The intake URL is computed using the a version of the SDK that might not match one used by the
* website.
*
* * Various tags like "api", "flush_reason", "retry_count" and "retry_after" are not included or
* hardcoded.
*
* * Various browser headers like "User-Agent" are not included.
*/
export function copyEventAsCurl(sdkInfos: SdkInfos, event: SdkEvent) {
const url = getIntakeUrlForEvent(sdkInfos, event)
copy(`curl '${url}' \\
-X POST \\
-H 'Content-Type: text/plain' \\
--data-raw ${escapeShellParameter(JSON.stringify(event))}`)
}
/**
* Copy the event as a fetch API call to the clipboard.
*
* This function is "naive" in the sense that it does not reflect the actual request done by the
* SDK:
*
* * Request payloads are sometimes compressed, and we don't compress them here.
*
* * The intake URL is computed using the a version of the SDK that might not match one used by the
* website.
*
* * Various tags like "api", "flush_reason", "retry_count" and "retry_after" are not included or
* hardcoded.
*
* * Various browser headers like "User-Agent" are not included.
*/
export function copyEventAsFetch(sdkInfos: SdkInfos, event: SdkEvent) {
const url = getIntakeUrlForEvent(sdkInfos, event)
copy(`fetch('${url}', {
method: 'POST',
headers: {
'Content-Type': 'text/plain'
},
body: JSON.stringify(${JSON.stringify(event, null, 2)})
})`)
}
export function escapeShellParameter(value: string) {
return `$'${value.replace(/\\/g, '\\\\').replace(/'/g, "\\'")}'`
}
export function getIntakeUrlForEvent(sdkInfos: SdkInfos, event: SdkEvent) {
let builder: EndpointBuilder
let version: string
switch (getEventSource(event)) {
case EventSource.RUM:
case EventSource.TELEMETRY: {
if (!sdkInfos.rum?.config || !sdkInfos.rum?.version) {
return
}
version = sdkInfos.rum.version
builder = createEndpointBuilder(sdkInfos.rum.config, 'rum')
break
}
case EventSource.LOGS:
if (!sdkInfos.logs?.config || !sdkInfos.logs?.version) {
return
}
version = sdkInfos.logs.version
builder = createEndpointBuilder(sdkInfos.logs.config, 'logs')
break
}
return builder
.build('manual', {})
.replace(/dd-evp-origin-version=[^&]+/g, `dd-evp-origin-version=${encodeURIComponent(version)}`)
}