MCPcopy Create free account
hub / github.com/GetStream/stream-js / constructor

Method constructor

src/client.ts:233–333  ·  view source on GitHub ↗

* Initialize a client * @link https://getstream.io/activity-feeds/docs/node/#setup * @method initialize * @memberof StreamClient.prototype * @param {string} apiKey - the api key * @param {string} [apiSecret] - the api secret * @param {string} [appId] - id of the app * @param {Cl

(apiKey: string, apiSecretOrToken: string | null, appId?: string, options: ClientOptions = {})

Source from the content-addressed store, hash-verified

231 * stream.connect(apiKey, null, appId);
232 */
233 constructor(apiKey: string, apiSecretOrToken: string | null, appId?: string, options: ClientOptions = {}) {
234 this.baseUrl = 'https://api.stream-io-api.com/api/';
235 this.baseAnalyticsUrl = 'https://analytics.stream-io-api.com/analytics/';
236 this.apiKey = apiKey;
237 this.usingApiSecret = apiSecretOrToken != null && !apiSecretOrToken.includes(`.`);
238 this.apiSecret = this.usingApiSecret ? apiSecretOrToken : null;
239 this.userToken = this.usingApiSecret ? null : apiSecretOrToken;
240 this.enrichByDefault = !this.usingApiSecret;
241
242 if (this.userToken != null) {
243 const jwtBody: { user_id?: string } = jwtDecode(this.userToken);
244 if (!jwtBody.user_id) {
245 throw new TypeError('user_id is missing in user token');
246 }
247 this.userId = jwtBody.user_id;
248 this.currentUser = this.user(this.userId);
249 }
250
251 this.appId = appId;
252 this.options = options;
253 this.version = this.options.version || 'v1.0';
254 this.fayeUrl = this.options.fayeUrl || 'https://faye-us-east.stream-io-api.com/faye';
255 this.fayeClient = null;
256 // track a source name for the api calls, ie get started or databrowser
257 this.group = this.options.group || 'unspecified';
258 // track subscriptions made on feeds created by this client
259 this.subscriptions = {};
260 this.expireTokens = this.options.expireTokens ? this.options.expireTokens : false;
261 // which data center to use
262 this.location = this.options.location as string;
263 this.baseUrl = this.getBaseUrl();
264
265 if (typeof process !== 'undefined') {
266 if (process.env?.LOCAL_FAYE) {
267 this.fayeUrl = 'http://localhost:9999/faye/';
268 }
269 if (process.env?.STREAM_ANALYTICS_BASE_URL) {
270 this.baseAnalyticsUrl = process.env.STREAM_ANALYTICS_BASE_URL;
271 }
272 }
273
274 this.handlers = {};
275 this.node = typeof window === 'undefined'; // use for real browser vs node behavior
276 // use for browser warnings
277 this.browser = typeof this.options.browser !== 'undefined' ? this.options.browser : !this.node;
278
279 if (this.node) {
280 const keepAlive = this.options.keepAlive === undefined ? true : this.options.keepAlive;
281 const keepAliveMsecs = this.options.keepAliveMsecs ?? 3000;
282 const agentOptions: http.AgentOptions = {
283 keepAlive,
284 keepAliveMsecs,
285 };
286 if (this.options.maxSockets !== undefined) {
287 agentOptions.maxSockets = this.options.maxSockets;
288 }
289 if (this.options.maxFreeSockets !== undefined) {
290 agentOptions.maxFreeSockets = this.options.maxFreeSockets;

Callers

nothing calls this directly

Calls 4

userMethod · 0.95
getBaseUrlMethod · 0.95
getOrCreateTokenMethod · 0.95
createMethod · 0.80

Tested by

no test coverage detected