plugnmeet-sdk-js 1.2.1 → 1.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (98) hide show
  1. package/README.md +1 -1
  2. package/dist/PlugNmeet.js +184 -137
  3. package/dist/PlugNmeet.js.map +1 -1
  4. package/dist/index.d.ts +280 -11
  5. package/dist/index.d.ts.map +1 -0
  6. package/dist/main.js +248 -0
  7. package/dist/main.js.map +1 -0
  8. package/dist/module.js +241 -0
  9. package/dist/module.js.map +1 -0
  10. package/package.json +28 -21
  11. package/dist/PlugNmeet.d.ts +0 -76
  12. package/dist/api.d.ts +0 -6
  13. package/dist/api.js +0 -67
  14. package/dist/api.js.map +0 -1
  15. package/dist/cjs/PlugNmeet.d.ts +0 -76
  16. package/dist/cjs/PlugNmeet.js +0 -231
  17. package/dist/cjs/PlugNmeet.js.map +0 -1
  18. package/dist/cjs/api.d.ts +0 -6
  19. package/dist/cjs/api.js +0 -85
  20. package/dist/cjs/api.js.map +0 -1
  21. package/dist/cjs/index.d.ts +0 -11
  22. package/dist/cjs/index.js +0 -6
  23. package/dist/cjs/index.js.map +0 -1
  24. package/dist/cjs/types/RecordingDownloadToken.d.ts +0 -8
  25. package/dist/cjs/types/RecordingDownloadToken.js +0 -3
  26. package/dist/cjs/types/RecordingDownloadToken.js.map +0 -1
  27. package/dist/cjs/types/activeRoomInfo.d.ts +0 -34
  28. package/dist/cjs/types/activeRoomInfo.js +0 -3
  29. package/dist/cjs/types/activeRoomInfo.js.map +0 -1
  30. package/dist/cjs/types/activeRoomsInfo.d.ts +0 -6
  31. package/dist/cjs/types/activeRoomsInfo.js +0 -3
  32. package/dist/cjs/types/activeRoomsInfo.js.map +0 -1
  33. package/dist/cjs/types/clientFiles.d.ts +0 -6
  34. package/dist/cjs/types/clientFiles.js +0 -3
  35. package/dist/cjs/types/clientFiles.js.map +0 -1
  36. package/dist/cjs/types/createRoom.d.ts +0 -82
  37. package/dist/cjs/types/createRoom.js +0 -3
  38. package/dist/cjs/types/createRoom.js.map +0 -1
  39. package/dist/cjs/types/deleteRecordings.d.ts +0 -7
  40. package/dist/cjs/types/deleteRecordings.js +0 -3
  41. package/dist/cjs/types/deleteRecordings.js.map +0 -1
  42. package/dist/cjs/types/endRoom.d.ts +0 -7
  43. package/dist/cjs/types/endRoom.js +0 -3
  44. package/dist/cjs/types/endRoom.js.map +0 -1
  45. package/dist/cjs/types/fetchRecordings.d.ts +0 -27
  46. package/dist/cjs/types/fetchRecordings.js +0 -3
  47. package/dist/cjs/types/fetchRecordings.js.map +0 -1
  48. package/dist/cjs/types/isRoomActive.d.ts +0 -7
  49. package/dist/cjs/types/isRoomActive.js +0 -3
  50. package/dist/cjs/types/isRoomActive.js.map +0 -1
  51. package/dist/cjs/types/joinToken.d.ts +0 -21
  52. package/dist/cjs/types/joinToken.js +0 -3
  53. package/dist/cjs/types/joinToken.js.map +0 -1
  54. package/dist/index.js +0 -2
  55. package/dist/index.js.map +0 -1
  56. package/dist/types/RecordingDownloadToken.d.ts +0 -8
  57. package/dist/types/RecordingDownloadToken.js +0 -2
  58. package/dist/types/RecordingDownloadToken.js.map +0 -1
  59. package/dist/types/activeRoomInfo.d.ts +0 -34
  60. package/dist/types/activeRoomInfo.js +0 -2
  61. package/dist/types/activeRoomInfo.js.map +0 -1
  62. package/dist/types/activeRoomsInfo.d.ts +0 -6
  63. package/dist/types/activeRoomsInfo.js +0 -2
  64. package/dist/types/activeRoomsInfo.js.map +0 -1
  65. package/dist/types/clientFiles.d.ts +0 -6
  66. package/dist/types/clientFiles.js +0 -2
  67. package/dist/types/clientFiles.js.map +0 -1
  68. package/dist/types/createRoom.d.ts +0 -82
  69. package/dist/types/createRoom.js +0 -2
  70. package/dist/types/createRoom.js.map +0 -1
  71. package/dist/types/deleteRecordings.d.ts +0 -7
  72. package/dist/types/deleteRecordings.js +0 -2
  73. package/dist/types/deleteRecordings.js.map +0 -1
  74. package/dist/types/endRoom.d.ts +0 -7
  75. package/dist/types/endRoom.js +0 -2
  76. package/dist/types/endRoom.js.map +0 -1
  77. package/dist/types/fetchRecordings.d.ts +0 -27
  78. package/dist/types/fetchRecordings.js +0 -2
  79. package/dist/types/fetchRecordings.js.map +0 -1
  80. package/dist/types/isRoomActive.d.ts +0 -7
  81. package/dist/types/isRoomActive.js +0 -2
  82. package/dist/types/isRoomActive.js.map +0 -1
  83. package/dist/types/joinToken.d.ts +0 -21
  84. package/dist/types/joinToken.js +0 -2
  85. package/dist/types/joinToken.js.map +0 -1
  86. package/src/PlugNmeet.ts +0 -255
  87. package/src/api.ts +0 -92
  88. package/src/index.ts +0 -48
  89. package/src/types/RecordingDownloadToken.ts +0 -9
  90. package/src/types/activeRoomInfo.ts +0 -38
  91. package/src/types/activeRoomsInfo.ts +0 -7
  92. package/src/types/clientFiles.ts +0 -6
  93. package/src/types/createRoom.ts +0 -94
  94. package/src/types/deleteRecordings.ts +0 -8
  95. package/src/types/endRoom.ts +0 -8
  96. package/src/types/fetchRecordings.ts +0 -30
  97. package/src/types/isRoomActive.ts +0 -8
  98. package/src/types/joinToken.ts +0 -25
