Skip to content

Commit

Permalink
refactor(*): 🎨 Change nomenclature from props to env
Browse files Browse the repository at this point in the history
Internal `this` property `props` was not making sense. This commits break this changing from `this.props` to `this.env` inside sub functions.
  • Loading branch information
iagocalazans committed Jun 14, 2022
1 parent 46a081e commit e89d658
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
14 changes: 8 additions & 6 deletions README.md
Expand Up @@ -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.

Expand All @@ -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.

Expand Down Expand Up @@ -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();
Expand All @@ -113,7 +115,7 @@ const { create } = require(Runtime.getAssets()['/create.js'].path)
* request: Record<string, unknown>,
* cookies: Record<string, string>,
* client: import('twilio').Twilio,
* props: {
* env: {
* TWILIO_WORKFLOW_SID: string,
* TWILIO_WORKFLOW_SID: string,
* DOMAIN_NAME: string
Expand All @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions lib/use.injection.js
Expand Up @@ -23,7 +23,7 @@ const { UnauthorizedError } = require('./errors/unauthorized.error');

/**
* @typedef { ActionFn } ControllerFn
* @this { { event: Record<string, unknown>, request: Record<string, unknown>, cookies: Record<string, string>, client: import('twilio').Twilio, props: { DOMAIN_NAME: string }, providers: Record<string, AsyncFunction>} }
* @this { { event: Record<string, unknown>, request: Record<string, unknown>, cookies: Record<string, string>, client: import('twilio').Twilio, env: { DOMAIN_NAME: string }, providers: Record<string, AsyncFunction>} }
*/

/**
Expand All @@ -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 {
Expand All @@ -58,7 +58,7 @@ exports.useInjection = (fn, params) =>
request,
cookies,
client,
props: properties,
env,
providers: providerNames.reduce((p, c) => {
Reflect.defineProperty(
p, c, {
Expand All @@ -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) {
Expand Down

0 comments on commit e89d658

Please sign in to comment.