1- import {
2- RequestPayload ,
3- RequestOptions ,
4- KlepperResponse ,
5- KlepperIncomingMessage ,
6- RequestStatus ,
7- } from "../transport" ;
81import * as http from "http" ;
2+ import { RequestStatus } from "../transport/enums" ;
3+ import { EventResponse , KlepperEvent } from "../transport/events" ;
4+ import { KlepperIncomingMessage , RequestOptions } from "../transport/http" ;
95import { getGlobalClientData } from "./global" ;
6+ import { isClientConnected } from "./is" ;
107
118const KLEPPER_HOST = process . env . KLEPPER_HOST || "localhost" ;
129const KLEPPER_API = process . env . KLEPPER_API || "/test" ;
1310const KLEPPER_PORT = process . env . KLEPPER_PORT || 3000 ;
1411
15- const createHttpOptions = ( payload : RequestPayload ) : http . RequestOptions => {
12+ const createHttpOptions = ( event : KlepperEvent ) : http . RequestOptions => {
1613 const client = getGlobalClientData ( ) ;
1714
1815 const { privateKey } = client ;
19- const { data } = payload ;
2016 const baseOptions : RequestOptions = {
2117 hostname : KLEPPER_HOST ,
2218 port : + KLEPPER_PORT ,
2319 method : "POST" ,
2420 headers : {
2521 "Content-Type" : "application/json" ,
26- "Content-Length" : `${ Buffer . byteLength ( JSON . stringify ( data ) ) } ` ,
22+ "Content-Length" : `${ Buffer . byteLength ( JSON . stringify ( event ) ) } ` ,
2723 "Klepper-Project-Key" : privateKey as string ,
2824 } ,
2925 } ;
@@ -37,60 +33,60 @@ const createHttpOptions = (payload: RequestPayload): http.RequestOptions => {
3733const statusFromCode = ( code : number ) =>
3834 code >= 200 && code <= 299 ? RequestStatus . SUCCESS : RequestStatus . ERROR ;
3935
40- /**
41- * Send data to Klepper server
42- *
43- * @param payload
44- */
4536export const sendEvent = async (
46- payload : RequestPayload
47- ) : Promise < KlepperResponse > => {
48- const { data } = payload ;
49-
50- try {
51- return new Promise < KlepperResponse > ( ( resolve , reject ) => {
52- const httpOptions = createHttpOptions ( payload ) ;
53- const client = getGlobalClientData ( ) ;
54-
55- if ( ! httpOptions ) {
56- reject ( new Error ( "NO HTTP OPTIONS" ) ) ;
57- }
37+ event : KlepperEvent
38+ ) : Promise < EventResponse > => {
39+ return new Promise < EventResponse > ( ( resolve , reject ) => {
40+ const httpOptions = createHttpOptions ( event ) ;
41+ if ( ! httpOptions ) {
42+ reject ( {
43+ statusCode : 400 ,
44+ statusMessage :
45+ "[Klepper] Error during sending event to Klepper. No HTTP options." ,
46+ } ) ;
47+ }
5848
59- if ( ! client ) {
60- reject ( new Error ( "NO CLIENT DATA IN GLOBAL OBJECT" ) ) ;
61- }
49+ if ( ! isClientConnected ( ) ) {
50+ reject ( {
51+ statusCode : 400 ,
52+ statusMessage :
53+ "[Klepper] Error during sending event to Klepper. No client global data in NodeJS scope." ,
54+ } ) ;
55+ }
6256
63- const request = http . request (
64- httpOptions ,
65- ( res : KlepperIncomingMessage ) => {
66- res . setEncoding ( "utf8" ) ;
57+ const request = http . request ( httpOptions , ( res : KlepperIncomingMessage ) => {
58+ res . setEncoding ( "utf8" ) ;
6759
68- const status = statusFromCode ( res ?. statusCode as number ) ;
69- const isSuccess = status === RequestStatus . SUCCESS ;
60+ const status = statusFromCode ( res ?. statusCode as number ) ;
61+ const isSuccess = status === RequestStatus . SUCCESS ;
7062
71- if ( ! isSuccess ) {
72- reject ( new Error ( "HTTP ERROR: ${res.statusCode}" ) ) ;
73- } else {
74- resolve ( {
75- statusCode : res ?. statusCode as number ,
76- statusMessage : "Event successfully sended to Klepper" ,
77- } ) ;
78- }
63+ if ( ! isSuccess ) {
64+ reject ( {
65+ statusCode : res ?. statusCode as number ,
66+ statusMessage : "[KLEPPER] Error during sending event to Klepper." ,
67+ } ) ;
68+ } else {
69+ resolve ( {
70+ statusCode : res ?. statusCode as number ,
71+ statusMessage : "[KLEPPER] Event successfully sended to Klepper." ,
72+ } ) ;
73+ }
7974
80- res . on ( "error" , reject ) ;
81- }
82- ) ;
83- request . on ( "error" , reject ) ;
75+ res . on ( "error" , reject ) ;
76+ } ) ;
77+
78+ request . on ( "error" , reject ) ;
8479
85- request . on ( "timeout" , ( ) => {
86- request . destroy ( ) ;
87- reject ( new Error ( "TIMEOUT" ) ) ;
80+ request . on ( "timeout" , ( ) => {
81+ request . destroy ( ) ;
82+ reject ( {
83+ statusCode : 400 ,
84+ statusMessage :
85+ "[Klepper] Error during sending event to Klepper. Connection timeout." ,
8886 } ) ;
89-
90- request . write ( JSON . stringify ( data ) ) ;
91- request . end ( ) ;
9287 } ) ;
93- } catch ( error ) {
94- throw new Error ( "" ) ; //to properly handling
95- }
88+
89+ request . write ( JSON . stringify ( event ) ) ;
90+ request . end ( ) ;
91+ } ) ;
9692} ;
0 commit comments