@stream-io/node-sdk 0.7.1 → 0.7.2

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.
@@ -1,8 +1,20 @@
1
- import { GetOrCreateCallRequest, QueryCallMembersRequest } from './gen/models';
1
+ import { VideoApi } from './gen-imports';
2
+ import { CallResponse, GetOrCreateCallRequest, QueryCallMembersRequest } from './gen/models';
2
3
  import { CallApi } from './gen/video/CallApi';
4
+ import { StreamClient } from './StreamClient';
3
5
  import { OmitTypeId } from './types';
4
6
  export declare class StreamCall extends CallApi {
7
+ readonly type: string;
8
+ readonly id: string;
9
+ private readonly streamClient;
10
+ data?: CallResponse;
11
+ constructor(videoApi: VideoApi, type: string, id: string, streamClient: StreamClient);
5
12
  get cid(): string;
6
13
  create: (request?: GetOrCreateCallRequest) => Promise<import("./types").StreamResponse<import("./gen/models").GetOrCreateCallResponse>>;
7
14
  queryMembers: (request?: OmitTypeId<QueryCallMembersRequest>) => Promise<import("./types").StreamResponse<import("./gen/models").QueryCallMembersResponse>>;
15
+ getOrCreate: (request?: GetOrCreateCallRequest) => Promise<import("./types").StreamResponse<import("./gen/models").GetOrCreateCallResponse>>;
16
+ get: () => Promise<import("./types").StreamResponse<import("./gen/models").GetCallResponse>>;
17
+ createSRTCredetials: (userID: string) => {
18
+ address: string;
19
+ };
8
20
  }
@@ -54,6 +54,16 @@ export declare class StreamClient extends CommonApi {
54
54
  exp?: number;
55
55
  iat?: number;
56
56
  } & Record<string, unknown>) => string;
57
+ /**
58
+ *
59
+ * @param payload
60
+ * - user_id - the id of the user the token is for
61
+ * - iat - issued at date of the token, unix timestamp in seconds, by default it's now
62
+ */
63
+ generatePermanentUserToken: (payload: {
64
+ user_id: string;
65
+ iat?: number;
66
+ } & Record<string, unknown>) => string;
57
67
  /**
58
68
  *
59
69
  * @param payload
@@ -7,6 +7,7 @@ export interface ApiConfig {
7
7
  /** The timeout for requests in milliseconds. The default is 3000. */
8
8
  timeout: number;
9
9
  agent?: RequestInit['dispatcher'];
10
+ secret?: string;
10
11
  }
