unnbound-events 1.0.20 → 1.0.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/events/src/index.d.ts +9 -1
- package/dist/events/src/index.js +23 -8
- package/dist/events/src/lib/adapters/express.d.ts +12 -10
- package/dist/events/src/lib/adapters/express.js +27 -31
- package/dist/events/src/lib/adapters/sqs.d.ts +27 -27
- package/dist/events/src/lib/adapters/sqs.js +39 -39
- package/dist/events/src/lib/client.js +242 -247
- package/dist/events/src/lib/types.d.ts +45 -45
- package/dist/events/src/lib/types.js +2 -2
- package/dist/lib/client.js +30 -33
- package/dist/logger/src/axios.d.ts +4 -1
- package/dist/logger/src/axios.js +70 -59
- package/dist/logger/src/index.js +50 -14
- package/dist/logger/src/logger.d.ts +18 -12
- package/dist/logger/src/logger.js +41 -39
- package/dist/logger/src/middleware.d.ts +4 -1
- package/dist/logger/src/middleware.js +74 -58
- package/dist/logger/src/span.d.ts +14 -8
- package/dist/logger/src/span.js +35 -28
- package/dist/logger/src/storage.d.ts +2 -2
- package/dist/logger/src/storage.js +3 -3
- package/dist/logger/src/trace.d.ts +8 -2
- package/dist/logger/src/trace.js +12 -5
- package/dist/logger/src/types.d.ts +56 -37
- package/dist/logger/src/types.js +2 -2
- package/dist/logger/src/utils.js +24 -26
- package/package.json +2 -2
|
@@ -1,260 +1,255 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports,
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3
3
|
exports.createEventsClient = createEventsClient;
|
|
4
|
-
const unnbound_logger_sdk_1 = require(
|
|
4
|
+
const unnbound_logger_sdk_1 = require('unnbound-logger-sdk');
|
|
5
5
|
const http = (() => {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
6
|
+
try {
|
|
7
|
+
return require && require('http');
|
|
8
|
+
} catch {
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
12
11
|
})();
|
|
13
12
|
function matchPath(matcher, path) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
13
|
+
if (typeof matcher === 'string') {
|
|
14
|
+
return matcher === path;
|
|
15
|
+
}
|
|
16
|
+
if (matcher instanceof RegExp) {
|
|
17
|
+
return matcher.test(path);
|
|
18
|
+
}
|
|
19
|
+
return matcher(path);
|
|
21
20
|
}
|
|
22
21
|
function createEventsClient() {
|
|
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
|
-
|
|
22
|
+
const routes = [];
|
|
23
|
+
return {
|
|
24
|
+
on(method, matcher, handler) {
|
|
25
|
+
routes.push({ method, matcher, handler });
|
|
26
|
+
},
|
|
27
|
+
get(matcher, handler) {
|
|
28
|
+
routes.push({ method: 'GET', matcher, handler });
|
|
29
|
+
},
|
|
30
|
+
post(matcher, handler) {
|
|
31
|
+
routes.push({ method: 'POST', matcher, handler });
|
|
32
|
+
},
|
|
33
|
+
put(matcher, handler) {
|
|
34
|
+
routes.push({ method: 'PUT', matcher, handler });
|
|
35
|
+
},
|
|
36
|
+
patch(matcher, handler) {
|
|
37
|
+
routes.push({ method: 'PATCH', matcher, handler });
|
|
38
|
+
},
|
|
39
|
+
delete(matcher, handler) {
|
|
40
|
+
routes.push({ method: 'DELETE', matcher, handler });
|
|
41
|
+
},
|
|
42
|
+
options(matcher, handler) {
|
|
43
|
+
routes.push({ method: 'OPTIONS', matcher, handler });
|
|
44
|
+
},
|
|
45
|
+
head(matcher, handler) {
|
|
46
|
+
routes.push({ method: 'HEAD', matcher, handler });
|
|
47
|
+
},
|
|
48
|
+
async handle(request) {
|
|
49
|
+
const route = routes.find(
|
|
50
|
+
(r) => r.method === request.method && matchPath(r.matcher, request.path)
|
|
51
|
+
);
|
|
52
|
+
if (!route) {
|
|
53
|
+
unnbound_logger_sdk_1.logger.warn(
|
|
54
|
+
{ path: request.path, method: request.method },
|
|
55
|
+
'No route match'
|
|
56
|
+
);
|
|
57
|
+
return { status: 404, body: { message: 'Not Found' } };
|
|
58
|
+
}
|
|
59
|
+
try {
|
|
60
|
+
const response = await route.handler(request);
|
|
61
|
+
return response;
|
|
62
|
+
} catch (error) {
|
|
63
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
64
|
+
unnbound_logger_sdk_1.logger.error({ err, path: request.path }, 'Handler error');
|
|
65
|
+
return { status: 500, body: { message: 'Internal Server Error' } };
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
async http(options) {
|
|
69
|
+
const port = options?.port ?? 3000;
|
|
70
|
+
const server = http.createServer((req, res) => {
|
|
71
|
+
const method = (req.method || 'GET').toUpperCase();
|
|
72
|
+
const url = new URL(req.url || '/', 'http://localhost');
|
|
73
|
+
const path = url.pathname;
|
|
74
|
+
const query = {};
|
|
75
|
+
url.searchParams.forEach((value, key) => {
|
|
76
|
+
query[key] = value;
|
|
77
|
+
});
|
|
78
|
+
const headers = {};
|
|
79
|
+
for (const [k, v] of Object.entries(req.headers)) {
|
|
80
|
+
if (typeof v === 'string') headers[k] = v;
|
|
81
|
+
else if (Array.isArray(v)) headers[k] = v.join(',');
|
|
82
|
+
}
|
|
83
|
+
let raw = '';
|
|
84
|
+
req.setEncoding('utf8');
|
|
85
|
+
req.on('data', (chunk) => {
|
|
86
|
+
raw += String(chunk);
|
|
87
|
+
});
|
|
88
|
+
req.on('end', async () => {
|
|
89
|
+
let body = undefined;
|
|
90
|
+
const contentType = headers['content-type'] || headers['Content-Type'];
|
|
91
|
+
if (raw) {
|
|
92
|
+
if (contentType && contentType.includes('application/json')) {
|
|
93
|
+
try {
|
|
94
|
+
body = JSON.parse(raw);
|
|
95
|
+
} catch {
|
|
96
|
+
body = raw;
|
|
97
|
+
}
|
|
98
|
+
} else {
|
|
99
|
+
body = raw;
|
|
63
100
|
}
|
|
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
|
-
headers,
|
|
107
|
-
query,
|
|
108
|
-
body,
|
|
109
|
-
metadata: { source: 'http' },
|
|
110
|
-
};
|
|
111
|
-
const eventRes = await this.handle(eventReq);
|
|
112
|
-
res.statusCode = eventRes.status;
|
|
113
|
-
if (eventRes.headers) {
|
|
114
|
-
for (const [k, v] of Object.entries(eventRes.headers))
|
|
115
|
-
res.setHeader(k, v);
|
|
116
|
-
}
|
|
117
|
-
if (eventRes.body !== undefined) {
|
|
118
|
-
const bodyStr = typeof eventRes.body === 'string' ? eventRes.body : JSON.stringify(eventRes.body);
|
|
119
|
-
res.end(bodyStr);
|
|
120
|
-
}
|
|
121
|
-
else {
|
|
122
|
-
res.end();
|
|
123
|
-
}
|
|
124
|
-
});
|
|
101
|
+
}
|
|
102
|
+
const eventReq = {
|
|
103
|
+
method,
|
|
104
|
+
path,
|
|
105
|
+
headers,
|
|
106
|
+
query,
|
|
107
|
+
body,
|
|
108
|
+
metadata: { source: 'http' },
|
|
109
|
+
};
|
|
110
|
+
const eventRes = await this.handle(eventReq);
|
|
111
|
+
res.statusCode = eventRes.status;
|
|
112
|
+
if (eventRes.headers) {
|
|
113
|
+
for (const [k, v] of Object.entries(eventRes.headers)) res.setHeader(k, v);
|
|
114
|
+
}
|
|
115
|
+
if (eventRes.body !== undefined) {
|
|
116
|
+
const bodyStr =
|
|
117
|
+
typeof eventRes.body === 'string' ? eventRes.body : JSON.stringify(eventRes.body);
|
|
118
|
+
res.end(bodyStr);
|
|
119
|
+
} else {
|
|
120
|
+
res.end();
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
await new Promise((resolve) => server.listen(port, resolve));
|
|
125
|
+
unnbound_logger_sdk_1.logger.info({ port }, 'HTTP server started');
|
|
126
|
+
return {
|
|
127
|
+
close() {
|
|
128
|
+
return new Promise((resolve, reject) => {
|
|
129
|
+
server.close((err) => {
|
|
130
|
+
if (err instanceof Error) {
|
|
131
|
+
reject(err);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
if (typeof err === 'string') {
|
|
135
|
+
reject(new Error(err));
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
if (err != null) {
|
|
139
|
+
reject(new Error('Server close failed'));
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
resolve();
|
|
125
143
|
});
|
|
126
|
-
|
|
127
|
-
unnbound_logger_sdk_1.logger.info({ port }, 'HTTP server started');
|
|
128
|
-
return {
|
|
129
|
-
close() {
|
|
130
|
-
return new Promise((resolve, reject) => {
|
|
131
|
-
server.close((err) => {
|
|
132
|
-
if (err instanceof Error) {
|
|
133
|
-
reject(err);
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
136
|
-
if (typeof err === 'string') {
|
|
137
|
-
reject(new Error(err));
|
|
138
|
-
return;
|
|
139
|
-
}
|
|
140
|
-
if (err != null) {
|
|
141
|
-
reject(new Error('Server close failed'));
|
|
142
|
-
return;
|
|
143
|
-
}
|
|
144
|
-
resolve();
|
|
145
|
-
});
|
|
146
|
-
});
|
|
147
|
-
},
|
|
148
|
-
};
|
|
149
|
-
},
|
|
150
|
-
sqs() {
|
|
151
|
-
return async (event) => {
|
|
152
|
-
let successes = 0;
|
|
153
|
-
const failures = [];
|
|
154
|
-
await Promise.all(event.Records.map(async (record) => {
|
|
155
|
-
try {
|
|
156
|
-
const envelope = JSON.parse(record.body);
|
|
157
|
-
const req = {
|
|
158
|
-
method: envelope.request.method,
|
|
159
|
-
path: envelope.request.path,
|
|
160
|
-
headers: envelope.request.headers,
|
|
161
|
-
query: envelope.request.query,
|
|
162
|
-
body: envelope.request.body,
|
|
163
|
-
metadata: { source: 'sqs', ...envelope.metadata },
|
|
164
|
-
};
|
|
165
|
-
const res = await this.handle(req);
|
|
166
|
-
if (res.status >= 200 && res.status < 300)
|
|
167
|
-
successes += 1;
|
|
168
|
-
else
|
|
169
|
-
failures.push({
|
|
170
|
-
messageId: record.messageId,
|
|
171
|
-
reason: `Handler returned status ${res.status}`,
|
|
172
|
-
});
|
|
173
|
-
}
|
|
174
|
-
catch (error) {
|
|
175
|
-
const err = error instanceof Error ? error : new Error(String(error));
|
|
176
|
-
failures.push({
|
|
177
|
-
messageId: record.messageId,
|
|
178
|
-
reason: err.message ?? 'Unknown error',
|
|
179
|
-
});
|
|
180
|
-
}
|
|
181
|
-
}));
|
|
182
|
-
return { successes, failures };
|
|
183
|
-
};
|
|
144
|
+
});
|
|
184
145
|
},
|
|
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
|
-
QueueUrl: queueUrl,
|
|
217
|
-
MaxNumberOfMessages: maxMessages,
|
|
218
|
-
WaitTimeSeconds: waitTimeSeconds,
|
|
219
|
-
};
|
|
220
|
-
if (visibilityTimeout)
|
|
221
|
-
params.VisibilityTimeout = visibilityTimeout;
|
|
222
|
-
const resp = await sqs.receiveMessage(params).promise();
|
|
223
|
-
const messages = (resp.Messages ?? []).map((m) => ({
|
|
224
|
-
id: m.MessageId ?? '',
|
|
225
|
-
body: String(m.Body ?? ''),
|
|
226
|
-
receiptHandle: String(m.ReceiptHandle ?? ''),
|
|
227
|
-
}));
|
|
228
|
-
if (messages.length === 0)
|
|
229
|
-
continue;
|
|
230
|
-
const result = await consume({
|
|
231
|
-
Records: messages.map((m) => ({ messageId: m.id, body: m.body })),
|
|
232
|
-
});
|
|
233
|
-
const failed = new Set(result.failures.map((f) => f.messageId));
|
|
234
|
-
const toDelete = messages
|
|
235
|
-
.filter((m) => !failed.has(m.id))
|
|
236
|
-
.map((m) => ({ Id: m.id, ReceiptHandle: m.receiptHandle }));
|
|
237
|
-
for (let i = 0; i < toDelete.length; i += 10) {
|
|
238
|
-
const chunk = toDelete.slice(i, i + 10);
|
|
239
|
-
if (chunk.length > 0) {
|
|
240
|
-
await sqs.deleteMessageBatch({ QueueUrl: queueUrl, Entries: chunk }).promise();
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
catch (err) {
|
|
245
|
-
unnbound_logger_sdk_1.logger.error({ err }, 'SQS listen loop error');
|
|
246
|
-
}
|
|
247
|
-
}
|
|
146
|
+
};
|
|
147
|
+
},
|
|
148
|
+
sqs() {
|
|
149
|
+
return async (event) => {
|
|
150
|
+
let successes = 0;
|
|
151
|
+
const failures = [];
|
|
152
|
+
await Promise.all(
|
|
153
|
+
event.Records.map(async (record) => {
|
|
154
|
+
try {
|
|
155
|
+
const envelope = JSON.parse(record.body);
|
|
156
|
+
const req = {
|
|
157
|
+
method: envelope.request.method,
|
|
158
|
+
path: envelope.request.path,
|
|
159
|
+
headers: envelope.request.headers,
|
|
160
|
+
query: envelope.request.query,
|
|
161
|
+
body: envelope.request.body,
|
|
162
|
+
metadata: { source: 'sqs', ...envelope.metadata },
|
|
163
|
+
};
|
|
164
|
+
const res = await this.handle(req);
|
|
165
|
+
if (res.status >= 200 && res.status < 300) successes += 1;
|
|
166
|
+
else
|
|
167
|
+
failures.push({
|
|
168
|
+
messageId: record.messageId,
|
|
169
|
+
reason: `Handler returned status ${res.status}`,
|
|
170
|
+
});
|
|
171
|
+
} catch (error) {
|
|
172
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
173
|
+
failures.push({
|
|
174
|
+
messageId: record.messageId,
|
|
175
|
+
reason: err.message ?? 'Unknown error',
|
|
176
|
+
});
|
|
248
177
|
}
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
178
|
+
})
|
|
179
|
+
);
|
|
180
|
+
return { successes, failures };
|
|
181
|
+
};
|
|
182
|
+
},
|
|
183
|
+
sqsListen(options) {
|
|
184
|
+
const AWS = (() => {
|
|
185
|
+
try {
|
|
186
|
+
return require('aws-sdk');
|
|
187
|
+
} catch {
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
190
|
+
})();
|
|
191
|
+
if (!AWS) {
|
|
192
|
+
throw new Error('aws-sdk is required to use sqsListen');
|
|
193
|
+
}
|
|
194
|
+
const env = globalThis.process?.env ?? {};
|
|
195
|
+
const queueUrl = options?.queueUrl || env.SQS_QUEUE_URL || env.QUEUE_URL;
|
|
196
|
+
if (!queueUrl || queueUrl === undefined) {
|
|
197
|
+
throw new Error(
|
|
198
|
+
'SQS queue URL not provided. Set SQS_QUEUE_URL env or pass options.queueUrl'
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
const region = options?.region || env.AWS_REGION || 'us-east-1';
|
|
202
|
+
const endpoint = env.AWS_SQS_ENDPOINT || env.AWS_ENDPOINT_URL || undefined;
|
|
203
|
+
const waitTimeSeconds = options?.waitTimeSeconds ?? 10;
|
|
204
|
+
const maxMessages = options?.maxMessages ?? 10;
|
|
205
|
+
const visibilityTimeout = options?.visibilityTimeoutSeconds;
|
|
206
|
+
const AWSLib = AWS;
|
|
207
|
+
const sqs = new AWSLib.SQS({ region, endpoint });
|
|
208
|
+
let stopped = false;
|
|
209
|
+
const consume = this.sqs();
|
|
210
|
+
async function loop() {
|
|
211
|
+
while (!stopped) {
|
|
212
|
+
try {
|
|
213
|
+
const params = {
|
|
214
|
+
QueueUrl: queueUrl,
|
|
215
|
+
MaxNumberOfMessages: maxMessages,
|
|
216
|
+
WaitTimeSeconds: waitTimeSeconds,
|
|
217
|
+
};
|
|
218
|
+
if (visibilityTimeout) params.VisibilityTimeout = visibilityTimeout;
|
|
219
|
+
const resp = await sqs.receiveMessage(params).promise();
|
|
220
|
+
const messages = (resp.Messages ?? []).map((m) => ({
|
|
221
|
+
id: m.MessageId ?? '',
|
|
222
|
+
body: String(m.Body ?? ''),
|
|
223
|
+
receiptHandle: String(m.ReceiptHandle ?? ''),
|
|
224
|
+
}));
|
|
225
|
+
if (messages.length === 0) continue;
|
|
226
|
+
const result = await consume({
|
|
227
|
+
Records: messages.map((m) => ({ messageId: m.id, body: m.body })),
|
|
257
228
|
});
|
|
229
|
+
const failed = new Set(result.failures.map((f) => f.messageId));
|
|
230
|
+
const toDelete = messages
|
|
231
|
+
.filter((m) => !failed.has(m.id))
|
|
232
|
+
.map((m) => ({ Id: m.id, ReceiptHandle: m.receiptHandle }));
|
|
233
|
+
for (let i = 0; i < toDelete.length; i += 10) {
|
|
234
|
+
const chunk = toDelete.slice(i, i + 10);
|
|
235
|
+
if (chunk.length > 0) {
|
|
236
|
+
await sqs.deleteMessageBatch({ QueueUrl: queueUrl, Entries: chunk }).promise();
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
} catch (err) {
|
|
240
|
+
unnbound_logger_sdk_1.logger.error({ err }, 'SQS listen loop error');
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
// Start async loop without awaiting
|
|
245
|
+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
246
|
+
loop();
|
|
247
|
+
return Promise.resolve({
|
|
248
|
+
stop() {
|
|
249
|
+
stopped = true;
|
|
250
|
+
return Promise.resolve();
|
|
258
251
|
},
|
|
259
|
-
|
|
252
|
+
});
|
|
253
|
+
},
|
|
254
|
+
};
|
|
260
255
|
}
|
|
@@ -1,60 +1,60 @@
|
|
|
1
1
|
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | 'HEAD';
|
|
2
2
|
export interface EventRequest<Body = unknown, Query = Record<string, unknown>> {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
3
|
+
method: HttpMethod;
|
|
4
|
+
path: string;
|
|
5
|
+
headers: Record<string, string>;
|
|
6
|
+
query: Query;
|
|
7
|
+
body: Body;
|
|
8
|
+
metadata?: Record<string, unknown>;
|
|
9
9
|
}
|
|
10
10
|
export interface EventResponse<Body = unknown> {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
status: number;
|
|
12
|
+
headers?: Record<string, string>;
|
|
13
|
+
body?: Body;
|
|
14
14
|
}
|
|
15
|
-
export type RouteHandler<ReqBody = unknown, ResBody = unknown> = (
|
|
15
|
+
export type RouteHandler<ReqBody = unknown, ResBody = unknown> = (
|
|
16
|
+
request: EventRequest<ReqBody>
|
|
17
|
+
) => Promise<EventResponse<ResBody>> | EventResponse<ResBody>;
|
|
16
18
|
export type RouteMatcher = string | RegExp | ((path: string) => boolean);
|
|
17
19
|
export interface RegisteredRoute {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
20
|
+
method: HttpMethod;
|
|
21
|
+
matcher: RouteMatcher;
|
|
22
|
+
handler: RouteHandler<any, any>;
|
|
21
23
|
}
|
|
22
24
|
export type SqsRecord = {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
+
messageId: string;
|
|
26
|
+
body: string;
|
|
25
27
|
};
|
|
26
28
|
export interface SqsBatchEvent {
|
|
27
|
-
|
|
29
|
+
Records: SqsRecord[];
|
|
28
30
|
}
|
|
29
31
|
export interface EventsClient {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
messageId: string;
|
|
48
|
-
reason: string;
|
|
49
|
-
}>;
|
|
50
|
-
}>;
|
|
51
|
-
sqsListen: (options?: {
|
|
52
|
-
queueUrl?: string;
|
|
53
|
-
region?: string;
|
|
54
|
-
waitTimeSeconds?: number;
|
|
55
|
-
maxMessages?: number;
|
|
56
|
-
visibilityTimeoutSeconds?: number;
|
|
57
|
-
}) => Promise<{
|
|
58
|
-
stop: () => Promise<void>;
|
|
32
|
+
on: (method: HttpMethod, matcher: RouteMatcher, handler: RouteHandler<any, any>) => void;
|
|
33
|
+
handle: (request: EventRequest<any>) => Promise<EventResponse<any>>;
|
|
34
|
+
get: (matcher: RouteMatcher, handler: RouteHandler<any, any>) => void;
|
|
35
|
+
post: (matcher: RouteMatcher, handler: RouteHandler<any, any>) => void;
|
|
36
|
+
put: (matcher: RouteMatcher, handler: RouteHandler<any, any>) => void;
|
|
37
|
+
patch: (matcher: RouteMatcher, handler: RouteHandler<any, any>) => void;
|
|
38
|
+
delete: (matcher: RouteMatcher, handler: RouteHandler<any, any>) => void;
|
|
39
|
+
options: (matcher: RouteMatcher, handler: RouteHandler<any, any>) => void;
|
|
40
|
+
head: (matcher: RouteMatcher, handler: RouteHandler<any, any>) => void;
|
|
41
|
+
http: (options?: { port?: number }) => Promise<{
|
|
42
|
+
close: () => Promise<void>;
|
|
43
|
+
}>;
|
|
44
|
+
sqs: () => (event: SqsBatchEvent) => Promise<{
|
|
45
|
+
successes: number;
|
|
46
|
+
failures: Array<{
|
|
47
|
+
messageId: string;
|
|
48
|
+
reason: string;
|
|
59
49
|
}>;
|
|
50
|
+
}>;
|
|
51
|
+
sqsListen: (options?: {
|
|
52
|
+
queueUrl?: string;
|
|
53
|
+
region?: string;
|
|
54
|
+
waitTimeSeconds?: number;
|
|
55
|
+
maxMessages?: number;
|
|
56
|
+
visibilityTimeoutSeconds?: number;
|
|
57
|
+
}) => Promise<{
|
|
58
|
+
stop: () => Promise<void>;
|
|
59
|
+
}>;
|
|
60
60
|
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports,
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|