Skip to content

Commit 758cb11

Browse files
committed
fix: lint and format source code with biome
Signed-off-by: Tierney Cyren <[email protected]>
1 parent 5b7e9fc commit 758cb11

File tree

14 files changed

+463
-435
lines changed

14 files changed

+463
-435
lines changed

src/commands/token/capability/client.js

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,52 @@ const { voiceFlags } = require('../../../helpers/voiceGlobals.js');
55
const { validateSid } = require('../../../helpers/validation-helpers.js');
66

77
class ClientCapabilityTokenGenerator extends TwilioClientCommand {
8-
constructor(argv, config) {
9-
super(argv, config);
10-
11-
this.showHeaders = true;
12-
}
13-
14-
async run() {
15-
await super.run();
16-
17-
const voiceAppSid = await this.flags['voice-app-sid'];
18-
const ttl = await this.flags['ttl'];
19-
const incomingAllow = await this.flags['allow-incoming'];
20-
const identity = await this.flags['identity'];
21-
const capability = new ClientCapability({
22-
accountSid: this.twilioClient.accountSid,
23-
authToken: this.twilioClient.password,
24-
ttl
25-
});
26-
27-
if (!validateSid('AP', voiceAppSid)) {
28-
this.logger.error(
29-
'Invalid TwiML Application SID, must look like APxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
30-
);
31-
process.exit(1);
32-
}
33-
34-
capability.addScope(new ClientCapability.OutgoingClientScope({
35-
applicationSid: voiceAppSid
36-
}));
37-
38-
if (incomingAllow) {
39-
capability.addScope(new ClientCapability.IncomingClientScope(identity))
40-
}
41-
42-
this.logger.info('Copy/paste this voice token into your test application:');
43-
this.output({ jwt: capability.toJwt() }, undefined, {
44-
showHeaders: false,
45-
});
46-
}
8+
constructor(argv, config) {
9+
super(argv, config);
10+
11+
this.showHeaders = true;
12+
}
13+
14+
async run() {
15+
await super.run();
16+
17+
const voiceAppSid = await this.flags['voice-app-sid'];
18+
const ttl = await this.flags.ttl;
19+
const incomingAllow = await this.flags['allow-incoming'];
20+
const identity = await this.flags.identity;
21+
const capability = new ClientCapability({
22+
accountSid: this.twilioClient.accountSid,
23+
authToken: this.twilioClient.password,
24+
ttl,
25+
});
26+
27+
if (!validateSid('AP', voiceAppSid)) {
28+
this.logger.error(
29+
'Invalid TwiML Application SID, must look like APxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
30+
);
31+
process.exit(1);
32+
}
33+
34+
capability.addScope(
35+
new ClientCapability.OutgoingClientScope({
36+
applicationSid: voiceAppSid,
37+
}),
38+
);
39+
40+
if (incomingAllow) {
41+
capability.addScope(new ClientCapability.IncomingClientScope(identity));
42+
}
43+
44+
this.logger.info('Copy/paste this voice token into your test application:');
45+
this.output({ jwt: capability.toJwt() }, undefined, {
46+
showHeaders: false,
47+
});
48+
}
4749
}
4850

4951
ClientCapabilityTokenGenerator.flags = Object.assign(
50-
voiceFlags,
51-
TwilioClientCommand.flags,
52-
globalFlags,
52+
voiceFlags,
53+
TwilioClientCommand.flags,
54+
globalFlags,
5355
);
5456
module.exports = ClientCapabilityTokenGenerator;

src/commands/token/capability/worker.js

Lines changed: 102 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -8,93 +8,111 @@ const TaskRouterCapability = taskrouter.TaskRouterCapability;
88
const Policy = TaskRouterCapability.Policy;
99

