{"version":3,"file":"getStorageDescription.mjs","names":[],"sources":["../../../src/requester/requests/getStorageDescription.ts"],"sourcesContent":["import { UnexpectedResourceError } from \"@ldo/connected\";\nimport LinkHeader from \"http-link-header\";\nimport type { SolidLeafUri } from \"../../types\";\nimport { guaranteeFetch } from \"../../util/guaranteeFetch\";\nimport {\n  HttpErrorResult,\n  type HttpErrorResultType,\n  NotFoundHttpError,\n} from \"../results/error/HttpErrorResult\";\nimport { NoncompliantPodError } from \"../results/error/NoncompliantPodError\";\nimport { GetStorageDescriptionUriSuccess } from \"../results/success/StorageDescriptionSuccess\";\nimport type { BasicRequestOptions } from \"./requestOptions\";\nimport type { SolidContainer } from \"../../resources/SolidContainer.js\";\nimport type { SolidLeaf } from \"../../resources/SolidLeaf.js\";\n\nexport type GetStorageDescriptionUriError<\n  ResourceType extends SolidContainer | SolidLeaf,\n> =\n  | HttpErrorResultType<ResourceType>\n  | NotFoundHttpError<ResourceType>\n  | NoncompliantPodError<ResourceType>\n  | UnexpectedResourceError<ResourceType>;\nexport type GetStorageDescriptionUriResult<\n  ResourceType extends SolidContainer | SolidLeaf,\n> =\n  | GetStorageDescriptionUriSuccess\n  | GetStorageDescriptionUriError<ResourceType>;\n\n/**\n * Get storage description URI from resource Link headers\n *\n * @param resource - Solid resource we start from\n * @param options - Options object that may contain custom (authenticated) fetch\n *\n * @returns GetStorageDescriptionUriResult\n *\n * https://solidproject.org/TR/protocol#server-storage-description\n */\nexport async function getStorageDescriptionUri(\n  resource: SolidLeaf | SolidContainer,\n  options?: BasicRequestOptions,\n): Promise<GetStorageDescriptionUriResult<SolidLeaf | SolidContainer>> {\n  try {\n    const fetch = guaranteeFetch(options?.fetch);\n    const response = await fetch(resource.uri, { method: \"HEAD\" });\n    const httpErrorResult = HttpErrorResult.checkResponse(resource, response);\n    if (httpErrorResult) return httpErrorResult;\n    if (NotFoundHttpError.is(response)) {\n      return new NotFoundHttpError(\n        resource,\n        response,\n        \"Could not get storage description of the resource because the resource does not exist.\",\n      );\n    }\n\n    const linkHeader = response.headers.get(\"link\");\n    if (!linkHeader) {\n      return new NoncompliantPodError(\n        resource,\n        \"No link header present in request.\",\n      );\n    }\n    const parsedLinkHeader = LinkHeader.parse(linkHeader);\n    const storageDescriptionLinks = parsedLinkHeader.get(\n      \"rel\",\n      \"http://www.w3.org/ns/solid/terms#storageDescription\",\n    );\n\n    if (storageDescriptionLinks.length !== 1) {\n      return new NoncompliantPodError(\n        resource,\n        'There must be one link with a rel=\"http://www.w3.org/ns/solid/terms#storageDescription\"',\n      );\n    }\n\n    return new GetStorageDescriptionUriSuccess(\n      resource,\n      storageDescriptionLinks[0].uri as SolidLeafUri,\n    );\n  } catch (e) {\n    return UnexpectedResourceError.fromThrown(resource, e);\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAsCA,eAAsB,yBACpB,UACA,SACqE;AACrE,KAAI;EAEF,MAAM,WAAW,MADH,eAAe,SAAS,MACV,CAAC,SAAS,KAAK,EAAE,QAAQ,QAAQ,CAAC;EAC9D,MAAM,kBAAkB,gBAAgB,cAAc,UAAU,SAAS;AACzE,MAAI,gBAAiB,QAAO;AAC5B,MAAI,kBAAkB,GAAG,SAAS,CAChC,QAAO,IAAI,kBACT,UACA,UACA,yFACD;EAGH,MAAM,aAAa,SAAS,QAAQ,IAAI,OAAO;AAC/C,MAAI,CAAC,WACH,QAAO,IAAI,qBACT,UACA,qCACD;EAGH,MAAM,0BADmB,WAAW,MAAM,WACM,CAAC,IAC/C,OACA,sDACD;AAED,MAAI,wBAAwB,WAAW,EACrC,QAAO,IAAI,qBACT,UACA,4FACD;AAGH,SAAO,IAAI,gCACT,UACA,wBAAwB,GAAG,IAC5B;UACM,GAAG;AACV,SAAO,wBAAwB,WAAW,UAAU,EAAE"}