Skip to content

Commit 3ebe3bf

Browse files
Get rid of client construction overloads
1 parent 8ee7bce commit 3ebe3bf

2 files changed

Lines changed: 474 additions & 64 deletions

File tree

src/client.ts

Lines changed: 13 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ export class StreamChat extends ChatApi {
184184
blockedUsers: StateStore<BlockedUsersState>;
185185
node: boolean;
186186
options: StreamChatOptions;
187-
secret?: string;
188187
setUserPromise: ConnectAPIResponse | null;
189188
state: ClientState;
190189
tokenManager: TokenManager;
@@ -244,7 +243,6 @@ export class StreamChat extends ChatApi {
244243
* new StreamChat('api_key', 'secret', { httpsAgent: customAgent })
245244
*
246245
* @param key - The API key.
247-
* @param secret - The API secret (optional).
248246
* @param options - Additional options; here you can pass custom options to the axios instance (optional).
249247
* @param options.browser - Enforce the client to be in browser mode (optional).
250248
* @param options.warmUp - If `true`, the client will open a connection as soon as possible to speed up following requests (optional, defaults to `false`).
@@ -253,13 +251,7 @@ export class StreamChat extends ChatApi {
253251
* @param options.timeout - Request timeout (optional, defaults to `3000`).
254252
* @param options.httpsAgent - Custom `httpsAgent` (optional, in Node defaults to `https.agent()`).
255253
*/
256-
constructor(key: string, options?: StreamChatOptions);
257-
constructor(key: string, secret?: string, options?: StreamChatOptions);
258-
constructor(
259-
key: string,
260-
secretOrOptions?: StreamChatOptions | string,
261-
options?: StreamChatOptions,
262-
) {
254+
constructor(key: string, options: StreamChatOptions = {}) {
263255
// generated client requires ApiClient right away
264256
super(new ApiClient());
265257
// but ApiClient relies on properties defined here so we set it after (can't pass `this` in super call)
@@ -279,37 +271,22 @@ export class StreamChat extends ChatApi {
279271
this.notifications = options?.notifications ?? new NotificationManager();
280272
this.uploadManager = new UploadManager(this);
281273

282-
// set the secret
283-
if (secretOrOptions && isString(secretOrOptions)) {
284-
this.secret = secretOrOptions;
285-
}
286-
287-
// set the options... and figure out defaults...
288-
const inputOptions = options
289-
? options
290-
: secretOrOptions && !isString(secretOrOptions)
291-
? secretOrOptions
292-
: {};
293-
294-
this.browser =
295-
typeof inputOptions.browser !== 'undefined'
296-
? inputOptions.browser
297-
: typeof window !== 'undefined';
274+
this.browser = options.browser ?? typeof window !== 'undefined';
298275
this.node = !this.browser;
299276

300277
this.options = {
301278
warmUp: false,
302279
recoverStateOnReconnect: true,
303280
disableCache: false,
304281
wsUrlParams: new URLSearchParams({}),
305-
...inputOptions,
282+
...options,
306283
};
307284

308285
chatLoggerSystem.configureLoggers({
309-
...inputOptions.logOptions,
286+
...options.logOptions,
310287
default: {
311-
level: inputOptions.logLevel ?? 'info',
312-
...inputOptions.logOptions?.default,
288+
level: options.logLevel ?? 'info',
289+
...options.logOptions?.default,
313290
},
314291
});
315292

@@ -347,7 +324,7 @@ export class StreamChat extends ChatApi {
347324

348325
// If its a server-side client, then lets initialize the tokenManager, since token will be
349326
// generated from secret.
350-
this.tokenManager = new TokenManager(this.secret);
327+
this.tokenManager = new TokenManager();
351328
this.insightMetrics = new InsightMetrics();
352329

353330
this.defaultWSTimeoutWithFallback = 6 * 1000;
@@ -376,7 +353,6 @@ export class StreamChat extends ChatApi {
376353
* StreamChat.getInstance('api_key', 'secret', { httpsAgent: customAgent })
377354
*
378355
* @param key - The API key.
379-
* @param secret - The API secret (optional).
380356
* @param options - Additional options; here you can pass custom options to the axios instance (optional).
381357
* @param options.browser - Enforce the client to be in browser mode (optional).
382358
* @param options.warmUp - If `true`, the client will open a connection as soon as possible to speed up following requests (optional, defaults to `false`).
@@ -386,23 +362,9 @@ export class StreamChat extends ChatApi {
386362
* @param options.httpsAgent - Custom `httpsAgent` (optional, in Node defaults to `https.agent()`).
387363
* @returns The shared client instance.
388364
*/
389-
public static getInstance(key: string, options?: StreamChatOptions): StreamChat;
390-
public static getInstance(
391-
key: string,
392-
secret?: string,
393-
options?: StreamChatOptions,
394-
): StreamChat;
395-
public static getInstance(
396-
key: string,
397-
secretOrOptions?: StreamChatOptions | string,
398-
options?: StreamChatOptions,
399-
): StreamChat {
365+
public static getInstance(key: string, options?: StreamChatOptions): StreamChat {
400366
if (!StreamChat._instance) {
401-
if (typeof secretOrOptions === 'string') {
402-
StreamChat._instance = new StreamChat(key, secretOrOptions, options);
403-
} else {
404-
StreamChat._instance = new StreamChat(key, secretOrOptions);
405-
}
367+
StreamChat._instance = new StreamChat(key, options);
406368
}
407369

408370
return StreamChat._instance as StreamChat;
@@ -470,10 +432,7 @@ export class StreamChat extends ChatApi {
470432
);
471433
}
472434

473-
if (
474-
(this._isUsingServerAuth() || this.node) &&
475-
!this.options.allowServerSideConnect
476-
) {
435+
if (this.node && !this.options.allowServerSideConnect) {
477436
logger
478437
.withExtraTags('connectUser')
479438
.warn(
@@ -719,10 +678,7 @@ export class StreamChat extends ChatApi {
719678
* @returns A promise that resolves when the connection is set up.
720679
*/
721680
connectAnonymousUser = () => {
722-
if (
723-
(this._isUsingServerAuth() || this.node) &&
724-
!this.options.allowServerSideConnect
725-
) {
681+
if (this.node && !this.options.allowServerSideConnect) {
726682
logger
727683
.withExtraTags('connectAnonymousUser')
728684
.warn(
@@ -1561,7 +1517,7 @@ export class StreamChat extends ChatApi {
15611517
channelIdOrCustom?: string | ChannelData | null,
15621518
custom: ChannelData = {},
15631519
) {
1564-
if (!this.userId && !this._isUsingServerAuth()) {
1520+
if (!this.userId) {
15651521
throw Error('Call connectUser or connectAnonymousUser before creating a channel');
15661522
}
15671523

@@ -2238,14 +2194,7 @@ export class StreamChat extends ChatApi {
22382194
this.userAgent = userAgent;
22392195
}
22402196

2241-
/**
2242-
* Returns true if we're using server-side auth.
2243-
*
2244-
* @returns `true` if a secret was provided.
2245-
*/
2246-
_isUsingServerAuth = () => !!this.secret;
2247-
2248-
_cacheEnabled = () => !this._isUsingServerAuth() || !this.options.disableCache;
2197+
_cacheEnabled = () => !this.options.disableCache;
22492198

22502199
_startCleaning() {
22512200
// eslint-disable-next-line @typescript-eslint/no-this-alias

0 commit comments

Comments
 (0)