Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ newIssueWelcomeComment: |
firstPRMergeComment: >
Congratulations on your first contribution to the Serverless Toolkit!

We'd love to say thank you and send you some swag. If you are interested please fill out this form at https://twil.io/hacktoberfest-swag

If you are on the look out for more ways to contribute to open-source,
check out a list of some of our repositories at https://github.com/twilio/opensource.

If you want to stay up-to-date with Twilio's OSS activities, subscribe here: https://twil.io/oss-updates

And if you love Twilio as much as we do, make sure to check out our
[Twilio Champions program](https://www.twilio.com/champions)!
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,5 @@ typings/
.vscode/settings.json
dist/

*.tsbuildinfo
*.tsbuildinfo
.twilio-functions
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ npm install

1. Perform changes
2. Make sure tests pass by running `npm test`
3. Run `git commit` to kick off validation and enter your commit message. We are using [conventional commits](https://www.conventionalcommits.org/en/) for this project. When you run `git commit` it will trigger [`commitizen`](https://npm.im/commitizen) to assist you with your commit message.
3. Run `git commit` to kick off validation and enter your commit message. We are using [conventional commits](https://www.conventionalcommits.org/en/) for this project. When you run `npm run cm` it will trigger [`commitizen`](https://npm.im/commitizen) to assist you with your commit message.
4. Submit a Pull Request

**Working on your first Pull Request?** You can learn how from this *free* series [How to Contribute to an Open Source Project on GitHub](https://egghead.io/series/how-to-contribute-to-an-open-source-project-on-github)
Expand Down
36 changes: 34 additions & 2 deletions __tests__/runtime/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
handleSuccess,
isTwiml,
} from '../../src/runtime/route';
import { EnvironmentVariablesWithAuth } from '../../src/types/generic';
import { wrapErrorInHtml } from '../../src/utils/error-html';

const { VoiceResponse, MessagingResponse, FaxResponse } = twiml;
Expand Down Expand Up @@ -182,13 +183,44 @@ describe('constructContext function', () => {
AUTH_TOKEN: 'xyz',
},
} as StartCliConfig;
const context = constructContext(config);
const context = constructContext(config, '/test');
expect(context.DOMAIN_NAME).toBe('localhost:8000');
expect(context.PATH).toBe('/test');
expect(context.ACCOUNT_SID).toBe('ACxxxxxxxxxxx');
expect(context.AUTH_TOKEN).toBe('xyz');
expect(typeof context.getTwilioClient).toBe('function');
});

test('does not override existing PATH values', () => {
const env: EnvironmentVariablesWithAuth = {
ACCOUNT_SID: 'ACxxxxxxxxxxx',
AUTH_TOKEN: 'xyz',
PATH: '/usr/bin:/bin',
};

const config = {
url: 'http://localhost:8000',
env,
} as StartCliConfig;
const context = constructContext(config, '/test2');
expect(context.PATH).toBe('/usr/bin:/bin');
});

test('does not override existing DOMAIN_NAME values', () => {
const env: EnvironmentVariablesWithAuth = {
ACCOUNT_SID: 'ACxxxxxxxxxxx',
AUTH_TOKEN: 'xyz',
DOMAIN_NAME: 'hello.world',
};

const config = {
url: 'http://localhost:8000',
env,
} as StartCliConfig;
const context = constructContext(config, '/test2');
expect(context.DOMAIN_NAME).toBe('hello.world');
});

test('getTwilioClient calls twilio constructor', () => {
const ACCOUNT_SID = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
const AUTH_TOKEN = 'xyz';
Expand All @@ -197,7 +229,7 @@ describe('constructContext function', () => {
url: 'http://localhost:8000',
env: { ACCOUNT_SID, AUTH_TOKEN },
} as StartCliConfig;
const context = constructContext(config);
const context = constructContext(config, '/test');
const twilioFn = require('twilio');
context.getTwilioClient();
expect(twilioFn).toHaveBeenCalledWith(
Expand Down
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,7 @@
"husky": {
"hooks": {
"pre-commit": "run-s lint-staged test",
"prepare-commit-msg": "exec < /dev/tty && git-cz --hook",
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
"post-checkout": "reset-dependencies",
"post-merge": "reset-dependencies"
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
}
},
"lint-staged": {
Expand Down
15 changes: 9 additions & 6 deletions src/runtime/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ export function constructEvent<T extends {} = {}>(req: ExpressRequest): T {
return { ...req.query, ...req.body };
}

export function constructContext<T extends {} = {}>({
url,
env,
}: StartCliConfig): Context<{
export function constructContext<T extends {} = {}>(
{ url, env }: StartCliConfig,
functionPath: string
): Context<{
ACCOUNT_SID?: string;
AUTH_TOKEN?: string;
DOMAIN_NAME: string;
PATH: string;
[key: string]: string | undefined | Function;
}> {
function getTwilioClient(): twilio.Twilio {
Expand All @@ -43,7 +45,8 @@ export function constructContext<T extends {} = {}>({
return twilio(env.ACCOUNT_SID, env.AUTH_TOKEN);
}
const DOMAIN_NAME = url.replace(/^https?:\/\//, '');
return { ...env, DOMAIN_NAME, getTwilioClient };
const PATH = functionPath;
return { PATH, DOMAIN_NAME, ...env, getTwilioClient };
}

export function constructGlobalScope(config: StartCliConfig): void {
Expand Down Expand Up @@ -129,7 +132,7 @@ export function functionToRoute(
) {
const event = constructEvent(req);
debug('Event for %s: %o', req.path, event);
const context = constructContext(config);
const context = constructContext(config, req.path);
debug('Context for %s: %p', req.path, context);
let run_timings: {
start: [number, number];
Expand Down