@@ -3,83 +3,275 @@ import { fetch } from 'cross-fetch';
3
3
import { Models } from './models';
4
4
import { Service } from './service';
5
5
6
+ /**
7
+ * Payload type representing a key-value pair with string keys and any values.
8
+ */
6
9
type Payload = {
7
10
[key: string]: any;
8
11
}
9
12
13
+ /**
14
+ * Headers type representing a key-value pair with string keys and string values.
15
+ */
10
16
type Headers = {
11
17
[key: string]: string;
12
18
}
13
19
20
+ /**
21
+ * Realtime response structure with different types.
22
+ */
14
23
type RealtimeResponse = {
24
+ /**
25
+ * Type of the response: 'error', 'event', 'connected', or 'response'.
26
+ */
15
27
type: 'error' | 'event' | 'connected' | 'response';
28
+
29
+ /**
30
+ * Data associated with the response based on the response type.
31
+ */
16
32
data: RealtimeResponseAuthenticated | RealtimeResponseConnected | RealtimeResponseError | RealtimeResponseEvent<unknown >;
17
33
}
18
34
35
+ /**
36
+ * Realtime request structure for authentication.
37
+ */
19
38
type RealtimeRequest = {
39
+ /**
40
+ * Type of the request: 'authentication'.
41
+ */
20
42
type: 'authentication';
43
+
44
+ /**
45
+ * Data required for authentication.
46
+ */
21
47
data: RealtimeRequestAuthenticate;
22
48
}
23
49
50
+ /**
51
+ * Realtime event response structure with generic payload type.
52
+ */
24
53
export type RealtimeResponseEvent<T extends unknown > = {
54
+ /**
55
+ * List of event names associated with the response.
56
+ */
25
57
events: string[];
58
+
59
+ /**
60
+ * List of channel names associated with the response.
61
+ */
26
62
channels: string[];
63
+
64
+ /**
65
+ * Timestamp indicating the time of the event.
66
+ */
27
67
timestamp: number;
68
+
69
+ /**
70
+ * Payload containing event-specific data.
71
+ */
28
72
payload: T;
29
73
}
30
74
75
+ /**
76
+ * Realtime response structure for errors.
77
+ */
31
78
type RealtimeResponseError = {
79
+ /**
80
+ * Numeric error code indicating the type of error.
81
+ */
32
82
code: number;
83
+
84
+ /**
85
+ * Error message describing the encountered error.
86
+ */
33
87
message: string;
34
88
}
35
89
90
+ /**
91
+ * Realtime response structure for a successful connection.
92
+ */
36
93
type RealtimeResponseConnected = {
94
+ /**
95
+ * List of channels the user is connected to.
96
+ */
37
97
channels: string[];
98
+
99
+ /**
100
+ * User object representing the connected user (optional).
101
+ */
38
102
user?: object;
39
103
}
40
104
105
+ /**
106
+ * Realtime response structure for authenticated connections.
107
+ */
41
108
type RealtimeResponseAuthenticated = {
109
+ /**
110
+ * Destination channel for the response.
111
+ */
42
112
to: string;
113
+
114
+ /**
115
+ * Boolean indicating the success of the authentication process.
116
+ */
43
117
success: boolean;
118
+
119
+ /**
120
+ * User object representing the authenticated user.
121
+ */
44
122
user: object;
45
123
}
46
124
125
+ /**
126
+ * Realtime request structure for authentication.
127
+ */
47
128
type RealtimeRequestAuthenticate = {
129
+ /**
130
+ * Session identifier for authentication.
131
+ */
48
132
session: string;
49
133
}
50
134
135
+ /**
136
+ * Realtime interface representing the structure of a realtime communication object.
137
+ */
51
138
type Realtime = {
139
+ /**
140
+ * WebSocket instance for realtime communication.
141
+ */
52
142
socket?: WebSocket;
143
+
144
+ /**
145
+ * Timeout duration for communication operations.
146
+ */
53
147
timeout?: number;
148
+
149
+ /**
150
+ * URL for establishing the WebSocket connection.
151
+ */
54
152
url?: string;
153
+
154
+ /**
155
+ * Last received message from the realtime server.
156
+ */
55
157
lastMessage?: RealtimeResponse;
158
+
159
+ /**
160
+ * Set of channel names the client is subscribed to.
161
+ */
56
162
channels: Set<string >;
163
+
164
+ /**
165
+ * Map of subscriptions containing channel names and corresponding callback functions.
166
+ */
57
167
subscriptions: Map<number , {
58
168
channels: string [];
59
169
callback: (payload: RealtimeResponseEvent <any >) => void
60
170
}>;
171
+
172
+ /**
173
+ * Counter for managing subscriptions.
174
+ */
61
175
subscriptionsCounter: number;
176
+
177
+ /**
178
+ * Boolean indicating whether automatic reconnection is enabled.
179
+ */
62
180
reconnect: boolean;
181
+
182
+ /**
183
+ * Number of reconnection attempts made.
184
+ */
63
185
reconnectAttempts: number;
186
+
187
+ /**
188
+ * Function to get the timeout duration for communication operations.
189
+ */
64
190
getTimeout: () => number;
191
+
192
+ /**
193
+ * Function to establish a WebSocket connection.
194
+ */
65
195
connect: () => void;
196
+
197
+ /**
198
+ * Function to create a new WebSocket instance.
199
+ */
66
200
createSocket: () => void;
201
+
202
+ /**
203
+ * Function to clean up resources associated with specified channels.
204
+ *
205
+ * @param {string[]} channels - List of channel names to clean up.
206
+ */
67
207
cleanUp: (channels: string[]) => void;
208
+
209
+ /**
210
+ * Function to handle incoming messages from the WebSocket connection.
211
+ *
212
+ * @param {MessageEvent} event - Event containing the received message.
213
+ */
68
214
onMessage: (event: MessageEvent) => void;
69
215
}
70
216
217
+ /**
218
+ * Type representing upload progress information.
219
+ */
71
220
export type UploadProgress = {
221
+ /**
222
+ * Identifier for the upload progress.
223
+ */
72
224
$id: string;
225
+
226
+ /**
227
+ * Current progress of the upload (in percentage).
228
+ */
73
229
progress: number;
230
+
231
+ /**
232
+ * Total size uploaded (in bytes) during the upload process.
233
+ */
74
234
sizeUploaded: number;
235
+
236
+ /**
237
+ * Total number of chunks that need to be uploaded.
238
+ */
75
239
chunksTotal: number;
240
+
241
+ /**
242
+ * Number of chunks that have been successfully uploaded.
243
+ */
76
244
chunksUploaded: number;
77
245
}
78
246
247
+ /**
248
+ * Exception thrown by the {{language .params .packageName }} package
249
+ */
79
250
class {{spec .title | caseUcfirst }}Exception extends Error {
251
+ /**
252
+ * The error code associated with the exception.
253
+ */
80
254
code: number;
255
+
256
+ /**
257
+ * The response string associated with the exception.
258
+ */
81
259
response: string;
260
+
261
+ /**
262
+ * Error type.
263
+ * See [Error Types]({{sdk .url }}/docs/response-codes#errorTypes) for more information.
264
+ */
82
265
type: string;
266
+
267
+ /**
268
+ * Initializes a {{spec .title | caseUcfirst }} Exception.
269
+ *
270
+ * @param {string} message - The error message.
271
+ * @param {number} code - The error code. Default is 0.
272
+ * @param {string} type - The error type. Default is an empty string.
273
+ * @param {string} response - The response string. Default is an empty string.
274
+ */
83
275
constructor(message: string, code: number = 0, type: string = '', response: string = '') {
84
276
super(message);
85
277
this.name = '{{spec .title | caseUcfirst }}Exception';
@@ -90,14 +282,24 @@ class {{spec.title | caseUcfirst}}Exception extends Error {
90
282
}
91
283
}
92
284
285
+ /**
286
+ * Client that handles requests to {{spec .title | caseUcfirst }}
287
+ */
93
288
class Client {
289
+ /**
290
+ * Holds configuration such as project.
291
+ */
94
292
config = {
95
293
endpoint: '{{ spec .endpoint }}',
96
294
endpointRealtime: '',
97
295
{% for header in spec .global .headers %}
98
296
{{ header .key | caseLower }}: '',
99
297
{% endfor %}
100
298
};
299
+
300
+ /**
301
+ * Custom headers for API requests.
302
+ */
101
303
headers: Headers = {
102
304
'x-sdk-name': '{{ sdk .name }}',
103
305
'x-sdk-platform': '{{ sdk .platform }}',
@@ -336,6 +538,19 @@ class Client {
336
538
}
337
539
}
338
540
541
+ /**
542
+ * Call API endpoint with the specified method, URL, headers, and parameters.
543
+ *
544
+ * @param {string} method - HTTP method (e.g., 'GET', 'POST', 'PUT', 'DELETE').
545
+ * @param {URL} url - The URL of the API endpoint.
546
+ * @param {Headers} headers - Custom headers for the API request.
547
+ * @param {Payload} params - Request parameters.
548
+ * @returns {Promise<any >} - A promise that resolves with the response data.
549
+ *
550
+ * @typedef {Object} Payload - Request payload data.
551
+ * @property {string} key - The key.
552
+ * @property {string} value - The value.
553
+ */
339
554
async call(method: string, url: URL, headers: Headers = {}, params: Payload = {}): Promise<any > {
340
555
method = method.toUpperCase();
341
556
0 commit comments