|
1 | 1 | import * as http from "http"; |
2 | 2 | import { sanitizeDsn } from "../node/helpers"; |
3 | 3 | import { RequestStatus } from "../transport/enums"; |
4 | | -import { |
5 | | - EventResponse, |
6 | | - TraceoEvent, |
7 | | - TraceoReleaseEvent, |
8 | | -} from "../transport/events"; |
| 4 | +import { EventResponse, TraceoEvent } from "../transport/events"; |
9 | 5 | import { TraceoIncomingMessage, RequestOptions } from "../transport/http"; |
10 | 6 | import { getGlobalClientData } from "./global"; |
11 | 7 | import { isClientConnected } from "./is"; |
| 8 | +import { TRACEO_SDK_VERSION } from "./version"; |
12 | 9 |
|
13 | 10 | const createHttpOptions = ({ |
14 | 11 | event, |
15 | | - api, |
16 | 12 | }: { |
17 | | - event?: TraceoEvent | TraceoReleaseEvent; |
18 | | - api?: string; |
| 13 | + event?: TraceoEvent; |
19 | 14 | }): http.RequestOptions => { |
20 | 15 | const client = getGlobalClientData(); |
21 | 16 |
|
22 | 17 | const { dsn } = client; |
23 | 18 | const { host, secretKey, appId } = sanitizeDsn(dsn); |
24 | 19 |
|
25 | | - api = api === "release" ? `/release/${appId}` : `/${appId}`; |
26 | | - |
27 | 20 | const baseOptions: RequestOptions = { |
28 | 21 | hostname: host, |
29 | 22 | method: "POST", |
30 | 23 | headers: { |
31 | 24 | "Content-Type": "application/json", |
32 | 25 | "Content-Length": `${Buffer.byteLength(JSON.stringify(event))}`, |
33 | | - "traceo-secret-key": secretKey, |
| 26 | + "Traceo-Secret-Key": secretKey, |
34 | 27 | }, |
35 | 28 | }; |
36 | 29 |
|
37 | 30 | return { |
38 | | - path: api, |
| 31 | + path: `/${appId}`, |
39 | 32 | ...baseOptions, |
40 | 33 | }; |
41 | 34 | }; |
42 | 35 |
|
43 | 36 | const statusFromCode = (code: number) => |
44 | 37 | code >= 200 && code <= 299 ? RequestStatus.SUCCESS : RequestStatus.ERROR; |
45 | 38 |
|
46 | | -export const sendRelease = (connectionData: TraceoReleaseEvent): void => { |
47 | | - const httpOptions = createHttpOptions({ |
48 | | - event: connectionData, |
49 | | - api: "release", |
50 | | - }); |
| 39 | +export const sendEvent = async ( |
| 40 | + event: TraceoEvent |
| 41 | +): Promise<EventResponse | void> => { |
| 42 | + const { environment, dsn } = getGlobalClientData(); |
51 | 43 |
|
52 | | - const request = http.request(httpOptions, (res: TraceoIncomingMessage) => { |
53 | | - res.setEncoding("utf8"); |
54 | | - }); |
55 | | - request.write(JSON.stringify(connectionData)); |
56 | | - request.end(); |
57 | | -}; |
| 44 | + if (!environment || !dsn) { |
| 45 | + return; |
| 46 | + } |
58 | 47 |
|
59 | | -export const sendEvent = async (event: TraceoEvent): Promise<EventResponse> => { |
60 | | - const client = getGlobalClientData(); |
61 | | - |
62 | | - const baseData = { |
63 | | - version: client?.version, |
64 | | - }; |
| 48 | + const version = TRACEO_SDK_VERSION; |
| 49 | + const baseData = { version, env: environment }; |
65 | 50 |
|
66 | 51 | const payload = Object.assign(event, baseData); |
67 | 52 |
|
|
0 commit comments