{"version":3,"file":"getWacUri.mjs","names":[],"sources":["../../src/wac/getWacUri.ts"],"sourcesContent":["import {\n  HttpErrorResult,\n  NotFoundHttpError,\n} from \"../requester/results/error/HttpErrorResult\";\nimport type { HttpErrorResultType } from \"../requester/results/error/HttpErrorResult\";\nimport { GetWacUriSuccess } from \"./results/GetWacUriSuccess\";\nimport LinkHeader from \"http-link-header\";\nimport { UnexpectedResourceError } from \"@ldo/connected\";\nimport { NoncompliantPodError } from \"../requester/results/error/NoncompliantPodError\";\nimport type { SolidContainer } from \"../resources/SolidContainer\";\nimport type { SolidLeaf } from \"../resources/SolidLeaf\";\nimport type { BasicRequestOptions } from \"../requester/requests/requestOptions\";\nimport { guaranteeFetch } from \"../util/guaranteeFetch\";\nimport type { SolidLeafUri } from \"../types\";\n\nexport type GetWacUriError<ResourceType extends SolidContainer | SolidLeaf> =\n  | HttpErrorResultType<ResourceType>\n  | NotFoundHttpError<ResourceType>\n  | NoncompliantPodError<ResourceType>\n  | UnexpectedResourceError<ResourceType>;\nexport type GetWacUriResult<ResourceType extends SolidContainer | SolidLeaf> =\n  | GetWacUriSuccess<ResourceType>\n  | GetWacUriError<ResourceType>;\n\n/**\n * Get the URI for the WAC rules of a specific resource\n * @param resourceUri: the URI of the resource\n * @param options: Options object to include an authenticated fetch function\n * @returns GetWacUriResult\n */\nexport async function getWacUri(\n  resource: SolidLeaf | SolidContainer,\n  options?: BasicRequestOptions,\n): Promise<GetWacUriResult<SolidLeaf | SolidContainer>> {\n  try {\n    const fetch = guaranteeFetch(options?.fetch);\n    const response = await fetch(resource.uri, {\n      method: \"head\",\n    });\n    const errorResult = HttpErrorResult.checkResponse(resource, response);\n    if (errorResult) return errorResult;\n    if (NotFoundHttpError.is(response)) {\n      return new NotFoundHttpError(\n        resource,\n        response,\n        \"Could not get access control rules because the resource does not exist.\",\n      );\n    }\n    // Get the URI from the link header\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 aclUris = parsedLinkHeader.get(\"rel\", \"acl\");\n    if (aclUris.length !== 1) {\n      return new NoncompliantPodError(\n        resource,\n        `There must be one link with a rel=\"acl\"`,\n      );\n    }\n\n    return new GetWacUriSuccess(resource, aclUris[0].uri as SolidLeafUri);\n  } catch (err: unknown) {\n    return UnexpectedResourceError.fromThrown(resource, err);\n  }\n}\n"],"mappings":";;;;;;;;;;;;;AA8BA,eAAsB,UACpB,UACA,SACsD;AACtD,KAAI;EAEF,MAAM,WAAW,MADH,eAAe,SAAS,MACV,CAAC,SAAS,KAAK,EACzC,QAAQ,QACT,CAAC;EACF,MAAM,cAAc,gBAAgB,cAAc,UAAU,SAAS;AACrE,MAAI,YAAa,QAAO;AACxB,MAAI,kBAAkB,GAAG,SAAS,CAChC,QAAO,IAAI,kBACT,UACA,UACA,0EACD;EAGH,MAAM,aAAa,SAAS,QAAQ,IAAI,OAAO;AAC/C,MAAI,CAAC,WACH,QAAO,IAAI,qBACT,UACA,qCACD;EAGH,MAAM,UADmB,WAAW,MAAM,WACV,CAAC,IAAI,OAAO,MAAM;AAClD,MAAI,QAAQ,WAAW,EACrB,QAAO,IAAI,qBACT,UACA,0CACD;AAGH,SAAO,IAAI,iBAAiB,UAAU,QAAQ,GAAG,IAAoB;UAC9D,KAAc;AACrB,SAAO,wBAAwB,WAAW,UAAU,IAAI"}