-
Notifications
You must be signed in to change notification settings - Fork 1
/
use.injection.js
92 lines (78 loc) · 2.69 KB
/
use.injection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const tokenValidator = require('twilio-flex-token-validator').validator;
const { InternalServerError } = require('./errors/internal-server.error');
const { UnauthorizedError } = require('./errors/unauthorized.error');
/**
* @typedef { (event: Record<string, unknown>) => Promise<unknown> } ActionFn
* @param { Record<string, unknown> } event
* @returns { Promise<unknown> }
*/
/**
* @typedef { (event: Record<string, unknown>) => Promise<unknown> } ProviderFn
* @param { Record<string, unknown> } event
* @returns { Promise<unknown> }
*/
/**
* @typedef { Object } InjectionProviders
* @property { Record<string, ProviderFn> } providers
* @property { boolean } validateToken
*/
/**
* @typedef { ActionFn } ControllerFn
* @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>} }
*/
/**
* @param { ControllerFn } fn
* @param { InjectionProviders } params
* @returns { void }
*/
exports.useInjection = (fn, params) =>
/**
* @param { [ import('@twilio-labs/serverless-runtime-types/types').Context & Record<string, string> , { request: Record<string, unknown>, cookies: Record<string, string> } & Record<string, unknown>, import('@twilio-labs/serverless-runtime-types/types').ServerlessCallback] } args
* @returns { Promise<void> }
*/
// eslint-disable-next-line implicit-arrow-linebreak
async function (...args) {
const { providers, validateToken } = params;
const [context, event, callback] = args;
const { getTwilioClient, ...env } = context;
const client = getTwilioClient();
const providerThat = {
client,
env,
};
const {
request, cookies, Token, ...values
} = event;
const providerNames = Object.keys(providers);
const that = {
request,
cookies,
client,
env,
providers: providerNames.reduce((p, c) => {
Reflect.defineProperty(
p, c, {
value: providers[c].bind(providerThat),
enumerable: true,
},
);
return p;
}, {}),
};
try {
if (validateToken) {
const validation = await tokenValidator(
Token, env.ACCOUNT_SID, env.AUTH_TOKEN,
);
if (!validation.valid) {
return callback(undefined, new UnauthorizedError(validation.message));
}
}
return callback(undefined, await fn.apply(that, [values]));
} catch (err) {
if (typeof err === 'string') {
return callback(undefined, new UnauthorizedError(err));
}
return callback(undefined, new InternalServerError(err.message));
}
};