Skip to content

Commit 30d5bdb

Browse files
feat(packages/twilio-run): regionalize toolkit config and api (#433)
* feat(packages/twilio-run): regionalize the .twiliodeployinfo * feat: regional support for config files * chore(runtime-handler/packager.json): override a check * chore: typescript override for lodash types * refactor: using optional chaining * fix: passing missing region param to related functions
1 parent d4fde96 commit 30d5bdb

File tree

20 files changed

+170
-29
lines changed

20 files changed

+170
-29
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
},
4040
"overrides": {
4141
"@types/prettier": "2.6.0",
42-
"@types/express-serve-static-core": "ts3.9"
42+
"@types/express-serve-static-core": "ts3.9",
43+
"@types/lodash": "ts3.9"
4344
},
4445
"lint-staged": {
4546
"*.{js,jsx,ts,tsx}": [

packages/runtime-handler/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@
7777
},
7878
"overrides": {
7979
"@types/prettier": "2.6.0",
80-
"@types/express-serve-static-core": "ts3.9"
80+
"@types/express-serve-static-core": "ts3.9",
81+
"@types/lodash": "ts3.9"
8182
},
8283
"gitHead": "6db273648ed19474f4125042556b10c051529912"
8384
}

packages/serverless-api/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@
7272
"upath": "^1.1.2"
7373
},
7474
"overrides": {
75-
"@types/prettier": "2.6.0"
75+
"@types/prettier": "2.6.0",
76+
"@types/lodash": "ts3.9"
7677
},
7778
"gitHead": "6db273648ed19474f4125042556b10c051529912"
7879
}

packages/serverless-api/src/api/utils/__tests__/api-client.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,18 @@ describe('getApiUrl', () => {
6262
const url = getApiUrl(DEFAULT_TEST_CLIENT_CONFIG);
6363
expect(url).toBe('https://serverless.sydney.au2.twilio.com/v1');
6464
});
65+
66+
test('handles edge with only eligible region with variable', () => {
67+
process.env.TWILIO_REGION = 'au1';
68+
const url = getApiUrl(DEFAULT_TEST_CLIENT_CONFIG);
69+
expect(url).toBe('https://serverless.sydney.au1.twilio.com/v1');
70+
});
71+
72+
test('handles edge with only eligible region with params', () => {
73+
const url = getApiUrl({
74+
...DEFAULT_TEST_CLIENT_CONFIG,
75+
region: 'us1',
76+
});
77+
expect(url).toBe('https://serverless.ashburn.us1.twilio.com/v1');
78+
});
6579
});
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
11
import { ClientConfig } from '../../types';
22

3+
const regionEdgeMap: { [index: string]: string } = {
4+
us1: 'ashburn',
5+
au1: 'sydney',
6+
ie1: 'dublin',
7+
'stage-us1': 'ashburn',
8+
'stage-au1': 'sydney',
9+
};
10+
311
export function getApiUrl(
412
config: ClientConfig,
513
product = 'serverless',
614
apiVersion = 'v1'
715
): string {
816
const configEdge = config.edge || process.env.TWILIO_EDGE;
917
const configRegion = config.region || process.env.TWILIO_REGION;
18+
const region = configRegion ? `${configRegion}.` : '';
19+
20+
if (!configEdge && configRegion) {
21+
const defaultEdge = regionEdgeMap[configRegion]
22+
? `${regionEdgeMap[configRegion]}.`
23+
: '';
24+
return `https://${product}.${defaultEdge}${region}twilio.com/${apiVersion}`;
25+
}
1026

1127
const edge = configEdge ? `${configEdge}.` : '';
12-
const region = configRegion ? `${configRegion}.` : '';
1328
return `https://${product}.${edge}${region}twilio.com/${apiVersion}`;
1429
}

