diff --git a/.changeset/nervous-dingos-ring.md b/.changeset/nervous-dingos-ring.md new file mode 100644 index 00000000..187d822b --- /dev/null +++ b/.changeset/nervous-dingos-ring.md @@ -0,0 +1,5 @@ +--- +'twilio-run': minor +--- + +Upgraded serverless-runtime-types package and fixed typing errors that arose diff --git a/packages/twilio-run/package.json b/packages/twilio-run/package.json index 2ced79c0..930fa472 100644 --- a/packages/twilio-run/package.json +++ b/packages/twilio-run/package.json @@ -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", diff --git a/packages/twilio-run/src/runtime/internal/response.ts b/packages/twilio-run/src/runtime/internal/response.ts index f21442e7..9053f18f 100644 --- a/packages/twilio-run/src/runtime/internal/response.ts +++ b/packages/twilio-run/src/runtime/internal/response.ts @@ -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; @@ -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 || {}; diff --git a/packages/twilio-run/src/runtime/route.ts b/packages/twilio-run/src/runtime/route.ts index 6616afaa..760ecc7b 100644 --- a/packages/twilio-run/src/runtime/route.ts +++ b/packages/twilio-run/src/runtime/route.ts @@ -1,6 +1,7 @@ import { Context, ServerlessCallback, + ServerlessEventObject, ServerlessFunctionSignature, } from '@twilio-labs/serverless-runtime-types/types'; import { fork } from 'child_process'; @@ -62,7 +63,14 @@ export function constructContext( } 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 { @@ -205,7 +213,7 @@ export function functionToRoute( res: ExpressResponse, next: NextFunction ) { - const event = constructEvent(req); + const event = constructEvent(req); debug('Event for %s: %o', req.path, event); const context = constructContext(config, req.path); debug('Context for %s: %p', req.path, context);