@stream-io/node-sdk 0.2.1 → 0.2.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.
@@ -49,7 +49,10 @@ export declare class StreamClient {
49
49
  * @param iat this is deprecated, the current date will be set internally
50
50
  * @returns
51
51
  */
52
- createCallToken(userID: string, call_cids: string[], exp?: number, iat?: number): string;
52
+ createCallToken(userIdOrObject: string | {
53
+ user_id: string;
54
+ role?: string;
55
+ }, call_cids: string[], exp?: number, iat?: number): string;
53
56
  createDevice: (createDeviceRequest: CreateDeviceRequest) => Promise<void>;
54
57
  deleteDevice: (requestParameters: DeleteDeviceRequest) => Promise<import("./gen/chat").Response>;
55
58
  listDevices: (requestParameters: ListDevicesRequest) => Promise<import("./gen/chat").ListDevicesResponse>;
@@ -1 +1,13 @@
1
1
  export type OmitTypeId<T> = Omit<T, 'type' | 'id' | 'connection_id'>;
2
+ interface BaseTokenPayload {
3
+ user_id: string;
4
+ exp: number;
5
+ iat: number;
6
+ call_cids?: string[];
7
+ }
8
+ export type UserTokenPayload = BaseTokenPayload;
9
+ export type CallTokenPayload = BaseTokenPayload & {
10
+ call_cids: string[];
11
+ role?: string;
12
+ };
13
+ export {};
@@ -1,3 +1,8 @@
1
1
  import { Secret, SignOptions } from 'jsonwebtoken';
2
- export declare function JWTUserToken(apiSecret: Secret, userId: string, extraData?: {}, jwtOptions?: SignOptions): string;
2
+ export declare function JWTUserToken(apiSecret: Secret, payload: {
3
+ user_id: string;
4
+ exp: number;
5
+ iat: number;
6
+ call_cids?: string[];
7
+ }): string;
3
8
  export declare function JWTServerToken(apiSecret: Secret, jwtOptions?: SignOptions): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stream-io/node-sdk",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.es.js",
@@ -58,6 +58,7 @@ import {
58
58
  import { v4 as uuidv4 } from 'uuid';
59
59
  import { JWTServerToken, JWTUserToken } from './utils/create-token';
60
60
  import crypto from 'crypto';
61
+ import { CallTokenPayload, UserTokenPayload } from './types';
61
62
 
62
63
  export interface StreamClientOptions {
63
64
  timeout?: number;
@@ -141,24 +142,20 @@ export class StreamClient {
141
142
  iat = Math.round(Date.now() / 1000),
142
143
  call_cids?: string[],
143
144
  ) {
144
- const extra: { exp?: number; iat?: number; call_cids?: string[] } = {};
145
-
146
- if (exp) {
147
- extra.exp = exp;
148
- }
149
-
150
- if (iat) {
151
- extra.iat = iat;
152
- }
145
+ const payload: UserTokenPayload = {
146
+ user_id: userID,
147
+ exp,
148
+ iat,
149
+ };
153
150
 
154
151
  if (call_cids) {
155
152
  console.warn(
156
153
  `Use createCallToken method for creating call tokens, the "call_cids" param will be removed from the createToken method with version 0.2.0`,
157
154
  );
158
- extra.call_cids = call_cids;
155
+ payload.call_cids = call_cids;
159
156
  }
160
157
 
161
- return JWTUserToken(this.secret, userID, extra);
158
+ return JWTUserToken(this.secret, payload);
162
159
  }
163
160
 
164
161
  /**
@@ -170,24 +167,26 @@ export class StreamClient {
170
167
  * @returns
171
168
  */
172
169
  createCallToken(
173
- userID: string,
170
+ userIdOrObject: string | { user_id: string; role?: string },
174
171
  call_cids: string[],
175
172
  exp = Math.round(new Date().getTime() / 1000) + 60 * 60,
176
173
  iat = Math.round(Date.now() / 1000),
177
174
  ) {
178
- const extra: { exp?: number; iat?: number; call_cids?: string[] } = {};
179
-
180
- if (exp) {
181
- extra.exp = exp;
182
- }
183
-
184
- if (iat) {
185
- extra.iat = iat;
175
+ const payload: CallTokenPayload = {
176
+ exp,
177
+ iat,
178
+ call_cids,
179
+ user_id:
180
+ typeof userIdOrObject === 'string'
181
+ ? userIdOrObject
182
+ : userIdOrObject.user_id,
183
+ };
184
+
185
+ if (typeof userIdOrObject === 'object' && userIdOrObject.role) {
186
+ payload.role = userIdOrObject.role;
186
187
  }
187
188
 
188
- extra.call_cids = call_cids;
189
-
190
- return JWTUserToken(this.secret, userID, extra);
189
+ return JWTUserToken(this.secret, payload);
191
190
  }
192
191
 
193
192
  createDevice = (createDeviceRequest: CreateDeviceRequest) => {
package/src/types.ts CHANGED
@@ -1 +1,15 @@
1
1
  export type OmitTypeId<T> = Omit<T, 'type' | 'id' | 'connection_id'>;
2
+
3
+ interface BaseTokenPayload {
4
+ user_id: string;
5
+ exp: number;
6
+ iat: number;
7
+ call_cids?: string[];
8
+ }
9
+
10
+ export type UserTokenPayload = BaseTokenPayload;
11
+
12
+ export type CallTokenPayload = BaseTokenPayload & {
13
+ call_cids: string[];
14
+ role?: string;
15
+ };
@@ -2,19 +2,8 @@ import jwt, { Secret, SignOptions } from 'jsonwebtoken';
2
2
 
3
3
  export function JWTUserToken(
4
4
  apiSecret: Secret,
5
- userId: string,
6
- extraData = {},
7
- jwtOptions: SignOptions = {},
5
+ payload: { user_id: string; exp: number; iat: number; call_cids?: string[] },
8
6
  ) {
9
- if (typeof userId !== 'string') {
10
- throw new TypeError('userId should be a string');
11
- }
12
-
13
- const payload: { user_id: string } & any = {
14
- user_id: userId,
15
- ...extraData,
16
- };
17
-
18
7
  // make sure we return a clear error when jwt is shimmed (ie. browser build)
19
8
  if (jwt == null || jwt.sign == null) {
20
9
  throw Error(
@@ -22,10 +11,10 @@ export function JWTUserToken(
22
11
  );
23
12
  }
24
13
 
25
- const opts: SignOptions = Object.assign(
26
- { algorithm: 'HS256', noTimestamp: true },
27
- jwtOptions,
28
- );
14
+ const opts: SignOptions = Object.assign({
15
+ algorithm: 'HS256',
16
+ noTimestamp: true,
17
+ });
29
18
 
30
19
  if (payload.iat) {
31
20
  opts.noTimestamp = false;