From e89d6586dc931d3ad8c2e403c9f9f7aaab1c0ec3 Mon Sep 17 00:00:00 2001 From: Iago Calazans Date: Tue, 14 Jun 2022 17:15:25 -0300 Subject: [PATCH] refactor(*): :art: Change nomenclature from props to env Internal `this` property `props` was not making sense. This commits break this changing from `this.props` to `this.env` inside sub functions. --- README.md | 14 ++++++++------ lib/use.injection.js | 10 +++++----- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 82f480d..7f3a7c5 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ This lib was created with the aim of simplifying the use of serverless Twilio, r The useInjection method takes two parameters. The first to apply as a handler and the last is an object of configuration options. -#### [useInjection] Options +##### [useInjection] Options Can contain providers that will be defined, which act as use cases to perform internal actions in the handler function through the "this" method. @@ -28,7 +28,7 @@ useInjection(yourFunction, ); ``` -#### [Twilio Flex Token Validator](https://github.com/twilio/twilio-flex-token-validator) +##### [Twilio Flex Token Validator](https://github.com/twilio/twilio-flex-token-validator) When using Token Validator, the Request body must contain a valid Token from Twilio. @@ -82,12 +82,14 @@ npm install twilio-functions-utils ## Usage +> **USE CONVENTIONAL FUNCTIONS** ➜ `Arrow functions didn't works as expected as 'this' can't be injected`. + ```js // File: assets/create.private.js exports.create = async function (event) { - // Here you can acess Twilio Client as client and Context as props (so you can get env vars). - const { client, props } = this + // Here you can acess Twilio Client as client and Context as env (so you can get env vars). + const { client, env } = this return new Promise((resolve, reject) => { const random = Math.random(); @@ -113,7 +115,7 @@ const { create } = require(Runtime.getAssets()['/create.js'].path) * request: Record, * cookies: Record, * client: import('twilio').Twilio, - * props: { + * env: { * TWILIO_WORKFLOW_SID: string, * TWILIO_WORKFLOW_SID: string, * DOMAIN_NAME: string @@ -125,7 +127,7 @@ const { create } = require(Runtime.getAssets()['/create.js'].path) */ async function createAction(event) { // You can perform all your "controller" level actions, as you have access to the request headers and cookies. - const { cookies, request, client, props } = this + const { cookies, request, client, env } = this // Then just call the providers you provided to handler by using useInjection. const providerResult = await this.providers.create(event) diff --git a/lib/use.injection.js b/lib/use.injection.js index 64c685b..3e5307a 100644 --- a/lib/use.injection.js +++ b/lib/use.injection.js @@ -23,7 +23,7 @@ const { UnauthorizedError } = require('./errors/unauthorized.error'); /** * @typedef { ActionFn } ControllerFn - * @this { { event: Record, request: Record, cookies: Record, client: import('twilio').Twilio, props: { DOMAIN_NAME: string }, providers: Record} } + * @this { { event: Record, request: Record, cookies: Record, client: import('twilio').Twilio, env: { DOMAIN_NAME: string }, providers: Record} } */ /** @@ -40,12 +40,12 @@ exports.useInjection = (fn, params) => async function (...args) { const { providers, validateToken } = params; const [context, event, callback] = args; - const { getTwilioClient, ...properties } = context; + const { getTwilioClient, ...env } = context; const client = getTwilioClient(); const providerThat = { client, - props: properties, + env, }; const { @@ -58,7 +58,7 @@ exports.useInjection = (fn, params) => request, cookies, client, - props: properties, + env, providers: providerNames.reduce((p, c) => { Reflect.defineProperty( p, c, { @@ -73,7 +73,7 @@ exports.useInjection = (fn, params) => try { if (validateToken) { const validation = await tokenValidator( - Token, context.ACCOUNT_SID, context.AUTH_TOKEN, + Token, env.ACCOUNT_SID, env.AUTH_TOKEN, ); if (!validation.valid) {