Skip to content

Commit 264aca0

Browse files
Merge pull request #721 from Im-Madhur-Gupta/docs-web-template
Add inline doc comments to the Web SDK template
2 parents 85f73a3 + fae8ce8 commit 264aca0

File tree

7 files changed

+451
-9
lines changed

7 files changed

+451
-9
lines changed

templates/web/src/client.ts.twig

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,275 @@
11
import { Models } from './models';
22
import { Service } from './service';
33

4+
/**
5+
* Payload type representing a key-value pair with string keys and any values.
6+
*/
47
type Payload = {
58
[key: string]: any;
69
}
710

11+
/**
12+
* Headers type representing a key-value pair with string keys and string values.
13+
*/
814
type Headers = {
915
[key: string]: string;
1016
}
1117

18+
/**
19+
* Realtime response structure with different types.
20+
*/
1221
type RealtimeResponse = {
22+
/**
23+
* Type of the response: 'error', 'event', 'connected', or 'response'.
24+
*/
1325
type: 'error' | 'event' | 'connected' | 'response';
26+
27+
/**
28+
* Data associated with the response based on the response type.
29+
*/
1430
data: RealtimeResponseAuthenticated | RealtimeResponseConnected | RealtimeResponseError | RealtimeResponseEvent<unknown>;
1531
}
1632

33+
/**
34+
* Realtime request structure for authentication.
35+
*/
1736
type RealtimeRequest = {
37+
/**
38+
* Type of the request: 'authentication'.
39+
*/
1840
type: 'authentication';
41+
42+
/**
43+
* Data required for authentication.
44+
*/
1945
data: RealtimeRequestAuthenticate;
2046
}
2147

48+
/**
49+
* Realtime event response structure with generic payload type.
50+
*/
2251
export type RealtimeResponseEvent<T extends unknown> = {
52+
/**
53+
* List of event names associated with the response.
54+
*/
2355
events: string[];
56+
57+
/**
58+
* List of channel names associated with the response.
59+
*/
2460
channels: string[];
61+
62+
/**
63+
* Timestamp indicating the time of the event.
64+
*/
2565
timestamp: number;
66+
67+
/**
68+
* Payload containing event-specific data.
69+
*/
2670
payload: T;
2771
}
2872

73+
/**
74+
* Realtime response structure for errors.
75+
*/
2976
type RealtimeResponseError = {
77+
/**
78+
* Numeric error code indicating the type of error.
79+
*/
3080
code: number;
81+
82+
/**
83+
* Error message describing the encountered error.
84+
*/
3185
message: string;
3286
}
3387

88+
/**
89+
* Realtime response structure for a successful connection.
90+
*/
3491
type RealtimeResponseConnected = {
92+
/**
93+
* List of channels the user is connected to.
94+
*/
3595
channels: string[];
96+
97+
/**
98+
* User object representing the connected user (optional).
99+
*/
36100
user?: object;
37101
}
38102

103+
/**
104+
* Realtime response structure for authenticated connections.
105+
*/
39106
type RealtimeResponseAuthenticated = {
107+
/**
108+
* Destination channel for the response.
109+
*/
40110
to: string;
111+
112+
/**
113+
* Boolean indicating the success of the authentication process.
114+
*/
41115
success: boolean;
116+
117+
/**
118+
* User object representing the authenticated user.
119+
*/
42120
user: object;
43121
}
44122

123+
/**
124+
* Realtime request structure for authentication.
125+
*/
45126
type RealtimeRequestAuthenticate = {
127+
/**
128+
* Session identifier for authentication.
129+
*/
46130
session: string;
47131
}
48132

133+
/**
134+
* Realtime interface representing the structure of a realtime communication object.
135+
*/
49136
type Realtime = {
137+
/**
138+
* WebSocket instance for realtime communication.
139+
*/
50140
socket?: WebSocket;
141+
142+
/**
143+
* Timeout duration for communication operations.
144+
*/
51145
timeout?: number;
146+
147+
/**
148+
* URL for establishing the WebSocket connection.
149+
*/
52150
url?: string;
151+
152+
/**
153+
* Last received message from the realtime server.
154+
*/
53155
lastMessage?: RealtimeResponse;
156+
157+
/**
158+
* Set of channel names the client is subscribed to.
159+
*/
54160
channels: Set<string>;
161+
162+
/**
163+
* Map of subscriptions containing channel names and corresponding callback functions.
164+
*/
55165
subscriptions: Map<number, {
56166
channels: string[];
57167
callback: (payload: RealtimeResponseEvent<any>) => void
58168
}>;
169+
170+
/**
171+
* Counter for managing subscriptions.
172+
*/
59173
subscriptionsCounter: number;
174+
175+
/**
176+
* Boolean indicating whether automatic reconnection is enabled.
177+
*/
60178
reconnect: boolean;
179+
180+
/**
181+
* Number of reconnection attempts made.
182+
*/
61183
reconnectAttempts: number;
184+
185+
/**
186+
* Function to get the timeout duration for communication operations.
187+
*/
62188
getTimeout: () => number;
189+
190+
/**
191+
* Function to establish a WebSocket connection.
192+
*/
63193
connect: () => void;
194+
195+
/**
196+
* Function to create a new WebSocket instance.
197+
*/
64198
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+
*/
65205
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+
*/
66212
onMessage: (event: MessageEvent) => void;
67213
}
68214

