{"version":3,"file":"flatten-connection.cjs","sources":["../../src/flatten-connection.ts"],"sourcesContent":["import type {PartialDeep} from 'type-fest';\n\n/**\n * The `flattenConnection` utility transforms a connection object from the Storefront API (for example, [Product-related connections](https://shopify.dev/api/storefront/reference/products/product)) into a flat array of nodes.\n * The utility works with either `nodes` or `edges.node`.\n */\nexport function flattenConnection<T>(\n  connection: PartialDeep<GraphQLConnection<T>>\n): PartialDeep<T>[] {\n  if (connection.nodes) {\n    return connection.nodes as PartialDeep<T>[];\n  }\n\n  if (connection.edges) {\n    return connection.edges.map((edge) => {\n      if (!edge?.node) {\n        throw new Error('Connection edges must contain nodes');\n      }\n      return edge.node;\n    });\n  }\n\n  if (__HYDROGEN_DEV__) {\n    console.warn(\n      `The connection did not contain either \"nodes\" or \"edges.node\". A empty array will be returned in its place.`\n    );\n  }\n\n  return [];\n}\n\ninterface GraphQLConnection<T> {\n  edges?: {node: T}[];\n  nodes?: T[];\n}\n"],"names":[],"mappings":";;AAMO,SAAS,kBACd,YACkB;AAClB,MAAI,WAAW,OAAO;AACpB,WAAO,WAAW;AAAA,EACpB;AAEA,MAAI,WAAW,OAAO;AACpB,WAAO,WAAW,MAAM,IAAI,CAAC,SAAS;AAChC,UAAA,EAAC,6BAAM,OAAM;AACT,cAAA,IAAI,MAAM,qCAAqC;AAAA,MACvD;AACA,aAAO,KAAK;AAAA,IAAA,CACb;AAAA,EACH;AAQA,SAAO;AACT;;"}