packages/twilio-run/__tests__/config/global.test.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,69 @@ describe('readSpecializedConfig', () => {
145145
env: '.env.stage',
146146
});
147147
});
148+
149+
test('account + region config override', () => {
150+
__setTestConfig({
151+
serviceSid: 'ZS11112222111122221111222211112222',
152+
env: '.env.example',
153+
commands: {
154+
deploy: {
155+
functionsFolder: '/tmp/functions',
156+
},
157+
},
158+
environments: {
159+
prod: {
160+
serviceSid: 'ZS11112222111122221111222211112223',
161+
env: '.env.prod',
162+
},
163+
},
164+
projects: {
165+
'AC11112222111122221111222211114444:au1': {
166+
serviceSid: 'ZS11112222111122221111222211114444',
167+
},
168+
'AC11112222111122221111222211114444:ie1': {
169+
serviceSid: 'ZS11112222111122221111222211114445',
170+
},
171+
AC11112222111122221111222211114444: {
172+
serviceSid: 'ZS11112222111122221111222211114446',
173+
},
174+
},
175+
});
176+
177+
expect(
178+
readSpecializedConfig('/tmp', '.twilioserverlessrc', 'deploy', {
179+
environmentSuffix: 'prod',
180+
username: 'AC11112222111122221111222211114444',
181+
region: 'ie1',
182+
})
183+
).toEqual({
184+
serviceSid: 'ZS11112222111122221111222211114445',
185+
functionsFolder: '/tmp/functions',
186+
env: '.env.prod',
187+
});
188+
189+
expect(
190+
readSpecializedConfig('/tmp', '.twilioserverlessrc', 'deploy', {
191+
environmentSuffix: 'prod',
192+
username: 'AC11112222111122221111222211114444',
193+
region: 'au1',
194+
})
195+
).toEqual({
196+
serviceSid: 'ZS11112222111122221111222211114444',
197+
functionsFolder: '/tmp/functions',
198+
env: '.env.prod',
199+
});
200+
201+
expect(
202+
readSpecializedConfig('/tmp', '.twilioserverlessrc', 'deploy', {
203+
environmentSuffix: 'prod',
204+
username: 'AC11112222111122221111222211114444',
205+
region: 'us1',
206+
})
207+
).toEqual({
208+
serviceSid: 'ZS11112222111122221111222211114446',
209+
functionsFolder: '/tmp/functions',
210+
env: '.env.prod',
211+
});
212+
});
148213
});

packages/twilio-run/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@
126126
"node": ">=12.22.1"
127127
},
128128
"overrides": {
129-
"@types/express-serve-static-core": "ts3.9"
129+
"@types/express-serve-static-core": "ts3.9",
130+
"@types/lodash": "ts3.9"
130131
},
131132
"gitHead": "6db273648ed19474f4125042556b10c051529912"
132133
}

packages/twilio-run/src/commands/deploy.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ export async function handler(
134134
buildSid,
135135
config.username.startsWith('AC')
136136
? config.username
137-
: externalCliOptions && externalCliOptions.accountSid
137+
: externalCliOptions && externalCliOptions.accountSid,
138+
config.region
138139
);
139140
} catch (err) {
140141
handleError(err, spinner, flags, config);

packages/twilio-run/src/config/deploy.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export async function getConfigFromFlags(
7474
(externalCliOptions && externalCliOptions.accountSid) ||
7575
undefined,
7676
environmentSuffix: flags.environment,
77+
region: flags.region,
7778
});
7879

7980
flags = mergeFlagsAndConfig<DeployCliFlags>(configFlags, flags, cliInfo);
@@ -98,7 +99,8 @@ export async function getConfigFromFlags(
9899
? flags.username
99100
: username.startsWith('AC')
100101
? username
101-
: externalCliOptions?.accountSid
102+
: externalCliOptions?.accountSid,
103+
flags.region
102104
));
103105

104106
const pkgJson = await readPackageJsonContent(flags);

packages/twilio-run/src/config/env/env-get.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export async function getConfigFromFlags(
4949
(externalCliOptions && externalCliOptions.accountSid) ||
5050
undefined,
5151
environmentSuffix: flags.environment,
52+
region: flags.region,
5253
});
5354

5455
flags = mergeFlagsAndConfig<EnvGetFlags>(configFlags, flags, cliInfo);
@@ -71,7 +72,8 @@ export async function getConfigFromFlags(
7172
? flags.username
7273
: username.startsWith('AC')
7374
? username
74-
: externalCliOptions?.accountSid
75+
: externalCliOptions?.accountSid,
76+
flags.region
7577
));
7678

7779
let serviceName = await getServiceNameFromFlags(flags);

0 commit comments

Comments
 (0)