Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion specs/swagger-appwrite.0.10.0.json

Large diffs are not rendered by default.

234 changes: 149 additions & 85 deletions templates/web/src/sdk.ts.twig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import "isomorphic-form-data";
import { fetch } from "cross-fetch";
import 'isomorphic-form-data';
import { fetch } from 'cross-fetch';

type Payload = {
[key: string]: any;
Expand All @@ -10,12 +10,12 @@ type Headers = {
}

type RealtimeResponse = {
type: "error"|"event"|"connected"|"response";
type: 'error'|'event'|'connected'|'response';
data: RealtimeResponseAuthenticated|RealtimeResponseConnected|RealtimeResponseError|RealtimeResponseEvent<unknown>;
}

type RealtimeRequest = {
type: "authentication";
type: 'authentication';
data: RealtimeRequestAuthenticate;
}

Expand Down Expand Up @@ -49,13 +49,21 @@ type RealtimeRequestAuthenticate = {
type Realtime = {
socket?: WebSocket;
timeout?: number;
url?: string;
lastMessage?: RealtimeResponse;
channels: {
[key: string]: ((event: MessageEvent) => void)[]
},
channels: Set<string>;
subscriptions: Map<number, {
channels: string[];
callback: (payload: RealtimeResponseEvent<any>) => void
}>;
subscriptionsCounter: number;
reconnect: boolean;
reconnectAttempts: number;
getTimeout: () => number;
connect: () => void;
createSocket: () => void;
authenticate: (event: MessageEvent) => void;
onMessage: <T extends unknown>(channel: string, callback: (response: RealtimeResponseEvent<T>) => void) => (event: MessageEvent) => void;
cleanUp: (channels: string[]) => void;
onMessage: (event: MessageEvent) => void;
}

class {{spec.title | caseUcfirst}}Exception extends Error {
Expand Down Expand Up @@ -96,7 +104,7 @@ class {{ spec.title | caseUcfirst }} {
*/
setEndpoint(endpoint: string): this {
this.config.endpoint = endpoint;
this.config.endpointRealtime = this.config.endpointRealtime || this.config.endpoint.replace("https://", "wss://").replace("http://", "ws://");
this.config.endpointRealtime = this.config.endpointRealtime || this.config.endpoint.replace('https://', 'wss://').replace('http://', 'ws://');

return this;
}
Expand Down Expand Up @@ -137,71 +145,136 @@ class {{ spec.title | caseUcfirst }} {
private realtime: Realtime = {
socket: undefined,
timeout: undefined,
channels: {},
url: '',
channels: new Set(),
subscriptions: new Map(),
subscriptionsCounter: 0,
reconnect: true,
reconnectAttempts: 0,
lastMessage: undefined,
connect: () => {
clearTimeout(this.realtime.timeout);
this.realtime.timeout = window?.setTimeout(() => {
this.realtime.createSocket();
}, 50);
},
getTimeout: () => {
switch (true) {
case this.realtime.reconnectAttempts < 5:
return 1000;
case this.realtime.reconnectAttempts < 15:
return 5000;
case this.realtime.reconnectAttempts < 100:
return 10_000;
default:
return 60_000;
}
},
createSocket: () => {
if (this.realtime.channels.size < 1) return;

const channels = new URLSearchParams();
channels.set('project', this.config.project);
for (const property in this.realtime.channels) {
channels.append('channels[]', property);
}
if (this.realtime.socket?.readyState === WebSocket.OPEN) {
this.realtime.socket.close();
}

this.realtime.socket = new WebSocket(this.config.endpointRealtime + '/realtime?' + channels.toString());
this.realtime.socket?.addEventListener('message', this.realtime.authenticate);
this.realtime.channels.forEach(channel => {
channels.append('channels[]', channel);
});

const url = this.config.endpointRealtime + '/realtime?' + channels.toString();

if (
url !== this.realtime.url || // Check if URL is present
!this.realtime.socket || // Check if WebSocket has not been created
this.realtime.socket?.readyState > WebSocket.OPEN // Check if WebSocket is CLOSING (3) or CLOSED (4)
) {
if (
this.realtime.socket &&
this.realtime.socket?.readyState < WebSocket.CLOSING // Close WebSocket if it is CONNECTING (0) or OPEN (1)
) {
this.realtime.reconnect = false;
this.realtime.socket.close();
}

for (const channel in this.realtime.channels) {
this.realtime.channels[channel].forEach(callback => {
this.realtime.socket?.addEventListener('message', callback);
this.realtime.url = url;
this.realtime.socket = new WebSocket(url);
this.realtime.socket.addEventListener('message', this.realtime.onMessage);
this.realtime.socket.addEventListener('open', _event => {
this.realtime.reconnectAttempts = 0;
});
}
this.realtime.socket.addEventListener('close', event => {
if (
!this.realtime.reconnect ||
(
this.realtime?.lastMessage?.type === 'error' && // Check if last message was of type error
(<RealtimeResponseError>this.realtime?.lastMessage.data).code === 1008 // Check for policy violation 1008
)
) {
this.realtime.reconnect = true;
return;
}

this.realtime.socket.addEventListener('close', event => {
if (this.realtime?.lastMessage?.type === 'error' && (<RealtimeResponseError>this.realtime?.lastMessage.data).code === 1008) {
return;
}
console.error('Realtime got disconnected. Reconnect will be attempted in 1 second.', event.reason);
setTimeout(() => {
this.realtime.createSocket();
}, 1000);
})
const timeout = this.realtime.getTimeout();
console.error(`Realtime got disconnected. Reconnect will be attempted in ${timeout / 1000} seconds.`, event.reason);

setTimeout(() => {
this.realtime.reconnectAttempts++;
this.realtime.createSocket();
}, timeout);
})
}
},
authenticate: (event) => {
const message: RealtimeResponse = JSON.parse(event.data);
if (message.type === 'connected' && this.realtime.socket?.readyState === WebSocket.OPEN) {
const cookie = JSON.parse(window.localStorage.getItem('cookieFallback') ?? "{}");
const session = cookie?.[`a_session_${this.config.project}`];
const data = <RealtimeResponseConnected>message.data;

if (session && !data.user) {
this.realtime.socket?.send(JSON.stringify(<RealtimeRequest>{
type: "authentication",
data: {
session
onMessage: (event) => {
try {
const message: RealtimeResponse = JSON.parse(event.data);
this.realtime.lastMessage = message;
switch (message.type) {
case 'connected':
const cookie = JSON.parse(window.localStorage.getItem('cookieFallback') ?? '{}');
const session = cookie?.[`a_session_${this.config.project}`];
const messageData = <RealtimeResponseConnected>message.data;

if (session && !messageData.user) {
this.realtime.socket?.send(JSON.stringify(<RealtimeRequest>{
type: 'authentication',
data: {
session
}
}));
}
break;
case 'event':
let data = <RealtimeResponseEvent<unknown>>message.data;
if (data?.channels) {
const isSubscribed = data.channels.some(channel => this.realtime.channels.has(channel));
if (!isSubscribed) return;
this.realtime.subscriptions.forEach(subscription => {
if (data.channels.some(channel => subscription.channels.includes(channel))) {
setTimeout(() => subscription.callback(data));
}
})
}
}));
break;
case 'error':
throw message.data;
default:
break;
}
} catch (e) {
console.error(e);
}
},
onMessage: <T extends unknown>(channel: string, callback: (response: RealtimeResponseEvent<T>) => void) =>
(event) => {
try {
const message: RealtimeResponse = JSON.parse(event.data);
this.realtime.lastMessage = message;
if (message.type === 'event') {
let data = <RealtimeResponseEvent<T>>message.data;
if (data.channels && data.channels.includes(channel)) {
callback(data);
}
} else if (message.type === 'error') {
throw message.data;
cleanUp: channels => {
this.realtime.channels.forEach(channel => {
if (channels.includes(channel)) {
let found = Array.from(this.realtime.subscriptions).some(([_key, subscription] )=> {
return subscription.channels.includes(channel);
})

if (!found) {
this.realtime.channels.delete(channel);
}
} catch (e) {
console.error(e);
}
}
})
}
}

/**
Expand Down Expand Up @@ -231,29 +304,20 @@ class {{ spec.title | caseUcfirst }} {
*/
subscribe<T extends unknown>(channels: string | string[], callback: (payload: RealtimeResponseEvent<T>) => void): () => void {
let channelArray = typeof channels === 'string' ? [channels] : channels;
let savedChannels: {
name: string;
index: number;
}[] = [];
channelArray.forEach((channel, index) => {
if (!(channel in this.realtime.channels)) {
this.realtime.channels[channel] = [];
}
savedChannels[index] = {
name: channel,
index: (this.realtime.channels[channel].push(this.realtime.onMessage<T>(channel, callback)) - 1)
};
clearTimeout(this.realtime.timeout);
this.realtime.timeout = window?.setTimeout(() => {
this.realtime.createSocket();
}, 1);
channelArray.forEach(channel => this.realtime.channels.add(channel));

const counter = this.realtime.subscriptionsCounter++;
this.realtime.subscriptions.set(counter, {
channels: channelArray,
callback
});

this.realtime.connect();

return () => {
savedChannels.forEach(channel => {
this.realtime.socket?.removeEventListener('message', this.realtime.channels[channel.name][channel.index]);
this.realtime.channels[channel.name].splice(channel.index, 1);
})
this.realtime.subscriptions.delete(counter);
this.realtime.cleanUp(channelArray);
this.realtime.connect();
}
}

Expand All @@ -270,7 +334,7 @@ class {{ spec.title | caseUcfirst }} {
};

if (typeof window !== 'undefined' && window.localStorage) {
headers['X-Fallback-Cookies'] = window.localStorage.getItem('cookieFallback') ?? "";
headers['X-Fallback-Cookies'] = window.localStorage.getItem('cookieFallback') ?? '';
}

if (method === 'GET') {
Expand Down Expand Up @@ -304,14 +368,14 @@ class {{ spec.title | caseUcfirst }} {
let data = null;
const response = await fetch(url.toString(), options);

if (response.headers.get("content-type")?.includes("application/json")) {
if (response.headers.get('content-type')?.includes('application/json')) {
data = await response.json();
} else {
data = {
message: await response.text()
};
}

if (400 <= response.status) {
throw new {{spec.title | caseUcfirst}}Exception(data?.message, response.status, data);
}
Expand Down Expand Up @@ -368,7 +432,7 @@ class {{ spec.title | caseUcfirst }} {
* @returns {% if method.type == 'webAuth' %}{void|string}{% elseif method.type == 'location' %}{URL}{% else %}{Promise}{% endif %}

*/
{{ method.name | caseCamel }}: {% if method.type != "location" and method.type != 'webAuth'%}async <T extends unknown>{% endif %}({% for parameter in method.parameters.all %}{{ parameter.name | caseCamel | escapeKeyword }}{% if not parameter.required %}?{% endif %}: {{ parameter.type | typeName }}{% if not loop.last %}, {% endif %}{% endfor %}): {% if method.type == 'webAuth' %}void | URL{% elseif method.type == 'location' %}URL{% else %}Promise<T>{% endif %} => {
{{ method.name | caseCamel }}: {% if method.type != 'location' and method.type != 'webAuth'%}async <T extends unknown>{% endif %}({% for parameter in method.parameters.all %}{{ parameter.name | caseCamel | escapeKeyword }}{% if not parameter.required %}?{% endif %}: {{ parameter.type | typeName }}{% if not loop.last %}, {% endif %}{% endfor %}): {% if method.type == 'webAuth' %}void | URL{% elseif method.type == 'location' %}URL{% else %}Promise<T>{% endif %} => {
{% for parameter in method.parameters.all %}
{% if parameter.required %}
if (typeof {{ parameter.name | caseCamel | escapeKeyword }} === 'undefined') {
Expand Down