-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.ts
110 lines (100 loc) · 2.59 KB
/
types.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
import {ConnLogger} from "./logger.ts";
interface Trigger {
class: string;
kind: string;
}
/**
* event is the current event, it contains the following:
* - body: Buffer (*not* string, use event.body.toString())
* - content_type: string
* - trigger:
* - class: string
* - kind: string
* - fields: object of field->value
* - headers: object of header->value
* - id: string
* - method: string
* - path: string
* - size: int
* - timestamp: Date
* - url: string
*/
interface ReqEvent<Body, Fields extends Record<string, any> = {}, Headers extends Record<string, any> = {}> {
body: Body;
content_type: string;
trigger: Trigger;
fields: Fields;
headers: Headers;
id: string;
method: string;
path: string;
size: number;
timestamp: Date;
url: string;
}
/**
* context is current call context, it contains the following:
* callback:
* Callback function, *must* be used to return response.
* Response can be one of:
* - string
* - Buffer
* - array of [status, body]
* - context.Response object
*
* Response:
* A response. Has the following fields
* - body
* - headers
* - content_type
* - status_code
*
* Logging functions:
* - logger.error: function(message)
* - logger.warn: function(message)
* - logger.info: function(message)
* - logger.debug: function(message)
* - logger.errorWith: function(message, with_data)
* - logger.warnWith: function(message, with_data)
* - logger.infoWith: function(message, with_data)
* - logger.debugWith: function(message, with_data)
*/
interface Context {
logger: ConnLogger
}
interface iResponse<Body, Headers extends Record<string, any> = {}> {
body: Body;
headers: Headers;
status_code: number;
}
interface Handler<Body, Headers extends Record<string, any> = {}, Fields extends Record<string, any> = {}> {
run(context: Context, event: ReqEvent<Body, Fields, Headers>): Promise<iResponse<any, any>>;
}
interface HandlerConstructor {
new<Body, Headers extends Record<string, any> = {}, Fields extends Record<string, any> = {}>(): Handler<Body, Headers, Fields>;
readonly prototype: Handler<any>
}
enum MESSAGE_TYPE {
LOG = 'l',
RESPONSE = 'r',
METRIC = 'm',
START = 's',
}
enum LOG_LEVEL {
DEBUG = 'debug',
INFO = 'info',
WARNING = 'warning',
ERROR = 'error',
}
export type {
Trigger,
ReqEvent,
iResponse,
Context,
Handler,
HandlerConstructor
}
export {
MESSAGE_TYPE,
LOG_LEVEL,
}