@azure/web-pubsub 1.0.0-beta.2 → 1.0.1-alpha.20211215.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. package/CHANGELOG.md +40 -0
  2. package/README.md +118 -31
  3. package/dist/index.js +1315 -567
  4. package/dist/index.js.map +1 -1
  5. package/dist-esm/samples-dev/broadcasting.js +16 -16
  6. package/dist-esm/samples-dev/broadcasting.js.map +1 -1
  7. package/dist-esm/samples-dev/directMessage.js +9 -9
  8. package/dist-esm/samples-dev/directMessage.js.map +1 -1
  9. package/dist-esm/samples-dev/managingGroups.js +11 -12
  10. package/dist-esm/samples-dev/managingGroups.js.map +1 -1
  11. package/dist-esm/src/generated/generatedClient.js +23 -0
  12. package/dist-esm/src/generated/generatedClient.js.map +1 -0
  13. package/dist-esm/src/generated/generatedClientContext.js +41 -0
  14. package/dist-esm/src/generated/generatedClientContext.js.map +1 -0
  15. package/dist-esm/src/generated/index.js +10 -2
  16. package/dist-esm/src/generated/index.js.map +1 -1
  17. package/dist-esm/src/generated/models/index.js +13 -1
  18. package/dist-esm/src/generated/models/index.js.map +1 -1
  19. package/dist-esm/src/generated/models/mappers.js +368 -0
  20. package/dist-esm/src/generated/models/mappers.js.map +1 -0
  21. package/dist-esm/src/generated/models/parameters.js +124 -43
  22. package/dist-esm/src/generated/models/parameters.js.map +1 -1
  23. package/dist-esm/src/generated/operations/healthApi.js +16 -11
  24. package/dist-esm/src/generated/operations/healthApi.js.map +1 -1
  25. package/dist-esm/src/generated/operations/index.js +7 -0
  26. package/dist-esm/src/generated/operations/index.js.map +1 -1
  27. package/dist-esm/src/generated/operations/webPubSub.js +341 -175
  28. package/dist-esm/src/generated/operations/webPubSub.js.map +1 -1
  29. package/dist-esm/src/generated/operationsInterfaces/healthApi.js +9 -0
  30. package/dist-esm/src/generated/operationsInterfaces/healthApi.js.map +1 -0
  31. package/dist-esm/src/generated/operationsInterfaces/index.js +10 -0
  32. package/dist-esm/src/generated/operationsInterfaces/index.js.map +1 -0
  33. package/dist-esm/src/generated/operationsInterfaces/webPubSub.js +9 -0
  34. package/dist-esm/src/generated/operationsInterfaces/webPubSub.js.map +1 -0
  35. package/dist-esm/src/groupClient.js +74 -92
  36. package/dist-esm/src/groupClient.js.map +1 -1
  37. package/dist-esm/src/hubClient.js +280 -196
  38. package/dist-esm/src/hubClient.js.map +1 -1
  39. package/dist-esm/src/index.js.map +1 -1
  40. package/dist-esm/src/logger.js +1 -1
  41. package/dist-esm/src/logger.js.map +1 -1
  42. package/dist-esm/src/parseConnectionString.js +3 -1
  43. package/dist-esm/src/parseConnectionString.js.map +1 -1
  44. package/dist-esm/src/reverseProxyPolicy.js +21 -0
  45. package/dist-esm/src/reverseProxyPolicy.js.map +1 -0
  46. package/dist-esm/src/utils.js +8 -6
  47. package/dist-esm/src/utils.js.map +1 -1
  48. package/dist-esm/src/webPubSubCredentialPolicy.js +18 -18
  49. package/dist-esm/src/webPubSubCredentialPolicy.js.map +1 -1
  50. package/dist-esm/test/conn.spec.js +7 -8
  51. package/dist-esm/test/conn.spec.js.map +1 -1
  52. package/dist-esm/test/groups.spec.js +39 -38
  53. package/dist-esm/test/groups.spec.js.map +1 -1
  54. package/dist-esm/test/hubs.spec.js +95 -49
  55. package/dist-esm/test/hubs.spec.js.map +1 -1
  56. package/dist-esm/test/integration.spec.js +128 -0
  57. package/dist-esm/test/integration.spec.js.map +1 -0
  58. package/dist-esm/test/testEnv.js +5 -1
  59. package/dist-esm/test/testEnv.js.map +1 -1
  60. package/package.json +37 -36
  61. package/types/web-pubsub.d.ts +186 -138
  62. package/dist-esm/src/generated/azureWebPubSubServiceRestAPI.js +0 -15
  63. package/dist-esm/src/generated/azureWebPubSubServiceRestAPI.js.map +0 -1
  64. package/dist-esm/src/generated/azureWebPubSubServiceRestAPIContext.js +0 -34
  65. package/dist-esm/src/generated/azureWebPubSubServiceRestAPIContext.js.map +0 -1
  66. package/dist-esm/src/normalizeOptions.js +0 -8
  67. package/dist-esm/src/normalizeOptions.js.map +0 -1