215+
/**
216+
* Type representing upload progress information.
217+
*/
69218
export type UploadProgress = {
219+
/**
220+
* Identifier for the upload progress.
221+
*/
70222
$id: string;
223+
224+
/**
225+
* Current progress of the upload (in percentage).
226+
*/
71227
progress: number;
228+
229+
/**
230+
* Total size uploaded (in bytes) during the upload process.
231+
*/
72232
sizeUploaded: number;
233+
234+
/**
235+
* Total number of chunks that need to be uploaded.
236+
*/
73237
chunksTotal: number;
238+
239+
/**
240+
* Number of chunks that have been successfully uploaded.
241+
*/
74242
chunksUploaded: number;
75243
}
76244

245+
/**
246+
* Exception thrown by the {{language.params.packageName}} package
247+
*/
77248
class {{spec.title | caseUcfirst}}Exception extends Error {
249+
/**
250+
* The error code associated with the exception.
251+
*/
78252
code: number;
253+
254+
/**
255+
* The response string associated with the exception.
256+
*/
79257
response: string;
258+
259+
/**
260+
* Error type.
261+
* See [Error Types]({{sdk.url}}/docs/response-codes#errorTypes) for more information.
262+
*/
80263
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+
*/
81273
constructor(message: string, code: number = 0, type: string = '', response: string = '') {
82274
super(message);
83275
this.name = '{{spec.title | caseUcfirst}}Exception';
@@ -88,14 +280,24 @@ class {{spec.title | caseUcfirst}}Exception extends Error {
88280
}
89281
}
90282

283+
/**
284+
* Client that handles requests to {{spec.title | caseUcfirst}}
285+
*/
91286
class Client {
287+
/**
288+
* Holds configuration such as project.
289+
*/
92290
config = {
93291
endpoint: '{{ spec.endpoint }}',
94292
endpointRealtime: '',
95293
{% for header in spec.global.headers %}
96294
{{ header.key | caseLower }}: '',
97295
{% endfor %}
98296
};
297+
298+
/**
299+
* Custom headers for API requests.
300+
*/
99301
headers: Headers = {
100302
'x-sdk-name': '{{ sdk.name }}',
101303
'x-sdk-platform': '{{ sdk.platform }}',
@@ -338,6 +540,19 @@ class Client {
338540
}
339541
}
340542

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+
*/
341556
async call(method: string, url: URL, headers: Headers = {}, params: Payload = {}): Promise<any> {
342557
method = method.toUpperCase();
343558

templates/web/src/id.ts.twig

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1+
/**
2+
* Helper class to generate ID strings for resources.
3+
*/
14
export class ID {
2-
// Generate an hex ID based on timestamp
3-
// Recreated from https://www.php.net/manual/en/function.uniqid.php
5+
/**
6+
* Generate an hex ID based on timestamp.
7+
* Recreated from https://www.php.net/manual/en/function.uniqid.php
8+
*
9+
* @returns {string}
10+
*/
411
static #hexTimestamp(): string {
512
const now = new Date();
613
const sec = Math.floor(now.getTime() / 1000);
@@ -11,10 +18,22 @@ export class ID {
1118
return hexTimestamp;
1219
}
1320

21+
/**
22+
* Uses the provided ID as the ID for the resource.
23+
*
24+
* @param {string} id
25+
* @returns {string}
26+
*/
1427
public static custom(id: string): string {
1528
return id
1629
}
1730

31+
/**
32+
* Have Appwrite generate a unique ID for you.
33+
*
34+
* @param {number} padding. Default is 7.
35+
* @returns {string}
36+
*/
1837
public static unique(padding: number = 7): string {
1938
// Generate a unique ID with padding to have a longer ID
2039
const baseId = ID.#hexTimestamp();

templates/web/src/index.ts.twig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/**
2+
* {{spec.title | caseUcfirst}} {{sdk.name}} SDK
3+
*
4+
* This SDK is compatible with Appwrite server version {{spec.version | split('.') | slice(0,2) | join('.') ~ '.x' }}.
5+
* For older versions, please check
6+
* [previous releases](https://github.com/{{sdk.gitUserName}}/{{sdk.gitRepoName}}/releases).
7+
*/
18
export { Client, Query, {{spec.title | caseUcfirst}}Exception } from './client';
29
{% for service in spec.services %}
310
export { {{service.name | caseUcfirst}} } from './services/{{service.name | caseDash}}';

templates/web/src/models.ts.twig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/**
2+
* {{spec.title | caseUcfirst}} Models
3+
*/
14
export namespace Models {
25
{% for definition in spec.definitions %}
36
/**

0 commit comments

Comments
 (0)