@jaypie/fabric 0.2.0 → 0.2.2

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.
Files changed (47) hide show
  1. package/dist/cjs/ServiceSuite.d.ts +3 -1
  2. package/dist/cjs/commander/index.cjs +42 -11
  3. package/dist/cjs/commander/index.cjs.map +1 -1
  4. package/dist/cjs/data/index.cjs +40 -11
  5. package/dist/cjs/data/index.cjs.map +1 -1
  6. package/dist/cjs/http/index.cjs +43 -13
  7. package/dist/cjs/http/index.cjs.map +1 -1
  8. package/dist/cjs/index.cjs +50 -21
  9. package/dist/cjs/index.cjs.map +1 -1
  10. package/dist/cjs/index.d.ts +1 -1
  11. package/dist/cjs/lambda/index.cjs +42 -11
  12. package/dist/cjs/lambda/index.cjs.map +1 -1
  13. package/dist/cjs/llm/index.cjs +42 -11
  14. package/dist/cjs/llm/index.cjs.map +1 -1
  15. package/dist/cjs/mcp/FabricMcpServer.d.ts +1 -1
  16. package/dist/cjs/mcp/index.cjs +43 -12
  17. package/dist/cjs/mcp/index.cjs.map +1 -1
  18. package/dist/cjs/models/base.d.ts +6 -6
  19. package/dist/cjs/resolveService.d.ts +7 -4
  20. package/dist/cjs/service.d.ts +6 -4
  21. package/dist/cjs/types.d.ts +9 -3
  22. package/dist/cjs/websocket/fabricWebSocket.d.ts +120 -0
  23. package/dist/cjs/websocket/index.d.ts +2 -0
  24. package/dist/esm/ServiceSuite.d.ts +3 -1
  25. package/dist/esm/commander/index.js +42 -11
  26. package/dist/esm/commander/index.js.map +1 -1
  27. package/dist/esm/data/index.js +40 -11
  28. package/dist/esm/data/index.js.map +1 -1
  29. package/dist/esm/http/index.js +43 -13
  30. package/dist/esm/http/index.js.map +1 -1
  31. package/dist/esm/index.d.ts +1 -1
  32. package/dist/esm/index.js +50 -21
  33. package/dist/esm/index.js.map +1 -1
  34. package/dist/esm/lambda/index.js +42 -11
  35. package/dist/esm/lambda/index.js.map +1 -1
  36. package/dist/esm/llm/index.js +42 -11
  37. package/dist/esm/llm/index.js.map +1 -1
  38. package/dist/esm/mcp/FabricMcpServer.d.ts +1 -1
  39. package/dist/esm/mcp/index.js +43 -12
  40. package/dist/esm/mcp/index.js.map +1 -1
  41. package/dist/esm/models/base.d.ts +6 -6
  42. package/dist/esm/resolveService.d.ts +7 -4
  43. package/dist/esm/service.d.ts +6 -4
  44. package/dist/esm/types.d.ts +9 -3
  45. package/dist/esm/websocket/fabricWebSocket.d.ts +120 -0
  46. package/dist/esm/websocket/index.d.ts +2 -0
  47. package/package.json +6 -1
@@ -681,42 +681,68 @@ async function processField(fieldName, value, definition) {
681
681
  }
682
682
  return convertedValue;
683
683
  }
684
+ /**
685
+ * Run serializer hook if provided
686
+ * Returns transformed output or original if serializer returns undefined/null/void
687
+ */
688
+ async function runSerializer(data, serializer, context) {
689
+ if (!serializer) {
690
+ return data.output;
691
+ }
692
+ const result = await serializer(data, context);
693
+ if (result !== undefined && result !== null) {
694
+ return result;
695
+ }
696
+ return data.output;
697
+ }
684
698
  /**
685
699
  * Fabric a service function
686
700
  *
687
- * Service builds a function that initiates a "controller" step that:
701
+ * Service builds a function that:
688
702
  * - Parses the input if it is a string to object
689
703
  * - Fabrics each input field to its type
690
704
  * - Calls the validation function or regular expression or checks the array
691
- * - Calls the service function and returns the response
705
+ * - Calls the service function
706
+ * - Calls the serializer hook (can transform output)
707
+ * - Returns the response
692
708
  *
693
709
  * The returned function has config properties for introspection.
694
710
  */