1010
class WorkerCapabilityTokenGenerator extends TwilioClientCommand {
11-
constructor(argv, config) {
12-
super(argv, config);
13-
14-
this.showHeaders = true;
15-
}
16-
17-
async run() {
18-
await super.run();
19-
20-
const workerSid = await this.flags['worker-sid'];
21-
const workspaceSid = await this.flags['workspace-sid'];
22-
let ttl = await this.flags['ttl'];
23-
const TASKROUTER_BASE_URL = 'https://taskrouter.twilio.com';
24-
const version = 'v1';
25-
26-
if (!validateSid('WK', workerSid)) {
27-
this.logger.error(
28-
'Invalid Worker SID, must look like WKxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
29-
);
30-
process.exit(1);
31-
}
32-
33-
if (!validateSid('WS', workspaceSid)) {
34-
this.logger.error(
35-
'Invalid Workspace SID, must look like WSxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
36-
);
37-
process.exit(1);
38-
}
39-
40-
const accountSid = this.twilioClient.accountSid;
41-
const authToken = this.twilioClient.password;
42-
43-
const capability = new TaskRouterCapability({
44-
accountSid,
45-
authToken,
46-
workspaceSid,
47-
channelId: workerSid,
48-
ttl
49-
});
50-
51-
// Helper function to create Policy
52-
function buildWorkspacePolicy(options) {
53-
options = options || {};
54-
var resources = options.resources || [];
55-
var urlComponents = [TASKROUTER_BASE_URL, version, 'Workspaces', workspaceSid]
56-
57-
return new Policy({
58-
url: urlComponents.concat(resources).join('/'),
59-
method: options.method || 'GET',
60-
allow: true
61-
});
62-
}
63-
64-
// Event Bridge Policies
65-
var eventBridgePolicies = util.defaultEventBridgePolicies(accountSid, workerSid);
66-
67-
// Worker Policies
68-
var workerPolicies = util.defaultWorkerPolicies(version, workspaceSid, workerSid);
69-
70-
var workspacePolicies = [
71-
// Workspace fetch Policy
72-
buildWorkspacePolicy(),
73-
// Workspace subresources fetch Policy
74-
buildWorkspacePolicy({ resources: ['**'] }),
75-
// Workspace Activities Update Policy
76-
buildWorkspacePolicy({ resources: ['Activities'], method: 'POST' }),
77-
// Workspace Activities Worker Reserations Policy
78-
buildWorkspacePolicy({ resources: ['Workers', workerSid, 'Reservations', '**'], method: 'POST' }),
79-
];
80-
81-
eventBridgePolicies.concat(workerPolicies).concat(workspacePolicies).forEach(function (policy) {
82-
capability.addPolicy(policy);
83-
});
84-
85-
this.logger.info('Copy/paste this video token into your test application:');
86-
this.output({ jwt: capability.toJwt() }, undefined, {
87-
showHeaders: false,
88-
});
89-
}
11+
constructor(argv, config) {
12+
super(argv, config);
13+
14+
this.showHeaders = true;
15+
}
16+
17+
async run() {
18+
await super.run();
19+
20+
const workerSid = await this.flags['worker-sid'];
21+
const workspaceSid = await this.flags['workspace-sid'];
22+
const ttl = await this.flags.ttl;
23+
const TASKROUTER_BASE_URL = 'https://taskrouter.twilio.com';
24+
const version = 'v1';
25+
26+
if (!validateSid('WK', workerSid)) {
27+
this.logger.error(
28+
'Invalid Worker SID, must look like WKxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
29+
);
30+
process.exit(1);
31+
}
32+
33+
if (!validateSid('WS', workspaceSid)) {
34+
this.logger.error(
35+
'Invalid Workspace SID, must look like WSxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
36+
);
37+
process.exit(1);
38+
}
39+
40+
const accountSid = this.twilioClient.accountSid;
41+
const authToken = this.twilioClient.password;
42+
43+
const capability = new TaskRouterCapability({
44+
accountSid,
45+
authToken,
46+
workspaceSid,
47+
channelId: workerSid,
48+
ttl,
49+
});
50+
51+
// Helper function to create Policy
52+
function buildWorkspacePolicy(options) {
53+
const internalOptions = options || {};
54+
const resources = internalOptions.resources || [];
55+
const urlComponents = [
56+
TASKROUTER_BASE_URL,
57+
version,
58+
'Workspaces',
59+
workspaceSid,
60+
];
61+
62+
return new Policy({
63+
url: urlComponents.concat(resources).join('/'),
64+
method: internalOptions.method || 'GET',
65+
allow: true,
66+
});
67+
}
68+
69+
// Event Bridge Policies
70+
const eventBridgePolicies = util.defaultEventBridgePolicies(
71+
accountSid,
72+
workerSid,
73+
);
74+
75+
// Worker Policies
76+
const workerPolicies = util.defaultWorkerPolicies(
77+
version,
78+
workspaceSid,
79+
workerSid,
80+
);
81+
82+
const workspacePolicies = [
83+
// Workspace fetch Policy
84+
buildWorkspacePolicy(),
85+
// Workspace subresources fetch Policy
86+
buildWorkspacePolicy({ resources: ['**'] }),
87+
// Workspace Activities Update Policy
88+
buildWorkspacePolicy({ resources: ['Activities'], method: 'POST' }),
89+
// Workspace Activities Worker Reserations Policy
90+
buildWorkspacePolicy({
91+
resources: ['Workers', workerSid, 'Reservations', '**'],
92+
method: 'POST',
93+
}),
94+
];
95+
96+
eventBridgePolicies.concat(workerPolicies).concat(workspacePolicies);
97+
98+
for (const policy of eventBridgePolicies) {
99+
capability.addPolicy(policy);
100+
}
101+
102+
this.logger.info('Copy/paste this video token into your test application:');
103+
this.output({ jwt: capability.toJwt() }, undefined, {
104+
showHeaders: false,
105+
});
106+
}
90107
}
91108

