1- import { RequestPayload , RequestOptions , KlepperResponse , KlepperIncomingMessage , RequestStatus } from "../transport" ;
1+ import {
2+ RequestPayload ,
3+ RequestOptions ,
4+ KlepperResponse ,
5+ KlepperIncomingMessage ,
6+ RequestStatus ,
7+ } from "../transport" ;
28import * as http from "http" ;
39import { getGlobalClientData } from "./global" ;
410
511const KLEPPER_HOST = process . env . KLEPPER_HOST || "localhost" ;
612const KLEPPER_API = process . env . KLEPPER_API || "/test" ;
7- const KLEPPER_PORT = process . env . KLEPPER_PORT || 3000 ;
13+ const KLEPPER_PORT = process . env . KLEPPER_PORT || 3000 ;
814
915const createHttpOptions = ( payload : RequestPayload ) : http . RequestOptions => {
1016 const client = getGlobalClientData ( ) ;
17+
1118 const { privateKey } = client ;
1219 const { data } = payload ;
1320 const baseOptions : RequestOptions = {
1421 hostname : KLEPPER_HOST ,
1522 port : + KLEPPER_PORT ,
16- method : ' POST' ,
23+ method : " POST" ,
1724 headers : {
18- ' Content-Type' : ' application/json' ,
19- ' Content-Length' : `${ Buffer . byteLength ( JSON . stringify ( data ) ) } ` ,
20- ' Klepper-Project-Key' : privateKey as string
21- }
25+ " Content-Type" : " application/json" ,
26+ " Content-Length" : `${ Buffer . byteLength ( JSON . stringify ( data ) ) } ` ,
27+ " Klepper-Project-Key" : privateKey as string ,
28+ } ,
2229 } ;
2330
2431 return {
@@ -27,63 +34,62 @@ const createHttpOptions = (payload: RequestPayload): http.RequestOptions => {
2734 } ;
2835} ;
2936
30- const statusFromCode = ( code : number ) => code >= 200 && code <= 299 ? RequestStatus . SUCCESS : RequestStatus . ERROR ;
37+ const statusFromCode = ( code : number ) =>
38+ code >= 200 && code <= 299 ? RequestStatus . SUCCESS : RequestStatus . ERROR ;
3139
3240/**
3341 * Send data to Klepper server
34- *
35- * @param payload
42+ *
43+ * @param payload
3644 */
37- export const sendEvent = async ( payload : RequestPayload ) : Promise < KlepperResponse > => {
45+ export const sendEvent = async (
46+ payload : RequestPayload
47+ ) : Promise < KlepperResponse > => {
3848 const { data } = payload ;
3949
40- // global.Object = Object({ test: "test" })
41- // console.log("GLOBAL: ", global.Object);
42-
43- /**
44- * IMPORTANT TO HANDLE GLOBAL DATA
45- */
46-
4750 try {
4851 return new Promise < KlepperResponse > ( ( resolve , reject ) => {
4952 const httpOptions = createHttpOptions ( payload ) ;
5053 const client = getGlobalClientData ( ) ;
5154
5255 if ( ! httpOptions ) {
53- reject ( new Error ( "NO HTTP OPTIONS" ) )
56+ reject ( new Error ( "NO HTTP OPTIONS" ) ) ;
5457 }
5558
5659 if ( ! client ) {
57- reject ( new Error ( "NO CLIENT DATA IN GLOBAL OBJECT" ) )
60+ reject ( new Error ( "NO CLIENT DATA IN GLOBAL OBJECT" ) ) ;
5861 }
5962
60- const request = http . request ( httpOptions , ( res : KlepperIncomingMessage ) => {
61- res . setEncoding ( "utf8" ) ;
63+ const request = http . request (
64+ httpOptions ,
65+ ( res : KlepperIncomingMessage ) => {
66+ res . setEncoding ( "utf8" ) ;
6267
63- const status = statusFromCode ( res ?. statusCode as number ) ;
64- const isSuccess = status === RequestStatus . SUCCESS ;
68+ const status = statusFromCode ( res ?. statusCode as number ) ;
69+ const isSuccess = status === RequestStatus . SUCCESS ;
6570
66- if ( ! isSuccess ) {
67- reject ( new Error ( "HTTP ERROR: ${res.statusCode}" ) )
68- } else {
69- resolve ( {
70- statusCode : res ?. statusCode as number ,
71- statusMessage : "Event successfully sended to Klepper"
72- } )
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+ }
79+
80+ res . on ( "error" , reject ) ;
7381 }
74-
75- res . on ( "error" , reject ) ;
76- } ) ;
82+ ) ;
7783 request . on ( "error" , reject ) ;
7884
79- request . on ( ' timeout' , ( ) => {
80- request . destroy ( )
85+ request . on ( " timeout" , ( ) => {
86+ request . destroy ( ) ;
8187 reject ( new Error ( "TIMEOUT" ) ) ;
82- } )
88+ } ) ;
8389
8490 request . write ( JSON . stringify ( data ) ) ;
8591 request . end ( ) ;
86- } )
92+ } ) ;
8793 } catch ( error ) {
8894 throw new Error ( "" ) ; //to properly handling
8995 }
0 commit comments