695
711
  function fabricService(config) {
696
- const { input: inputDefinitions, service } = config;
712
+ const { input: inputDefinitions, serializer, service } = config;
697
713
  const handler = async (rawInput, context) => {
698
714
  // Parse input (handles string JSON)
699
715
  const parsedInput = parseInput(rawInput);
700
716
  // If no input definitions, pass through to service or return parsed input
701
717
  if (!inputDefinitions) {
718
+ let output;
702
719
  if (service) {
703
- return service(parsedInput, context);
720
+ output = await service(parsedInput, context);
721
+ }
722
+ else {
723
+ output = parsedInput;
704
724
  }
705
- return parsedInput;
725
+ // Run serializer
726
+ return (await runSerializer({ input: parsedInput, output }, serializer, context));
706
727
  }
707
728
  // Process all fields in parallel
708
729
  const entries = Object.entries(inputDefinitions);
709
730
  const processedValues = await Promise.all(entries.map(([fieldName, definition]) => processField(fieldName, parsedInput[fieldName], definition)));
710
731
  // Build processed input object
711
- const processedInput = {};
732
+ const processedInputObj = {};
712
733
  entries.forEach(([fieldName], index) => {
713
- processedInput[fieldName] = processedValues[index];
734
+ processedInputObj[fieldName] = processedValues[index];
714
735
  });
715
- // Return processed input if no service, otherwise call service
736
+ // Call service or return processed input
737
+ let output;
716
738
  if (service) {
717
- return service(processedInput, context);
739
+ output = await service(processedInputObj, context);
740
+ }
741
+ else {
742
+ output = processedInputObj;
718
743
  }
719
- return processedInput;
744
+ // Run serializer hook
745
+ return (await runSerializer({ input: processedInputObj, output }, serializer, context));
720
746
  };
721
747
  // Attach config properties directly to handler for flat access
722
748
  const typedHandler = handler;
@@ -727,6 +753,8 @@ function fabricService(config) {
727
753
  typedHandler.description = config.description;
728
754
  if (config.input !== undefined)
729
755
  typedHandler.input = config.input;
756
+ if (config.serializer !== undefined)
757
+ typedHandler.serializer = config.serializer;
730
758
  if (config.service !== undefined)
731
759
  typedHandler.service = config.service;
732
760
  return typedHandler;
@@ -1144,7 +1172,8 @@ exports.HttpStreamEventType = void 0;
1144
1172
  * Check if event is API Gateway v2 format
1145
1173
  */
1146
1174
  function isApiGatewayV2Event(event) {
1147
- return "requestContext" in event && "http" in event.requestContext;
1175
+ return ("requestContext" in event &&
1176
+ "http" in event.requestContext);
1148
1177
  }
1149
1178
  /**
1150
1179
  * Check if entry is a FabricHttpServerRoute (has service property)
@@ -1194,7 +1223,8 @@ function matchRoute(requestPath, route) {
1194
1223
  const requestSegments = requestPath.split("/").filter(Boolean);
1195
1224
  const routeSegments = route.segments;
1196
1225
  // Check segment count (allow route to have optional params)
1197
- if (requestSegments.length < routeSegments.filter((s) => !s.endsWith("?")).length) {
1226
+ if (requestSegments.length <
1227
+ routeSegments.filter((s) => !s.endsWith("?")).length) {
1198
1228
  return undefined;
1199
1229
  }
1200
1230
  if (requestSegments.length > routeSegments.length) {
@@ -1350,7 +1380,7 @@ function FabricHttpServer(config) {
1350
1380
  */
1351
1381
  const handler = async (event) => {
1352
1382
  // Extract request data from API Gateway event
1353
- const { body, headers, method, path: requestPath, queryString, pathParams } = extractRequestData(event);
1383
+ const { body, headers, method, path: requestPath, queryString, pathParams, } = extractRequestData(event);
1354
1384
  // Normalize headers to Headers object for consistency
1355
1385
  const headersObj = new Headers(headers);
1356
1386
  const origin = headersObj.get("origin");