Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(twilio-run): upgrading serverless-runtime-types #470

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/nervous-dingos-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'twilio-run': minor
---

Upgraded serverless-runtime-types package and fixed typing errors that arose
2 changes: 1 addition & 1 deletion packages/twilio-run/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"license": "MIT",
"dependencies": {
"@twilio-labs/serverless-api": "^5.5.0",
"@twilio-labs/serverless-runtime-types": "2.1.0-rc.0",
"@twilio-labs/serverless-runtime-types": "^2.2.3",
"@types/express": "4.17.7",
"@types/inquirer": "^6.0.3",
"@types/is-ci": "^2.0.0",
Expand Down
26 changes: 26 additions & 0 deletions packages/twilio-run/src/runtime/internal/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,25 @@ type Headers = {
[key: string]: HeaderValue;
};

type CookieValue = {
value: string;
attributes?: string[] | undefined;
};
type Cookies = {
[key: string]: CookieValue;
};

export class Response implements TwilioResponse {
private body: null | any;
private statusCode: number;
private headers: Headers;
private cookies: Cookies;

constructor(options?: ResponseOptions) {
this.body = null;
this.statusCode = 200;
this.headers = {};
this.cookies = {};

if (options && options.statusCode) {
this.statusCode = options.statusCode;
Expand Down Expand Up @@ -57,6 +67,22 @@ export class Response implements TwilioResponse {
return this;
}

setCookie(
key: string,
value: string,
attributes?: string[] | undefined
): Response {
debug('Setting cookie %s', key, value, attributes);
this.cookies[key] = { value, attributes };
return this;
}

removeCookie(key: string): Response {
debug('Deleting cookie %s', key);
delete this.cookies[key];
return this;
}

appendHeader(key: string, value: HeaderValue): Response {
debug('Appending header for %s', key, value);
this.headers = this.headers || {};
Expand Down
12 changes: 10 additions & 2 deletions packages/twilio-run/src/runtime/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
Context,
ServerlessCallback,
ServerlessEventObject,
ServerlessFunctionSignature,
} from '@twilio-labs/serverless-runtime-types/types';
import { fork } from 'child_process';
Expand Down Expand Up @@ -62,7 +63,14 @@ export function constructContext<T extends {} = {}>(
}
const DOMAIN_NAME = url.replace(/^https?:\/\//, '');
const PATH = functionPath;
return { PATH, DOMAIN_NAME, ...env, getTwilioClient };
return {
PATH,
DOMAIN_NAME,
...env,
getTwilioClient,
SERVICE_SID: env.SERVICE_SID,
ENVIRONMENT_SID: env.ENVIRONMENT_SID,
};
}

export function constructGlobalScope(config: StartCliConfig): void {
Expand Down Expand Up @@ -205,7 +213,7 @@ export function functionToRoute(
res: ExpressResponse,
next: NextFunction
) {
const event = constructEvent(req);
const event = constructEvent<ServerlessEventObject>(req);
debug('Event for %s: %o', req.path, event);
const context = constructContext(config, req.path);
debug('Context for %s: %p', req.path, context);
Expand Down