-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.ts
103 lines (89 loc) · 3.04 KB
/
utils.ts
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
93
94
95
96
97
98
99
100
101
102
103
/**
* Miscellaneous utility functions.
* @module
*/
import '@twilio-labs/serverless-runtime-types';
import { ServerlessCallback } from '@twilio-labs/serverless-runtime-types/types';
/**
* Works like NodeJS `require`, but loads code from the requested Twilio Asset
* path.
*
* Usually, non-NPM code libraries included in your Function deployment bundle
* cannot be accessed by their filename; instead, use either a Twilio Asset path
* or a private Function name, depending on whether your code is in the `assets`
* or the `functions` directory.
*
* Usage: `const mylib = requireAsset('mylib');`
*
* @param assetPath The path to the Twilio Asset you want to load as code.
* @returns A NodeJS module.
*/
export function requireAsset(assetPath: string): any {
const assets = Runtime.getAssets();
// Prepend '/' to assetPath if not already present
assetPath = assetPath.startsWith('/') ? assetPath : `/${assetPath}`;
if (assets[assetPath]) {
return require(assets[assetPath].path);
} else if (assets[`${assetPath}.json`]) {
return require(assets[`${assetPath}.js`].path);
} else {
return require(assets[`${assetPath}.json`].path);
}
}
/**
* Works like NodeJS `require`, but loads code from the requested Twilio Function
* name.
*
* Usually, non-NPM code libraries included in your Function deployment bundle
* cannot be accessed by their filename; instead, use either a Twilio Asset path
* or a private Function name, depending on whether your code is in the `assets`
* or the `functions` directory.
*
* Usage: `const mylib = requireFunction('mylib');`
*
* @param functionName The name of the Twilio Function whose code you want to load.
* @returns A NodeJS module.
*/
export function requireFunction(functionName: string): any {
const functions = Runtime.getFunctions();
// If functionName starts with '/', remove it
functionName = functionName.startsWith('/')
? functionName.slice(1)
: functionName;
return require(functions[functionName].path);
}
/**
* Small helper function to cause a Function to emit a successful response.
*
* Usage: `return success(callback, {message: 'Completed successfully'});`
*
* @param callback A Serverless callback.
* @param data An optional JSON object to be included with the response.
*/
export function success(callback: ServerlessCallback, data: {} = {}) {
const response = new Twilio.Response();
response.appendHeader('Content-Type', 'application/json');
response.setBody({
success: true,
...data,
});
return callback(null, response);
}
/**
* Small helper function to cause a Function to emit an error response.
*
* Usage: `return failure(callback, 'An error occurred');`
*
* @param callback A Serverless callback.
* @param reason An error message.
*/
export function failure(callback: ServerlessCallback, reason: string) {
const response = new Twilio.Response();
response.appendHeader('Content-Type', 'application/json');
response.setStatusCode(500);
response.setBody({
success: false,
error: reason,
});
return callback(null, response);
}