@@ -1,3 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- //# sourceMappingURL=joinToken.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"joinToken.js","sourceRoot":"","sources":["../../../src/types/joinToken.ts"],"names":[],"mappings":""}
package/dist/index.js DELETED
@@ -1,2 +0,0 @@
1
- export { PlugNmeet } from './PlugNmeet';
2
- //# sourceMappingURL=index.js.map
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AA+CA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC"}
@@ -1,8 +0,0 @@
1
- export type RecordingDownloadTokenParams = {
2
- record_id: string;
3
- };
4
- export type RecordingDownloadTokenResponse = {
5
- status: boolean;
6
- msg: string;
7
- token?: string;
8
- };
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=RecordingDownloadToken.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"RecordingDownloadToken.js","sourceRoot":"","sources":["../../src/types/RecordingDownloadToken.ts"],"names":[],"mappings":""}
@@ -1,34 +0,0 @@
1
- export type ActiveRoomInfoParams = {
2
- room_id: string;
3
- };
4
- export type ActiveRoomInfoResponse = {
5
- status: boolean;
6
- msg: string;
7
- room?: Room;
8
- };
9
- export type Room = {
10
- room_info: ActiveRoomInfo;
11
- participants_info: Array<ParticipantInfo>;
12
- };
13
- export type ActiveRoomInfo = {
14
- room_title: string;
15
- room_id: string;
16
- sid: string;
17
- joined_participants: number;
18
- is_running: boolean;
19
- is_recording: boolean;
20
- is_active_rtmp: boolean;
21
- webhook_url: string;
22
- creation_time: number;
23
- metadata: string;
24
- };
25
- export type ParticipantInfo = {
26
- sid: string;
27
- identity: string;
28
- state: string;
29
- metadata: string;
30
- joined_at: number;
31
- name: string;
32
- version: number;
33
- permission: any;
34
- };
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=activeRoomInfo.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"activeRoomInfo.js","sourceRoot":"","sources":["../../src/types/activeRoomInfo.ts"],"names":[],"mappings":""}
@@ -1,6 +0,0 @@
1
- import { Room } from './activeRoomInfo';
2
- export type ActiveRoomsInfoResponse = {
3
- status: boolean;
4
- msg: string;
5
- rooms?: Array<Room>;
6
- };
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=activeRoomsInfo.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"activeRoomsInfo.js","sourceRoot":"","sources":["../../src/types/activeRoomsInfo.ts"],"names":[],"mappings":""}
@@ -1,6 +0,0 @@
1
- export type ClientFilesResponse = {
2
- status: boolean;
3
- msg: string;
4
- css?: Array<string>;
5
- js?: Array<string>;
6
- };
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=clientFiles.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"clientFiles.js","sourceRoot":"","sources":["../../src/types/clientFiles.ts"],"names":[],"mappings":""}
@@ -1,82 +0,0 @@
1
- export type CreateRoomParams = {
2
- room_id: string;
3
- max_participants?: number;
4
- empty_timeout?: number;
5
- metadata: RooMetadata;
6
- };
7
- export type RooMetadata = {
8
- room_title: string;
9
- welcome_message?: string;
10
- webhook_url?: string;
11
- room_features: RoomFeaturesParams;
12
- default_lock_settings?: LockSettingsParams;
13
- };
14
- export type RoomFeaturesParams = {
15
- allow_webcams: boolean;
16
- mute_on_start: boolean;
17
- allow_screen_share: boolean;
18
- allow_rtmp: boolean;
19
- admin_only_webcams: boolean;
20
- allow_view_other_webcams: boolean;
21
- allow_view_other_users_list: boolean;
22
- recording_features: RecordingFeaturesParams;
23
- chat_features: ChatFeaturesParams;
24
- shared_note_pad_features?: SharedNotePadFeaturesParams;
25
- whiteboard_features?: WhiteboardFeaturesParams;
26
- external_media_player_features?: ExternalMediaPlayerFeatures;
27
- waiting_room_features?: WaitingRoomFeatures;
28
- breakout_room_features?: BreakoutRoomFeatures;
29
- };
30
- export type RecordingFeaturesParams = {
31
- is_allow: boolean;
32
- is_allow_cloud: boolean;
33
- is_allow_local: boolean;
34
- enable_auto_cloud_recording: boolean;
35
- };
36
- export type ChatFeaturesParams = {
37
- allow_chat: boolean;
38
- allow_file_upload: boolean;
39
- allowed_file_types: Array<string>;
40
- max_file_size: number;
41
- };
42
- export type SharedNotePadFeaturesParams = {
43
- allowed_shared_note_pad: boolean;
44
- };
45
- export type WhiteboardFeaturesParams = {
46
- allowed_whiteboard: boolean;
47
- };
48
- export type ExternalMediaPlayerFeatures = {
49
- allowed_external_media_player: boolean;
50
- };
51
- export type WaitingRoomFeatures = {
52
- is_active: boolean;
53
- waiting_room_msg?: string;
54
- };
55
- export type BreakoutRoomFeatures = {
56
- is_allow: boolean;
57
- };
58
- export type LockSettingsParams = {
59
- lock_microphone?: boolean;
60
- lock_webcam?: boolean;
61
- lock_screen_sharing?: boolean;
62
- lock_whiteboard?: boolean;
63
- lock_shared_notepad?: boolean;
64
- lock_chat?: boolean;
65
- lock_chat_send_message?: boolean;
66
- lock_chat_file_share?: boolean;
67
- };
68
- export type CreateRoomResponse = {
69
- status: boolean;
70
- msg: string;
71
- roomInfo?: CreateRoomResponseRoomInfo;
72
- };
73
- export type CreateRoomResponseRoomInfo = {
74
- sid: string;
75
- name: string;
76
- max_participants: number;
77
- empty_timeout: number;
78
- creation_time: number;
79
- turn_password: string;
80
- enabled_codecs: Array<any>;
81
- metadata: string;
82
- };
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=createRoom.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"createRoom.js","sourceRoot":"","sources":["../../src/types/createRoom.ts"],"names":[],"mappings":""}
@@ -1,7 +0,0 @@
1
- export type DeleteRecordingsParams = {
2
- record_id: string;
3
- };
4
- export type DeleteRecordingsResponse = {
5
- status: boolean;
6
- msg: string;
7
- };
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=deleteRecordings.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"deleteRecordings.js","sourceRoot":"","sources":["../../src/types/deleteRecordings.ts"],"names":[],"mappings":""}
@@ -1,7 +0,0 @@
1
- export type EndRoomParams = {
2
- room_id: string;
3
- };
4
- export type EndRoomResponse = {
5
- status: boolean;
6
- msg: string;
7
- };
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=endRoom.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"endRoom.js","sourceRoot":"","sources":["../../src/types/endRoom.ts"],"names":[],"mappings":""}
@@ -1,27 +0,0 @@
1
- export type FetchRecordingsParams = {
2
- room_ids: Array<string>;
3
- from?: number;
4
- limit?: number;
5
- order_by?: 'ASC' | 'DESC';
6
- };
7
- export type FetchRecordingsResponse = {
8
- status: boolean;
9
- msg: string;
10
- result?: FetchRecordingsResult;
11
- };
12
- export type FetchRecordingsResult = {
13
- total_recordings: number;
14
- from: number;
15
- limit: number;
16
- order_by: string;
17
- recordings_list: Array<RecordingInfo>;
18
- };
19
- export type RecordingInfo = {
20
- record_id: string;
21
- room_id: string;
22
- room_sid: string;
23
- file_path: string;
24
- file_size: number;
25
- creation_time: number;
26
- room_creation_time: number;
27
- };
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=fetchRecordings.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"fetchRecordings.js","sourceRoot":"","sources":["../../src/types/fetchRecordings.ts"],"names":[],"mappings":""}
@@ -1,7 +0,0 @@
1
- export type IsRoomActiveParams = {
2
- room_id: string;
3
- };
4
- export type IsRoomActiveResponse = {
5
- status: boolean;
6
- msg: string;
7
- };
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=isRoomActive.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"isRoomActive.js","sourceRoot":"","sources":["../../src/types/isRoomActive.ts"],"names":[],"mappings":""}
@@ -1,21 +0,0 @@
1
- import { LockSettingsParams } from './createRoom';
2
- export type JoinTokenParams = {
3
- room_id: string;
4
- user_info: JoinTokenUserInfo;
5
- };
6
- export type JoinTokenUserInfo = {
7
- name: string;
8
- user_id: string;
9
- is_admin: boolean;
10
- is_hidden: boolean;
11
- user_metadata?: JoinTokenUserMetadata;
12
- };
13
- export type JoinTokenUserMetadata = {
14
- profile_pic?: string;
15
- lock_settings?: LockSettingsParams;
16
- };
17
- export type JoinTokenResponse = {
18
- status: boolean;
19
- msg: string;
20
- token?: string;
21
- };
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=joinToken.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"joinToken.js","sourceRoot":"","sources":["../../src/types/joinToken.ts"],"names":[],"mappings":""}
package/src/PlugNmeet.ts DELETED
@@ -1,255 +0,0 @@
1
- import { prepareAPI, sendRequest } from './api';
2
- import { CreateRoomParams, CreateRoomResponse } from './types/createRoom';
3
- import { JoinTokenParams, JoinTokenResponse } from './types/joinToken';
4
- import { IsRoomActiveParams, IsRoomActiveResponse } from './types/isRoomActive';
5
- import {
6
- ActiveRoomInfoParams,
7
- ActiveRoomInfoResponse,
8
- } from './types/activeRoomInfo';
9
- import { ActiveRoomsInfoResponse } from './types/activeRoomsInfo';
10
- import { EndRoomParams, EndRoomResponse } from './types/endRoom';
11
- import {
12
- FetchRecordingsParams,
13
- FetchRecordingsResponse,
14
- } from './types/fetchRecordings';
15
- import {
16
- DeleteRecordingsParams,
17
- DeleteRecordingsResponse,
18
- } from './types/deleteRecordings';
19
- import {
20
- RecordingDownloadTokenParams,
21
- RecordingDownloadTokenResponse,
22
- } from './types/RecordingDownloadToken';
23
- import { ClientFilesResponse } from './types/clientFiles';
24
-
25
- export class PlugNmeet {
26
- protected defaultPath = '/auth';
27
-
28
- /**
29
- * @param serverUrl plugNmeet server URL
30
- * @param apiKey plugNmeet API_Key
31
- * @param apiSecret plugNmeet API_Secret
32
- */
33
- constructor(serverUrl: string, apiKey: string, apiSecret: string) {
34
- prepareAPI(serverUrl + this.defaultPath, apiKey, apiSecret);
35
- }
36
-
37
- /**
38
- * Create new room
39
- * @param params: CreateRoomParams
40
- * @returns Promise<CreateRoomResponse>
41
- */
42
- public async createRoom(
43
- params: CreateRoomParams,
44
- ): Promise<CreateRoomResponse> {
45
- const output = await sendRequest('/room/create', params);
46
- if (!output.status) {
47
- return {
48
- status: false,
49
- msg: output.response,
50
- };
51
- }
52
-
53
- return {
54
- status: output.response.status,
55
- msg: output.response.msg,
56
- roomInfo: output.response.roomInfo,
57
- };
58
- }
59
-
60
- /**
61
- * Generate join token
62
- * @param params: JoinTokenParams
63
- * @returns Promise<JoinTokenResponse>
64
- */
65
- public async getJoinToken(
66
- params: JoinTokenParams,
67
- ): Promise<JoinTokenResponse> {
68
- const output = await sendRequest('/room/getJoinToken', params);
69
- if (!output.status) {
70
- return {
71
- status: false,
72
- msg: output.response,
73
- };
74
- }
75
-
76
- return {
77
- status: output.response.status,
78
- msg: output.response.msg,
79
- token: output.response.token,
80
- };
81
- }
82
-
83
- /**
84
- * If room is active or not
85
- * @param params: IsRoomActiveParams
86
- * @returns Promise<IsRoomActiveResponse>
87
- */
88
- public async isRoomActive(
89
- params: IsRoomActiveParams,
90
- ): Promise<IsRoomActiveResponse> {
91
- const output = await sendRequest('/room/isRoomActive', params);
92
- if (!output.status) {
93
- return {
94
- status: false,
95
- msg: output.response,
96
- };
97
- }
98
-
99
- return {
100
- status: output.response.status,
101
- msg: output.response.msg,
102
- };
103
- }
104
-
105
- /**
106
- * Get active room information
107
- * @param params: ActiveRoomInfoParams
108
- * @returns Promise<ActiveRoomInfoResponse>
109
- */
110
- public async getActiveRoomInfo(
111
- params: ActiveRoomInfoParams,
112
- ): Promise<ActiveRoomInfoResponse> {
113
- const output = await sendRequest('/room/getActiveRoomInfo', params);
114
- if (!output.status) {
115
- return {
116
- status: false,
117
- msg: output.response,
118
- };
119
- }
120
-
121
- return {
122
- status: output.response.status,
123
- msg: output.response.msg,
124
- room: output.response.room,
125
- };
126
- }
127
-
128
- /**
129
- * Get all active rooms
130
- * @returns Promise<ActiveRoomsInfoResponse>
131
- */
132
- public async getActiveRoomsInfo(): Promise<ActiveRoomsInfoResponse> {
133
- const output = await sendRequest('/room/getActiveRoomsInfo', {});
134
- if (!output.status) {
135
- return {
136
- status: false,
137
- msg: output.response,
138
- };
139
- }
140
-
141
- return {
142
- status: output.response.status,
143
- msg: output.response.msg,
144
- rooms: output.response.rooms,
145
- };
146
- }
147
-
148
- /**
149
- * End active room
150
- * @param params: EndRoomParams
151
- * @returns Promise<EndRoomResponse>
152
- */
153
- public async endRoom(params: EndRoomParams): Promise<EndRoomResponse> {
154
- const output = await sendRequest('/room/endRoom', params);
155
- if (!output.status) {
156
- return {
157
- status: false,
158
- msg: output.response,
159
- };
160
- }
161
-
162
- return {
163
- status: output.response.status,
164
- msg: output.response.msg,
165
- };
166
- }
167
-
168
- /**
169
- * Fetch recordings
170
- * @param params: FetchRecordingsParams
171
- * @returns Promise<FetchRecordingsResponse>
172
- */
173
- public async fetchRecordings(
174
- params: FetchRecordingsParams,
175
- ): Promise<FetchRecordingsResponse> {
176
- const output = await sendRequest('/recording/fetch', params);
177
- if (!output.status) {
178
- return {
179
- status: false,
180
- msg: output.response,
181
- };
182
- }
183
-
184
- return {
185
- status: output.response.status,
186
- msg: output.response.msg,
187
- result: output.response.result,
188
- };
189
- }
190
-
191
- /**
192
- * Delete recording
193
- * @param params: DeleteRecordingsParams
194
- * @returns Promise<DeleteRecordingsResponse>
195
- */
196
- public async deleteRecordings(
197
- params: DeleteRecordingsParams,
198
- ): Promise<DeleteRecordingsResponse> {
199
- const output = await sendRequest('/recording/delete', params);
200
- if (!output.status) {
201
- return {
202
- status: false,
203
- msg: output.response,
204
- };
205
- }
206
-
207
- return {
208
- status: output.response.status,
209
- msg: output.response.msg,
210
- };
211
- }
212
-
213
- /**
214
- * @returns Promise<ClientFilesResponse>
215
- */
216
- public async getClientFiles(): Promise<ClientFilesResponse> {
217
- const output = await sendRequest('/getClientFiles', {});
218
- if (!output.status) {
219
- return {
220
- status: false,
221
- msg: output.response,
222
- };
223
- }
224
-
225
- return {
226
- status: output.response.status,
227
- msg: output.response.msg,
228
- css: output.response.css,
229
- js: output.response.js,
230
- };
231
- }
232
-
233
- /**
234
- * Generate token to download recording
235
- * @param params: RecordingDownloadTokenParams
236
- * @returns Promise<RecordingDownloadTokenResponse>
237
- */
238
- public async getRecordingDownloadToken(
239
- params: RecordingDownloadTokenParams,
240
- ): Promise<RecordingDownloadTokenResponse> {
241
- const output = await sendRequest('/recording/getDownloadToken', params);
242
- if (!output.status) {
243
- return {
244
- status: false,
245
- msg: output.response,
246
- };
247
- }
248
-
249
- return {
250
- status: output.response.status,
251
- msg: output.response.msg,
252
- token: output.response.token,
253
- };
254
- }
255
- }
package/src/api.ts DELETED
@@ -1,92 +0,0 @@
1
- import http from 'http';
2
- import https, { RequestOptions } from 'https';
3
- import { createHmac } from 'crypto';
4
-
5
- let mainOptions: RequestOptions,
6
- isSecure = true, _apiKey = "", _apiSecret = "";
7
-
8
- export type APIResponse = {
9
- status: boolean;
10
- response: any;
11
- };
12
-
13
- export const prepareAPI = (
14
- apiEndPointURL: string,
15
- apiKey: string,
16
- apiSecret: string,
17
- ) => {
18
- _apiKey = apiKey;
19
- _apiSecret = apiSecret;
20
-
21
- const url = new URL(apiEndPointURL);
22
- let port = url.protocol === 'https:' ? 443 : 80;
23
- isSecure = url.protocol === 'https:' ?? false;
24
-
25
- // use port if supplied with url
26
- if (url.port) {
27
- port = Number(url.port);
28
- }
29
-
30
- mainOptions = {
31
- hostname: url.hostname,
32
- path: url.pathname,
33
- method: 'POST',
34
- port,
35
- };
36
- };
37
-
38
- const prepareHeader = (body: string) => {
39
- const signature = createHmac('sha256', _apiSecret)
40
- .update(body)
41
- .digest('hex');
42
-
43
- const headers = {
44
- 'Content-Type': 'application/json',
45
- 'API-KEY': _apiKey,
46
- 'HASH-SIGNATURE': signature,
47
- };
48
-
49
- return headers;
50
- };
51
-
52
- export const sendRequest = async (path: string, body: any) => {
53
- return new Promise<APIResponse>((resolve) => {
54
- const output: APIResponse = {
55
- status: false,
56
- response: undefined,
57
- };
58
-
59
- const chunk = JSON.stringify(body);
60
-
61
- const options = { ...mainOptions };
62
- options.path += path;
63
- options.headers = prepareHeader(chunk);
64
-
65
- const req = (isSecure ? https : http).request(options, (res) => {
66
- const body: Array<Uint8Array> = [];
67
- res.on('data', (chunk) => body.push(chunk));
68
-
69
- res.on('end', () => {
70
- const resString = Buffer.concat(body).toString();
71
-
72
- try {
73
- output.status = true;
74
- output.response = JSON.parse(resString);
75
- } catch (error) {
76
- output.status = false;
77
- output.response = error;
78
- }
79
-
80
- resolve(output);
81
- });
82
- });
83
-
84
- req.on('error', (error) => {
85
- output.response = error.message;
86
- resolve(output);
87
- });
88
-
89
- req.write(chunk);
90
- req.end();
91
- });
92
- };