-
Notifications
You must be signed in to change notification settings - Fork 59
/
types.d.ts
417 lines (404 loc) · 15.1 KB
/
types.d.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import * as twilio from 'twilio';
import { ServiceContext } from 'twilio/lib/rest/sync/v1/service';
import { SyncListListInstance } from 'twilio/lib/rest/sync/v1/service/syncList';
import { SyncMapListInstance } from 'twilio/lib/rest/sync/v1/service/syncMap';
import { TwilioClientOptions } from 'twilio/lib/rest/Twilio';
export type EnvironmentVariables = {
[key: string]: string | undefined;
};
export type ResourceMap = {
[name: string]: {
path: string;
};
};
export type AssetResourceMap = {
[name: string]: {
path: string;
open(): string;
};
};
export interface TwilioResponse {
/**
* Set the status code of the response.
*
* @param code - Integer value of the status code
* @returns This `TwilioResponse` object to enable method chaining
*
* @see https://www.twilio.com/docs/runtime/functions/invocation#twilio-response-methods
*
* Example usage of Response.setStatusCode():
* ```ts
* exports.handler = (context, event, callback) => {
* const response = new Twilio.Response();
* // Set the status code to 204 Not Content
* response.setStatusCode(204);
*
* return callback(null, response);
* };
* ```
*/
setStatusCode(code: number): TwilioResponse;
/**
* Set the body of the response. Takes either a string or an object.
*
* @param body - The body of the response
* @returns This `TwilioResponse` object to enable method chaining
*
* @see https://www.twilio.com/docs/runtime/functions/invocation#twilio-response-methods
*
* Example usage of Response.setBody() with a string
* ```ts
* exports.handler = (context, event, callback) => {
* const response = new Twilio.Response();
* // Set the response body
* response.setBody('Everything is fine');
*
* return callback(null, response);
* };
* ```
*
* Example usage of Response.setBody() with an object
* ```ts
* exports.handler = (context, event, callback) => {
* const response = new Twilio.Response();
* // Set the response body
* response.setBody({ everything: 'is fine' });
*
* return callback(null, response);
* };
* ```
*/
setBody(body: string | object): TwilioResponse;
/**
* Add a header to the HTTP response. The first argument specifies the header name and the second argument the header value.
*
* If Response.appendHeader is called with the name of a header that already exists on this Response object, that header will be converted from a string to an array, and the provided value will be concatenated to that array of values.
*
* @param key - The name of the header
* @param value - The value of the header
* @returns This `TwilioResponse` object to enable method chaining
*
* @see https://www.twilio.com/docs/runtime/functions/headers-and-cookies/setting-and-modifying#responseappendheaderkey-value
*
* Example usage of Response.appendHeader()
* ```ts
* exports.handler = (context, event, callback) => {
* const response = new Twilio.Response();
* response
* .appendHeader('content-type', 'application/json')
* // You can append a multi-value header by passing a list of strings
* .appendHeader('yes', ['no', 'maybe', 'so'])
* // Instead of setting the header to an array, it's also valid to
* // pass a comma-separated string of values
* .appendHeader('cache-control', 'no-store, max-age=0');
* .appendHeader('never', 'gonna')
* // Appending a header that already exists will convert that header to
* // a multi-value header and concatenate the new value
* .appendHeader('never', 'give')
* .appendHeader('never', 'you')
* .appendHeader('never', 'up');
* // The header is now `'never': ['gonna', 'give', 'you', 'up']`
*
* return callback(null, response);
* };
* ```
*/
appendHeader(key: string, value: string): TwilioResponse;
/**
* Set multiple headers on the HTTP response in one method call.
* Accepts an object of key-value pairs of headers and their corresponding values.
* You may also set multi-value headers by making the intended header an array.
*
* @param headers - An object of headers to append to the response. Can include set-cookie
* @returns This `TwilioResponse` object to enable method chaining
*
* @see https://www.twilio.com/docs/runtime/functions/headers-and-cookies/setting-and-modifying#responsesetheadersheaders
*
* Example usage of Response.setHeaders()
* ```ts
* exports.handler = (context, event, callback) => {
* const response = new Twilio.Response();
* response.setHeaders({
* // Set a single header
* 'content-type': 'application/json',
* // You can set a header with multiple values by providing an array
* 'cache-control': ['no-cache', 'private'],
* // You may also optionally set cookies via the "Set-Cookie" key
* 'set-cookie': 'Foo=Bar',
* });
*
* return callback(null, response);
* };
* ```
*/
setHeaders(headers: { [key: string]: string }): TwilioResponse;
/**
* Add a cookie to the HTTP response.
* Accepts the name of the cookie, its value, and any optional attributes to be assigned to the cookie.
*
* @param key - The name of the cookie to be set
* @param value - The value of the cookie
* @param attributes - Optional attributes to assign to the cookie
* @returns This `TwilioResponse` object to enable method chaining
*
* @see https://www.twilio.com/docs/runtime/functions/headers-and-cookies/setting-and-modifying#responsesetcookiekey-value-attributes
*
* Example usage of Response.setCookie()
* ```ts
* exports.handler = (context, event, callback) => {
* const response = new Twilio.Response();
* response
* .setCookie('has_recent_activity', 'true')
* .setCookie('tz', 'America/Los_Angeles', [
* 'HttpOnly',
* 'Secure',
* 'SameSite=Strict',
* 'Max-Age=86400',
* ]);
*
* return callback(null, response);
* };
* ```
*/
setCookie(key: string, value: string, attributes?: string[]): TwilioResponse;
/**
* Effectively remove a specific cookie from the HTTP response.
* Accepts the name of the cookie to be removed, and sets the Max-Age attribute of the cookie equal to 0 so that clients and browsers will remove the cookie upon receiving the response.
*
* @param key - The name of the cookie to be removed
* @returns This `TwilioResponse` object to enable method chaining
*
* @see https://www.twilio.com/docs/runtime/functions/headers-and-cookies/setting-and-modifying#responseremovecookiekey
*
* Example usage of Response.removeCookie()
* ```ts
* exports.handler = (context, event, callback) => {
* const response = new Twilio.Response();
* response.removeCookie('tz');
*
* return callback(null, response);
* };
* ```
*/
removeCookie(key: string): TwilioResponse;
}
export type RuntimeSyncClientOptions = TwilioClientOptions & {
serviceName?: string;
};
export type RuntimeSyncServiceContext = ServiceContext & {
/**
* Alias for `syncMaps`
*
* @see https://www.twilio.com/docs/sync/api/list-resource
*/
maps: SyncMapListInstance;
/**
* Alias for `syncLists`
*
* @see https://www.twilio.com/docs/sync/api/map-resource
*/
lists: SyncListListInstance;
};
export type RuntimeInstance = {
/**
* Returns an object containing the names each private Asset in the same Service as this Function.
* Each Asset name serves as the key to an Asset object that contains the path to that Asset, as well as an open method that can be used to access its contents.
*
* @returns Key-value pairs of Asset names and Asset objects
*
* @see https://www.twilio.com/docs/runtime/client#runtimegetassets
*
* Usage of Runtime.getAssets() to read the file contents of an Asset
* ```ts
* exports.handler = function (context, event, callback) {
* const openFile = Runtime.getAssets()['/my_file.txt'].open;
* // Calling open is equivalent to using fs.readFileSync(asset.filePath, 'utf8')
* const text = openFile();
* console.log('Your file contents: ' + text);
* return callback();
* };
* ```
*
* Usage of Runtime.getAssets() to load a JavaScript module from an Asset
* ```ts
* exports.handler = function (context, event, callback) {
* // First, get the path for the Asset
* const path = Runtime.getAssets()['/answer-generator.js'].path;
* // Next, you can use require() to import the library
* const module = require(path);
* // Finally, use the module as you would any other
* console.log('The answer to your riddle is: ' + module.getAnswer());
* return callback();
* };
* ```
*/
getAssets(): AssetResourceMap;
/**
* Returns an object that contains the names of every Function in the Service.
* Each Function name serves as the key to a Function object that contains the path to that Function.
* These paths can be used to import code from other Functions and to compose code hosted on Twilio Functions.
*
* @returns Key-value pairs of Function names and Function objects
*
* @see https://www.twilio.com/docs/runtime/client#runtimegetfunctions
*
* Usage of Runtime.getFunctions() to import code from another Function.
* Suppose we define a Function titled zoltar:
* ```ts
* exports.ask = () => {
* const fortunes = [...];
* // Generate a random index and return the given fortune
* return fortunes[Math.floor(Math.random() * fortunes.length)];
* };
* ```
*
* You could then use the ask function in another Function:
* ```ts
* exports.handler = function (context, event, callback) {
* // First, get the path for the Function. Note that the key of the function
* // is not preceded by a "/" as is the case with Assets
* const zoltarPath = Runtime.getFunctions()['zoltar'].path;
* // Next, use require() to import the library
* const zoltar = require(zoltarPath);
* // Finally, use the module as you would any other!
* console.log('The answer to your riddle is: ' + zoltar.ask());
* return callback();
* }
* ```
*/
getFunctions(): ResourceMap;
/**
* Returns a Sync Service Context object that has been configured to work with your default Sync Service.
*
* @param options - Optional object to configure the Sync Context, such as the name of a different Sync Service
* @returns A Sync Context object for interacting with Twilio Sync
*
* @see https://www.twilio.com/docs/runtime/client#runtimegetsyncoptions
*
* Usage of Runtime.getSync() to get information about the default Sync instance.
* ```ts
* exports.handler = (context, event, callback) => {
* // Use the getSync method with no arguments to get a reference to the default
* // Sync document for your account. Fetch returns a Promise, which will
* // eventually resolve to metadata about the Sync Service, such as its SID
* Runtime.getSync()
* .fetch()
* .then((defaultSyncService) => {
* console.log('Sync Service SID: ', defaultSyncService.sid);
* return callback(null, defaultSyncService.sid);
* })
* .catch((error) => {
* console.log('Sync Error: ', error);
* return callback(error);
* });
* };
* ```
*
* You can use the Sync Client for many other operations, such as appending item(s) to a Sync List.
* ```ts
* exports.handler = (context, event, callback) => {
* // Given an existing Sync List with the uniqueName of spaceShips, you can use
* // syncListItems.create to append a new data entry which will be accessible
* // to any other Function or product with access to the Sync List
* Runtime.getSync()
* .lists('spaceShips')
* .syncListItems.create({
* data: {
* text: 'Millennium Falcon',
* },
* })
* .then((response) => {
* console.log(response);
* return callback(null, response);
* })
* .catch((error) => {
* console.log('Sync Error: ', error);
* return callback(error);
* });
* };
* ```
*/
getSync(options?: RuntimeSyncClientOptions): RuntimeSyncServiceContext;
};
export type Context<T = {}> = {
/**
* If you have enabled the inclusion of your account credentials in your Function, this will return an initialized Twilio REST Helper Library. If you have not included account credentials in your Function, calling this method will result in an error.
*
* @param options - Optional object to configure the Twilio Client, such as enabling lazy loading
* @returns An initialized Twilio Client
*
* @see https://www.twilio.com/docs/runtime/functions/invocation#helper-methods
*
* Usage of context.getTwilioClient() to get an initialized Twilio Client and send a SMS.
* ```ts
* exports.handler = function (context, event, callback) {
* // Fetch already initialized Twilio REST client
* const twilioClient = context.getTwilioClient();
* // Determine message details from the incoming event, with fallback values
* const from = event.From || '+15095550100';
* const to = event.To || '+15105550101';
* const body = event.Body || 'Ahoy, World!';
*
* twilioClient.messages
* .create({ to, from, body })
* .then((result) => {
* console.log('Created message using callback: ', result.sid);
* return callback();
* })
* .catch((error) => {
* console.error(error);
* return callback(error);
* });
* };
* ```
*/
getTwilioClient(options?: TwilioClientOptions): twilio.Twilio;
/**
* The domain name for the Service that contains this Function.
*/
DOMAIN_NAME: string;
/**
* The path of this Function
*/
PATH: string;
/**
* The unique SID of the Service that contains this Function
*/
SERVICE_SID: string | undefined;
/**
* The unique SID of the Environment that this Function has been deployed to
*/
ENVIRONMENT_SID: string | undefined;
} & T;
export type ServerlessCallback = (
error: null | Error | string | object,
payload?: object | string | number | boolean
) => void;
export type ServerlessEventObject<
RequestBodyAndQuery = {},
Headers = {},
Cookies = {}
> = {
request: {
cookies: Cookies;
headers: Headers;
};
} & RequestBodyAndQuery;
export type ServerlessFunctionSignature<
T extends EnvironmentVariables = {},
U extends ServerlessEventObject = { request: { cookies: {}; headers: {} } }
> = (
context: Context<T>,
event: U,
callback: ServerlessCallback
) => void | Promise<void>;
export type ResponseConstructor = new (...args: any[]) => TwilioResponse;
export type GlobalTwilio = Omit<typeof twilio, 'default'> & {
Response: ResponseConstructor;
};
export { ServiceContext } from 'twilio/lib/rest/sync/v1/service';
export { SyncListListInstance } from 'twilio/lib/rest/sync/v1/service/syncList';
export { SyncMapListInstance } from 'twilio/lib/rest/sync/v1/service/syncMap';
export { TwilioClientOptions } from 'twilio/lib/rest/Twilio';
export type TwilioClient = twilio.Twilio;
export type TwilioPackage = typeof twilio;