Skip to content

Commit 6316ec5

Browse files
committed
renaming connect function to init, added env and version property, added sendConnection function to send initial connection to klepper
1 parent 9717191 commit 6316ec5

9 files changed

Lines changed: 68 additions & 18 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "klepper",
3-
"version": "0.0.45-alpha",
3+
"version": "0.0.47-alpha",
44
"license": "MIT",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/core/global.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,3 @@ export const getGlobalClientData = (): KlepperGlobal =>
66
export const setGlobalClientData = (data: KlepperGlobal): void => {
77
global.__KLEPPER__ = { ...data };
88
};
9-
10-
export const clearGlobalClientData = (): void => {
11-
setGlobalClientData({
12-
privateKey: undefined,
13-
});
14-
};

src/core/http.ts

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import * as http from "http";
22
import { RequestStatus } from "../transport/enums";
3-
import { EventResponse, KlepperEvent } from "../transport/events";
3+
import {
4+
EventResponse,
5+
KlepperConnectionEvent,
6+
KlepperEvent,
7+
} from "../transport/events";
48
import { KlepperIncomingMessage, RequestOptions } from "../transport/http";
59
import { getGlobalClientData } from "./global";
610
import { isClientConnected } from "./is";
@@ -9,7 +13,13 @@ const KLEPPER_HOST = process.env.KLEPPER_HOST || "localhost";
913
const KLEPPER_API = process.env.KLEPPER_API || "/sdk/incident";
1014
const KLEPPER_PORT = process.env.KLEPPER_PORT || 8080;
1115

12-
const createHttpOptions = (event: KlepperEvent): http.RequestOptions => {
16+
const createHttpOptions = ({
17+
event,
18+
api = KLEPPER_API,
19+
}: {
20+
event?: KlepperEvent | KlepperConnectionEvent | KlepperConnectionEvent;
21+
api?: string;
22+
}): http.RequestOptions => {
1323
const client = getGlobalClientData();
1424

1525
const { privateKey, appId } = client;
@@ -26,19 +36,43 @@ const createHttpOptions = (event: KlepperEvent): http.RequestOptions => {
2636
};
2737

2838
return {
29-
path: KLEPPER_API,
39+
path: api,
3040
...baseOptions,
3141
};
3242
};
3343

3444
const statusFromCode = (code: number) =>
3545
code >= 200 && code <= 299 ? RequestStatus.SUCCESS : RequestStatus.ERROR;
3646

47+
export const sendConnection = (
48+
connectionData: KlepperConnectionEvent
49+
): void => {
50+
const httpOptions = createHttpOptions({
51+
event: connectionData,
52+
api: "/sdk/release",
53+
});
54+
55+
const request = http.request(httpOptions, (res: KlepperIncomingMessage) => {
56+
res.setEncoding("utf8");
57+
});
58+
request.write(JSON.stringify(connectionData));
59+
request.end();
60+
};
61+
3762
export const sendEvent = async (
3863
event: KlepperEvent
3964
): Promise<EventResponse> => {
65+
const client = getGlobalClientData();
66+
67+
const baseData = {
68+
env: client?.environment,
69+
version: client?.version,
70+
};
71+
72+
const payload = Object.assign(event, baseData);
73+
4074
return new Promise<EventResponse>((resolve, reject) => {
41-
const httpOptions = createHttpOptions(event);
75+
const httpOptions = createHttpOptions({ event });
4276
if (!httpOptions) {
4377
reject({
4478
statusCode: 400,
@@ -87,7 +121,9 @@ export const sendEvent = async (
87121
});
88122
});
89123

90-
request.write(JSON.stringify(event));
124+
request.write(JSON.stringify(payload));
91125
request.end();
92126
});
93127
};
128+
129+
const send = () => {};

src/node/middlewares.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ const handleException = async (
134134
stack: error.stack as string,
135135
catchType: req ? CatchType.MIDDLEWARE : CatchType.INTERNAL,
136136
options,
137-
sdk: "NodeJS"
138137
};
139138

140139
if (req !== undefined) {

src/node/sdk.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { setGlobalClientData } from "../core/global";
2+
import { sendConnection } from "../core/http";
23
import { isClientConnected } from "../core/is";
4+
import { KlepperConnectionEvent } from "../transport/events";
35
import { KlepperOptions } from "../transport/options";
46

57
const defaultBooleanCallback = () => true;
@@ -14,15 +16,15 @@ const defaultBooleanCallback = () => true;
1416
* @example
1517
*
1618
* ```
17-
* Klepper.connect({ credentials: { privateKey: "" } }, () => {
19+
* Klepper.connect({ /credentials/, () => {
1820
* if (process.env.ENV === "prod") {
1921
* return true;
2022
* }
2123
* return false;
2224
* })
2325
* ```
2426
*/
25-
export const connect = (
27+
export const init = (
2628
options: KlepperOptions,
2729
callback = defaultBooleanCallback
2830
): void => {
@@ -42,6 +44,15 @@ export const connect = (
4244
setGlobalClientData({
4345
privateKey: options?.privateKey,
4446
appId: options?.appId,
47+
environment: options?.environment,
48+
version: options?.version,
4549
});
4650
}
51+
52+
const conn: KlepperConnectionEvent = {
53+
env: options?.environment,
54+
version: options?.version,
55+
};
56+
57+
sendConnection(conn);
4758
};

src/transport/base.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface KlepperGlobal {
88
environment?: Environment;
99
privateKey?: string;
1010
appId?: string;
11+
version?: string;
1112
}
1213

1314
export interface KlepperError extends Error {}

src/transport/events.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { CatchType, ExceptionPriority } from "./enums";
22
import { KlepperRequest } from "./http";
3+
import { Environment } from "./types";
34

45
export interface EventResponse {
56
statusCode: number;
@@ -32,5 +33,12 @@ export interface KlepperEvent {
3233
priority?: ExceptionPriority;
3334
tag?: string;
3435
};
35-
sdk: string;
36+
sdk?: string;
37+
env?: Environment;
38+
version?: string;
39+
}
40+
41+
export interface KlepperConnectionEvent {
42+
version?: string;
43+
env: Environment;
3644
}

src/transport/options.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { ExceptionPriority } from "./enums";
22
import { Environment } from "./types";
33

44
export interface KlepperOptions {
5-
environment?: Environment;
5+
version?: string;
6+
environment: Environment;
67
privateKey: string;
78
appId: string;
89
}

src/transport/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export type RequestMethodType = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
22

3-
export type Environment = undefined | "prod" | "dev" | "test";
3+
export type Environment = "prod" | "dev";

0 commit comments

Comments
 (0)