-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhandler.ts
More file actions
212 lines (197 loc) · 6.41 KB
/
Copy pathhandler.ts
File metadata and controls
212 lines (197 loc) · 6.41 KB
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
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
/**
* Trigger adapters for multi-source function invocation.
*
* Implements the Ports & Adapters (hexagonal architecture) pattern for
* function invocation. Define business logic once as a `Handler`, then
* bind it to multiple trigger sources (HTTP, queue, CLI, cron) via `Adapter`.
*
* ### Define a Handler
* ```ts
* import * as handler from "@eserstack/functions/handler";
* import * as task from "@eserstack/functions/task";
* import * as results from "@eserstack/primitives/results";
*
* const createOrder: handler.Handler<OrderInput, Order, OrderError, AppCtx> =
* (input) => task.task(async (ctx) => {
* const order = await ctx.db.orders.insert(input);
* return results.ok(order);
* });
* ```
*
* ### Bind to Triggers
* ```ts
* import * as triggers from "@eserstack/functions/triggers";
*
* const fromHttp: handler.Adapter<triggers.HttpEvent, OrderInput> = (event) =>
* event.method === "POST"
* ? results.ok(event.body as OrderInput)
* : results.fail(handler.adaptError("Method not allowed"));
*
* const httpHandler = handler.bind(createOrder, fromHttp);
* const result = await task.runTask(httpHandler(httpEvent), appCtx);
* ```
*
* ### FP Pattern: Contravariant Functor on Input
*
* `bind()` is a contravariant map — it maps over the Handler's input type
* in reverse. The adapter can fail, so the error type naturally widens:
* `E | AdaptError`.
*
* @see {@link "./triggers.ts"} for standard trigger event types
* @see {@link "./task.ts"} for Task and context management
*
* @module
*/
import * as results from "@eserstack/primitives/results";
import { failTask, runTask, type Task } from "./task.ts";
// --- Core Types ---
/**
* A Handler is a function from typed Input to a context-aware Task.
* This is the core business logic — agnostic of how it's triggered.
*
* Analogous to AWS Lambda's handler function, but with typed context.
*
* @template I - Input type
* @template O - Output (success) type
* @template E - Error type
* @template R - Requirements/context type
*/
export type Handler<I, O, E = Error, R = void> = (input: I) => Task<O, E, R>;
/**
* An Adapter transforms a trigger-specific event into the handler's
* expected input type. Returns Result to handle malformed events.
*
* This is the "port" in hexagonal architecture — it translates
* external protocols into internal domain types.
*
* @template TriggerEvent - The trigger-specific event type
* @template I - The handler's expected input type
*/
export type Adapter<TriggerEvent, I> = (
event: TriggerEvent,
) => results.Result<I, AdaptError>;
/**
* AdaptError represents a failure to transform a trigger event
* into the handler's expected input (e.g., missing fields, invalid format).
*/
export type AdaptError = {
readonly _tag: "AdaptError";
readonly message: string;
readonly source?: unknown;
};
/**
* Maps a function's Result to a trigger-specific response format.
*
* @template O - Output (success) type
* @template E - Error type
* @template TriggerResponse - The trigger-specific response type
*/
export type ResponseMapper<O, E, TriggerResponse> = (
result: results.Result<O, E>,
) => TriggerResponse;
/**
* A complete trigger binding — adapts both input and output for a
* specific invocation source. This is the full "port" definition.
*
* @template TriggerEvent - The trigger-specific event type
* @template TriggerResponse - The trigger-specific response type
* @template I - Handler input type
* @template O - Handler output type
* @template E - Handler error type
* @template R - Handler context requirements
*/
export type TriggerBinding<TriggerEvent, TriggerResponse, I, O, E, R> = {
readonly handler: Handler<I, O, E, R>;
readonly adaptInput: Adapter<TriggerEvent, I>;
readonly adaptOutput: ResponseMapper<O, E | AdaptError, TriggerResponse>;
};
// --- Constructors ---
/**
* Create an AdaptError with the given message.
*
* ```ts
* const fromHttp: Adapter<HttpEvent, OrderInput> = (event) =>
* event.method !== "POST"
* ? results.fail(adaptError("Method not allowed"))
* : results.ok(event.body as OrderInput);
* ```
*/
export const adaptError = (
message: string,
source?: unknown,
): AdaptError => ({
_tag: "AdaptError",
message,
source,
});
// --- Composition ---
/**
* Bind an adapter to a handler — creates a new handler that accepts
* the trigger event type directly.
*
* Error type widens: `E | AdaptError` (adapter can fail).
* Context R passes through unchanged.
*
* This is a **contravariant map** on the input side — it maps over the
* Handler's input type in reverse.
*
* ```ts
* const httpHandler = bind(createOrder, fromHttp);
* const result = await runTask(httpHandler(httpEvent), appCtx);
* ```
*
* @param handler - The core business logic handler
* @param adapter - Transforms trigger events into handler input
* @returns A new handler accepting the trigger event type
*/
export const bind = <TriggerEvent, I, O, E, R = void>(
handler: Handler<I, O, E, R>,
adapter: Adapter<TriggerEvent, I>,
): Handler<TriggerEvent, O, E | AdaptError, R> =>
(event: TriggerEvent) => {
const adapted = adapter(event);
if (results.isFail(adapted)) {
return failTask(adapted.error) as Task<O, E | AdaptError, R>;
}
return handler(adapted.value) as Task<O, E | AdaptError, R>;
};
/**
* Create a runnable function from a complete trigger binding.
* Returns an async function that accepts the trigger event and context,
* producing a trigger-specific response.
*
* ```ts
* const handleHttpOrder = createTrigger({
* handler: createOrder,
* adaptInput: fromHttp,
* adaptOutput: toHttpResponse,
* });
*
* const response = await handleHttpOrder(httpEvent, appCtx);
* ```
*
* @param binding - Complete trigger binding (input adapter + handler + output mapper)
* @returns An async function from (event, ctx) → response
*/
export const createTrigger = <
TriggerEvent,
TriggerResponse,
I,
O,
E,
R = void,
>(
binding: TriggerBinding<TriggerEvent, TriggerResponse, I, O, E, R>,
): (
event: TriggerEvent,
...args: R extends void ? [] : [ctx: R]
) => Promise<TriggerResponse> =>
async (event, ...args) => {
const boundHandler = bind(binding.handler, binding.adaptInput);
const result = await runTask(
boundHandler(event),
...args as R extends void ? [] : [ctx: R],
);
return binding.adaptOutput(result);
};