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