92-
let globals = { ...globalFlags };
93-
delete globals.identity;
109+
const globals = { ...globalFlags };
110+
globals.identity = undefined;
94111

95112
WorkerCapabilityTokenGenerator.flags = Object.assign(
96-
taskrouterFlags,
97-
TwilioClientCommand.flags,
98-
globals,
113+
taskrouterFlags,
114+
TwilioClientCommand.flags,
115+
globals,
99116
);
117+
100118
module.exports = WorkerCapabilityTokenGenerator;

src/commands/token/chat.js

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,51 @@ const globalFlags = require('../../helpers/globalFlags.js');
66
const { validateSid } = require('../../helpers/validation-helpers.js');
77

88
class ChatTokenGenerator extends TwilioClientCommand {
9-
constructor(argv, config) {
10-
super(argv, config);
11-
12-
this.showHeaders = true;
13-
}
14-
15-
async run() {
16-
await super.run();
17-
18-
const chatServiceSid = await this.flags['chat-service-sid'];
19-
const accessToken = createToken.call(this);
20-
21-
if (!validateSid('IS', chatServiceSid)) {
22-
this.logger.error(
23-
'Invalid Chat Service SID, must look like ISxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
24-
);
25-
process.exit(1);
26-
}
27-
28-
let chatGrant = new Twilio.jwt.AccessToken.ChatGrant({
29-
serviceSid: chatServiceSid
30-
});
31-
accessToken.addGrant(chatGrant);
32-
33-
this.logger.info('Copy/paste this chat token into your test application:');
34-
this.output({ jwt: accessToken.toJwt() }, undefined, {
35-
showHeaders: false,
36-
});
37-
}
9+
constructor(argv, config) {
10+
super(argv, config);
11+
12+
this.showHeaders = true;
13+
}
14+
15+
async run() {
16+
await super.run();
17+
18+
const chatServiceSid = await this.flags['chat-service-sid'];
19+
const accessToken = createToken.call(this);
20+
21+
if (!validateSid('IS', chatServiceSid)) {
22+
this.logger.error(
23+
'Invalid Chat Service SID, must look like ISxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
24+
);
25+
process.exit(1);
26+
}
27+
28+
const chatGrant = new Twilio.jwt.AccessToken.ChatGrant({
29+
serviceSid: chatServiceSid,
30+
});
31+
accessToken.addGrant(chatGrant);
32+
33+
this.logger.info('Copy/paste this chat token into your test application:');
34+
this.output({ jwt: accessToken.toJwt() }, undefined, {
35+
showHeaders: false,
36+
});
37+
}
3838
}
3939

4040
const ChatTokenGeneratorFlags = {
41-
identity: Flags.string({
42-
description: 'The user identity for this Chat',
43-
required: true
44-
}),
45-
'chat-service-sid': Flags.string({
46-
description: 'The service SID for the Chat, starts with ISXXX',
47-
required: true
48-
}),
41+
identity: Flags.string({
42+
description: 'The user identity for this Chat',
43+
required: true,
44+
}),
45+
'chat-service-sid': Flags.string({
46+
description: 'The service SID for the Chat, starts with ISXXX',
47+
required: true,
48+
}),
4949
};
5050

5151
ChatTokenGenerator.flags = Object.assign(
52-
ChatTokenGeneratorFlags,
53-
TwilioClientCommand.flags,
54-
globalFlags
52+
ChatTokenGeneratorFlags,
53+
TwilioClientCommand.flags,
54+
globalFlags,
5555
);
5656
module.exports = ChatTokenGenerator;

0 commit comments

Comments
 (0)