{"version":3,"file":"getStorageFromWebId.mjs","names":[],"sources":["../src/getStorageFromWebId.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport type { ConnectedLdoDataset, ConnectedPlugin } from \"@ldo/connected\";\nimport type { SolidContainerUri, SolidLeafUri } from \"./types\";\nimport { GetStorageContainerFromWebIdSuccess } from \"./requester/results/success/CheckRootContainerSuccess\";\nimport type { CheckRootResultError } from \"./requester/requests/checkRootContainer\";\nimport type { ReadResultError } from \"./requester/requests/readResource\";\nimport type { NoRootContainerError } from \"./requester/results/error/NoRootContainerError\";\nimport type { SolidLeaf } from \"./resources/SolidLeaf\";\nimport type { SolidContainer } from \"./resources/SolidContainer\";\nimport type { SolidConnectedPlugin } from \"./SolidConnectedPlugin\";\nimport { ProfileWithStorageShapeType } from \"./_ldo/solid.shapeTypes\";\n\n/**\n * Gets a list of root storage containers for a user given their WebId\n * @param webId: The webId for the user\n * @returns A list of storages if successful, an error if not\n * @example\n * ```typescript\n * import { getStorageFromWebId } from \"@ldo/connected-solid\";\n *\n * const result = await getStorageFromWebId(\n *   solidLdoDataset,\n *   \"https://example.com/profile/card#me\"\n * );\n * if (result.isError) {\n *   // Do something\n * }\n * console.log(result.storageContainer[0].uri);\n * ```\n */\nexport async function getStorageFromWebId(\n  webId: SolidLeafUri,\n  dataset: ConnectedLdoDataset<(SolidConnectedPlugin | ConnectedPlugin)[]>,\n): Promise<\n  | GetStorageContainerFromWebIdSuccess\n  | CheckRootResultError\n  | ReadResultError<SolidLeaf | SolidContainer>\n  | NoRootContainerError<SolidContainer>\n> {\n  const webIdResource = dataset.getResource(webId) as SolidLeaf;\n  const readResult = await webIdResource.readIfUnfetched();\n  if (readResult.isError) return readResult;\n  const profile = dataset\n    .usingType(ProfileWithStorageShapeType)\n    .fromSubject(webId);\n  if (profile.storage && profile.storage.size > 0) {\n    const containers = profile.storage\n      .map((storageNode) =>\n        dataset.getResource(storageNode[\"@id\"] as SolidContainerUri),\n      )\n      .filter((container): container is SolidContainer => {\n        return container.type === \"SolidContainer\";\n      });\n\n    return new GetStorageContainerFromWebIdSuccess(containers);\n  }\n  const getContainerResult = await webIdResource.getRootContainer();\n  if (getContainerResult.type === \"SolidContainer\")\n    return new GetStorageContainerFromWebIdSuccess([getContainerResult]);\n  return getContainerResult;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AA8BA,eAAsB,oBACpB,OACA,SAMA;CACA,MAAM,gBAAgB,QAAQ,YAAY,MAAM;CAChD,MAAM,aAAa,MAAM,cAAc,iBAAiB;AACxD,KAAI,WAAW,QAAS,QAAO;CAC/B,MAAM,UAAU,QACb,UAAU,4BAA4B,CACtC,YAAY,MAAM;AACrB,KAAI,QAAQ,WAAW,QAAQ,QAAQ,OAAO,EAS5C,QAAO,IAAI,oCARQ,QAAQ,QACxB,KAAK,gBACJ,QAAQ,YAAY,YAAY,OAA4B,CAC7D,CACA,QAAQ,cAA2C;AAClD,SAAO,UAAU,SAAS;GAG2B,CAAC;CAE5D,MAAM,qBAAqB,MAAM,cAAc,kBAAkB;AACjE,KAAI,mBAAmB,SAAS,iBAC9B,QAAO,IAAI,oCAAoC,CAAC,mBAAmB,CAAC;AACtE,QAAO"}