Skip to content

Commit 6446c4f

Browse files
committed
feat: add api for fetching app settings
1 parent d614dcb commit 6446c4f

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

src/client.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,17 @@ export class StreamChat {
802802
* getAppSettings - retrieves application settings
803803
*/
804804
async getAppSettings() {
805-
return await this.get<AppSettingsAPIResponse>(this.baseURL + '/app');
805+
const userId = this.userID as string;
806+
if (!this.wsConnection?.isHealthy && this.offlineDb && userId) {
807+
return await this.offlineDb?.getAppSettings({ userId });
808+
}
809+
const appSettings = await this.get<AppSettingsAPIResponse>(this.baseURL + '/app');
810+
811+
if (userId) {
812+
this.offlineDb?.upsertAppSettings({ appSettings, userId });
813+
}
814+
815+
return appSettings;
806816
}
807817

808818
/**

src/offline_support_api.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type {
22
APIErrorResponse,
3+
AppSettingsAPIResponse,
34
ChannelAPIResponse,
45
ChannelFilters,
56
ChannelSort,
@@ -28,6 +29,12 @@ export type UpsertChannelsType = {
2829
sort?: ChannelSort;
2930
};
3031

32+
export type UpsertAppSettingsType = {
33+
appSettings: AppSettingsAPIResponse;
34+
userId: string;
35+
flush?: boolean;
36+
};
37+
3138
export type UpsertUserSyncStatusType = {
3239
userId: string;
3340
lastSyncedAt: string;
@@ -50,6 +57,10 @@ export type GetLastSyncedAtType = {
5057

5158
export type GetPendingTasksType = { messageId?: string };
5259

60+
export type GetAppSettingsType = {
61+
userId: string;
62+
};
63+
5364
export type DeletePendingTaskType = { id: number };
5465

5566
export type DeleteReactionType = {
@@ -68,20 +79,22 @@ export interface OfflineDBApi {
6879
upsertCidsForQuery: (options: UpsertCidsForQueryType) => Promise<unknown>;
6980
upsertChannels: (options: UpsertChannelsType) => Promise<unknown>;
7081
upsertUserSyncStatus: (options: UpsertUserSyncStatusType) => Promise<unknown>;
82+
upsertAppSettings: (options: UpsertAppSettingsType) => Promise<unknown>;
7183
getChannels: (options: GetChannelsType) => Promise<unknown>;
7284
getChannelsForQuery: (
7385
options: GetChannelsForQueryType,
7486
) => Promise<Omit<ChannelAPIResponse, 'duration'>[] | null>;
7587
getAllChannelCids: () => Promise<string[]>;
7688
getLastSyncedAt: (options: GetLastSyncedAtType) => Promise<number | undefined>;
77-
resetDB: () => Promise<unknown>;
89+
getAppSettings: (options: GetAppSettingsType) => Promise<unknown>;
7890
executeSqlBatch: (queries: ExecuteBatchQueriesType) => Promise<unknown>;
7991
addPendingTask: (task: PendingTask) => Promise<() => Promise<void>>;
8092
getPendingTasks: (conditions?: GetPendingTasksType) => Promise<PendingTask[]>;
8193
deletePendingTask: (options: DeletePendingTaskType) => Promise<unknown>;
8294
deleteReaction: (options: DeleteReactionType) => Promise<unknown>;
83-
hardDeleteMessage: (optins: DeleteMessageType) => Promise<unknown>;
84-
softDeleteMessage: (optins: DeleteMessageType) => Promise<unknown>;
95+
hardDeleteMessage: (options: DeleteMessageType) => Promise<unknown>;
96+
softDeleteMessage: (options: DeleteMessageType) => Promise<unknown>;
97+
resetDB: () => Promise<unknown>;
8598
}
8699

87100
export abstract class AbstractOfflineDB implements OfflineDBApi {
@@ -97,6 +110,10 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
97110

98111
abstract upsertChannels: OfflineDBApi['upsertChannels'];
99112

113+
abstract upsertUserSyncStatus: OfflineDBApi['upsertUserSyncStatus'];
114+
115+
abstract upsertAppSettings: OfflineDBApi['upsertAppSettings'];
116+
100117
abstract getChannels: OfflineDBApi['getChannels'];
101118

102119
abstract getChannelsForQuery: OfflineDBApi['getChannelsForQuery'];
@@ -105,16 +122,14 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
105122

106123
abstract getLastSyncedAt: OfflineDBApi['getLastSyncedAt'];
107124

108-
abstract resetDB: OfflineDBApi['resetDB'];
125+
abstract getPendingTasks: OfflineDBApi['getPendingTasks'];
109126

110-
abstract executeSqlBatch: OfflineDBApi['executeSqlBatch'];
127+
abstract getAppSettings: OfflineDBApi['getAppSettings'];
111128

112-
abstract upsertUserSyncStatus: OfflineDBApi['upsertUserSyncStatus'];
129+
abstract executeSqlBatch: OfflineDBApi['executeSqlBatch'];
113130

114131
abstract addPendingTask: OfflineDBApi['addPendingTask'];
115132

116-
abstract getPendingTasks: OfflineDBApi['getPendingTasks'];
117-
118133
abstract deletePendingTask: OfflineDBApi['deletePendingTask'];
119134

120135
abstract deleteReaction: OfflineDBApi['deleteReaction'];
@@ -123,6 +138,8 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
123138

124139
abstract softDeleteMessage: OfflineDBApi['softDeleteMessage'];
125140

141+
abstract resetDB: OfflineDBApi['resetDB'];
142+
126143
public queueTask = async ({ task }: { task: PendingTask }) => {
127144
let response;
128145
try {

0 commit comments

Comments
 (0)