@@ -1,31 +1,32 @@
1
1
  // Copyright (c) Microsoft Corporation.
2
2
  // Licensed under the MIT license.
3
- import { __awaiter } from "tslib";
4
- import { RestError, createPipelineFromOptions } from "@azure/core-http";
5
- import { AzureWebPubSubServiceRestAPI as GeneratedClient } from "./generated/azureWebPubSubServiceRestAPI";
3
+ import { RestError } from "@azure/core-rest-pipeline";
4
+ import { GeneratedClient } from "./generated/generatedClient";
6
5
  import { WebPubSubGroupImpl } from "./groupClient";
7
- import normalizeSendToAllOptions from "./normalizeOptions";
8
- import { webPubSubAzureKeyCredentialPolicyFactory } from "./webPubSubCredentialPolicy";
6
+ import { isTokenCredential } from "@azure/core-auth";
7
+ import { webPubSubKeyCredentialPolicy } from "./webPubSubCredentialPolicy";
9
8
  import { createSpan } from "./tracing";
10
9
  import { logger } from "./logger";
11
10
  import { parseConnectionString } from "./parseConnectionString";
12
11
  import jwt from "jsonwebtoken";
13
- import { getContentTypeForMessage } from "./utils";
12
+ import { getPayloadForMessage } from "./utils";
13
+ import { webPubSubReverseProxyPolicy } from "./reverseProxyPolicy";
14
14
  /**
15
15
  * Client for connecting to a Web PubSub hub
16
16
  */
