Skip to content

Commit

Permalink
Add ThreadResponseCustomData interface
Browse files Browse the repository at this point in the history
  • Loading branch information
arnautov-anton committed Jan 10, 2025
1 parent 6b7fb82 commit ade824e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
defaultWSTimeoutWithFallback: number;
defaultWSTimeout: number;
private nextRequestAbortController: AbortController | null = null;
public readonly customPropertyKeys: NonNullable<StreamChatOptions['customPropertyKeys']>;

/**
* Initialize a client
Expand Down Expand Up @@ -363,6 +364,7 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
this.defaultWSTimeout = 15 * 1000;

this.axiosInstance.defaults.paramsSerializer = axiosParamsSerializer;
this.customPropertyKeys = options?.customPropertyKeys ?? { thread: [] };

/**
* logger function should accept 3 parameters:
Expand Down
19 changes: 18 additions & 1 deletion src/thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
MessageResponse,
ReadResponse,
ThreadResponse,
ThreadResponseCustomData,
UserResponse,
} from './types';
import { addToMessageList, findIndexInSortedArray, formatMessage, throttle } from './utils';
Expand All @@ -27,6 +28,7 @@ export type ThreadState<SCG extends ExtendableGenerics = DefaultGenerics> = {
active: boolean;
channel: Channel<SCG>;
createdAt: Date;
custom: ThreadResponseCustomData;
deletedAt: Date | null;
isLoading: boolean;
isStateStale: boolean;
Expand Down Expand Up @@ -67,6 +69,19 @@ const DEFAULT_PAGE_LIMIT = 50;
const DEFAULT_SORT: { created_at: AscDesc }[] = [{ created_at: -1 }];
const MARK_AS_READ_THROTTLE_TIMEOUT = 1000;

// TODO: remove this once we move to API v2
const constructCustomDataObject = <SCG extends ExtendableGenerics>(
customPropertyKeys: (keyof ThreadResponseCustomData)[],
threadData: ThreadResponse<SCG>,
) =>
customPropertyKeys.reduce<ThreadResponseCustomData>((custom, key) => {
if (threadData[key]) {
custom[key] = threadData[key];
}

return custom;
}, {});

export class Thread<SCG extends ExtendableGenerics = DefaultGenerics> {
public readonly state: StateStore<ThreadState<SCG>>;
public readonly id: string;
Expand Down Expand Up @@ -107,6 +122,7 @@ export class Thread<SCG extends ExtendableGenerics = DefaultGenerics> {
replyCount: threadData.reply_count ?? 0,
updatedAt: threadData.updated_at ? new Date(threadData.updated_at) : null,
title: threadData.title,
custom: constructCustomDataObject(client.customPropertyKeys.thread, threadData),
});

this.id = threadData.parent_message_id;
Expand Down Expand Up @@ -213,7 +229,8 @@ export class Thread<SCG extends ExtendableGenerics = DefaultGenerics> {
title: threadData.title,
updatedAt: new Date(threadData.updated_at),
deletedAt: threadData.deleted_at ? new Date(threadData.deleted_at) : null,
// TODO: handle custom data - ideally one property (like `custom`)
// TODO: use threadData.custom once we move to API v2
custom: constructCustomDataObject(this.client.customPropertyKeys.thread, threadData),
});
}).unsubscribe;
};
Expand Down
17 changes: 16 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,9 @@ export type GetMessageAPIResponse<
StreamChatGenerics extends ExtendableGenerics = DefaultGenerics
> = SendMessageAPIResponse<StreamChatGenerics>;

export interface ThreadResponse<SCG extends ExtendableGenerics = DefaultGenerics> {
export interface ThreadResponseCustomData {}

Check failure on line 523 in src/types.ts

View workflow job for this annotation

GitHub Actions / lint

An empty interface is equivalent to `{}`

export interface ThreadResponse<SCG extends ExtendableGenerics = DefaultGenerics> extends ThreadResponseCustomData {
// FIXME: according to OpenAPI, `channel` could be undefined but since cid is provided I'll asume that it's wrong
channel: ChannelResponse<SCG>;
channel_cid: string;
Expand All @@ -547,6 +549,8 @@ export interface ThreadResponse<SCG extends ExtendableGenerics = DefaultGenerics
user?: UserResponse<SCG>;
user_id?: string;
}>;
// TODO: when moving to API v2 we should do this instead
// custom: ThreadResponseCustomData;
}

// TODO: Figure out a way to strongly type set and unset.
Expand Down Expand Up @@ -1195,6 +1199,17 @@ export type StreamChatOptions = AxiosRequestConfig & {
// Set the instance of StableWSConnection on chat client. Its purely for testing purpose and should
// not be used in production apps.
wsConnection?: StableWSConnection;
/**
* To access your custom data from `StateStore` you'll need to declare your custom property names for each feature.
* At the moment this is only needed for the Thread class. To get full type support make sure to merge interface
* declarations too.
*/
customPropertyKeys?: {

Check failure on line 1207 in src/types.ts

View workflow job for this annotation

GitHub Actions / lint

Expected interface keys to be in required first natural insensitive ascending order. 'customPropertyKeys' should be before 'wsConnection'
/**
* Interface declaration for custom object properties: `ThreadResponseCustomData`
*/
thread: (keyof ThreadResponseCustomData)[];
};
};

export type SyncOptions = {
Expand Down

0 comments on commit ade824e

Please sign in to comment.