@vyriy/handler 0.1.9

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Vyriy contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,318 @@
1
+ # @vyriy/handler
2
+
3
+ Composable AWS Lambda handler chains and wrappers for Vyriy projects.
4
+
5
+ ## Purpose
6
+
7
+ This package provides ready-made Lambda handler chains for common Vyriy workloads and a small set of reusable wrappers for logging, timeouts, smoke checks, development chaos injection, context setup, and error handling.
8
+
9
+ It is designed for projects that want a consistent handler pipeline without repeating the same boilerplate in every Lambda entrypoint.
10
+
11
+ ## Install
12
+
13
+ With npm:
14
+
15
+ ```bash
16
+ npm install @vyriy/handler
17
+ ```
18
+
19
+ With Yarn:
20
+
21
+ ```bash
22
+ yarn add @vyriy/handler
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ Use a prebuilt API Gateway handler chain:
28
+
29
+ ```ts
30
+ import { api } from '@vyriy/handler';
31
+
32
+ export const handler = api(async (event) => ({
33
+ statusCode: 200,
34
+ body: JSON.stringify({
35
+ path: event.path,
36
+ }),
37
+ }));
38
+ ```
39
+
40
+ Use a prebuilt queue or event handler chain:
41
+
42
+ ```ts
43
+ import { sqs } from '@vyriy/handler';
44
+
45
+ export const handler = sqs(async (event) => {
46
+ for (const record of event.Records) {
47
+ console.info(record.body);
48
+ }
49
+ });
50
+ ```
51
+
52
+ Use a prebuilt SNS handler chain:
53
+
54
+ ```ts
55
+ import { sns } from '@vyriy/handler';
56
+
57
+ export const handler = sns(async (event) => {
58
+ for (const record of event.Records) {
59
+ console.info(record.Sns.Message);
60
+ }
61
+ });
62
+ ```
63
+
64
+ Use a prebuilt schedule handler chain:
65
+
66
+ ```ts
67
+ import { schedule } from '@vyriy/handler';
68
+
69
+ export const handler = schedule(async (event) => {
70
+ console.info('Scheduled event:', event['detail-type']);
71
+ });
72
+ ```
73
+
74
+ Compose a custom handler pipeline from individual helpers:
75
+
76
+ ```ts
77
+ import { compose, withChaos, withContext, withError, withLogger, withSmoke, withTimeout } from '@vyriy/handler';
78
+
79
+ export const handler = compose(
80
+ withError({ throwError: true }),
81
+ withLogger(),
82
+ withChaos(),
83
+ withTimeout(),
84
+ withContext(),
85
+ withSmoke(),
86
+ )(async (event) => {
87
+ return {
88
+ ok: true,
89
+ event,
90
+ };
91
+ });
92
+ ```
93
+
94
+ Create a custom wrapper with `factory(...)` and compose it with the built-in helpers:
95
+
96
+ ```ts
97
+ import { compose, factory, withError, withLogger, withTimeout } from '@vyriy/handler';
98
+
99
+ const withRequestId = factory<{ headerName?: string }>(async (handler, args, options = {}) => {
100
+ const [event] = args;
101
+ const headerName = options.headerName ?? 'x-request-id';
102
+ const requestId =
103
+ typeof event === 'object' && event && 'headers' in event
104
+ ? (event.headers as Record<string, string | undefined> | undefined)?.[headerName]
105
+ : undefined;
106
+
107
+ if (requestId) {
108
+ console.info('Request ID:', requestId);
109
+ }
110
+
111
+ return handler(...args);
112
+ });
113
+
114
+ export const handler = compose(
115
+ withError({ throwError: true }),
116
+ withLogger(),
117
+ withTimeout(),
118
+ withRequestId({
119
+ headerName: 'x-request-id',
120
+ }),
121
+ )(async (event) => {
122
+ return {
123
+ ok: true,
124
+ event,
125
+ };
126
+ });
127
+ ```
128
+
129
+ ## Prebuilt Chains
130
+
131
+ - `api`
132
+ API Gateway chain with error handling, logging, timeout handling, context setup, smoke checks, healthcheck handling, default headers, and CORS preflight handling.
133
+
134
+ - `schedule`
135
+ EventBridge schedule chain with logging, timeout handling, context setup, smoke checks, and rethrown errors.
136
+
137
+ - `sns`
138
+ SNS chain with logging, timeout handling, context setup, smoke checks, and rethrown errors.
139
+
140
+ - `sqs`
141
+ SQS chain with logging, timeout handling, context setup, smoke checks, and rethrown errors.
142
+
143
+ ## Wrappers
144
+
145
+ ### `withError(options?)`
146
+
147
+ Catches handler failures, optionally runs an async error handler, and rethrows only when `throwError` is enabled.
148
+
149
+ Options:
150
+
151
+ ```ts
152
+ {
153
+ errorHandler?: (error: unknown) => Promise<void>;
154
+ throwError?: boolean;
155
+ }
156
+ ```
157
+
158
+ - `errorHandler`
159
+ Async callback invoked with the caught error.
160
+
161
+ - `throwError`
162
+ Re-throws the original error after `errorHandler` runs. Defaults to `false`.
163
+
164
+ Example:
165
+
166
+ ```ts
167
+ import { withError } from '@vyriy/handler';
168
+
169
+ export const handler = withError({
170
+ errorHandler: async (error) => {
171
+ console.error('Handler failed:', error);
172
+ },
173
+ throwError: true,
174
+ })(async () => {
175
+ throw new Error('boom');
176
+ });
177
+ ```
178
+
179
+ ### `withLogger(options?)`
180
+
181
+ Logs the incoming event and context, then logs either the result or the thrown error.
182
+
183
+ Options:
184
+
185
+ ```ts
186
+ {
187
+ logger?: typeof console;
188
+ }
189
+ ```
190
+
191
+ - `logger`
192
+ Console-compatible logger implementation. By default the wrapper creates one via `@vyriy/logger`.
193
+
194
+ Example:
195
+
196
+ ```ts
197
+ import { withLogger } from '@vyriy/handler';
198
+
199
+ export const handler = withLogger({
200
+ logger: console,
201
+ })(async (event) => {
202
+ return {
203
+ ok: true,
204
+ event,
205
+ };
206
+ });
207
+ ```
208
+
209
+ ### `withTimeout()`
210
+
211
+ Races the handler against a timeout scheduled one second before the Lambda runtime limit.
212
+
213
+ Example:
214
+
215
+ ```ts
216
+ import { withTimeout } from '@vyriy/handler';
217
+
218
+ export const handler = withTimeout()(async () => {
219
+ await doWork();
220
+ });
221
+ ```
222
+
223
+ ### `withContext()`
224
+
225
+ Sets `context.callbackWaitsForEmptyEventLoop = false` before calling the handler.
226
+
227
+ Example:
228
+
229
+ ```ts
230
+ import { withContext } from '@vyriy/handler';
231
+
232
+ export const handler = withContext()(async (_event, context) => {
233
+ return {
234
+ waitForEmptyLoop: context.callbackWaitsForEmptyEventLoop,
235
+ };
236
+ });
237
+ ```
238
+
239
+ ### `withChaos(options?)`
240
+
241
+ Injects development-only random failures before the wrapped handler runs.
242
+
243
+ Options:
244
+
245
+ ```ts
246
+ {
247
+ enabled?: boolean;
248
+ probability?: number;
249
+ strategy?: 'error' | 'timeout' | 'random';
250
+ timeoutMs?: number;
251
+ error?: unknown;
252
+ }
253
+ ```
254
+
255
+ - `enabled`
256
+ Turns chaos injection on. By default the wrapper reads `CHAOS_ENABLED` from `@vyriy/env`.
257
+
258
+ - `probability`
259
+ Probability from `0` to `1` that a failure is injected. By default the wrapper reads `CHAOS_PROBABILITY`.
260
+
261
+ - `strategy`
262
+ Chooses whether to throw an error, wait and time out, or pick one randomly. Defaults to `'random'`.
263
+
264
+ - `timeoutMs`
265
+ Timeout delay used when the timeout strategy is selected. By default the wrapper reads `CHAOS_TIMEOUT_MS`.
266
+
267
+ - `error`
268
+ Error value normalized through `@vyriy/error` when the error strategy is selected.
269
+
270
+ Example:
271
+
272
+ ```ts
273
+ import { withChaos } from '@vyriy/handler';
274
+
275
+ export const handler = withChaos({
276
+ enabled: true,
277
+ probability: 0.2,
278
+ strategy: 'random',
279
+ timeoutMs: 1500,
280
+ })(async () => {
281
+ return {
282
+ ok: true,
283
+ };
284
+ });
285
+ ```
286
+
287
+ ### `withSmoke()`
288
+
289
+ Returns the smoke response when the incoming event matches the smoke request payload.
290
+
291
+ Example:
292
+
293
+ ```ts
294
+ import { withSmoke } from '@vyriy/handler';
295
+
296
+ export const handler = withSmoke()(async () => {
297
+ return {
298
+ statusCode: 200,
299
+ body: JSON.stringify({
300
+ status: 'runtime',
301
+ }),
302
+ };
303
+ });
304
+ ```
305
+
306
+ ## Types
307
+
308
+ The package also exports shared handler types:
309
+
310
+ ```ts
311
+ import type { Context, Decorator, Handler, Response } from '@vyriy/handler';
312
+ ```
313
+
314
+ ## Notes
315
+
316
+ - `api` includes API-specific wrappers such as healthcheck handling, default headers, and CORS preflight handling
317
+ - `schedule`, `sns`, and `sqs` enable `throwError: true` in `withError(...)`
318
+ - `withSmoke()` delegates matching to `@vyriy/smoke`
package/api.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import type { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
2
+ export declare const api: import("./types.js").Decorator<APIGatewayProxyEvent, APIGatewayProxyResult>;
package/api.js ADDED
@@ -0,0 +1,17 @@
1
+ import { compose } from './compose.js';
2
+ import { withError } from './wrapper/error.js';
3
+ import { withLogger } from './wrapper/logger.js';
4
+ import { withTimeout } from './wrapper/timeout.js';
5
+ import { withContext } from './wrapper/context.js';
6
+ import { withSmoke } from './wrapper/smoke.js';
7
+ import { withHealthcheck } from './wrapper/healthcheck.js';
8
+ import { withHeaders } from './wrapper/headers.js';
9
+ import { withCors } from './wrapper/cors.js';
10
+ import { withChaos } from './wrapper/chaos.js';
11
+ export const api = compose(withError(), withLogger(), withTimeout(), withContext(), withSmoke(), withHealthcheck(), withHeaders({
12
+ 'Access-Control-Allow-Origin': '*',
13
+ 'Access-Control-Allow-Methods': 'GET, POST, PUT, PATCH, DELETE, OPTIONS',
14
+ 'Access-Control-Allow-Headers': 'Content-Type, Authorization, X-Requested-With, X-Api-Key, Accept, User-Agent, X-CSRF-Token',
15
+ 'Content-Type': 'application/json',
16
+ 'X-Robots-Tag': 'noindex, nofollow',
17
+ }), withCors(), withChaos());
package/compose.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import type { Compose } from './types.js';
2
+ export declare const compose: Compose;
package/compose.js ADDED
@@ -0,0 +1 @@
1
+ export const compose = (...fns) => fns.reduceRight((prevDecorator, nextDecorator) => (handler) => nextDecorator(prevDecorator(handler)));
package/factory.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import type { Factory } from './types.js';
2
+ export declare const factory: Factory;
package/factory.js ADDED
@@ -0,0 +1,2 @@
1
+ const createFactory = (wrapper) => (options) => (handler) => async (event, context) => wrapper(handler, [event, context], options);
2
+ export const factory = createFactory;
package/index.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ export * from './api.js';
2
+ export * from './compose.js';
3
+ export * from './factory.js';
4
+ export * from './schedule.js';
5
+ export * from './sns.js';
6
+ export * from './sqs.js';
7
+ export * from './wrapper/context.js';
8
+ export * from './wrapper/cors.js';
9
+ export * from './wrapper/chaos.js';
10
+ export * from './wrapper/error.js';
11
+ export * from './wrapper/headers.js';
12
+ export * from './wrapper/healthcheck.js';
13
+ export * from './wrapper/logger.js';
14
+ export * from './wrapper/smoke.js';
15
+ export * from './wrapper/timeout.js';
16
+ export type * from './types.js';
package/index.js ADDED
@@ -0,0 +1,15 @@
1
+ export * from './api.js';
2
+ export * from './compose.js';
3
+ export * from './factory.js';
4
+ export * from './schedule.js';
5
+ export * from './sns.js';
6
+ export * from './sqs.js';
7
+ export * from './wrapper/context.js';
8
+ export * from './wrapper/cors.js';
9
+ export * from './wrapper/chaos.js';
10
+ export * from './wrapper/error.js';
11
+ export * from './wrapper/headers.js';
12
+ export * from './wrapper/healthcheck.js';
13
+ export * from './wrapper/logger.js';
14
+ export * from './wrapper/smoke.js';
15
+ export * from './wrapper/timeout.js';
package/package.json ADDED
@@ -0,0 +1,184 @@
1
+ {
2
+ "name": "@vyriy/handler",
3
+ "version": "0.1.9",
4
+ "description": "Composable AWS Lambda handler chains and wrappers for Vyriy projects",
5
+ "type": "module",
6
+ "main": "./index.js",
7
+ "dependencies": {
8
+ "@types/aws-lambda": "^8.10.161",
9
+ "@vyriy/chaos": "0.1.9",
10
+ "@vyriy/env": "0.1.9",
11
+ "@vyriy/logger": "0.1.9",
12
+ "@vyriy/smoke": "0.1.9",
13
+ "@vyriy/timeout": "0.1.9"
14
+ },
15
+ "license": "MIT",
16
+ "types": "./index.d.ts",
17
+ "exports": {
18
+ ".": {
19
+ "types": "./index.d.ts",
20
+ "import": "./index.js",
21
+ "default": "./index.js"
22
+ },
23
+ "./api": {
24
+ "types": "./api.d.ts",
25
+ "import": "./api.js",
26
+ "default": "./api.js"
27
+ },
28
+ "./api.js": {
29
+ "types": "./api.d.ts",
30
+ "import": "./api.js",
31
+ "default": "./api.js"
32
+ },
33
+ "./compose": {
34
+ "types": "./compose.d.ts",
35
+ "import": "./compose.js",
36
+ "default": "./compose.js"
37
+ },
38
+ "./compose.js": {
39
+ "types": "./compose.d.ts",
40
+ "import": "./compose.js",
41
+ "default": "./compose.js"
42
+ },
43
+ "./factory": {
44
+ "types": "./factory.d.ts",
45
+ "import": "./factory.js",
46
+ "default": "./factory.js"
47
+ },
48
+ "./factory.js": {
49
+ "types": "./factory.d.ts",
50
+ "import": "./factory.js",
51
+ "default": "./factory.js"
52
+ },
53
+ "./index": {
54
+ "types": "./index.d.ts",
55
+ "import": "./index.js",
56
+ "default": "./index.js"
57
+ },
58
+ "./index.js": {
59
+ "types": "./index.d.ts",
60
+ "import": "./index.js",
61
+ "default": "./index.js"
62
+ },
63
+ "./schedule": {
64
+ "types": "./schedule.d.ts",
65
+ "import": "./schedule.js",
66
+ "default": "./schedule.js"
67
+ },
68
+ "./schedule.js": {
69
+ "types": "./schedule.d.ts",
70
+ "import": "./schedule.js",
71
+ "default": "./schedule.js"
72
+ },
73
+ "./sns": {
74
+ "types": "./sns.d.ts",
75
+ "import": "./sns.js",
76
+ "default": "./sns.js"
77
+ },
78
+ "./sns.js": {
79
+ "types": "./sns.d.ts",
80
+ "import": "./sns.js",
81
+ "default": "./sns.js"
82
+ },
83
+ "./sqs": {
84
+ "types": "./sqs.d.ts",
85
+ "import": "./sqs.js",
86
+ "default": "./sqs.js"
87
+ },
88
+ "./sqs.js": {
89
+ "types": "./sqs.d.ts",
90
+ "import": "./sqs.js",
91
+ "default": "./sqs.js"
92
+ },
93
+ "./wrapper/chaos": {
94
+ "types": "./wrapper/chaos.d.ts",
95
+ "import": "./wrapper/chaos.js",
96
+ "default": "./wrapper/chaos.js"
97
+ },
98
+ "./wrapper/chaos.js": {
99
+ "types": "./wrapper/chaos.d.ts",
100
+ "import": "./wrapper/chaos.js",
101
+ "default": "./wrapper/chaos.js"
102
+ },
103
+ "./wrapper/context": {
104
+ "types": "./wrapper/context.d.ts",
105
+ "import": "./wrapper/context.js",
106
+ "default": "./wrapper/context.js"
107
+ },
108
+ "./wrapper/context.js": {
109
+ "types": "./wrapper/context.d.ts",
110
+ "import": "./wrapper/context.js",
111
+ "default": "./wrapper/context.js"
112
+ },
113
+ "./wrapper/cors": {
114
+ "types": "./wrapper/cors.d.ts",
115
+ "import": "./wrapper/cors.js",
116
+ "default": "./wrapper/cors.js"
117
+ },
118
+ "./wrapper/cors.js": {
119
+ "types": "./wrapper/cors.d.ts",
120
+ "import": "./wrapper/cors.js",
121
+ "default": "./wrapper/cors.js"
122
+ },
123
+ "./wrapper/error": {
124
+ "types": "./wrapper/error.d.ts",
125
+ "import": "./wrapper/error.js",
126
+ "default": "./wrapper/error.js"
127
+ },
128
+ "./wrapper/error.js": {
129
+ "types": "./wrapper/error.d.ts",
130
+ "import": "./wrapper/error.js",
131
+ "default": "./wrapper/error.js"
132
+ },
133
+ "./wrapper/headers": {
134
+ "types": "./wrapper/headers.d.ts",
135
+ "import": "./wrapper/headers.js",
136
+ "default": "./wrapper/headers.js"
137
+ },
138
+ "./wrapper/headers.js": {
139
+ "types": "./wrapper/headers.d.ts",
140
+ "import": "./wrapper/headers.js",
141
+ "default": "./wrapper/headers.js"
142
+ },
143
+ "./wrapper/healthcheck": {
144
+ "types": "./wrapper/healthcheck.d.ts",
145
+ "import": "./wrapper/healthcheck.js",
146
+ "default": "./wrapper/healthcheck.js"
147
+ },
148
+ "./wrapper/healthcheck.js": {
149
+ "types": "./wrapper/healthcheck.d.ts",
150
+ "import": "./wrapper/healthcheck.js",
151
+ "default": "./wrapper/healthcheck.js"
152
+ },
153
+ "./wrapper/logger": {
154
+ "types": "./wrapper/logger.d.ts",
155
+ "import": "./wrapper/logger.js",
156
+ "default": "./wrapper/logger.js"
157
+ },
158
+ "./wrapper/logger.js": {
159
+ "types": "./wrapper/logger.d.ts",
160
+ "import": "./wrapper/logger.js",
161
+ "default": "./wrapper/logger.js"
162
+ },
163
+ "./wrapper/smoke": {
164
+ "types": "./wrapper/smoke.d.ts",
165
+ "import": "./wrapper/smoke.js",
166
+ "default": "./wrapper/smoke.js"
167
+ },
168
+ "./wrapper/smoke.js": {
169
+ "types": "./wrapper/smoke.d.ts",
170
+ "import": "./wrapper/smoke.js",
171
+ "default": "./wrapper/smoke.js"
172
+ },
173
+ "./wrapper/timeout": {
174
+ "types": "./wrapper/timeout.d.ts",
175
+ "import": "./wrapper/timeout.js",
176
+ "default": "./wrapper/timeout.js"
177
+ },
178
+ "./wrapper/timeout.js": {
179
+ "types": "./wrapper/timeout.d.ts",
180
+ "import": "./wrapper/timeout.js",
181
+ "default": "./wrapper/timeout.js"
182
+ }
183
+ }
184
+ }
package/schedule.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import type { ScheduledEvent } from 'aws-lambda';
2
+ export declare const schedule: import("./types.js").Decorator<ScheduledEvent<any>, void>;
package/schedule.js ADDED
@@ -0,0 +1,7 @@
1
+ import { compose } from './compose.js';
2
+ import { withError } from './wrapper/error.js';
3
+ import { withLogger } from './wrapper/logger.js';
4
+ import { withTimeout } from './wrapper/timeout.js';
5
+ import { withContext } from './wrapper/context.js';
6
+ import { withSmoke } from './wrapper/smoke.js';
7
+ export const schedule = compose(withError({ throwError: true }), withLogger(), withTimeout(), withContext(), withSmoke());
package/sns.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import type { SNSEvent } from 'aws-lambda';
2
+ export declare const sns: import("./types.js").Decorator<SNSEvent, void>;
package/sns.js ADDED
@@ -0,0 +1,7 @@
1
+ import { compose } from './compose.js';
2
+ import { withError } from './wrapper/error.js';
3
+ import { withLogger } from './wrapper/logger.js';
4
+ import { withTimeout } from './wrapper/timeout.js';
5
+ import { withContext } from './wrapper/context.js';
6
+ import { withSmoke } from './wrapper/smoke.js';
7
+ export const sns = compose(withError({ throwError: true }), withLogger(), withTimeout(), withContext(), withSmoke());
package/sqs.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import type { SQSEvent } from 'aws-lambda';
2
+ export declare const sqs: import("./types.js").Decorator<SQSEvent, void>;
package/sqs.js ADDED
@@ -0,0 +1,7 @@
1
+ import { compose } from './compose.js';
2
+ import { withError } from './wrapper/error.js';
3
+ import { withLogger } from './wrapper/logger.js';
4
+ import { withTimeout } from './wrapper/timeout.js';
5
+ import { withContext } from './wrapper/context.js';
6
+ import { withSmoke } from './wrapper/smoke.js';
7
+ export const sqs = compose(withError({ throwError: true }), withLogger(), withTimeout(), withContext(), withSmoke());
package/types.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ import type { Context } from 'aws-lambda';
2
+ export type { Context } from 'aws-lambda';
3
+ export type Response<Result> = Promise<Result>;
4
+ export type Handler<Event, Result> = (event: Event, context: Context) => Response<Result>;
5
+ export type Decorator<Event, Result> = (handler: Handler<Event, Result>) => Handler<Event, Result>;
6
+ export type Compose = <Event, Result>(...decorators: Array<Decorator<Event, Result>>) => Decorator<Event, Result>;
7
+ export type Wrapper<Options> = <Event, Result>(handler: Handler<Event, Result>, args: [event: Event, context: Context], options?: Options) => Response<Result>;
8
+ export type TypedWrapper<Event, Result, Options> = (handler: Handler<Event, Result>, args: [event: Event, context: Context], options?: Options) => Response<Result>;
9
+ export type Factory = {
10
+ <Options = undefined>(wrapper: Wrapper<Options>): <Event, Result>(options?: Options) => Decorator<Event, Result>;
11
+ <Options, Event, Result>(wrapper: TypedWrapper<Event, Result, Options>): (options?: Options) => Decorator<Event, Result>;
12
+ };
@@ -0,0 +1,2 @@
1
+ import { type ChaosOptions } from '@vyriy/chaos';
2
+ export declare const withChaos: <Event, Result>(options?: ChaosOptions | undefined) => import("../types.js").Decorator<Event, Result>;
@@ -0,0 +1,31 @@
1
+ import { chaos } from '@vyriy/chaos';
2
+ import { getChaosEnabled, getChaosErrorEnabled, getChaosTimeoutEnabled, getChaosTimeoutMs } from '@vyriy/env';
3
+ import { factory } from '../factory.js';
4
+ const getStrategy = (options) => {
5
+ if (options.strategy) {
6
+ return options.strategy;
7
+ }
8
+ const isErrorEnabled = getChaosErrorEnabled();
9
+ const isTimeoutEnabled = getChaosTimeoutEnabled();
10
+ if (isErrorEnabled && isTimeoutEnabled) {
11
+ return 'random';
12
+ }
13
+ if (isErrorEnabled) {
14
+ return 'error';
15
+ }
16
+ if (isTimeoutEnabled) {
17
+ return 'timeout';
18
+ }
19
+ return 'random';
20
+ };
21
+ export const withChaos = factory(async (handler, args, options = {}) => {
22
+ const enabled = options.enabled ?? getChaosEnabled();
23
+ const strategy = getStrategy(options);
24
+ await chaos({
25
+ ...options,
26
+ enabled: enabled && (strategy !== 'random' || getChaosErrorEnabled() || getChaosTimeoutEnabled()),
27
+ strategy,
28
+ timeoutMs: options.timeoutMs ?? getChaosTimeoutMs(),
29
+ });
30
+ return handler(...args);
31
+ });
@@ -0,0 +1 @@
1
+ export declare const withContext: <Event, Result>(options?: undefined) => import("../types.js").Decorator<Event, Result>;
@@ -0,0 +1,6 @@
1
+ import { factory } from '../factory.js';
2
+ export const withContext = factory(async (handler, args) => {
3
+ const [, ctx] = args;
4
+ ctx.callbackWaitsForEmptyEventLoop = false;
5
+ return handler(...args);
6
+ });
@@ -0,0 +1,2 @@
1
+ import type { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
2
+ export declare const withCors: (options?: undefined) => import("../types.js").Decorator<APIGatewayProxyEvent, APIGatewayProxyResult>;
@@ -0,0 +1,11 @@
1
+ import { factory } from '../factory.js';
2
+ export const withCors = factory(async (handler, args) => {
3
+ const [request] = args;
4
+ if (request.httpMethod === 'OPTIONS') {
5
+ return {
6
+ body: '',
7
+ statusCode: 204,
8
+ };
9
+ }
10
+ return handler(...args);
11
+ });
@@ -0,0 +1,5 @@
1
+ export type ErrorOptions = {
2
+ errorHandler?: (err: unknown) => Promise<void>;
3
+ throwError?: boolean;
4
+ };
5
+ export declare const withError: <Event, Result>(options?: ErrorOptions | undefined) => import("../types.js").Decorator<Event, Result>;
@@ -0,0 +1,17 @@
1
+ import { factory } from '../factory.js';
2
+ export const withError = factory(async (handler, args, options = {}) => {
3
+ const { errorHandler, throwError = false } = options;
4
+ let result;
5
+ try {
6
+ result = await handler(...args);
7
+ }
8
+ catch (err) {
9
+ if (errorHandler) {
10
+ await errorHandler(err);
11
+ }
12
+ if (throwError) {
13
+ throw err;
14
+ }
15
+ }
16
+ return result;
17
+ });
@@ -0,0 +1,2 @@
1
+ import type { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
2
+ export declare const withHeaders: (options?: Record<string, string> | undefined) => import("../types.js").Decorator<APIGatewayProxyEvent, APIGatewayProxyResult>;
@@ -0,0 +1,9 @@
1
+ import { factory } from '../factory.js';
2
+ export const withHeaders = factory(async (handler, args, options = {}) => {
3
+ const result = await handler(...args);
4
+ result.headers = {
5
+ ...options,
6
+ ...result.headers,
7
+ };
8
+ return result;
9
+ });
@@ -0,0 +1,5 @@
1
+ import type { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
2
+ export declare const withHealthcheck: (options?: {
3
+ path?: string;
4
+ action?: () => Promise<void>;
5
+ } | undefined) => import("../types.js").Decorator<APIGatewayProxyEvent, APIGatewayProxyResult>;
@@ -0,0 +1,18 @@
1
+ import { STATUS_CODES } from 'node:http';
2
+ import { factory } from '../factory.js';
3
+ export const withHealthcheck = factory(async (handler, args, options = {}) => {
4
+ const [event] = args;
5
+ const { path = '/healthcheck', action } = options;
6
+ if (event.path === path) {
7
+ if (action) {
8
+ await action();
9
+ }
10
+ return {
11
+ statusCode: 200,
12
+ body: JSON.stringify({
13
+ message: STATUS_CODES[200],
14
+ }),
15
+ };
16
+ }
17
+ return handler(...args);
18
+ });
@@ -0,0 +1,4 @@
1
+ export type LoggerOptions = {
2
+ logger?: typeof console;
3
+ };
4
+ export declare const withLogger: <Event, Result>(options?: LoggerOptions | undefined) => import("../types.js").Decorator<Event, Result>;
@@ -0,0 +1,20 @@
1
+ import { createLogger } from '@vyriy/logger';
2
+ import { factory } from '../factory.js';
3
+ export const withLogger = factory(async (handler, args, options = {}) => {
4
+ const { logger = createLogger() } = options;
5
+ const [event, context] = args;
6
+ logger.info('Event:', event);
7
+ logger.info('Context:', context);
8
+ try {
9
+ const result = await handler(...args);
10
+ logger.info('Result:', result);
11
+ return result;
12
+ }
13
+ catch (error) {
14
+ if (error instanceof Error) {
15
+ logger.error('Error:', error.message);
16
+ }
17
+ logger.error(error);
18
+ throw error;
19
+ }
20
+ });
@@ -0,0 +1,2 @@
1
+ import type { Decorator } from '../types.js';
2
+ export declare const withSmoke: <Event, Result>() => Decorator<Event, Result>;
@@ -0,0 +1,4 @@
1
+ import { smoke } from '@vyriy/smoke';
2
+ import { factory } from '../factory.js';
3
+ const smokeWrapper = async (handler, args) => smoke(args[0]) || handler(...args);
4
+ export const withSmoke = factory(smokeWrapper);
@@ -0,0 +1 @@
1
+ export declare const withTimeout: <Event, Result>(options?: undefined) => import("../types.js").Decorator<Event, Result>;
@@ -0,0 +1,6 @@
1
+ import { timeout as error } from '@vyriy/timeout';
2
+ import { factory } from '../factory.js';
3
+ export const withTimeout = factory(async (handler, args) => Promise.race([
4
+ error(args[1].getRemainingTimeInMillis() - 1000),
5
+ handler(...args),
6
+ ]));