Skip to content

Commit b0802ad

Browse files
Use runtime-helpers in apps (part 3/3) (#307)
* feat(lib): Add runtime-helpers library * feat(docs): Document runtime-helpers usage * feat(lib): Use runtime-helpers for 4 apps verify-totp, verify, voice-client-javascript, voice-javascript-sdk
1 parent 5840546 commit b0802ad

File tree

23 files changed

+136
-659
lines changed

23 files changed

+136
-659
lines changed

verify-totp/functions/create-challenge.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* "message": string
66
* }
77
*/
8+
const { createCORSResponse } = require('@twilio-labs/runtime-helpers').response;
9+
810
const assets = Runtime.getAssets();
911
const { detectMissingParams } = require(assets['/utils.js'].path);
1012

@@ -34,16 +36,14 @@ async function checkToken(entity, factorSid, code) {
3436
}
3537

3638
exports.handler = async function (context, event, callback) {
37-
const response = new Twilio.Response();
39+
// set to true to support CORS
40+
const supportCors = false;
41+
/* istanbul ignore next */
42+
const response = supportCors
43+
? createCORSResponse('*')
44+
: new Twilio.Response();
3845
response.appendHeader('Content-Type', 'application/json');
3946

40-
/*
41-
* uncomment to support CORS
42-
* response.appendHeader('Access-Control-Allow-Origin', '*');
43-
* response.appendHeader('Access-Control-Allow-Methods', 'POST, OPTIONS');
44-
* response.appendHeader('Access-Control-Allow-Headers', 'Content-Type');
45-
*/
46-
4747
try {
4848
const missingParams = detectMissingParams(
4949
['identity', 'code', 'factorSid'],

verify-totp/functions/create-factor.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const { createCORSResponse } = require('@twilio-labs/runtime-helpers').response;
12
const { v4: uuidv4 } = require('uuid');
23

34
/**
@@ -15,16 +16,14 @@ const assets = Runtime.getAssets();
1516
const { detectMissingParams } = require(assets['/utils.js'].path);
1617

1718
exports.handler = async function (context, event, callback) {
18-
const response = new Twilio.Response();
19+
// set to true to support CORS
20+
const supportCors = false;
21+
/* istanbul ignore next */
22+
const response = supportCors
23+
? createCORSResponse('*')
24+
: new Twilio.Response();
1925
response.appendHeader('Content-Type', 'application/json');
2026

21-
/*
22-
* uncomment to support CORS
23-
* response.appendHeader('Access-Control-Allow-Origin', '*');
24-
* response.appendHeader('Access-Control-Allow-Methods', 'POST, OPTIONS');
25-
* response.appendHeader('Access-Control-Allow-Headers', 'Content-Type');
26-
*/
27-
2827
try {
2928
const missingParams = detectMissingParams(['name'], event);
3029
if (missingParams.length > 0) {

verify-totp/functions/verify-new-factor.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55
* "message": string,
66
* }
77
*/
8+
const { createCORSResponse } = require('@twilio-labs/runtime-helpers').response;
9+
810
const assets = Runtime.getAssets();
911
const { detectMissingParams } = require(assets['/utils.js'].path);
1012

1113
exports.handler = async function (context, event, callback) {
12-
const response = new Twilio.Response();
14+
// set to true to support CORS
15+
const supportCors = false;
16+
/* istanbul ignore next */
17+
const response = supportCors
18+
? createCORSResponse('*')
19+
: new Twilio.Response();
1320
response.appendHeader('Content-Type', 'application/json');
1421

15-
/*
16-
* uncomment to support CORS
17-
* response.appendHeader('Access-Control-Allow-Origin', '*');
18-
* response.appendHeader('Access-Control-Allow-Methods', 'POST, OPTIONS');
19-
* response.appendHeader('Access-Control-Allow-Headers', 'Content-Type');
20-
*/
21-
2222
try {
2323
const missingParams = detectMissingParams(
2424
['identity', 'code', 'factorSid'],

verify-totp/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"private": true,
44
"dependencies": {
55
"twilio": "^3.67.2",
6+
"@twilio-labs/runtime-helpers": "^0.1.2",
67
"uuid": "^8.3.2"
78
}
89
}

verify/functions/check-verify.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@
1616
* }
1717
*/
1818

19+
const { createCORSResponse } = require('@twilio-labs/runtime-helpers').response;
20+
1921
// eslint-disable-next-line consistent-return
2022
exports.handler = function (context, event, callback) {
21-
const response = new Twilio.Response();
23+
// set to true to support CORS
24+
const supportCors = false;
25+
/* istanbul ignore next */
26+
const response = supportCors
27+
? createCORSResponse('*')
28+
: new Twilio.Response();
2229
response.appendHeader('Content-Type', 'application/json');
2330

24-
/*
25-
* uncomment to support CORS
26-
* response.appendHeader('Access-Control-Allow-Origin', '*');
27-
* response.appendHeader('Access-Control-Allow-Methods', 'POST, OPTIONS');
28-
* response.appendHeader('Access-Control-Allow-Headers', 'Content-Type');
29-
*/
30-
3131
if (
3232
typeof event.to === 'undefined' ||
3333
typeof event.verification_code === 'undefined'

verify/functions/start-verify.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@
1919
* }
2020
*/
2121

22+
const { createCORSResponse } = require('@twilio-labs/runtime-helpers').response;
23+
2224
// eslint-disable-next-line consistent-return
2325
exports.handler = function (context, event, callback) {
24-
const response = new Twilio.Response();
26+
// set to true to support CORS
27+
const supportCors = false;
28+
/* istanbul ignore next */
29+
const response = supportCors
30+
? createCORSResponse('*')
31+
: new Twilio.Response();
2532
response.appendHeader('Content-Type', 'application/json');
2633

27-
/*
28-
* uncomment to support CORS
29-
* response.appendHeader('Access-Control-Allow-Origin', '*');
30-
* response.appendHeader('Access-Control-Allow-Methods', 'POST, OPTIONS');
31-
* response.appendHeader('Access-Control-Allow-Headers', 'Content-Type');
32-
*/
33-
3434
if (typeof event.to === 'undefined') {
3535
response.setBody({
3636
success: false,

verify/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"version": "1.0.0",
33
"private": true,
44
"dependencies": {
5-
"twilio": "^3.61.0"
5+
"twilio": "^3.61.0",
6+
"@twilio-labs/runtime-helpers": "^0.1.2"
67
}
78
}

voice-client-javascript/assets/admin/shared.private.js

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -32,86 +32,6 @@ function checkAuthorization(context, event, callback) {
3232
return true;
3333
}
3434

35-
async function getCurrentEnvironment(context) {
36-
if (context.DOMAIN_NAME && context.DOMAIN_NAME.startsWith('localhost')) {
37-
return;
38-
}
39-
const client = context.getTwilioClient();
40-
const services = await client.serverless.services.list();
41-
for (const service of services) {
42-
const environments = await client.serverless
43-
.services(service.sid)
44-
.environments.list();
45-
const environment = environments.find(
46-
(env) => env.domainName === context.DOMAIN_NAME
47-
);
48-
if (environment) {
49-
// Exit the function
50-
// eslint-disable-next-line consistent-return
51-
return environment;
52-
}
53-
}
54-
}
55-
56-
async function getEnvironmentVariables(context, environment) {
57-
const client = context.getTwilioClient();
58-
return client.serverless
59-
.services(environment.serviceSid)
60-
.environments(environment.sid)
61-
.variables.list();
62-
}
63-
64-
async function getEnvironmentVariable(context, environment, key) {
65-
const client = context.getTwilioClient();
66-
// The list filter method isn't implemented yet.
67-
const envVars = await getEnvironmentVariables(context, environment);
68-
return envVars.find((variable) => variable.key === key);
69-
}
70-
71-
async function setEnvironmentVariable(
72-
context,
73-
environment,
74-
key,
75-
value,
76-
override = true
77-
) {
78-
const client = context.getTwilioClient();
79-
try {
80-
const currentVariable = await getEnvironmentVariable(
81-
context,
82-
environment,
83-
key
84-
);
85-
if (currentVariable) {
86-
if (currentVariable.value !== value) {
87-
if (override) {
88-
console.log(`Updating ${key}...`);
89-
await currentVariable.update({ value });
90-
return true;
91-
}
92-
console.log(
93-
`Not overriding existing variable '${key}' which is set to '${currentVariable.value}'`
94-
);
95-
return false;
96-
}
97-
console.warn(`Variable '${key}' was already set to '${value}'`);
98-
return false;
99-
}
100-
console.log(`Creating variable ${key}`);
101-
await client.serverless
102-
.services(environment.serviceSid)
103-
.environments(environment.sid)
104-
.variables.create({
105-
key,
106-
value,
107-
});
108-
} catch (err) {
109-
console.error(`Error creating '${key}' with '${value}': ${err}`);
110-
return false;
111-
}
112-
return true;
113-
}
114-
11535
function urlForSiblingPage(newPage, ...paths) {
11636
const url = path.resolve(...paths);
11737
const parts = url.split('/');
@@ -123,9 +43,5 @@ function urlForSiblingPage(newPage, ...paths) {
12343
module.exports = {
12444
checkAuthorization,
12545
createToken,
126-
getCurrentEnvironment,
127-
getEnvironmentVariables,
128-
getEnvironmentVariable,
129-
setEnvironmentVariable,
13046
urlForSiblingPage,
13147
};

voice-client-javascript/assets/admin/statuses.private.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
const { stripIndents } = require('common-tags');
33

44
const assets = Runtime.getAssets();
5-
const { getCurrentEnvironment, urlForSiblingPage } = require(assets[
6-
'/admin/shared.js'
7-
].path);
5+
const { getCurrentEnvironment } =
6+
require('@twilio-labs/runtime-helpers').environment;
7+
8+
const { urlForSiblingPage } = require(assets['/admin/shared.js'].path);
89

910
async function checkEnvironmentInitialization(context) {
1011
const environment = await getCurrentEnvironment(context);

voice-client-javascript/functions/admin/perform-action.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
const assets = Runtime.getAssets();
22
const Actions = require(assets['/admin/actions.js'].path);
3-
const {
4-
getCurrentEnvironment,
5-
setEnvironmentVariable,
6-
checkAuthorization,
7-
} = require(assets['/admin/shared.js'].path);
3+
const { getCurrentEnvironment, setEnvironmentVariable } =
4+
require('@twilio-labs/runtime-helpers').environment;
5+
6+
const { checkAuthorization } = require(assets['/admin/shared.js'].path);
87

98
exports.handler = async function (context, event, callback) {
109
if (!checkAuthorization(context, event, callback)) {

0 commit comments

Comments
 (0)