Skip to content

Commit 7662cdf

Browse files
Add inline doc comments to the Web SDK template
1 parent 2069238 commit 7662cdf

File tree

8 files changed

+483
-61
lines changed

8 files changed

+483
-61
lines changed

templates/web/README.md.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# {{ spec.title }} {{sdk.name}} SDK
22

33
![License](https://img.shields.io/github/license/{{ sdk.gitUserName|url_encode }}/{{ sdk.gitRepoName|url_encode }}.svg?style=flat-square)
4-
![Version](https://img.shields.io/badge/api%20version-{{ spec.version|url_encode }}-blue.svg?style=flat-square)
4+
![Version](https://img.shields.io/badge/api%20version-{{ spec.version | split('.') | slice(0,2) | join('.') ~ '.x' | url_encode }}-blue.svg?style=flat-square)
55
[![Build Status](https://img.shields.io/travis/com/appwrite/sdk-generator?style=flat-square)](https://travis-ci.com/appwrite/sdk-generator)
66
{% if sdk.twitterHandle %}
77
[![Twitter Account](https://img.shields.io/twitter/follow/{{ sdk.twitterHandle }}?color=00acee&label=twitter&style=flat-square)](https://twitter.com/{{ sdk.twitterHandle }})

templates/web/src/client.ts.twig

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

6+
/**
7+
* Payload type representing a key-value pair with string keys and any values.
8+
*/
69
type Payload = {
710
[key: string]: any;
811
}
912

13+
/**
14+
* Headers type representing a key-value pair with string keys and string values.
15+
*/
1016
type Headers = {
1117
[key: string]: string;
1218
}
1319

20+
/**
21+
* Realtime response structure with different types.
22+
*/
1423
type RealtimeResponse = {
24+
/**
25+
* Type of the response: 'error', 'event', 'connected', or 'response'.
26+
*/
1527
type: 'error' | 'event' | 'connected' | 'response';
28+
29+
/**
30+
* Data associated with the response based on the response type.
31+
*/
1632
data: RealtimeResponseAuthenticated | RealtimeResponseConnected | RealtimeResponseError | RealtimeResponseEvent<unknown>;
1733
}
1834

35+
/**
36+
* Realtime request structure for authentication.
37+
*/
1938
type RealtimeRequest = {
39+
/**
40+
* Type of the request: 'authentication'.
41+
*/
2042
type: 'authentication';
43+
44+
/**
45+
* Data required for authentication.
46+
*/
2147
data: RealtimeRequestAuthenticate;
2248
}
2349

50+
/**
51+
* Realtime event response structure with generic payload type.
52+
*/
2453
export type RealtimeResponseEvent<T extends unknown> = {
54+
/**
55+
* List of event names associated with the response.
56+
*/
2557
events: string[];
58+
59+
/**
60+
* List of channel names associated with the response.
61+
*/
2662
channels: string[];
63+
64+
/**
65+
* Timestamp indicating the time of the event.
66+
*/
2767
timestamp: number;
68+
69+
/**
70+
* Payload containing event-specific data.
71+
*/
2872
payload: T;
2973
}
3074

75+
/**
76+
* Realtime response structure for errors.
77+
*/
3178
type RealtimeResponseError = {
79+
/**
80+
* Numeric error code indicating the type of error.
81+
*/
3282
code: number;
83+
84+
/**
85+
* Error message describing the encountered error.
86+
*/
3387
message: string;
3488
}
3589

90+
/**
91+
* Realtime response structure for a successful connection.
92+
*/
3693
type RealtimeResponseConnected = {
94+
/**
95+
* List of channels the user is connected to.
96+
*/
3797
channels: string[];
98+
99+
/**
100+
* User object representing the connected user (optional).
101+
*/
38102
user?: object;
39103
}
40104

105+
/**
106+
* Realtime response structure for authenticated connections.
107+
*/
41108
type RealtimeResponseAuthenticated = {
109+
/**
110+
* Destination channel for the response.
111+
*/
42112
to: string;
113+
114+
/**
115+
* Boolean indicating the success of the authentication process.
116+
*/
43117
success: boolean;
118+
119+
/**
120+
* User object representing the authenticated user.
121+
*/
44122
user: object;
45123
}
46124

125+
/**
126+
* Realtime request structure for authentication.
127+
*/
47128
type RealtimeRequestAuthenticate = {
129+
/**
130+
* Session identifier for authentication.
131+
*/
48132
session: string;
49133
}
50134

135+
/**
136+
* Realtime interface representing the structure of a realtime communication object.
137+
*/
51138
type Realtime = {
139+
/**
140+
* WebSocket instance for realtime communication.
141+
*/
52142
socket?: WebSocket;
143+
144+
/**
145+
* Timeout duration for communication operations.
146+
*/
53147
timeout?: number;
148+
149+
/**
150+
* URL for establishing the WebSocket connection.
151+
*/
54152
url?: string;
153+
154+
/**
155+
* Last received message from the realtime server.
156+
*/
55157
lastMessage?: RealtimeResponse;
158+
159+
/**
160+
* Set of channel names the client is subscribed to.
161+
*/
56162
channels: Set<string>;
163+
164+
/**
165+
* Map of subscriptions containing channel names and corresponding callback functions.
166+
*/
57167
subscriptions: Map<number, {
58168
channels: string[];
59169
callback: (payload: RealtimeResponseEvent<any>) => void
60170
}>;
171+
172+
/**
173+
* Counter for managing subscriptions.
174+
*/
61175
subscriptionsCounter: number;
176+
177+
/**
178+
* Boolean indicating whether automatic reconnection is enabled.
179+
*/
62180
reconnect: boolean;
181+
182+
/**
183+
* Number of reconnection attempts made.
184+
*/
63185
reconnectAttempts: number;
186+
187+
/**
188+
* Function to get the timeout duration for communication operations.
189+
*/
64190
getTimeout: () => number;
191+
192+
/**
193+
* Function to establish a WebSocket connection.
194+
*/
65195
connect: () => void;
196+
197+
/**
198+
* Function to create a new WebSocket instance.
199+
*/
66200
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+
*/
67207
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+
*/
68214
onMessage: (event: MessageEvent) => void;
69215
}
70216

217+
/**
218+
* Type representing upload progress information.
219+
*/
71220
export type UploadProgress = {
221+
/**
222+
* Identifier for the upload progress.
223+
*/
72224
$id: string;
225+
226+
/**
227+
* Current progress of the upload (in percentage).
228+
*/
73229
progress: number;
230+
231+
/**
232+
* Total size uploaded (in bytes) during the upload process.
233+
*/
74234
sizeUploaded: number;
235+
236+
/**
237+
* Total number of chunks that need to be uploaded.
238+
*/
75239
chunksTotal: number;
240+
241+
/**
242+
* Number of chunks that have been successfully uploaded.
243+
*/
76244
chunksUploaded: number;
77245
}
78246

247+
/**
248+
* Exception thrown by the {{language.params.packageName}} package
249+
*/
79250
class {{spec.title | caseUcfirst}}Exception extends Error {
251+
/**
252+
* The error code associated with the exception.
253+
*/
80254
code: number;
255+
256+
/**
257+
* The response string associated with the exception.
258+
*/
81259
response: string;
260+
261+
/**
262+
* Error type.
263+
* See [Error Types]({{sdk.url}}/docs/response-codes#errorTypes) for more information.
264+
*/
82265
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+
*/
83275
constructor(message: string, code: number = 0, type: string = '', response: string = '') {
84276
super(message);
85277
this.name = '{{spec.title | caseUcfirst}}Exception';
@@ -90,14 +282,24 @@ class {{spec.title | caseUcfirst}}Exception extends Error {
90282
}
91283
}
92284

285+
/**
286+
* Client that handles requests to {{spec.title | caseUcfirst}}
287+
*/
93288
class Client {
289+
/**
290+
* Holds configuration such as project.
291+
*/
94292
config = {
95293
endpoint: '{{ spec.endpoint }}',
96294
endpointRealtime: '',
97295
{% for header in spec.global.headers %}
98296
{{ header.key | caseLower }}: '',
99297
{% endfor %}
100298
};
299+
300+
/**
301+
* Custom headers for API requests.
302+
*/
101303
headers: Headers = {
102304
'x-sdk-name': '{{ sdk.name }}',
103305
'x-sdk-platform': '{{ sdk.platform }}',
@@ -336,6 +538,19 @@ class Client {
336538
}
337539
}
338540

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

templates/web/src/id.ts.twig

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
1+
/**
2+
* Helper class to generate ID strings for resources.
3+
*/
14
export class ID {
2-
public static custom(id: string): string {
3-
return id
5+
/**
6+
* Uses the provided ID as the ID for the resource.
7+
*
8+
* @param {string} id
9+
* @returns {string}
10+
*/
11+
static custom(id: string): string {
12+
return id;
413
}
5-
6-
public static unique(): string {
7-
return 'unique()'
14+
15+
/**
16+
* Have Appwrite generate a unique ID for you.
17+
*
18+
* @returns {string}
19+
*/
20+
static unique(): string {
21+
return 'unique()';
822
}
9-
}
23+
}

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)