@jaypie/fabric 0.1.0
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/README.md +677 -0
- package/dist/cjs/commander/FabricCommander.d.ts +94 -0
- package/dist/cjs/commander/createCommanderOptions.d.ts +25 -0
- package/dist/cjs/commander/fabricCommand.d.ts +43 -0
- package/dist/cjs/commander/index.cjs +1487 -0
- package/dist/cjs/commander/index.cjs.map +1 -0
- package/dist/cjs/commander/index.d.ts +6 -0
- package/dist/cjs/commander/parseCommanderOptions.d.ts +32 -0
- package/dist/cjs/commander/registerServiceCommand.d.ts +43 -0
- package/dist/cjs/commander/types.d.ts +107 -0
- package/dist/cjs/constants.d.ts +12 -0
- package/dist/cjs/convert-date.d.ts +47 -0
- package/dist/cjs/convert.d.ts +69 -0
- package/dist/cjs/data/FabricData.d.ts +42 -0
- package/dist/cjs/data/index.cjs +1575 -0
- package/dist/cjs/data/index.cjs.map +1 -0
- package/dist/cjs/data/index.d.ts +5 -0
- package/dist/cjs/data/services/archive.d.ts +8 -0
- package/dist/cjs/data/services/create.d.ts +8 -0
- package/dist/cjs/data/services/delete.d.ts +8 -0
- package/dist/cjs/data/services/execute.d.ts +8 -0
- package/dist/cjs/data/services/index.d.ts +7 -0
- package/dist/cjs/data/services/list.d.ts +8 -0
- package/dist/cjs/data/services/read.d.ts +8 -0
- package/dist/cjs/data/services/update.d.ts +8 -0
- package/dist/cjs/data/transforms.d.ts +80 -0
- package/dist/cjs/data/types.d.ts +190 -0
- package/dist/cjs/express/FabricRouter.d.ts +29 -0
- package/dist/cjs/express/fabricExpress.d.ts +16 -0
- package/dist/cjs/express/index.cjs +505 -0
- package/dist/cjs/express/index.cjs.map +1 -0
- package/dist/cjs/express/index.d.ts +3 -0
- package/dist/cjs/express/types.d.ts +51 -0
- package/dist/cjs/helpers/fallback.d.ts +21 -0
- package/dist/cjs/helpers/index.d.ts +3 -0
- package/dist/cjs/helpers/resolvedName.d.ts +24 -0
- package/dist/cjs/http/FabricHttpServer.d.ts +31 -0
- package/dist/cjs/http/authorization.d.ts +30 -0
- package/dist/cjs/http/cors.d.ts +40 -0
- package/dist/cjs/http/fabricHttp.d.ts +28 -0
- package/dist/cjs/http/httpTransform.d.ts +36 -0
- package/dist/cjs/http/index.cjs +1820 -0
- package/dist/cjs/http/index.cjs.map +1 -0
- package/dist/cjs/http/index.d.ts +10 -0
- package/dist/cjs/http/stream.d.ts +185 -0
- package/dist/cjs/http/types.d.ts +343 -0
- package/dist/cjs/index/index.d.ts +8 -0
- package/dist/cjs/index/keyBuilder.d.ts +81 -0
- package/dist/cjs/index/registry.d.ts +56 -0
- package/dist/cjs/index/types.d.ts +54 -0
- package/dist/cjs/index.cjs +1674 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.ts +18 -0
- package/dist/cjs/lambda/createLambdaService.d.ts +33 -0
- package/dist/cjs/lambda/fabricLambda.d.ts +36 -0
- package/dist/cjs/lambda/index.cjs +967 -0
- package/dist/cjs/lambda/index.cjs.map +1 -0
- package/dist/cjs/lambda/index.d.ts +2 -0
- package/dist/cjs/lambda/types.d.ts +68 -0
- package/dist/cjs/llm/createLlmTool.d.ts +40 -0
- package/dist/cjs/llm/fabricTool.d.ts +40 -0
- package/dist/cjs/llm/index.cjs +1107 -0
- package/dist/cjs/llm/index.cjs.map +1 -0
- package/dist/cjs/llm/index.d.ts +3 -0
- package/dist/cjs/llm/inputToJsonSchema.d.ts +32 -0
- package/dist/cjs/llm/types.d.ts +61 -0
- package/dist/cjs/mcp/fabricMcp.d.ts +38 -0
- package/dist/cjs/mcp/index.cjs +938 -0
- package/dist/cjs/mcp/index.cjs.map +1 -0
- package/dist/cjs/mcp/index.d.ts +2 -0
- package/dist/cjs/mcp/registerMcpTool.d.ts +38 -0
- package/dist/cjs/mcp/types.d.ts +60 -0
- package/dist/cjs/models/base.d.ts +209 -0
- package/dist/cjs/resolve-date.d.ts +47 -0
- package/dist/cjs/resolve.d.ts +69 -0
- package/dist/cjs/resolveService.d.ts +49 -0
- package/dist/cjs/service.d.ts +13 -0
- package/dist/cjs/status.d.ts +30 -0
- package/dist/cjs/types/elementaryTypes.d.ts +84 -0
- package/dist/cjs/types/fieldCategory.d.ts +20 -0
- package/dist/cjs/types/fieldDefinition.d.ts +46 -0
- package/dist/cjs/types/index.d.ts +4 -0
- package/dist/cjs/types.d.ts +56 -0
- package/dist/esm/commander/FabricCommander.d.ts +94 -0
- package/dist/esm/commander/createCommanderOptions.d.ts +25 -0
- package/dist/esm/commander/fabricCommand.d.ts +43 -0
- package/dist/esm/commander/index.d.ts +6 -0
- package/dist/esm/commander/index.js +1482 -0
- package/dist/esm/commander/index.js.map +1 -0
- package/dist/esm/commander/parseCommanderOptions.d.ts +32 -0
- package/dist/esm/commander/registerServiceCommand.d.ts +43 -0
- package/dist/esm/commander/types.d.ts +107 -0
- package/dist/esm/constants.d.ts +12 -0
- package/dist/esm/convert-date.d.ts +47 -0
- package/dist/esm/convert.d.ts +69 -0
- package/dist/esm/data/FabricData.d.ts +42 -0
- package/dist/esm/data/index.d.ts +5 -0
- package/dist/esm/data/index.js +1548 -0
- package/dist/esm/data/index.js.map +1 -0
- package/dist/esm/data/services/archive.d.ts +8 -0
- package/dist/esm/data/services/create.d.ts +8 -0
- package/dist/esm/data/services/delete.d.ts +8 -0
- package/dist/esm/data/services/execute.d.ts +8 -0
- package/dist/esm/data/services/index.d.ts +7 -0
- package/dist/esm/data/services/list.d.ts +8 -0
- package/dist/esm/data/services/read.d.ts +8 -0
- package/dist/esm/data/services/update.d.ts +8 -0
- package/dist/esm/data/transforms.d.ts +80 -0
- package/dist/esm/data/types.d.ts +190 -0
- package/dist/esm/express/FabricRouter.d.ts +29 -0
- package/dist/esm/express/fabricExpress.d.ts +16 -0
- package/dist/esm/express/index.d.ts +3 -0
- package/dist/esm/express/index.js +500 -0
- package/dist/esm/express/index.js.map +1 -0
- package/dist/esm/express/types.d.ts +51 -0
- package/dist/esm/helpers/fallback.d.ts +21 -0
- package/dist/esm/helpers/index.d.ts +3 -0
- package/dist/esm/helpers/resolvedName.d.ts +24 -0
- package/dist/esm/http/FabricHttpServer.d.ts +31 -0
- package/dist/esm/http/authorization.d.ts +30 -0
- package/dist/esm/http/cors.d.ts +40 -0
- package/dist/esm/http/fabricHttp.d.ts +28 -0
- package/dist/esm/http/httpTransform.d.ts +36 -0
- package/dist/esm/http/index.d.ts +10 -0
- package/dist/esm/http/index.js +1775 -0
- package/dist/esm/http/index.js.map +1 -0
- package/dist/esm/http/stream.d.ts +185 -0
- package/dist/esm/http/types.d.ts +343 -0
- package/dist/esm/index/index.d.ts +8 -0
- package/dist/esm/index/keyBuilder.d.ts +81 -0
- package/dist/esm/index/registry.d.ts +56 -0
- package/dist/esm/index/types.d.ts +54 -0
- package/dist/esm/index.d.ts +18 -0
- package/dist/esm/index.js +1606 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/lambda/createLambdaService.d.ts +33 -0
- package/dist/esm/lambda/fabricLambda.d.ts +36 -0
- package/dist/esm/lambda/index.d.ts +2 -0
- package/dist/esm/lambda/index.js +965 -0
- package/dist/esm/lambda/index.js.map +1 -0
- package/dist/esm/lambda/types.d.ts +68 -0
- package/dist/esm/llm/createLlmTool.d.ts +40 -0
- package/dist/esm/llm/fabricTool.d.ts +40 -0
- package/dist/esm/llm/index.d.ts +3 -0
- package/dist/esm/llm/index.js +1104 -0
- package/dist/esm/llm/index.js.map +1 -0
- package/dist/esm/llm/inputToJsonSchema.d.ts +32 -0
- package/dist/esm/llm/types.d.ts +61 -0
- package/dist/esm/mcp/fabricMcp.d.ts +38 -0
- package/dist/esm/mcp/index.d.ts +2 -0
- package/dist/esm/mcp/index.js +936 -0
- package/dist/esm/mcp/index.js.map +1 -0
- package/dist/esm/mcp/registerMcpTool.d.ts +38 -0
- package/dist/esm/mcp/types.d.ts +60 -0
- package/dist/esm/models/base.d.ts +209 -0
- package/dist/esm/resolve-date.d.ts +47 -0
- package/dist/esm/resolve.d.ts +69 -0
- package/dist/esm/resolveService.d.ts +49 -0
- package/dist/esm/service.d.ts +13 -0
- package/dist/esm/status.d.ts +30 -0
- package/dist/esm/types/elementaryTypes.d.ts +84 -0
- package/dist/esm/types/fieldCategory.d.ts +20 -0
- package/dist/esm/types/fieldDefinition.d.ts +46 -0
- package/dist/esm/types/index.d.ts +4 -0
- package/dist/esm/types.d.ts +56 -0
- package/package.json +122 -0
|
@@ -0,0 +1,965 @@
|
|
|
1
|
+
import { getMessages } from '@jaypie/aws';
|
|
2
|
+
import { lambdaHandler } from '@jaypie/lambda';
|
|
3
|
+
import { BadRequestError } from '@jaypie/errors';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Meta-modeling Constants
|
|
7
|
+
*/
|
|
8
|
+
// =============================================================================
|
|
9
|
+
// Constants
|
|
10
|
+
// =============================================================================
|
|
11
|
+
/** Root organizational unit */
|
|
12
|
+
/** Fabric version - used to identify pre-instantiated Services */
|
|
13
|
+
const FABRIC_VERSION = "0.1.0";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Date Type Conversion for @jaypie/fabric
|
|
17
|
+
*
|
|
18
|
+
* Adds Date as a supported type in the fabric type system.
|
|
19
|
+
* Follows the same conversion patterns as String, Number, Boolean.
|
|
20
|
+
*/
|
|
21
|
+
/**
|
|
22
|
+
* Convert a value to a Date
|
|
23
|
+
*
|
|
24
|
+
* Supported inputs:
|
|
25
|
+
* - Date: returned as-is (validated)
|
|
26
|
+
* - Number: treated as Unix timestamp (milliseconds)
|
|
27
|
+
* - String: parsed via Date constructor (ISO 8601, etc.)
|
|
28
|
+
* - Object with value property: unwrapped and converted
|
|
29
|
+
*
|
|
30
|
+
* @throws BadRequestError if value cannot be converted to valid Date
|
|
31
|
+
*/
|
|
32
|
+
function fabricDate(value) {
|
|
33
|
+
// Already a Date
|
|
34
|
+
if (value instanceof Date) {
|
|
35
|
+
if (Number.isNaN(value.getTime())) {
|
|
36
|
+
throw new BadRequestError("Invalid Date value");
|
|
37
|
+
}
|
|
38
|
+
return value;
|
|
39
|
+
}
|
|
40
|
+
// Null/undefined
|
|
41
|
+
if (value === null || value === undefined) {
|
|
42
|
+
throw new BadRequestError("Cannot convert null or undefined to Date");
|
|
43
|
+
}
|
|
44
|
+
// Object with value property (fabric pattern)
|
|
45
|
+
if (typeof value === "object" && value !== null && "value" in value) {
|
|
46
|
+
return fabricDate(value.value);
|
|
47
|
+
}
|
|
48
|
+
// Number (timestamp in milliseconds)
|
|
49
|
+
if (typeof value === "number") {
|
|
50
|
+
if (Number.isNaN(value)) {
|
|
51
|
+
throw new BadRequestError("Cannot convert NaN to Date");
|
|
52
|
+
}
|
|
53
|
+
const date = new Date(value);
|
|
54
|
+
if (Number.isNaN(date.getTime())) {
|
|
55
|
+
throw new BadRequestError(`Cannot convert ${value} to Date`);
|
|
56
|
+
}
|
|
57
|
+
return date;
|
|
58
|
+
}
|
|
59
|
+
// String (ISO 8601 or parseable format)
|
|
60
|
+
if (typeof value === "string") {
|
|
61
|
+
// Empty string is invalid
|
|
62
|
+
if (value.trim() === "") {
|
|
63
|
+
throw new BadRequestError("Cannot convert empty string to Date");
|
|
64
|
+
}
|
|
65
|
+
const date = new Date(value);
|
|
66
|
+
if (Number.isNaN(date.getTime())) {
|
|
67
|
+
throw new BadRequestError(`Cannot convert "${value}" to Date`);
|
|
68
|
+
}
|
|
69
|
+
return date;
|
|
70
|
+
}
|
|
71
|
+
// Boolean cannot be converted to Date
|
|
72
|
+
if (typeof value === "boolean") {
|
|
73
|
+
throw new BadRequestError("Cannot convert boolean to Date");
|
|
74
|
+
}
|
|
75
|
+
// Arrays - attempt single element extraction
|
|
76
|
+
if (Array.isArray(value)) {
|
|
77
|
+
if (value.length === 1) {
|
|
78
|
+
return fabricDate(value[0]);
|
|
79
|
+
}
|
|
80
|
+
throw new BadRequestError(`Cannot convert array with ${value.length} elements to Date`);
|
|
81
|
+
}
|
|
82
|
+
throw new BadRequestError(`Cannot convert ${typeof value} to Date`);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Type guard for Date type in schema definitions
|
|
86
|
+
*/
|
|
87
|
+
function isDateType(type) {
|
|
88
|
+
return type === Date;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Fabric functions for @jaypie/fabric
|
|
92
|
+
/**
|
|
93
|
+
* Try to parse a string as JSON if it looks like JSON
|
|
94
|
+
* Returns the parsed value or the original string if not JSON
|
|
95
|
+
*/
|
|
96
|
+
function tryParseJson(value) {
|
|
97
|
+
const trimmed = value.trim();
|
|
98
|
+
if ((trimmed.startsWith("{") && trimmed.endsWith("}")) ||
|
|
99
|
+
(trimmed.startsWith("[") && trimmed.endsWith("]"))) {
|
|
100
|
+
try {
|
|
101
|
+
return JSON.parse(trimmed);
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
// Not valid JSON, return original
|
|
105
|
+
return value;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return value;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Unwrap arrays and objects to get to the scalar value
|
|
112
|
+
* - Single-element arrays unwrap to their element
|
|
113
|
+
* - Objects with value property unwrap to that value
|
|
114
|
+
* - Recursively unwraps nested structures
|
|
115
|
+
*/
|
|
116
|
+
function unwrapToScalar(value) {
|
|
117
|
+
if (value === undefined || value === null) {
|
|
118
|
+
return value;
|
|
119
|
+
}
|
|
120
|
+
// Unwrap single-element arrays
|
|
121
|
+
if (Array.isArray(value)) {
|
|
122
|
+
if (value.length === 0) {
|
|
123
|
+
return undefined;
|
|
124
|
+
}
|
|
125
|
+
if (value.length === 1) {
|
|
126
|
+
return unwrapToScalar(value[0]);
|
|
127
|
+
}
|
|
128
|
+
throw new BadRequestError("Cannot convert multi-value array to scalar");
|
|
129
|
+
}
|
|
130
|
+
// Unwrap objects with value property
|
|
131
|
+
if (typeof value === "object") {
|
|
132
|
+
const obj = value;
|
|
133
|
+
if ("value" in obj) {
|
|
134
|
+
return unwrapToScalar(obj.value);
|
|
135
|
+
}
|
|
136
|
+
throw new BadRequestError("Object must have a value attribute");
|
|
137
|
+
}
|
|
138
|
+
return value;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Prepare a value for scalar conversion by parsing JSON strings and unwrapping
|
|
142
|
+
*/
|
|
143
|
+
function prepareForScalarConversion(value) {
|
|
144
|
+
if (value === undefined || value === null) {
|
|
145
|
+
return value;
|
|
146
|
+
}
|
|
147
|
+
// Try to parse JSON strings
|
|
148
|
+
if (typeof value === "string") {
|
|
149
|
+
const parsed = tryParseJson(value);
|
|
150
|
+
if (parsed !== value) {
|
|
151
|
+
// Successfully parsed, unwrap the result
|
|
152
|
+
return unwrapToScalar(parsed);
|
|
153
|
+
}
|
|
154
|
+
return value;
|
|
155
|
+
}
|
|
156
|
+
// Unwrap arrays and objects
|
|
157
|
+
if (Array.isArray(value) || typeof value === "object") {
|
|
158
|
+
return unwrapToScalar(value);
|
|
159
|
+
}
|
|
160
|
+
return value;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Convert a value to a boolean
|
|
164
|
+
* - Arrays, objects, and JSON strings are unwrapped first
|
|
165
|
+
* - String "true" becomes true
|
|
166
|
+
* - String "false" becomes false
|
|
167
|
+
* - Strings that parse to numbers: positive = true, zero or negative = false
|
|
168
|
+
* - Numbers: positive = true, zero or negative = false
|
|
169
|
+
* - Boolean passes through
|
|
170
|
+
*/
|
|
171
|
+
function fabricBoolean(value) {
|
|
172
|
+
// Prepare value by parsing JSON and unwrapping arrays/objects
|
|
173
|
+
const prepared = prepareForScalarConversion(value);
|
|
174
|
+
if (prepared === undefined || prepared === null) {
|
|
175
|
+
return undefined;
|
|
176
|
+
}
|
|
177
|
+
if (typeof prepared === "boolean") {
|
|
178
|
+
return prepared;
|
|
179
|
+
}
|
|
180
|
+
if (typeof prepared === "string") {
|
|
181
|
+
if (prepared === "") {
|
|
182
|
+
return undefined;
|
|
183
|
+
}
|
|
184
|
+
const lower = prepared.toLowerCase();
|
|
185
|
+
if (lower === "true") {
|
|
186
|
+
return true;
|
|
187
|
+
}
|
|
188
|
+
if (lower === "false") {
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
// Try to parse as number
|
|
192
|
+
const num = parseFloat(prepared);
|
|
193
|
+
if (isNaN(num)) {
|
|
194
|
+
throw new BadRequestError(`Cannot convert "${prepared}" to Boolean`);
|
|
195
|
+
}
|
|
196
|
+
return num > 0;
|
|
197
|
+
}
|
|
198
|
+
if (typeof prepared === "number") {
|
|
199
|
+
if (isNaN(prepared)) {
|
|
200
|
+
throw new BadRequestError("Cannot convert NaN to Boolean");
|
|
201
|
+
}
|
|
202
|
+
return prepared > 0;
|
|
203
|
+
}
|
|
204
|
+
throw new BadRequestError(`Cannot convert ${typeof prepared} to Boolean`);
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Convert a value to a number
|
|
208
|
+
* - Arrays, objects, and JSON strings are unwrapped first
|
|
209
|
+
* - String "" becomes undefined
|
|
210
|
+
* - String "true" becomes 1
|
|
211
|
+
* - String "false" becomes 0
|
|
212
|
+
* - Strings that parse to numbers use those values
|
|
213
|
+
* - Strings that parse to NaN throw BadRequestError
|
|
214
|
+
* - Boolean true becomes 1, false becomes 0
|
|
215
|
+
* - Number passes through
|
|
216
|
+
*/
|
|
217
|
+
function fabricNumber(value) {
|
|
218
|
+
// Prepare value by parsing JSON and unwrapping arrays/objects
|
|
219
|
+
const prepared = prepareForScalarConversion(value);
|
|
220
|
+
if (prepared === undefined || prepared === null) {
|
|
221
|
+
return undefined;
|
|
222
|
+
}
|
|
223
|
+
if (typeof prepared === "number") {
|
|
224
|
+
if (isNaN(prepared)) {
|
|
225
|
+
throw new BadRequestError("Cannot convert NaN to Number");
|
|
226
|
+
}
|
|
227
|
+
return prepared;
|
|
228
|
+
}
|
|
229
|
+
if (typeof prepared === "boolean") {
|
|
230
|
+
return prepared ? 1 : 0;
|
|
231
|
+
}
|
|
232
|
+
if (typeof prepared === "string") {
|
|
233
|
+
if (prepared === "") {
|
|
234
|
+
return undefined;
|
|
235
|
+
}
|
|
236
|
+
const lower = prepared.toLowerCase();
|
|
237
|
+
if (lower === "true") {
|
|
238
|
+
return 1;
|
|
239
|
+
}
|
|
240
|
+
if (lower === "false") {
|
|
241
|
+
return 0;
|
|
242
|
+
}
|
|
243
|
+
const num = parseFloat(prepared);
|
|
244
|
+
if (isNaN(num)) {
|
|
245
|
+
throw new BadRequestError(`Cannot convert "${prepared}" to Number`);
|
|
246
|
+
}
|
|
247
|
+
return num;
|
|
248
|
+
}
|
|
249
|
+
throw new BadRequestError(`Cannot convert ${typeof prepared} to Number`);
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Convert a value to a string
|
|
253
|
+
* - Arrays, objects, and JSON strings are unwrapped first
|
|
254
|
+
* - String "" becomes undefined
|
|
255
|
+
* - Boolean true becomes "true", false becomes "false"
|
|
256
|
+
* - Number converts to string representation
|
|
257
|
+
* - String passes through
|
|
258
|
+
*/
|
|
259
|
+
function fabricString(value) {
|
|
260
|
+
// Prepare value by parsing JSON and unwrapping arrays/objects
|
|
261
|
+
const prepared = prepareForScalarConversion(value);
|
|
262
|
+
if (prepared === undefined || prepared === null) {
|
|
263
|
+
return undefined;
|
|
264
|
+
}
|
|
265
|
+
if (typeof prepared === "string") {
|
|
266
|
+
if (prepared === "") {
|
|
267
|
+
return undefined;
|
|
268
|
+
}
|
|
269
|
+
return prepared;
|
|
270
|
+
}
|
|
271
|
+
if (typeof prepared === "boolean") {
|
|
272
|
+
return prepared ? "true" : "false";
|
|
273
|
+
}
|
|
274
|
+
if (typeof prepared === "number") {
|
|
275
|
+
if (isNaN(prepared)) {
|
|
276
|
+
throw new BadRequestError("Cannot convert NaN to String");
|
|
277
|
+
}
|
|
278
|
+
return String(prepared);
|
|
279
|
+
}
|
|
280
|
+
throw new BadRequestError(`Cannot convert ${typeof prepared} to String`);
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Convert a value to an array
|
|
284
|
+
* - Non-arrays become arrays containing that value
|
|
285
|
+
* - Arrays of a single value become that value (unwrapped)
|
|
286
|
+
* - Multi-value arrays throw BadRequestError
|
|
287
|
+
* - undefined/null become undefined
|
|
288
|
+
*/
|
|
289
|
+
function fabricArray(value) {
|
|
290
|
+
if (value === undefined || value === null) {
|
|
291
|
+
return undefined;
|
|
292
|
+
}
|
|
293
|
+
if (Array.isArray(value)) {
|
|
294
|
+
// Arrays pass through (single-element unwrapping happens when converting FROM array)
|
|
295
|
+
return value;
|
|
296
|
+
}
|
|
297
|
+
// Non-arrays become single-element arrays
|
|
298
|
+
return [value];
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Convert a value to an object with a value property
|
|
302
|
+
* - Scalars become { value: scalar }
|
|
303
|
+
* - Arrays become { value: array }
|
|
304
|
+
* - Objects with a value attribute pass through
|
|
305
|
+
* - Objects without a value attribute throw BadRequestError
|
|
306
|
+
* - undefined/null become undefined
|
|
307
|
+
*/
|
|
308
|
+
function fabricObject(value) {
|
|
309
|
+
if (value === undefined || value === null) {
|
|
310
|
+
return undefined;
|
|
311
|
+
}
|
|
312
|
+
// Check if already an object (but not an array)
|
|
313
|
+
if (typeof value === "object" && !Array.isArray(value)) {
|
|
314
|
+
const obj = value;
|
|
315
|
+
if ("value" in obj) {
|
|
316
|
+
return obj;
|
|
317
|
+
}
|
|
318
|
+
throw new BadRequestError("Object must have a value attribute");
|
|
319
|
+
}
|
|
320
|
+
// Scalars and arrays become { value: ... }
|
|
321
|
+
return { value };
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* Check if a type is a typed array (e.g., [String], [Number], [], etc.)
|
|
325
|
+
*/
|
|
326
|
+
function isTypedArrayType(type) {
|
|
327
|
+
return Array.isArray(type);
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Split a string on comma or tab delimiters for typed array conversion.
|
|
331
|
+
* Only splits if the string contains commas or tabs.
|
|
332
|
+
* Returns the original value if not a string or no delimiters found.
|
|
333
|
+
*/
|
|
334
|
+
function splitStringForArray(value) {
|
|
335
|
+
if (typeof value !== "string") {
|
|
336
|
+
return value;
|
|
337
|
+
}
|
|
338
|
+
// Check for comma or tab delimiters
|
|
339
|
+
if (value.includes(",")) {
|
|
340
|
+
return value.split(",").map((s) => s.trim());
|
|
341
|
+
}
|
|
342
|
+
if (value.includes("\t")) {
|
|
343
|
+
return value.split("\t").map((s) => s.trim());
|
|
344
|
+
}
|
|
345
|
+
return value;
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Try to parse a string as JSON for array context.
|
|
349
|
+
* Returns parsed value if it's an array, otherwise returns original.
|
|
350
|
+
*/
|
|
351
|
+
function tryParseJsonArray(value) {
|
|
352
|
+
if (typeof value !== "string") {
|
|
353
|
+
return value;
|
|
354
|
+
}
|
|
355
|
+
const trimmed = value.trim();
|
|
356
|
+
if (trimmed.startsWith("[") && trimmed.endsWith("]")) {
|
|
357
|
+
try {
|
|
358
|
+
const parsed = JSON.parse(trimmed);
|
|
359
|
+
if (Array.isArray(parsed)) {
|
|
360
|
+
return parsed;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
catch {
|
|
364
|
+
// Not valid JSON, fall through
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
return value;
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* Get the element type from a typed array type
|
|
371
|
+
* Returns undefined for untyped arrays ([])
|
|
372
|
+
*/
|
|
373
|
+
function getArrayElementType(type) {
|
|
374
|
+
if (type.length === 0) {
|
|
375
|
+
return undefined; // Untyped array
|
|
376
|
+
}
|
|
377
|
+
const elementType = type[0];
|
|
378
|
+
// Handle constructor types
|
|
379
|
+
if (elementType === Boolean)
|
|
380
|
+
return "boolean";
|
|
381
|
+
if (elementType === Number)
|
|
382
|
+
return "number";
|
|
383
|
+
if (elementType === String)
|
|
384
|
+
return "string";
|
|
385
|
+
if (elementType === Object)
|
|
386
|
+
return "object";
|
|
387
|
+
// Handle string types
|
|
388
|
+
if (elementType === "boolean")
|
|
389
|
+
return "boolean";
|
|
390
|
+
if (elementType === "number")
|
|
391
|
+
return "number";
|
|
392
|
+
if (elementType === "string")
|
|
393
|
+
return "string";
|
|
394
|
+
if (elementType === "object")
|
|
395
|
+
return "object";
|
|
396
|
+
// Handle shorthand types
|
|
397
|
+
if (elementType === "")
|
|
398
|
+
return "string"; // "" shorthand for String
|
|
399
|
+
if (typeof elementType === "object" &&
|
|
400
|
+
elementType !== null &&
|
|
401
|
+
Object.keys(elementType).length === 0) {
|
|
402
|
+
return "object"; // {} shorthand for Object
|
|
403
|
+
}
|
|
404
|
+
throw new BadRequestError(`Unknown array element type: ${String(elementType)}`);
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* Convert a value to a typed array
|
|
408
|
+
* - Tries to parse JSON arrays first
|
|
409
|
+
* - Splits strings on comma/tab if present
|
|
410
|
+
* - Wraps non-arrays in an array
|
|
411
|
+
* - Converts each element to the specified element type
|
|
412
|
+
*/
|
|
413
|
+
function fabricTypedArray(value, elementType) {
|
|
414
|
+
// Try to parse JSON array first
|
|
415
|
+
let processed = tryParseJsonArray(value);
|
|
416
|
+
// If still a string, try to split on comma/tab
|
|
417
|
+
processed = splitStringForArray(processed);
|
|
418
|
+
// Convert to array (wraps non-arrays)
|
|
419
|
+
const array = fabricArray(processed);
|
|
420
|
+
if (array === undefined) {
|
|
421
|
+
return undefined;
|
|
422
|
+
}
|
|
423
|
+
// If no element type specified, return as-is
|
|
424
|
+
if (elementType === undefined) {
|
|
425
|
+
return array;
|
|
426
|
+
}
|
|
427
|
+
// Convert each element to the element type
|
|
428
|
+
return array.map((element, index) => {
|
|
429
|
+
try {
|
|
430
|
+
switch (elementType) {
|
|
431
|
+
case "boolean":
|
|
432
|
+
return fabricBoolean(element);
|
|
433
|
+
case "number":
|
|
434
|
+
return fabricNumber(element);
|
|
435
|
+
case "object":
|
|
436
|
+
return fabricObject(element);
|
|
437
|
+
case "string":
|
|
438
|
+
return fabricString(element);
|
|
439
|
+
default:
|
|
440
|
+
throw new BadRequestError(`Unknown element type: ${elementType}`);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
catch (error) {
|
|
444
|
+
if (error instanceof BadRequestError) {
|
|
445
|
+
throw new BadRequestError(`Cannot convert array element at index ${index}: ${error.message}`);
|
|
446
|
+
}
|
|
447
|
+
throw error;
|
|
448
|
+
}
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* Fabric a value to the specified type
|
|
453
|
+
*/
|
|
454
|
+
function fabric(value, type) {
|
|
455
|
+
// Check for Date type first
|
|
456
|
+
if (isDateType(type)) {
|
|
457
|
+
return fabricDate(value);
|
|
458
|
+
}
|
|
459
|
+
// Check for typed array types
|
|
460
|
+
if (isTypedArrayType(type)) {
|
|
461
|
+
const elementType = getArrayElementType(type);
|
|
462
|
+
return fabricTypedArray(value, elementType);
|
|
463
|
+
}
|
|
464
|
+
const normalizedType = normalizeType(type);
|
|
465
|
+
switch (normalizedType) {
|
|
466
|
+
case "array":
|
|
467
|
+
return fabricArray(value);
|
|
468
|
+
case "boolean":
|
|
469
|
+
return fabricBoolean(value);
|
|
470
|
+
case "number":
|
|
471
|
+
return fabricNumber(value);
|
|
472
|
+
case "object":
|
|
473
|
+
return fabricObject(value);
|
|
474
|
+
case "string":
|
|
475
|
+
return fabricString(value);
|
|
476
|
+
default:
|
|
477
|
+
throw new BadRequestError(`Unknown type: ${String(type)}`);
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
/**
|
|
481
|
+
* Normalize type to string representation
|
|
482
|
+
*/
|
|
483
|
+
function normalizeType(type) {
|
|
484
|
+
if (type === Array || type === "array") {
|
|
485
|
+
return "array";
|
|
486
|
+
}
|
|
487
|
+
if (type === Boolean || type === "boolean") {
|
|
488
|
+
return "boolean";
|
|
489
|
+
}
|
|
490
|
+
if (type === Number || type === "number") {
|
|
491
|
+
return "number";
|
|
492
|
+
}
|
|
493
|
+
if (type === Object || type === "object") {
|
|
494
|
+
return "object";
|
|
495
|
+
}
|
|
496
|
+
if (type === String || type === "string") {
|
|
497
|
+
return "string";
|
|
498
|
+
}
|
|
499
|
+
throw new BadRequestError(`Unknown type: ${String(type)}`);
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
// Service for @jaypie/fabric
|
|
503
|
+
/**
|
|
504
|
+
* Check if a single-element array is a typed array type constructor.
|
|
505
|
+
*/
|
|
506
|
+
function isTypedArrayConstructor(element) {
|
|
507
|
+
return (element === Boolean ||
|
|
508
|
+
element === Number ||
|
|
509
|
+
element === String ||
|
|
510
|
+
element === Object ||
|
|
511
|
+
element === "boolean" ||
|
|
512
|
+
element === "number" ||
|
|
513
|
+
element === "string" ||
|
|
514
|
+
element === "object" ||
|
|
515
|
+
element === "" ||
|
|
516
|
+
(typeof element === "object" &&
|
|
517
|
+
element !== null &&
|
|
518
|
+
!(element instanceof RegExp) &&
|
|
519
|
+
Object.keys(element).length === 0));
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* Check if a type is a validated string type (array of string literals and/or RegExp).
|
|
523
|
+
* Distinguishes from typed arrays like [String], [Number], etc.
|
|
524
|
+
*/
|
|
525
|
+
function isValidatedStringType(type) {
|
|
526
|
+
if (!Array.isArray(type)) {
|
|
527
|
+
return false;
|
|
528
|
+
}
|
|
529
|
+
// Empty array is untyped array, not validated string
|
|
530
|
+
if (type.length === 0) {
|
|
531
|
+
return false;
|
|
532
|
+
}
|
|
533
|
+
// Single-element arrays with type constructors are typed arrays
|
|
534
|
+
if (type.length === 1 && isTypedArrayConstructor(type[0])) {
|
|
535
|
+
return false;
|
|
536
|
+
}
|
|
537
|
+
// Check that all elements are strings or RegExp
|
|
538
|
+
return type.every((item) => typeof item === "string" || item instanceof RegExp);
|
|
539
|
+
}
|
|
540
|
+
/**
|
|
541
|
+
* Check if a type is a validated number type (array of number literals).
|
|
542
|
+
* Distinguishes from typed arrays like [Number], etc.
|
|
543
|
+
*/
|
|
544
|
+
function isValidatedNumberType(type) {
|
|
545
|
+
if (!Array.isArray(type)) {
|
|
546
|
+
return false;
|
|
547
|
+
}
|
|
548
|
+
// Empty array is untyped array, not validated number
|
|
549
|
+
if (type.length === 0) {
|
|
550
|
+
return false;
|
|
551
|
+
}
|
|
552
|
+
// Single-element arrays with type constructors are typed arrays
|
|
553
|
+
if (type.length === 1 && isTypedArrayConstructor(type[0])) {
|
|
554
|
+
return false;
|
|
555
|
+
}
|
|
556
|
+
// Check that all elements are numbers
|
|
557
|
+
return type.every((item) => typeof item === "number");
|
|
558
|
+
}
|
|
559
|
+
/**
|
|
560
|
+
* Parse input string as JSON if it's a string
|
|
561
|
+
*/
|
|
562
|
+
function parseInput(input) {
|
|
563
|
+
if (input === undefined || input === null) {
|
|
564
|
+
return {};
|
|
565
|
+
}
|
|
566
|
+
if (typeof input === "string") {
|
|
567
|
+
if (input === "") {
|
|
568
|
+
return {};
|
|
569
|
+
}
|
|
570
|
+
try {
|
|
571
|
+
const parsed = JSON.parse(input);
|
|
572
|
+
if (typeof parsed !== "object" ||
|
|
573
|
+
parsed === null ||
|
|
574
|
+
Array.isArray(parsed)) {
|
|
575
|
+
throw new BadRequestError("Input must be an object");
|
|
576
|
+
}
|
|
577
|
+
return parsed;
|
|
578
|
+
}
|
|
579
|
+
catch (error) {
|
|
580
|
+
if (error instanceof BadRequestError) {
|
|
581
|
+
throw error;
|
|
582
|
+
}
|
|
583
|
+
throw new BadRequestError("Invalid JSON input");
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
if (typeof input === "object" && !Array.isArray(input)) {
|
|
587
|
+
return input;
|
|
588
|
+
}
|
|
589
|
+
throw new BadRequestError("Input must be an object or JSON string");
|
|
590
|
+
}
|
|
591
|
+
/**
|
|
592
|
+
* Run validation on a value (supports async validators)
|
|
593
|
+
*/
|
|
594
|
+
async function runValidation(value, validate, fieldName) {
|
|
595
|
+
if (typeof validate === "function") {
|
|
596
|
+
const result = await validate(value);
|
|
597
|
+
if (result === false) {
|
|
598
|
+
throw new BadRequestError(`Validation failed for field "${fieldName}"`);
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
else if (validate instanceof RegExp) {
|
|
602
|
+
if (typeof value !== "string" || !validate.test(value)) {
|
|
603
|
+
throw new BadRequestError(`Validation failed for field "${fieldName}"`);
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
else if (Array.isArray(validate)) {
|
|
607
|
+
// Check if value matches any item in the array
|
|
608
|
+
for (const item of validate) {
|
|
609
|
+
if (item instanceof RegExp) {
|
|
610
|
+
if (typeof value === "string" && item.test(value)) {
|
|
611
|
+
return; // Match found
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
else if (typeof item === "function") {
|
|
615
|
+
try {
|
|
616
|
+
const result = await item(value);
|
|
617
|
+
if (result !== false) {
|
|
618
|
+
return; // Match found
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
catch {
|
|
622
|
+
// Continue to next item
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
else if (value === item) {
|
|
626
|
+
return; // Scalar match found
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
throw new BadRequestError(`Validation failed for field "${fieldName}"`);
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
/**
|
|
633
|
+
* Check if a field is required
|
|
634
|
+
* A field is required unless it has a default OR required is explicitly false
|
|
635
|
+
*/
|
|
636
|
+
function isFieldRequired(definition) {
|
|
637
|
+
if (definition.required === false) {
|
|
638
|
+
return false;
|
|
639
|
+
}
|
|
640
|
+
if (definition.default !== undefined) {
|
|
641
|
+
return false;
|
|
642
|
+
}
|
|
643
|
+
return true;
|
|
644
|
+
}
|
|
645
|
+
/**
|
|
646
|
+
* Process a single field through conversion and validation
|
|
647
|
+
*/
|
|
648
|
+
async function processField(fieldName, value, definition) {
|
|
649
|
+
// Apply default if value is undefined
|
|
650
|
+
let processedValue = value;
|
|
651
|
+
if (processedValue === undefined && definition.default !== undefined) {
|
|
652
|
+
processedValue = definition.default;
|
|
653
|
+
}
|
|
654
|
+
// Determine actual type and validation
|
|
655
|
+
let actualType = definition.type;
|
|
656
|
+
let validation = definition.validate;
|
|
657
|
+
// Handle bare RegExp shorthand: /regex/
|
|
658
|
+
if (definition.type instanceof RegExp) {
|
|
659
|
+
actualType = String;
|
|
660
|
+
validation = definition.type; // The RegExp becomes the validation
|
|
661
|
+
}
|
|
662
|
+
// Handle validated string shorthand: ["value1", "value2"] or [/regex/]
|
|
663
|
+
else if (isValidatedStringType(definition.type)) {
|
|
664
|
+
actualType = String;
|
|
665
|
+
validation = definition.type; // The array becomes the validation
|
|
666
|
+
}
|
|
667
|
+
// Handle validated number shorthand: [1, 2, 3]
|
|
668
|
+
else if (isValidatedNumberType(definition.type)) {
|
|
669
|
+
actualType = Number;
|
|
670
|
+
validation = definition.type; // The array becomes the validation
|
|
671
|
+
}
|
|
672
|
+
// Fabric to target type
|
|
673
|
+
const convertedValue = fabric(processedValue, actualType);
|
|
674
|
+
// Check if required field is missing
|
|
675
|
+
if (convertedValue === undefined && isFieldRequired(definition)) {
|
|
676
|
+
throw new BadRequestError(`Missing required field "${fieldName}"`);
|
|
677
|
+
}
|
|
678
|
+
// Run validation if provided
|
|
679
|
+
if (validation !== undefined && convertedValue !== undefined) {
|
|
680
|
+
await runValidation(convertedValue, validation, fieldName);
|
|
681
|
+
}
|
|
682
|
+
return convertedValue;
|
|
683
|
+
}
|
|
684
|
+
/**
|
|
685
|
+
* Fabric a service function
|
|
686
|
+
*
|
|
687
|
+
* Service builds a function that initiates a "controller" step that:
|
|
688
|
+
* - Parses the input if it is a string to object
|
|
689
|
+
* - Fabrics each input field to its type
|
|
690
|
+
* - Calls the validation function or regular expression or checks the array
|
|
691
|
+
* - Calls the service function and returns the response
|
|
692
|
+
*
|
|
693
|
+
* The returned function has config properties for introspection.
|
|
694
|
+
*/
|
|
695
|
+
function fabricService(config) {
|
|
696
|
+
const { input: inputDefinitions, service } = config;
|
|
697
|
+
const handler = async (rawInput, context) => {
|
|
698
|
+
// Parse input (handles string JSON)
|
|
699
|
+
const parsedInput = parseInput(rawInput);
|
|
700
|
+
// If no input definitions, pass through to service or return parsed input
|
|
701
|
+
if (!inputDefinitions) {
|
|
702
|
+
if (service) {
|
|
703
|
+
return service(parsedInput, context);
|
|
704
|
+
}
|
|
705
|
+
return parsedInput;
|
|
706
|
+
}
|
|
707
|
+
// Process all fields in parallel
|
|
708
|
+
const entries = Object.entries(inputDefinitions);
|
|
709
|
+
const processedValues = await Promise.all(entries.map(([fieldName, definition]) => processField(fieldName, parsedInput[fieldName], definition)));
|
|
710
|
+
// Build processed input object
|
|
711
|
+
const processedInput = {};
|
|
712
|
+
entries.forEach(([fieldName], index) => {
|
|
713
|
+
processedInput[fieldName] = processedValues[index];
|
|
714
|
+
});
|
|
715
|
+
// Return processed input if no service, otherwise call service
|
|
716
|
+
if (service) {
|
|
717
|
+
return service(processedInput, context);
|
|
718
|
+
}
|
|
719
|
+
return processedInput;
|
|
720
|
+
};
|
|
721
|
+
// Attach config properties directly to handler for flat access
|
|
722
|
+
const typedHandler = handler;
|
|
723
|
+
typedHandler.$fabric = FABRIC_VERSION;
|
|
724
|
+
if (config.alias !== undefined)
|
|
725
|
+
typedHandler.alias = config.alias;
|
|
726
|
+
if (config.description !== undefined)
|
|
727
|
+
typedHandler.description = config.description;
|
|
728
|
+
if (config.input !== undefined)
|
|
729
|
+
typedHandler.input = config.input;
|
|
730
|
+
if (config.service !== undefined)
|
|
731
|
+
typedHandler.service = config.service;
|
|
732
|
+
return typedHandler;
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
// Resolve inline service definitions to full Service objects
|
|
736
|
+
/**
|
|
737
|
+
* Type guard to check if a value is a pre-instantiated Service
|
|
738
|
+
* A Service is a function with the `$fabric` property set by fabricService
|
|
739
|
+
*/
|
|
740
|
+
function isService$1(value) {
|
|
741
|
+
return typeof value === "function" && "$fabric" in value;
|
|
742
|
+
}
|
|
743
|
+
/**
|
|
744
|
+
* Resolve a service configuration to a full Service object
|
|
745
|
+
*
|
|
746
|
+
* Supports two patterns:
|
|
747
|
+
* 1. Inline service definition - pass a plain function as `service` along with
|
|
748
|
+
* `alias`, `description`, and `input` in the config
|
|
749
|
+
* 2. Pre-instantiated Service - pass a Service object as `service`
|
|
750
|
+
*
|
|
751
|
+
* When a pre-instantiated Service is passed, config fields act as overrides:
|
|
752
|
+
* - `alias` overrides service.alias
|
|
753
|
+
* - `description` overrides service.description
|
|
754
|
+
* - `input` overrides service.input
|
|
755
|
+
*
|
|
756
|
+
* The original Service is never mutated - a new Service is created when overrides
|
|
757
|
+
* are applied.
|
|
758
|
+
*
|
|
759
|
+
* @example
|
|
760
|
+
* ```typescript
|
|
761
|
+
* // Inline service definition
|
|
762
|
+
* const service = resolveService({
|
|
763
|
+
* alias: "greet",
|
|
764
|
+
* description: "Greet a user",
|
|
765
|
+
* input: { name: { type: String } },
|
|
766
|
+
* service: ({ name }) => `Hello, ${name}!`,
|
|
767
|
+
* });
|
|
768
|
+
*
|
|
769
|
+
* // Pre-instantiated with override
|
|
770
|
+
* const baseService = fabricService({ alias: "foo", service: (x) => x });
|
|
771
|
+
* const overridden = resolveService({
|
|
772
|
+
* alias: "bar", // Override alias
|
|
773
|
+
* service: baseService,
|
|
774
|
+
* });
|
|
775
|
+
* ```
|
|
776
|
+
*/
|
|
777
|
+
function resolveService(config) {
|
|
778
|
+
const { alias, description, input, service } = config;
|
|
779
|
+
if (isService$1(service)) {
|
|
780
|
+
// Service is pre-instantiated - config fields act as overrides
|
|
781
|
+
// Create new Service with merged properties (config overrides service)
|
|
782
|
+
return fabricService({
|
|
783
|
+
alias: alias ?? service.alias,
|
|
784
|
+
description: description ?? service.description,
|
|
785
|
+
input: input ?? service.input,
|
|
786
|
+
service: service.service,
|
|
787
|
+
});
|
|
788
|
+
}
|
|
789
|
+
// Service is an inline function - create Service from config
|
|
790
|
+
return fabricService({
|
|
791
|
+
alias,
|
|
792
|
+
description,
|
|
793
|
+
input,
|
|
794
|
+
service,
|
|
795
|
+
});
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
// Lambda Service adapter for @jaypie/fabric
|
|
799
|
+
/**
|
|
800
|
+
* Type guard to check if a value is a pre-instantiated Service
|
|
801
|
+
* A Service is a function with the `$fabric` property set by fabricService
|
|
802
|
+
*/
|
|
803
|
+
function isService(value) {
|
|
804
|
+
return typeof value === "function" && "$fabric" in value;
|
|
805
|
+
}
|
|
806
|
+
/**
|
|
807
|
+
* Type guard to check if a value is a config object (has service property)
|
|
808
|
+
*/
|
|
809
|
+
function isConfig(value) {
|
|
810
|
+
return (typeof value === "object" &&
|
|
811
|
+
value !== null &&
|
|
812
|
+
"service" in value &&
|
|
813
|
+
typeof value.service === "function");
|
|
814
|
+
}
|
|
815
|
+
/**
|
|
816
|
+
* Fabric a Lambda handler that wraps a service
|
|
817
|
+
*
|
|
818
|
+
* This function creates a Lambda-compatible handler that:
|
|
819
|
+
* - Uses getMessages() to extract messages from SQS/SNS events
|
|
820
|
+
* - Calls the service once for each message
|
|
821
|
+
* - Returns the single response if one message, or an array of responses if multiple
|
|
822
|
+
* - Integrates with lambdaHandler for lifecycle management (secrets, setup, teardown, etc.)
|
|
823
|
+
*
|
|
824
|
+
* @param serviceOrConfig - The service function or configuration object
|
|
825
|
+
* @param options - Lambda handler options (secrets, setup, teardown, etc.)
|
|
826
|
+
* @returns A Lambda handler function
|
|
827
|
+
*
|
|
828
|
+
* @example
|
|
829
|
+
* ```typescript
|
|
830
|
+
* import { fabricLambda } from "@jaypie/fabric/lambda";
|
|
831
|
+
* import { myService } from "./services";
|
|
832
|
+
*
|
|
833
|
+
* // Direct service style
|
|
834
|
+
* export const handler = fabricLambda(myService);
|
|
835
|
+
*
|
|
836
|
+
* // Config object style
|
|
837
|
+
* export const handler2 = fabricLambda({
|
|
838
|
+
* service: myService,
|
|
839
|
+
* secrets: ["ANTHROPIC_API_KEY", "OPENAI_API_KEY"],
|
|
840
|
+
* });
|
|
841
|
+
*
|
|
842
|
+
* // Service with options style
|
|
843
|
+
* export const handler3 = fabricLambda(myService, {
|
|
844
|
+
* secrets: ["ANTHROPIC_API_KEY"],
|
|
845
|
+
* });
|
|
846
|
+
* ```
|
|
847
|
+
*/
|
|
848
|
+
function fabricLambda(serviceOrConfig, options = {}) {
|
|
849
|
+
// Normalize arguments and resolve service
|
|
850
|
+
let service;
|
|
851
|
+
let opts;
|
|
852
|
+
if (isConfig(serviceOrConfig)) {
|
|
853
|
+
// Config object with service property
|
|
854
|
+
const { alias, description, input, service: configService, ...configOpts } = serviceOrConfig;
|
|
855
|
+
// Resolve inline service or apply overrides
|
|
856
|
+
service = resolveService({
|
|
857
|
+
alias,
|
|
858
|
+
description,
|
|
859
|
+
input,
|
|
860
|
+
service: configService,
|
|
861
|
+
});
|
|
862
|
+
opts = configOpts;
|
|
863
|
+
}
|
|
864
|
+
else if (isService(serviceOrConfig)) {
|
|
865
|
+
// Pre-instantiated Service passed directly
|
|
866
|
+
service = serviceOrConfig;
|
|
867
|
+
opts = options;
|
|
868
|
+
}
|
|
869
|
+
else {
|
|
870
|
+
// Plain function passed directly - wrap it
|
|
871
|
+
service = resolveService({ service: serviceOrConfig });
|
|
872
|
+
opts = options;
|
|
873
|
+
}
|
|
874
|
+
// Use service.alias as the name for logging (can be overridden via options.name)
|
|
875
|
+
const name = opts.name ?? service.alias;
|
|
876
|
+
// Create context callbacks that wrap the registration callbacks with error swallowing
|
|
877
|
+
// Callback failures should never halt service execution
|
|
878
|
+
const sendMessage = opts.onMessage
|
|
879
|
+
? async (message) => {
|
|
880
|
+
try {
|
|
881
|
+
await opts.onMessage(message);
|
|
882
|
+
}
|
|
883
|
+
catch {
|
|
884
|
+
// Swallow errors - callback failures should not halt execution
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
: undefined;
|
|
888
|
+
const contextOnError = opts.onError
|
|
889
|
+
? async (error) => {
|
|
890
|
+
try {
|
|
891
|
+
await opts.onError(error);
|
|
892
|
+
}
|
|
893
|
+
catch {
|
|
894
|
+
// Swallow errors - callback failures should not halt execution
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
: undefined;
|
|
898
|
+
const contextOnFatal = opts.onFatal
|
|
899
|
+
? async (error) => {
|
|
900
|
+
try {
|
|
901
|
+
await opts.onFatal(error);
|
|
902
|
+
}
|
|
903
|
+
catch {
|
|
904
|
+
// Swallow errors - callback failures should not halt execution
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
: undefined;
|
|
908
|
+
// Create context for the service
|
|
909
|
+
const context = {
|
|
910
|
+
onError: contextOnError,
|
|
911
|
+
onFatal: contextOnFatal,
|
|
912
|
+
sendMessage,
|
|
913
|
+
};
|
|
914
|
+
// Create the inner Lambda handler logic (context param required for lambdaHandler signature)
|
|
915
|
+
const innerHandler = async (event,
|
|
916
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
917
|
+
_context) => {
|
|
918
|
+
// Extract messages from SQS/SNS event wrapper
|
|
919
|
+
const messages = getMessages(event);
|
|
920
|
+
// Process each message through the service
|
|
921
|
+
const results = [];
|
|
922
|
+
for (const message of messages) {
|
|
923
|
+
try {
|
|
924
|
+
const result = await service(message, context);
|
|
925
|
+
results.push(result);
|
|
926
|
+
}
|
|
927
|
+
catch (error) {
|
|
928
|
+
// Any thrown error is fatal - call onFatal or onError as fallback
|
|
929
|
+
if (opts.onFatal) {
|
|
930
|
+
await opts.onFatal(error);
|
|
931
|
+
}
|
|
932
|
+
else if (opts.onError) {
|
|
933
|
+
await opts.onError(error);
|
|
934
|
+
}
|
|
935
|
+
// Re-throw to let lambdaHandler handle it
|
|
936
|
+
throw error;
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
// Call onComplete if provided
|
|
940
|
+
const response = results.length === 1 ? results[0] : results;
|
|
941
|
+
if (opts.onComplete) {
|
|
942
|
+
try {
|
|
943
|
+
await opts.onComplete(response);
|
|
944
|
+
}
|
|
945
|
+
catch {
|
|
946
|
+
// Swallow errors - callback failures should not halt execution
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
return response;
|
|
950
|
+
};
|
|
951
|
+
// Wrap with lambdaHandler for lifecycle management
|
|
952
|
+
return lambdaHandler(innerHandler, {
|
|
953
|
+
chaos: opts.chaos,
|
|
954
|
+
name,
|
|
955
|
+
secrets: opts.secrets,
|
|
956
|
+
setup: opts.setup,
|
|
957
|
+
teardown: opts.teardown,
|
|
958
|
+
throw: opts.throw,
|
|
959
|
+
unavailable: opts.unavailable,
|
|
960
|
+
validate: opts.validate,
|
|
961
|
+
});
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
export { fabricLambda };
|
|
965
|
+
//# sourceMappingURL=index.js.map
|