Skip to content

Commit 1e6e2c5

Browse files
authored
revert: "Use runtime-helpers in apps (part 1/3) (twilio-labs#305)" (twilio-labs#310)
This reverts commit 78019ad.
1 parent b3aeaad commit 1e6e2c5

File tree

26 files changed

+549
-112
lines changed

26 files changed

+549
-112
lines changed

chat-token/functions/chat-token.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
* - Create an API Key (https://www.twilio.com/console/runtime/api-keys)
1111
*/
1212

13-
const { createCORSResponse } = require('@twilio-labs/runtime-helpers').response;
14-
1513
exports.handler = function (context, event, callback) {
1614
/*
1715
* Change these values for your use case
@@ -37,12 +35,14 @@ exports.handler = function (context, event, callback) {
3735
accessToken.addGrant(chatGrant);
3836
accessToken.identity = IDENTITY;
3937

40-
// set to true to support CORS
41-
const supportCors = false;
42-
/* istanbul ignore next */
43-
const response = supportCors
44-
? createCORSResponse('*')
45-
: new Twilio.Response();
38+
const response = new Twilio.Response();
39+
40+
/*
41+
* Uncomment these lines for CORS support
42+
* response.appendHeader('Access-Control-Allow-Origin', '*');
43+
* response.appendHeader('Access-Control-Allow-Methods', 'GET');
44+
* response.appendHeader('Access-Control-Allow-Headers', 'Content-Type');
45+
*/
4646

4747
response.appendHeader('Content-Type', 'application/json');
4848
response.setBody({ token: accessToken.toJwt() });

chat-token/package.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
{
22
"version": "1.0.0",
33
"private": true,
4-
"dependencies": {
5-
"@twilio-labs/runtime-helpers": "^0.1.2"
6-
}
4+
"dependencies": {}
75
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
async function getCurrentEnvironment(context) {
2+
if (context.DOMAIN_NAME && context.DOMAIN_NAME.startsWith('localhost')) {
3+
return null;
4+
}
5+
const client = context.getTwilioClient();
6+
const services = await client.serverless.services.list();
7+
for (const service of services) {
8+
const environments = await client.serverless
9+
.services(service.sid)
10+
.environments.list();
11+
const environment = environments.find(
12+
(env) => env.domainName === context.DOMAIN_NAME
13+
);
14+
if (environment) {
15+
// Exit the function
16+
return environment;
17+
}
18+
}
19+
return environment;
20+
}
21+
22+
async function getEnvironmentVariables(context, environment) {
23+
const client = context.getTwilioClient();
24+
return client.serverless
25+
.services(environment.serviceSid)
26+
.environments(environment.sid)
27+
.variables.list();
28+
}
29+
30+
async function getEnvironmentVariable(context, environment, key) {
31+
const client = context.getTwilioClient();
32+
// The list filter method isn't implemented yet.
33+
const envVars = await getEnvironmentVariables(context, environment);
34+
return envVars.find((variable) => variable.key === key);
35+
}
36+
37+
async function setEnvironmentVariable(
38+
context,
39+
environment,
40+
key,
41+
value,
42+
override = true
43+
) {
44+
const client = context.getTwilioClient();
45+
try {
46+
const currentVariable = await getEnvironmentVariable(
47+
context,
48+
environment,
49+
key
50+
);
51+
if (currentVariable) {
52+
if (currentVariable.value !== value) {
53+
if (override) {
54+
console.log(`Updating ${key}...`);
55+
await currentVariable.update({ value });
56+
return true;
57+
}
58+
console.log(
59+
`Not overriding existing variable '${key}' which is set to '${currentVariable.value}'`
60+
);
61+
return false;
62+
}
63+
console.warn(`Variable '${key}' was already set to '${value}'`);
64+
return false;
65+
}
66+
console.log(`Creating variable ${key}`);
67+
await client.serverless
68+
.services(environment.serviceSid)
69+
.environments(environment.sid)
70+
.variables.create({
71+
key,
72+
value,
73+
});
74+
} catch (err) {
75+
console.error(`Error creating '${key}' with '${value}': ${err}`);
76+
return false;
77+
}
78+
return true;
79+
}
80+
81+
module.exports = {
82+
getCurrentEnvironment,
83+
setEnvironmentVariable,
84+
};

covid-vaccine-faq-bot/functions/setup.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
exports.handler = async function (context, event, callback) {
33
const assets = Runtime.getAssets();
44
const flowDefinition = require(assets['/studio_flow.js'].path);
5-
const { getCurrentEnvironment, setEnvironmentVariable } =
6-
require('@twilio-labs/runtime-helpers').environment;
5+
const { path } = Runtime.getFunctions().auth;
6+
const { getCurrentEnvironment, setEnvironmentVariable } = require(path);
77

88
const client = context.getTwilioClient();
99

covid-vaccine-faq-bot/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"version": "1.0.0",
44
"private": true,
55
"dependencies": {
6-
"@twilio-labs/runtime-helpers": "^0.1.2",
76
"@google-cloud/dialogflow": "^4.1.0",
87
"crypto": "^1.0.1",
98
"twilio": "^3.64.0"

international-telephone-input/functions/lookup.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,18 @@
1313
* }
1414
*/
1515

16-
const { createCORSResponse } = require('@twilio-labs/runtime-helpers').response;
17-
1816
// eslint-disable-next-line consistent-return
1917
exports.handler = function (context, event, callback) {
20-
// set to true to support CORS
21-
const supportCors = false;
22-
const response = supportCors
23-
? createCORSResponse('*')
24-
: new Twilio.Response();
18+
const response = new Twilio.Response();
2519
response.appendHeader('Content-Type', 'application/json');
2620

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+
2728
if (event.phone === '' || typeof event.phone === 'undefined') {
2829
response.setBody({
2930
success: false,

international-telephone-input/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"version": "1.0.0",
33
"private": true,
44
"dependencies": {
5-
"twilio": "^3.61.0",
6-
"@twilio-labs/runtime-helpers": "^0.1.2"
5+
"twilio": "^3.61.0"
76
}
87
}

lookup/functions/lookup.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@
77
* "phone" - string - phone number in E.164 format (https://www.twilio.com/docs/glossary/what-e164)
88
*/
99

10-
const { createCORSResponse } = require('@twilio-labs/runtime-helpers').response;
11-
1210
// eslint-disable-next-line consistent-return
1311
exports.handler = async function (context, event, callback) {
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();
12+
const response = new Twilio.Response();
2013
response.appendHeader('Content-Type', 'application/json');
2114

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
if (event.phone === '' || typeof event.phone === 'undefined') {
2424
throw new Error('Missing parameter; please provide a phone number.');

lookup/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"version": "1.0.0",
33
"private": true,
44
"dependencies": {
5-
"twilio": "^3.61.0",
6-
"@twilio-labs/runtime-helpers": "^0.1.2"
5+
"twilio": "^3.61.0"
76
}
87
}

magic-links/functions/check-verify.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,18 @@
1616
* }
1717
*/
1818

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

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+
3031
if (
3132
typeof event.to === 'undefined' ||
3233
typeof event.verification_code === 'undefined'

magic-links/functions/start-verify.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,18 @@
1919
* }
2020
*/
2121

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

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+
3334
if (typeof event.to === 'undefined') {
3435
response.setBody({
3536
success: false,

magic-links/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"version": "1.0.0",
33
"private": true,
44
"dependencies": {
5-
"twilio": "^3.61.0",
6-
"@twilio-labs/runtime-helpers": "^0.1.2"
5+
"twilio": "^3.61.0"
76
}
87
}

sip-quickstart/assets/admin/shared.private.js

+88
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,90 @@ 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+
// The list filter method isn't implemented yet.
66+
const envVars = await getEnvironmentVariables(context, environment);
67+
return envVars.find((variable) => variable.key === key);
68+
}
69+
70+
async function setEnvironmentVariable(
71+
context,
72+
environment,
73+
key,
74+
value,
75+
override = true
76+
) {
77+
const client = context.getTwilioClient();
78+
try {
79+
const currentVariable = await getEnvironmentVariable(
80+
context,
81+
environment,
82+
key
83+
);
84+
if (currentVariable) {
85+
if (currentVariable.value !== value) {
86+
if (override) {
87+
if (value === undefined) {
88+
console.log(`Removing ${key}...`);
89+
await currentVariable.remove();
90+
} else {
91+
console.log(`Updating ${key}...`);
92+
await currentVariable.update({ value });
93+
}
94+
return true;
95+
}
96+
console.log(
97+
`Not overriding existing variable '${key}' which is set to '${currentVariable.value}'`
98+
);
99+
return false;
100+
}
101+
console.warn(`Variable '${key}' was already set to '${value}'`);
102+
return false;
103+
}
104+
console.log(`Creating variable ${key}`);
105+
await client.serverless
106+
.services(environment.serviceSid)
107+
.environments(environment.sid)
108+
.variables.create({
109+
key,
110+
value,
111+
});
112+
} catch (err) {
113+
console.error(`Error creating '${key}' with '${value}': ${err}`);
114+
return false;
115+
}
116+
return true;
117+
}
118+
35119
function urlForSiblingPage(newPage, ...paths) {
36120
const url = path.resolve(...paths);
37121
const parts = url.split('/');
@@ -43,5 +127,9 @@ function urlForSiblingPage(newPage, ...paths) {
43127
module.exports = {
44128
checkAuthorization,
45129
createToken,
130+
getCurrentEnvironment,
131+
getEnvironmentVariables,
132+
getEnvironmentVariable,
133+
setEnvironmentVariable,
46134
urlForSiblingPage,
47135
};

sip-quickstart/assets/admin/statuses.private.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
const { stripIndents } = require('common-tags');
22

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

109
async function checkEnvironmentInitialization(context) {

0 commit comments

Comments
 (0)