17
17
  export class WebPubSubServiceClient {
18
18
  constructor(endpointOrConnectionString, credsOrHubName, hubNameOrOpts, opts) {
19
+ var _a, _b;
19
20
  /**
20
21
  * The Web PubSub API version being used by this client
21
22
  */
22
- this.apiVersion = "2020-10-01";
23
+ this.apiVersion = "2021-10-01";
23
24
  // unpack constructor arguments
24
- if (typeof credsOrHubName === "object" && "key" in credsOrHubName) {
25
+ if (typeof credsOrHubName === "object") {
25
26
  this.endpoint = endpointOrConnectionString;
26
- this.credential = credsOrHubName;
27
27
  this.hubName = hubNameOrOpts;
28
28
  this.clientOptions = opts;
29
+ this.credential = credsOrHubName;
29
30
  }
30
31
  else {
31
32
  const parsedCs = parseConnectionString(endpointOrConnectionString);
@@ -34,254 +35,337 @@ export class WebPubSubServiceClient {
34
35
  this.hubName = credsOrHubName;
35
36
  this.clientOptions = hubNameOrOpts;
36
37
  }
37
- const internalPipelineOptions = Object.assign(Object.assign({}, this.clientOptions), {
38
+ const internalPipelineOptions = Object.assign(Object.assign(Object.assign({}, this.clientOptions), {
39
+ apiVersion: this.apiVersion,
38
40
  loggingOptions: {
39
41
  logger: logger.info
40
42
  }
41
- });
42
- const pipeline = createPipelineFromOptions(internalPipelineOptions, webPubSubAzureKeyCredentialPolicyFactory(this.credential));
43
- this.client = new GeneratedClient(this.endpoint, pipeline);
44
- }
45
- /**
46
- * Auth the client connection with userId and custom claims if any
47
- * @param options The options that the client has
48
- */
49
- getAuthenticationToken(options) {
50
- return __awaiter(this, void 0, void 0, function* () {
51
- const endpoint = this.endpoint.endsWith("/") ? this.endpoint : this.endpoint + "/";
52
- const key = this.credential.key;
53
- const hub = this.hubName;
54
- const clientEndpoint = endpoint.replace(/(http)(s?:\/\/)/gi, "ws$2");
55
- const clientUrl = `${clientEndpoint}client/hubs/${hub}`;
56
- const audience = `${endpoint}client/hubs/${hub}`;
57
- const payload = { role: options === null || options === void 0 ? void 0 : options.roles };
58
- const signOptions = {
59
- audience: audience,
60
- expiresIn: (options === null || options === void 0 ? void 0 : options.ttl) === undefined ? "1h" : `${options.ttl}m`,
61
- algorithm: "HS256"
62
- };
63
- if (options === null || options === void 0 ? void 0 : options.userId) {
64
- signOptions.subject = options === null || options === void 0 ? void 0 : options.userId;
43
+ }), (isTokenCredential(this.credential)
44
+ ? {
45
+ credential: this.credential,
46
+ credentialScopes: ["https://webpubsub.azure.com/.default"]
65
47
  }
66
- const token = jwt.sign(payload, key, signOptions);
67
- const url = `${clientUrl}?access_token=${token}`;
68
- return {
69
- baseUrl: clientUrl,
70
- token: jwt.sign(payload, key, signOptions),
71
- url: url
72
- };
73
- });
48
+ : {}));
49
+ this.client = new GeneratedClient(this.endpoint, internalPipelineOptions);
50
+ if (!isTokenCredential(this.credential)) {
51
+ this.client.pipeline.addPolicy(webPubSubKeyCredentialPolicy(this.credential));
52
+ }
53
+ if ((_a = this.clientOptions) === null || _a === void 0 ? void 0 : _a.reverseProxyEndpoint) {
54
+ this.client.pipeline.addPolicy(webPubSubReverseProxyPolicy((_b = this.clientOptions) === null || _b === void 0 ? void 0 : _b.reverseProxyEndpoint));
55
+ }
74
56
  }
75
57
  /**
76
58
  * Get a client for a group
77
- * @param groupName The name of the group to connect to.
59
+ * @param groupName - The name of the group to connect to.
78
60
  */
79
61
  group(groupName) {
80
62
  return new WebPubSubGroupImpl(this.client, this.hubName, groupName);
81
63
  }
82
- sendToAll(message, options = {}) {
83
- return __awaiter(this, void 0, void 0, function* () {
84
- const normalizedOptions = normalizeSendToAllOptions(options);
85
- const { span, updatedOptions } = createSpan("WebPubSubServiceClient-hub-sendToAll", normalizedOptions);
86
- const contentType = getContentTypeForMessage(message, updatedOptions);
87
- try {
88
- return yield this.client.webPubSub.sendToAll(this.hubName, contentType, contentType === "application/json" ? JSON.stringify(message) : message, updatedOptions);
89
- }
90
- finally {
91
- span.end();
92
- }
93
- });
64
+ async sendToAll(message, options = {}) {
65
+ const { span, updatedOptions } = createSpan("WebPubSubServiceClient-hub-sendToAll", options);
66
+ const { contentType, payload } = getPayloadForMessage(message, updatedOptions);
67
+ try {
68
+ return await this.client.webPubSub.sendToAll(this.hubName, contentType, payload, updatedOptions);
69
+ }
70
+ finally {
71
+ span.end();
72
+ }
94
73
  }
95
- sendToUser(username, message, options = {}) {
96
- return __awaiter(this, void 0, void 0, function* () {
97
- const { span, updatedOptions } = createSpan("WebPubSubServiceClient-hub-sendToUser", options);
98
- const contentType = getContentTypeForMessage(message, updatedOptions);
99
- try {
100
- return yield this.client.webPubSub.sendToUser(this.hubName, username, contentType, contentType === "application/json" ? JSON.stringify(message) : message, updatedOptions);
101
- }
102
- finally {
103
- span.end();
104
- }
105
- });
74
+ async sendToUser(username, message, options = {}) {
75
+ const { span, updatedOptions } = createSpan("WebPubSubServiceClient-hub-sendToUser", options);
76
+ const { contentType, payload } = getPayloadForMessage(message, updatedOptions);
77
+ try {
78
+ return await this.client.webPubSub.sendToUser(this.hubName, username, contentType, payload, updatedOptions);
79
+ }
80
+ finally {
81
+ span.end();
82
+ }
106
83
  }
107
- sendToConnection(connectionId, message, options = {}) {
108
- return __awaiter(this, void 0, void 0, function* () {
109
- const { span, updatedOptions } = createSpan("WebPubSubServiceClient-hub-sendToConnection", options);
110
- const contentType = getContentTypeForMessage(message, updatedOptions);
111
- try {
112
- return yield this.client.webPubSub.sendToConnection(this.hubName, connectionId, contentType, contentType === "application/json" ? JSON.stringify(message) : message, updatedOptions);
113
- }
114
- finally {
115
- span.end();
116
- }
117
- });
84
+ async sendToConnection(connectionId, message, options = {}) {
85
+ const { span, updatedOptions } = createSpan("WebPubSubServiceClient-hub-sendToConnection", options);
86
+ const { contentType, payload } = getPayloadForMessage(message, updatedOptions);
87
+ try {
88
+ return await this.client.webPubSub.sendToConnection(this.hubName, connectionId, contentType, payload, updatedOptions);
89
+ }
90
+ finally {
91
+ span.end();
92
+ }
118
93
  }
119
94
  /**
120
95
  * Check if a specific connection is connected to this hub
121
96
  *
122
- * @param connectionId Connection id to check
123
- * @param options Additional options
97
+ * @param connectionId - Connection id to check
98
+ * @param options - Additional options
124
99
  */
125
- hasConnection(connectionId, options = {}) {
126
- return __awaiter(this, void 0, void 0, function* () {
127
- const { span, updatedOptions } = createSpan("WebPubSubServiceClient-hub-hasConnection", options);
128
- try {
129
- const res = yield this.client.webPubSub.connectionExists(this.hubName, connectionId, updatedOptions);
130
- if (res._response.status === 200) {
131
- return true;
132
- }
133
- else if (res._response.status === 404) {
134
- return false;
135
- }
136
- else {
137
- // this is sad - wish this was handled by autorest.
138
- throw new RestError(res._response.bodyAsText, undefined, res._response.status, res._response.request, res._response);
139
- }
100
+ async connectionExists(connectionId, options = {}) {
101
+ const { span, updatedOptions } = createSpan("WebPubSubServiceClient-hub-hasConnection", options);
102
+ let response;
103
+ function onResponse(rawResponse, flatResponse) {
104
+ response = rawResponse;
105
+ if (updatedOptions.onResponse) {
106
+ updatedOptions.onResponse(rawResponse, flatResponse);
107
+ }
108
+ }
109
+ try {
110
+ await this.client.webPubSub.connectionExists(this.hubName, connectionId, Object.assign(Object.assign({}, updatedOptions), { onResponse }));
111
+ if (response.status === 200) {
112
+ return true;
140
113
  }
141
- finally {
142
- span.end();
114
+ else if (response.status === 404) {
115
+ return false;
143
116
  }
144
- });
117
+ else {
118
+ // this is sad - wish this was handled by autorest.
119
+ throw new RestError(response.bodyAsText, {
120
+ statusCode: response === null || response === void 0 ? void 0 : response.status,
121
+ request: response === null || response === void 0 ? void 0 : response.request,
122
+ response: response
123
+ });
124
+ }
125
+ }
126
+ finally {
127
+ span.end();
128
+ }
145
129
  }
146
130
  /**
147
131
  * Close a specific connection to this hub
148
132
  *
149
- * @param connectionId Connection id to close
150
- * @param options Additional options
133
+ * @param connectionId - Connection id to close
134
+ * @param options - Additional options
151
135
  */
152
- closeConnection(connectionId, options = {}) {
153
- return __awaiter(this, void 0, void 0, function* () {
154
- const { span, updatedOptions } = createSpan("WebPubSubServiceClient-hub-removeConnection", options);
155
- try {
156
- return yield this.client.webPubSub.closeClientConnection(this.hubName, connectionId, updatedOptions);
157
- }
158
- finally {
159
- span.end();
160
- }
161
- });
136
+ async closeConnection(connectionId, options = {}) {
137
+ const { span, updatedOptions } = createSpan("WebPubSubServiceClient-hub-closeConnection", options);
138
+ try {
139
+ return await this.client.webPubSub.closeConnection(this.hubName, connectionId, updatedOptions);
140
+ }
141
+ finally {
142
+ span.end();
143
+ }
144
+ }
145
+ /**
146
+ * Close all connections to this hub
147
+ *
148
+ * @param options - Additional options
149
+ */
150
+ async closeAllConnections(options = {}) {
151
+ const { span, updatedOptions } = createSpan("WebPubSubServiceClient-hub-closeAllConnections", options);
152
+ try {
153
+ return await this.client.webPubSub.closeAllConnections(this.hubName, updatedOptions);
154
+ }
155
+ finally {
156
+ span.end();
157
+ }
158
+ }
159
+ /**
160
+ * Close all connections with the given user id
161
+ *
162
+ * @param user - User id to close
163
+ * @param options - Additional options
164
+ */
165
+ async closeUserConnections(userId, options = {}) {
166
+ const { span, updatedOptions } = createSpan("WebPubSubServiceClient-hub-closeUserConnections", options);
167
+ try {
168
+ return await this.client.webPubSub.closeUserConnections(this.hubName, userId, updatedOptions);
169
+ }
170
+ finally {
171
+ span.end();
172
+ }
162
173
  }
163
174
  /**
164
175
  * Remove a specific user from all groups they are joined to
165
- * @param userId The user id to remove from all groups
166
- * @param options Additional options
176
+ * @param userId - The user id to remove from all groups
177
+ * @param options - Additional options
167
178
  */
168
- removeUserFromAllGroups(userId, options = {}) {
169
- return __awaiter(this, void 0, void 0, function* () {
170
- const { span, updatedOptions } = createSpan("WebPubSubServiceClient-hub-removeUserFromAllGroups", options);
171
- try {
172
- return yield this.client.webPubSub.removeUserFromAllGroups(this.hubName, userId, updatedOptions);
173
- }
174
- finally {
175
- span.end();
176
- }
177
- });
179
+ async removeUserFromAllGroups(userId, options = {}) {
180
+ const { span, updatedOptions } = createSpan("WebPubSubServiceClient-hub-removeUserFromAllGroups", options);
181
+ try {
182
+ await this.client.webPubSub.removeUserFromAllGroups(this.hubName, userId, updatedOptions);
183
+ }
184
+ finally {
185
+ span.end();
186
+ }
178
187
  }
179
188
  /**
180
189
  * Check if a particular group exists (i.e. has active connections).
181
190
  *
182
- * @param groupName The group name to check for
183
- * @param options Additional options
191
+ * @param groupName - The group name to check for
192
+ * @param options - Additional options
184
193
  */
185
- hasGroup(groupName, options = {}) {
186
- return __awaiter(this, void 0, void 0, function* () {
187
- const { span, updatedOptions } = createSpan("WebPubSubServiceClient-hub-hasGroup", options);
188
- try {
189
- const res = yield this.client.webPubSub.groupExists(this.hubName, groupName, updatedOptions);
190
- if (res._response.status === 200) {
191
- return true;
192
- }
193
- else if (res._response.status === 404) {
194
- return false;
195
- }
196
- else {
197
- throw new RestError(res._response.bodyAsText, undefined, res._response.status, res._response.request, res._response);
198
- }
194
+ async groupExists(groupName, options = {}) {
195
+ const { span, updatedOptions } = createSpan("WebPubSubServiceClient-hub-hasGroup", options);
196
+ let response;
197
+ function onResponse(rawResponse, flatResponse) {
198
+ response = rawResponse;
199
+ if (updatedOptions.onResponse) {
200
+ updatedOptions.onResponse(rawResponse, flatResponse);
201
+ }
202
+ }
203
+ try {
204
+ await this.client.webPubSub.groupExists(this.hubName, groupName, Object.assign(Object.assign({}, updatedOptions), { onResponse }));
205
+ if (response.status === 200) {
206
+ return true;
207
+ }
208
+ else if (response.status === 404) {
209
+ return false;
199
210
  }
200
- finally {
201
- span.end();
211
+ else {
212
+ throw new RestError(response.bodyAsText, {
213
+ statusCode: response === null || response === void 0 ? void 0 : response.status,
214
+ request: response === null || response === void 0 ? void 0 : response.request,
215
+ response: response
216
+ });
202
217
  }
203
- });
218
+ }
219
+ finally {
220
+ span.end();
221
+ }
204
222
  }
205
223
  /**
206
224
  * Check if a particular user is connected to this hub.
207
225
  *
208
- * @param username The user name to check for
209
- * @param options Additional options
226
+ * @param username - The user name to check for
227
+ * @param options - Additional options
210
228
  */
211
- hasUser(username, options = {}) {
212
- return __awaiter(this, void 0, void 0, function* () {
213
- const { span, updatedOptions } = createSpan("WebPubSubServiceClient-hub-hasUser", options);
214
- try {
215
- const res = yield this.client.webPubSub.userExists(this.hubName, username, updatedOptions);
216
- if (res._response.status === 200) {
217
- return true;
218
- }
219
- else if (res._response.status === 404) {
220
- return false;
221
- }
222
- else {
223
- // this is sad - wish this was handled by autorest.
224
- throw new RestError(res._response.bodyAsText, undefined, res._response.status, res._response.request, res._response);
225
- }
229
+ async userExists(username, options = {}) {
230
+ const { span, updatedOptions } = createSpan("WebPubSubServiceClient-hub-hasUser", options);
231
+ let response;
232
+ function onResponse(rawResponse, flatResponse) {
233
+ response = rawResponse;
234
+ if (updatedOptions.onResponse) {
235
+ updatedOptions.onResponse(rawResponse, flatResponse);
226
236
  }
227
- finally {
228
- span.end();
237
+ }
238
+ try {
239
+ await this.client.webPubSub.userExists(this.hubName, username, Object.assign(Object.assign({}, updatedOptions), { onResponse }));
240
+ if (response.status === 200) {
241
+ return true;
229
242
  }
230
- });
243
+ else if (response.status === 404) {
244
+ return false;
245
+ }
246
+ else {
247
+ // this is sad - wish this was handled by autorest.
248
+ throw new RestError(response.bodyAsText, {
249
+ statusCode: response === null || response === void 0 ? void 0 : response.status,
250
+ request: response === null || response === void 0 ? void 0 : response.request,
251
+ response: response
252
+ });
253
+ }
254
+ }
255
+ finally {
256
+ span.end();
257
+ }
231
258
  }
232
259
  /**
233
260
  * Grant permissions to a connection
234
261
  *
235
- * @param connectionId The connection id to grant permissions to
236
- * @param Permission The permission to grant
237
- * @param options Additional options
262
+ * @param connectionId - The connection id to grant permissions to
263
+ * @param Permission - The permission to grant
264
+ * @param options - Additional options
238
265
  */
239
- grantPermission(connectionId, permission, options = {}) {
240
- return __awaiter(this, void 0, void 0, function* () {
241
- const { span, updatedOptions } = createSpan("WebPubSubServiceClient-hub-grantPermission", options);
242
- try {
243
- return yield this.client.webPubSub.grantPermission(this.hubName, permission, connectionId, updatedOptions);
244
- }
245
- finally {
246
- span.end();
247
- }
248
- });
266
+ async grantPermission(connectionId, permission, options = {}) {
267
+ const { span, updatedOptions } = createSpan("WebPubSubServiceClient-hub-grantPermission", options);
268
+ try {
269
+ return await this.client.webPubSub.grantPermission(this.hubName, permission, connectionId, updatedOptions);
270
+ }
271
+ finally {
272
+ span.end();
273
+ }
249
274
  }
250
275
  /**
251
276
  * Revoke permissions from a connection
252
277
  *
253
- * @param connectionId The connection id to revoke permissions from
254
- * @param Permission The permission to revoke
255
- * @param options Additional options
278
+ * @param connectionId - The connection id to revoke permissions from
279
+ * @param Permission - The permission to revoke
280
+ * @param options - Additional options
281
+ */
282
+ async revokePermission(connectionId, permission, options = {}) {
283
+ const { span, updatedOptions } = createSpan("WebPubSubServiceClient-hub-revokePermission", options);
284
+ try {
285
+ return await this.client.webPubSub.revokePermission(this.hubName, permission, connectionId, updatedOptions);
286
+ }
287
+ finally {
288
+ span.end();
289
+ }
290
+ }
291
+ /**
292
+ * Check if the connection has the specified permission
293
+ *
294
+ * @param connectionId - The connection id to check permission
295
+ * @param Permission - The permission to check
296
+ * @param options - Additional options
256
297
  */
257
- revokePermission(connectionId, permission, options = {}) {
258
- return __awaiter(this, void 0, void 0, function* () {
259
- const { span, updatedOptions } = createSpan("WebPubSubServiceClient-hub-revokePermission", options);
260
- try {
261
- return yield this.client.webPubSub.revokePermission(this.hubName, permission, connectionId, updatedOptions);
298
+ async hasPermission(connectionId, permission, options = {}) {
299
+ const { span, updatedOptions } = createSpan("WebPubSubServiceClient-hub-hasPermission", options);
300
+ let response;
301
+ function onResponse(rawResponse, flatResponse) {
302
+ response = rawResponse;
303
+ if (updatedOptions.onResponse) {
304
+ updatedOptions.onResponse(rawResponse, flatResponse);
305
+ }
306
+ }
307
+ try {
308
+ await this.client.webPubSub.checkPermission(this.hubName, permission, connectionId, Object.assign(Object.assign({}, updatedOptions), { onResponse }));
309
+ if (response.status === 200) {
310
+ return true;
262
311
  }
263
- finally {
264
- span.end();
312
+ else if (response.status === 404) {
313
+ return false;
265
314
  }
266
- });
315
+ else {
316
+ // this is sad - wish this was handled by autorest.
317
+ throw new RestError(response.bodyAsText, {
318
+ statusCode: response === null || response === void 0 ? void 0 : response.status,
319
+ request: response === null || response === void 0 ? void 0 : response.request,
320
+ response: response
321
+ });
322
+ }
323
+ }
324
+ finally {
325
+ span.end();
326
+ }
267
327
  }
268
328
  /**
269
- * Check if the connection has the specified permission
329
+ * Generate a token for a client to connect to the Azure Web PubSub service.
270
330
  *
271
- * @param connectionId The connection id to check permission
272
- * @param Permission The permission to check
273
- * @param options Additional options
331
+ * @param options - Additional options
274
332
  */
275
- hasPermission(connectionId, permission, options = {}) {
276
- return __awaiter(this, void 0, void 0, function* () {
277
- const { span, updatedOptions } = createSpan("WebPubSubServiceClient-hub-hasPermission", options);
278
- try {
279
- return yield this.client.webPubSub.checkPermission(this.hubName, permission, connectionId, updatedOptions);
333
+ async getClientAccessToken(options = {}) {
334
+ const { span, updatedOptions } = createSpan("WebPubSubServiceClient-hub-generateClientToken", options);
335
+ try {
336
+ const endpoint = this.endpoint.endsWith("/") ? this.endpoint : this.endpoint + "/";
337
+ const clientEndpoint = endpoint.replace(/(http)(s?:\/\/)/gi, "ws$2");
338
+ const baseUrl = `${clientEndpoint}client/hubs/${this.hubName}`;
339
+ let token;
340
+ if (isTokenCredential(this.credential)) {
341
+ const response = await this.client.webPubSub.generateClientToken(this.hubName, updatedOptions);
342
+ token = response.token;
280
343
  }
281
- finally {
282
- span.end();
344
+ else {
345
+ const key = this.credential.key;
346
+ const audience = `${endpoint}client/hubs/${this.hubName}`;
347
+ const payload = { role: options === null || options === void 0 ? void 0 : options.roles };
348
+ const signOptions = {
349
+ audience: audience,
350
+ expiresIn: (options === null || options === void 0 ? void 0 : options.expirationTimeInMinutes) === undefined
351
+ ? "1h"
352
+ : `${options.expirationTimeInMinutes}m`,
353
+ algorithm: "HS256"
354
+ };
355
+ if (options === null || options === void 0 ? void 0 : options.userId) {
356
+ signOptions.subject = options === null || options === void 0 ? void 0 : options.userId;
357
+ }
358
+ token = jwt.sign(payload, key, signOptions);
283
359
  }
284
- });
360
+ return {
361
+ token,
362
+ baseUrl,
363
+ url: `${baseUrl}?access_token=${token}`
364
+ };
365
+ }
366
+ finally {
367
+ span.end();
368
+ }
285
369
  }
286
370
  }
287
371
  //# sourceMappingURL=hubClient.js.map