|
| 1 | +import { RequestPayload, RequestOptions, KlepperResponse, KlepperIncomingMessage, RequestStatus } from "@klepper/transport"; |
1 | 2 | import * as http from "http"; |
| 3 | +import { getGlobalClientData } from "./global"; |
2 | 4 |
|
3 | | -type RequestType = "" | "" | ""; |
| 5 | +const KLEPPER_HOST = process.env.KLEPPER_HOST || "localhost"; |
| 6 | +const KLEPPER_API = process.env.KLEPPER_API || "/test"; |
| 7 | +const KLEPPER_PORT = process.env.KLEPPER_PORT || 3000; |
4 | 8 |
|
5 | | -type HttpMethodType = "GET" | "POST" | "PATCH" | "PUT" | "DELETE"; |
6 | | - |
7 | | -const KLEPPER_API = process.env.KLEPPER_API; |
8 | | -const KLEPPER_PORT = process.env.KLEPPER_PORT; |
9 | | - |
10 | | -export interface RequestOptions extends http.RequestOptions { |
11 | | - hostname: string; |
12 | | - port: number; |
13 | | - method: HttpMethodType; |
14 | | - path?: string; |
15 | | - headers?: { [key: string]: string }; |
16 | | -} |
17 | | - |
18 | | -export interface RequestPayload { |
19 | | - data: string; |
20 | | - type: RequestType; |
21 | | - url: string; |
22 | | -} |
23 | | - |
24 | | -export const getRequestPath = (path: string) => |
25 | | - `http://api.klepper.io/api/${path}`; |
26 | | - |
27 | | -export const getRequestHeaders = () => {}; |
28 | | - |
29 | | -export const createHttpOptions = (payload: RequestPayload): RequestOptions => { |
30 | | - const { url } = payload; |
| 9 | +const createHttpOptions = (payload: RequestPayload): http.RequestOptions => { |
| 10 | + const client = getGlobalClientData(); |
| 11 | + const { privateKey } = client; |
| 12 | + const { data } = payload; |
31 | 13 | const baseOptions: RequestOptions = { |
32 | | - hostname: KLEPPER_API!, |
33 | | - method: "POST", |
34 | | - port: +KLEPPER_PORT!, |
| 14 | + hostname: KLEPPER_HOST, |
| 15 | + port: +KLEPPER_PORT, |
| 16 | + method: 'POST', |
| 17 | + headers: { |
| 18 | + 'Content-Type': 'application/json', |
| 19 | + 'Content-Length': `${Buffer.byteLength(JSON.stringify(data))}`, |
| 20 | + 'Klepper-Project-Key': privateKey as string |
| 21 | + } |
35 | 22 | }; |
36 | 23 |
|
37 | 24 | return { |
38 | | - path: getRequestPath(url), |
| 25 | + path: KLEPPER_API, |
39 | 26 | ...baseOptions, |
40 | 27 | }; |
41 | 28 | }; |
42 | 29 |
|
| 30 | +const statusFromCode = (code: number) => code >= 200 && code <= 299 ? RequestStatus.SUCCESS : RequestStatus.ERROR; |
| 31 | + |
43 | 32 | /** |
44 | 33 | * Send data to Klepper server |
45 | 34 | * |
46 | 35 | * @param payload |
47 | 36 | */ |
48 | | -export const sendHttpRequest = (payload: RequestPayload) => { |
| 37 | +export const sendEvent = async (payload: RequestPayload): Promise<KlepperResponse> => { |
49 | 38 | const { data } = payload; |
50 | | - const options = createHttpOptions(payload); |
51 | | - const request = http.request(options, (res: http.IncomingMessage) => { |
52 | | - res.setEncoding("utf8"); |
53 | | - |
54 | | - res.on("data", () => {}); |
55 | | - res.on("end", () => {}); |
56 | | - }); |
57 | | - request.end(data); |
| 39 | + |
| 40 | + // global.Object = Object({ test: "test" }) |
| 41 | + // console.log("GLOBAL: ", global.Object); |
| 42 | + |
| 43 | + /** |
| 44 | + * IMPORTANT TO HANDLE GLOBAL DATA |
| 45 | + */ |
| 46 | + |
| 47 | + try { |
| 48 | + return new Promise<KlepperResponse>((resolve, reject) => { |
| 49 | + const httpOptions = createHttpOptions(payload); |
| 50 | + const client = getGlobalClientData(); |
| 51 | + |
| 52 | + if (!httpOptions) { |
| 53 | + reject(new Error("NO HTTP OPTIONS")) |
| 54 | + } |
| 55 | + |
| 56 | + if (!client) { |
| 57 | + reject(new Error("NO CLIENT DATA IN GLOBAL OBJECT")) |
| 58 | + } |
| 59 | + |
| 60 | + const request = http.request(httpOptions, (res: KlepperIncomingMessage) => { |
| 61 | + res.setEncoding("utf8"); |
| 62 | + |
| 63 | + const status = statusFromCode(res?.statusCode as number); |
| 64 | + const isSuccess = status === RequestStatus.SUCCESS; |
| 65 | + |
| 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 | + }) |
| 73 | + } |
| 74 | + |
| 75 | + res.on("error", reject); |
| 76 | + }); |
| 77 | + request.on("error", reject); |
| 78 | + |
| 79 | + request.on('timeout', () => { |
| 80 | + request.destroy() |
| 81 | + reject(new Error("TIMEOUT")); |
| 82 | + }) |
| 83 | + |
| 84 | + request.write(JSON.stringify(data)); |
| 85 | + request.end(); |
| 86 | + }) |
| 87 | + } catch (error) { |
| 88 | + throw new Error(""); //to properly handling |
| 89 | + } |
58 | 90 | }; |
0 commit comments