polfan-server-js-client 0.1.99906 → 0.1.99908
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/index.js +209 -63
- package/build/index.js.map +1 -1
- package/build/types/AbstractRestClient.d.ts +2 -0
- package/build/types/FilesClient.d.ts +13 -0
- package/build/types/index.d.ts +3 -2
- package/build/types/state-tracker/MessagesManager.d.ts +1 -1
- package/build/types/types/src/schemes/Message.d.ts +1 -0
- package/package.json +1 -1
- package/src/AbstractRestClient.ts +22 -14
- package/src/FilesClient.ts +29 -0
- package/src/index.ts +4 -2
- package/src/state-tracker/MessagesManager.ts +7 -4
- package/src/types/src/schemes/Message.ts +1 -0
|
@@ -13,7 +13,9 @@ export declare abstract class AbstractRestClient {
|
|
|
13
13
|
protected abstract defaultUrl: string;
|
|
14
14
|
constructor(options: RestClientOptions);
|
|
15
15
|
protected send<ResponseT = any>(method: HttpMethod, uri: string, data?: any): Promise<RestClientResponse<ResponseT>>;
|
|
16
|
+
protected getHeaders(): any;
|
|
16
17
|
protected getUrl(uri: string): string;
|
|
18
|
+
protected convertFetchResponse<T>(result: Response): Promise<RestClientResponse<T>>;
|
|
17
19
|
private removeStartingSlash;
|
|
18
20
|
private removeEndingSlash;
|
|
19
21
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { AbstractRestClient, RestClientResponse } from "./AbstractRestClient";
|
|
2
|
+
export interface File {
|
|
3
|
+
id: string;
|
|
4
|
+
url: string;
|
|
5
|
+
original_name: string;
|
|
6
|
+
mime_type: string;
|
|
7
|
+
size: number;
|
|
8
|
+
}
|
|
9
|
+
export declare class FilesClient extends AbstractRestClient {
|
|
10
|
+
protected defaultUrl: string;
|
|
11
|
+
uploadFile(file: Parameters<typeof FormData.prototype.append>[1]): Promise<RestClientResponse<File>>;
|
|
12
|
+
getFileMetadata(id: string): Promise<RestClientResponse<File>>;
|
|
13
|
+
}
|
package/build/types/index.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { WebSocketChatClient } from "./WebSocketChatClient";
|
|
2
2
|
import { WebApiChatClient } from "./WebApiChatClient";
|
|
3
3
|
import { IndexedCollection, IndexedObjectCollection, ObservableIndexedCollection, ObservableIndexedObjectCollection } from "./IndexedObjectCollection";
|
|
4
|
-
import { AuthClient } from "./AuthClient";
|
|
4
|
+
import { AuthClient, MyAccountInterface, TokenInterface } from "./AuthClient";
|
|
5
|
+
import { FilesClient, File } from "./FilesClient";
|
|
5
6
|
import { Permissions, PermissionDefinition, Layer } from "./Permissions";
|
|
6
7
|
import * as ChatTypes from './types/src';
|
|
7
|
-
export { IndexedCollection, ObservableIndexedCollection, IndexedObjectCollection, ObservableIndexedObjectCollection, Permissions, PermissionDefinition, Layer, WebSocketChatClient, WebApiChatClient, AuthClient, };
|
|
8
|
+
export { IndexedCollection, ObservableIndexedCollection, IndexedObjectCollection, ObservableIndexedObjectCollection, Permissions, PermissionDefinition, Layer, WebSocketChatClient, WebApiChatClient, AuthClient, MyAccountInterface, TokenInterface, FilesClient, File };
|
|
8
9
|
export type { ChatTypes };
|
|
@@ -18,7 +18,7 @@ export declare class MessagesManager {
|
|
|
18
18
|
* Then you can get them using getRoomFollowedTopics().
|
|
19
19
|
* @see getRoomFollowedTopics
|
|
20
20
|
*/
|
|
21
|
-
cacheSpaceFollowedTopics(spaceId: string): Promise<void>;
|
|
21
|
+
cacheSpaceFollowedTopics(spaceId: string | null): Promise<void>;
|
|
22
22
|
/**
|
|
23
23
|
* Get followed topics for the given room.
|
|
24
24
|
* @return Undefined if you are not in the room, collection otherwise.
|
package/package.json
CHANGED
|
@@ -23,15 +23,6 @@ export abstract class AbstractRestClient {
|
|
|
23
23
|
uri: string,
|
|
24
24
|
data: any = undefined
|
|
25
25
|
): Promise<RestClientResponse<ResponseT>> {
|
|
26
|
-
const headers: any = {
|
|
27
|
-
'Content-Type': 'application/json',
|
|
28
|
-
Accept: 'application/json'
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
if (this.options.token) {
|
|
32
|
-
headers.Authorization = `Bearer ${this.options.token}`;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
26
|
let url = this.getUrl(uri);
|
|
36
27
|
let body = undefined;
|
|
37
28
|
|
|
@@ -43,19 +34,36 @@ export abstract class AbstractRestClient {
|
|
|
43
34
|
}
|
|
44
35
|
}
|
|
45
36
|
|
|
46
|
-
const result = await fetch(url, {method, body, headers});
|
|
37
|
+
const result = await fetch(url, {method, body, headers: this.getHeaders()});
|
|
47
38
|
|
|
48
|
-
return
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
39
|
+
return this.convertFetchResponse(result);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
protected getHeaders(): any {
|
|
43
|
+
const headers: any = {
|
|
44
|
+
'Content-Type': 'application/json',
|
|
45
|
+
Accept: 'application/json'
|
|
52
46
|
};
|
|
47
|
+
|
|
48
|
+
if (this.options.token) {
|
|
49
|
+
headers.Authorization = `Bearer ${this.options.token}`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return headers;
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
protected getUrl(uri: string): string {
|
|
56
56
|
return this.removeEndingSlash(this.options.url ?? this.defaultUrl) + '/' + this.removeStartingSlash(uri);
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
+
protected async convertFetchResponse<T>(result: Response): Promise<RestClientResponse<T>> {
|
|
60
|
+
return {
|
|
61
|
+
ok: result.ok,
|
|
62
|
+
status: result.status,
|
|
63
|
+
data: result.headers.get('content-type')?.includes('json') ? await result.json() : await result.text(),
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
59
67
|
private removeStartingSlash(text: string): string {
|
|
60
68
|
return text.replace(/^\/+/, '');
|
|
61
69
|
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import {AbstractRestClient, RestClientResponse} from "./AbstractRestClient";
|
|
2
|
+
|
|
3
|
+
export interface File {
|
|
4
|
+
id: string;
|
|
5
|
+
url: string;
|
|
6
|
+
original_name: string;
|
|
7
|
+
mime_type: string;
|
|
8
|
+
size: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export class FilesClient extends AbstractRestClient {
|
|
12
|
+
protected defaultUrl: string = 'https://polfan.pl/webservice/api/files';
|
|
13
|
+
|
|
14
|
+
public async uploadFile(file: Parameters<typeof FormData.prototype.append>[1]): Promise<RestClientResponse<File>> {
|
|
15
|
+
const formData = new FormData();
|
|
16
|
+
formData.append('file', file);
|
|
17
|
+
|
|
18
|
+
let headers = this.getHeaders();
|
|
19
|
+
headers['Content-Type'] = 'multipart/form-data';
|
|
20
|
+
|
|
21
|
+
const response = await fetch(this.defaultUrl, {method: 'POST', body: formData, headers});
|
|
22
|
+
|
|
23
|
+
return this.convertFetchResponse<File>(response);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public async getFileMetadata(id: string): Promise<RestClientResponse<File>> {
|
|
27
|
+
return this.send('GET', '/' + id);
|
|
28
|
+
}
|
|
29
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -6,7 +6,8 @@ import {
|
|
|
6
6
|
ObservableIndexedCollection,
|
|
7
7
|
ObservableIndexedObjectCollection
|
|
8
8
|
} from "./IndexedObjectCollection";
|
|
9
|
-
import { AuthClient } from "./AuthClient";
|
|
9
|
+
import { AuthClient, MyAccountInterface, TokenInterface } from "./AuthClient";
|
|
10
|
+
import {FilesClient, File} from "./FilesClient";
|
|
10
11
|
import { Permissions, PermissionDefinition, Layer } from "./Permissions";
|
|
11
12
|
import * as ChatTypes from './types/src';
|
|
12
13
|
|
|
@@ -15,7 +16,8 @@ export {
|
|
|
15
16
|
IndexedObjectCollection, ObservableIndexedObjectCollection,
|
|
16
17
|
Permissions, PermissionDefinition, Layer,
|
|
17
18
|
WebSocketChatClient, WebApiChatClient,
|
|
18
|
-
AuthClient,
|
|
19
|
+
AuthClient, MyAccountInterface, TokenInterface,
|
|
20
|
+
FilesClient, File
|
|
19
21
|
};
|
|
20
22
|
|
|
21
23
|
export type {ChatTypes};
|
|
@@ -49,8 +49,8 @@ export class MessagesManager {
|
|
|
49
49
|
* Then you can get them using getRoomFollowedTopics().
|
|
50
50
|
* @see getRoomFollowedTopics
|
|
51
51
|
*/
|
|
52
|
-
public async cacheSpaceFollowedTopics(spaceId: string): Promise<void> {
|
|
53
|
-
if (! (await this.tracker.spaces.get()).has(spaceId)) {
|
|
52
|
+
public async cacheSpaceFollowedTopics(spaceId: string | null): Promise<void> {
|
|
53
|
+
if (spaceId && ! (await this.tracker.spaces.get()).has(spaceId)) {
|
|
54
54
|
throw new Error(`You are not in space ${spaceId}`);
|
|
55
55
|
}
|
|
56
56
|
|
|
@@ -61,8 +61,11 @@ export class MessagesManager {
|
|
|
61
61
|
return;
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
|
|
65
|
-
|
|
64
|
+
const resultPromise = this.tracker.client.send('GetFollowedTopics', {location: {spaceId}});
|
|
65
|
+
|
|
66
|
+
roomIds.forEach(roomId => this.followedTopicsPromises.register(resultPromise, roomId));
|
|
67
|
+
|
|
68
|
+
const result = await resultPromise;
|
|
66
69
|
|
|
67
70
|
if (result.error) {
|
|
68
71
|
throw result.error;
|