@jaypie/fabric 0.1.0 → 0.1.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.
package/dist/esm/index.js CHANGED
@@ -1564,6 +1564,156 @@ function resolveService(config) {
1564
1564
  });
1565
1565
  }
1566
1566
 
1567
+ // ServiceSuite for @jaypie/fabric
1568
+ // Groups fabricService instances for discovery, metadata export, and direct execution
1569
+ /**
1570
+ * Derive type string from InputFieldDefinition.type
1571
+ */
1572
+ function deriveTypeString(type) {
1573
+ // Handle constructors
1574
+ if (type === String || type === "string")
1575
+ return "string";
1576
+ if (type === Number || type === "number")
1577
+ return "number";
1578
+ if (type === Boolean || type === "boolean")
1579
+ return "boolean";
1580
+ if (type === Object || type === "object")
1581
+ return "object";
1582
+ if (type === Array || type === "array")
1583
+ return "array";
1584
+ if (type === Date)
1585
+ return "string"; // Dates are passed as strings
1586
+ // Handle typed arrays: [String], [Number], etc.
1587
+ if (Array.isArray(type)) {
1588
+ if (type.length === 0)
1589
+ return "array";
1590
+ const first = type[0];
1591
+ // If it's a type constructor, it's a typed array
1592
+ if (first === String ||
1593
+ first === Number ||
1594
+ first === Boolean ||
1595
+ first === Object ||
1596
+ first === "string" ||
1597
+ first === "number" ||
1598
+ first === "boolean" ||
1599
+ first === "object" ||
1600
+ first === "") {
1601
+ return "array";
1602
+ }
1603
+ // If all elements are strings (or RegExp), it's a validated string enum
1604
+ if (type.every((item) => typeof item === "string" || item instanceof RegExp)) {
1605
+ return "string";
1606
+ }
1607
+ // If all elements are numbers, it's a validated number enum
1608
+ if (type.every((item) => typeof item === "number")) {
1609
+ return "number";
1610
+ }
1611
+ return "array";
1612
+ }
1613
+ // Handle RegExp (validated string)
1614
+ if (type instanceof RegExp) {
1615
+ return "string";
1616
+ }
1617
+ return "string"; // Default fallback
1618
+ }
1619
+ /**
1620
+ * Extract enum values if the type is a validated string/number array
1621
+ */
1622
+ function extractEnumValues(type) {
1623
+ if (!Array.isArray(type))
1624
+ return undefined;
1625
+ if (type.length === 0)
1626
+ return undefined;
1627
+ // Check if it's a validated string enum (array of strings, possibly with RegExp)
1628
+ const stringValues = type.filter((item) => typeof item === "string");
1629
+ if (stringValues.length > 0 && stringValues.length === type.filter((item) => typeof item === "string").length) {
1630
+ // All non-RegExp items are strings
1631
+ return stringValues;
1632
+ }
1633
+ // Check if it's a validated number enum
1634
+ if (type.every((item) => typeof item === "number")) {
1635
+ return type.map((n) => String(n));
1636
+ }
1637
+ return undefined;
1638
+ }
1639
+ /**
1640
+ * Convert fabricService input definitions to ServiceInput array
1641
+ */
1642
+ function extractInputs(inputDefinitions) {
1643
+ if (!inputDefinitions)
1644
+ return [];
1645
+ return Object.entries(inputDefinitions).map(([name, def]) => {
1646
+ const required = def.required !== false && def.default === undefined;
1647
+ const enumValues = extractEnumValues(def.type);
1648
+ return {
1649
+ name,
1650
+ type: deriveTypeString(def.type),
1651
+ required,
1652
+ description: def.description || "",
1653
+ ...(enumValues && { enum: enumValues }),
1654
+ };
1655
+ });
1656
+ }
1657
+ /**
1658
+ * Check if a service has any required inputs
1659
+ */
1660
+ function hasRequiredInputs(inputs) {
1661
+ return inputs.some((input) => input.required);
1662
+ }
1663
+ /**
1664
+ * Create a ServiceSuite instance
1665
+ */
1666
+ function createServiceSuite(config) {
1667
+ const { name, version } = config;
1668
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1669
+ const serviceRegistry = new Map();
1670
+ const categorySet = new Set();
1671
+ const suite = {
1672
+ name,
1673
+ version,
1674
+ get categories() {
1675
+ return Array.from(categorySet).sort();
1676
+ },
1677
+ get services() {
1678
+ return Array.from(serviceRegistry.values()).map((entry) => entry.meta);
1679
+ },
1680
+ getService(serviceName) {
1681
+ return serviceRegistry.get(serviceName)?.meta;
1682
+ },
1683
+ getServicesByCategory(category) {
1684
+ return Array.from(serviceRegistry.values())
1685
+ .filter((entry) => entry.meta.category === category)
1686
+ .map((entry) => entry.meta);
1687
+ },
1688
+ async execute(serviceName, inputs) {
1689
+ const entry = serviceRegistry.get(serviceName);
1690
+ if (!entry) {
1691
+ throw new Error(`Service "${serviceName}" not found in suite "${name}"`);
1692
+ }
1693
+ return entry.service(inputs);
1694
+ },
1695
+ register(
1696
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1697
+ service, category) {
1698
+ const serviceName = service.alias;
1699
+ if (!serviceName) {
1700
+ throw new Error("Service must have an alias to be registered");
1701
+ }
1702
+ const inputs = extractInputs(service.input);
1703
+ const meta = {
1704
+ name: serviceName,
1705
+ description: service.description || "",
1706
+ category,
1707
+ inputs,
1708
+ executable: !hasRequiredInputs(inputs),
1709
+ };
1710
+ serviceRegistry.set(serviceName, { service, meta });
1711
+ categorySet.add(category);
1712
+ },
1713
+ };
1714
+ return suite;
1715
+ }
1716
+
1567
1717
  /**
1568
1718
  * Status Type for @jaypie/fabric
1569
1719
  *
@@ -1602,5 +1752,5 @@ function isStatus(value) {
1602
1752
  STATUS_VALUES.includes(value));
1603
1753
  }
1604
1754
 
1605
- export { APEX, ARCHIVED_SUFFIX, BOOLEAN_TYPE, DATETIME_TYPE, DATE_TYPE, DEFAULT_INDEXES, DEFAULT_SORT_KEY, DELETED_SUFFIX, DOLLARS_TYPE, DateType, ELEMENTARY_TYPES, ELEMENTARY_TYPE_REGISTRY, FABRIC_MODEL_AUTO_FIELDS, FABRIC_MODEL_FIELDS, FABRIC_MODEL_REQUIRED_FIELDS, FABRIC_MODEL_TIMESTAMP_FIELDS, FABRIC_VERSION, MULTISELECT_TYPE, NUMBER_TYPE, REFERENCE_TYPE, SELECT_TYPE, SEPARATOR, STATUS_VALUES, SYSTEM_MODELS, StatusType, TEXTAREA_TYPE, TEXT_TYPE, buildCompositeKey, calculateIndexSuffix, calculateScope, clearRegistry, computeResolvedName, createFabricModelInput, fabric, fabricArray, fabricBoolean, fabricDate, fabricNumber, fabricObject, fabricService, fabricString, generateIndexName, getAllElementaryTypes, getAllRegisteredIndexes, getElementaryType, getModelIndexes, getModelSchema, getRegisteredModels, hasFabricModelShape, isAutoField, isDateType, isElementaryType, isFabricModel, isFieldDefinition, isModelRegistered, isStatus, isTimestampField, isValidDate, pickFabricModelFields, populateIndexKeys, registerModel, resolveFromArray, resolveFromDate, resolveFromObject, resolveService, resolveWithFallback, tryBuildCompositeKey };
1755
+ export { APEX, ARCHIVED_SUFFIX, BOOLEAN_TYPE, DATETIME_TYPE, DATE_TYPE, DEFAULT_INDEXES, DEFAULT_SORT_KEY, DELETED_SUFFIX, DOLLARS_TYPE, DateType, ELEMENTARY_TYPES, ELEMENTARY_TYPE_REGISTRY, FABRIC_MODEL_AUTO_FIELDS, FABRIC_MODEL_FIELDS, FABRIC_MODEL_REQUIRED_FIELDS, FABRIC_MODEL_TIMESTAMP_FIELDS, FABRIC_VERSION, MULTISELECT_TYPE, NUMBER_TYPE, REFERENCE_TYPE, SELECT_TYPE, SEPARATOR, STATUS_VALUES, SYSTEM_MODELS, StatusType, TEXTAREA_TYPE, TEXT_TYPE, buildCompositeKey, calculateIndexSuffix, calculateScope, clearRegistry, computeResolvedName, createFabricModelInput, createServiceSuite, fabric, fabricArray, fabricBoolean, fabricDate, fabricNumber, fabricObject, fabricService, fabricString, generateIndexName, getAllElementaryTypes, getAllRegisteredIndexes, getElementaryType, getModelIndexes, getModelSchema, getRegisteredModels, hasFabricModelShape, isAutoField, isDateType, isElementaryType, isFabricModel, isFieldDefinition, isModelRegistered, isStatus, isTimestampField, isValidDate, pickFabricModelFields, populateIndexKeys, registerModel, resolveFromArray, resolveFromDate, resolveFromObject, resolveService, resolveWithFallback, tryBuildCompositeKey };
1606
1756
  //# sourceMappingURL=index.js.map