11
12
  export interface RequestMetadata {
12
13
  responseHeaders: Headers;
@@ -29,7 +30,7 @@ export interface RateLimit {
29
30
  }
30
31
  interface BaseTokenPayload {
31
32
  user_id: string;
32
- exp: number;
33
+ exp?: number;
33
34
  iat: number;
34
35
  call_cids?: string[];
35
36
  }
@@ -1,7 +1,7 @@
1
1
  import { Secret, SignOptions } from 'jsonwebtoken';
2
2
  export declare function JWTUserToken(apiSecret: Secret, payload: {
3
3
  user_id: string;
4
- exp: number;
4
+ exp?: number;
5
5
  iat: number;
6
6
  call_cids?: string[];
7
7
  } & {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stream-io/node-sdk",
3
- "version": "0.7.1",
3
+ "version": "0.7.2",
4
4
  "description": "",
5
5
  "exports": {
6
6
  ".": {
package/src/StreamCall.ts CHANGED
@@ -1,8 +1,25 @@
1
- import { GetOrCreateCallRequest, QueryCallMembersRequest } from './gen/models';
1
+ import { VideoApi } from './gen-imports';
2
+ import {
3
+ CallResponse,
4
+ GetOrCreateCallRequest,
5
+ QueryCallMembersRequest,
6
+ } from './gen/models';
2
7
  import { CallApi } from './gen/video/CallApi';
8
+ import { StreamClient } from './StreamClient';
3
9
  import { OmitTypeId } from './types';
4
10
 
5
11
  export class StreamCall extends CallApi {
12
+ data?: CallResponse;
13
+
14
+ constructor(
15
+ videoApi: VideoApi,
16
+ readonly type: string,
17
+ readonly id: string,
18
+ private readonly streamClient: StreamClient,
19
+ ) {
20
+ super(videoApi, type, id);
21
+ }
22
+
6
23
  get cid() {
7
24
  return `${this.type}:${this.id}`;
8
25
  }
@@ -16,4 +33,42 @@ export class StreamCall extends CallApi {
16
33
  ...(request ?? {}),
17
34
  });
18
35
  };
36
+
37
+ getOrCreate = async (request?: GetOrCreateCallRequest) => {
38
+ const response = await super.getOrCreate(request);
39
+ this.data = response.call;
40
+ return response;
41
+ };
42
+
43
+ get = async () => {
44
+ const response = await super.get();
45
+ this.data = response.call;
46
+ return response;
47
+ };
48
+
49
+ createSRTCredetials = (
50
+ userID: string,
51
+ ): {
52
+ address: string;
53
+ } => {
54
+ if (!this.data) {
55
+ throw new Error(
56
+ 'Object is not initialized, call get() or getOrCreate() first',
57
+ );
58
+ }
59
+
60
+ const token = this.streamClient.generatePermanentUserToken({
61
+ user_id: userID,
62
+ });
63
+ const segments = token.split('.');
64
+ if (segments.length !== 3) {
65
+ throw new Error('Invalid token format');
66
+ }
67
+
68
+ return {
69
+ address: this.data.ingress.srt.address
70
+ .replace('{passphrase}', segments[2])
71
+ .replace('{token}', token),
72
+ };
73
+ };
19
74
  }
@@ -145,6 +145,24 @@ export class StreamClient extends CommonApi {
145
145
  return JWTUserToken(this.secret, payload as UserTokenPayload);
146
146
  };
147
147
 
148
+ /**
149
+ *
150
+ * @param payload
151
+ * - user_id - the id of the user the token is for
152
+ * - iat - issued at date of the token, unix timestamp in seconds, by default it's now
153
+ */
154
+ generatePermanentUserToken = (
155
+ payload: {
156
+ user_id: string;
157
+ iat?: number;
158
+ } & Record<string, unknown>,
159
+ ) => {
160
+ const defaultIat = Math.floor((Date.now() - 1000) / 1000);
161
+ payload.iat = payload.iat ?? defaultIat;
162
+
163
+ return JWTUserToken(this.secret, payload as UserTokenPayload);
164
+ };
165
+
148
166
  /**
149
167
  *
150
168
  * @param payload
@@ -25,7 +25,7 @@ export class StreamVideoClient extends VideoApi {
25
25
  }
26
26
 
27
27
  call = (type: string, id: string) => {
28
- return new StreamCall(this, type, id);
28
+ return new StreamCall(this, type, id, this.streamClient);
29
29
  };
30
30
 
31
31
  connectOpenAi = async (options: {
package/src/types.ts CHANGED
@@ -7,6 +7,7 @@ export interface ApiConfig {
7
7
  /** The timeout for requests in milliseconds. The default is 3000. */
8
8
  timeout: number;
9
9
  agent?: RequestInit['dispatcher'];
10
+ secret?: string;
10
11
  }
11
12
 
12
13
  export interface RequestMetadata {
@@ -39,7 +40,7 @@ export interface RateLimit {
39
40
 
40
41
  interface BaseTokenPayload {
41
42
  user_id: string;
42
- exp: number;
43
+ exp?: number;
43
44
  iat: number;
44
45
  call_cids?: string[];
45
46
  }
@@ -4,7 +4,7 @@ export function JWTUserToken(
4
4
  apiSecret: Secret,
5
5
  payload: {
6
6
  user_id: string;
7
- exp: number;
7
+ exp?: number;
8
8
  iat: number;
9
9
  call_cids?: string[];
10
10
  } & { [key: string]: any },