{"version":3,"file":"updateDataResource.mjs","names":[],"sources":["../../../src/requester/requests/updateDataResource.ts"],"sourcesContent":["import type { DatasetChanges } from \"@ldo/rdf-utils\";\nimport { changesToSparqlUpdate } from \"@ldo/rdf-utils\";\nimport type { Quad } from \"@rdfjs/types\";\nimport { guaranteeFetch } from \"../../util/guaranteeFetch\";\nimport type { Resource } from \"@ldo/connected\";\nimport { UnexpectedResourceError, UpdateSuccess } from \"@ldo/connected\";\nimport type { HttpErrorResultType } from \"../results/error/HttpErrorResult\";\nimport { HttpErrorResult } from \"../results/error/HttpErrorResult\";\nimport type { DatasetRequestOptions } from \"./requestOptions\";\nimport type { SolidContainer } from \"../../resources/SolidContainer\";\nimport type { SolidLeaf } from \"../../resources/SolidLeaf\";\n\n/**\n * All return values for updateDataResource\n */\nexport type UpdateResult<ResourceType extends Resource> =\n  | UpdateSuccess<ResourceType>\n  | UpdateResultError<ResourceType>;\n\n/**\n * All errors updateDataResource can return\n */\nexport type UpdateResultError<ResourceType extends Resource> =\n  | HttpErrorResultType<ResourceType>\n  | UnexpectedResourceError<ResourceType>;\n\n/**\n * @internal\n * Updates a specific data resource with the provided dataset changes\n *\n * @param uri - the URI of the data resource\n * @param datasetChanges - A set of triples added and removed from this dataset\n * @param options - Options to provide a fetch function and a local dataset to\n * update.\n * @returns An UpdateResult\n */\nexport async function updateDataResource(\n  resource: SolidLeaf,\n  datasetChanges: DatasetChanges<Quad>,\n  options?: DatasetRequestOptions,\n): Promise<UpdateResult<SolidLeaf>>;\nexport async function updateDataResource(\n  resource: SolidContainer,\n  datasetChanges: DatasetChanges<Quad>,\n  options?: DatasetRequestOptions,\n): Promise<UpdateResult<SolidContainer>>;\nexport async function updateDataResource(\n  resource: SolidLeaf | SolidContainer,\n  datasetChanges: DatasetChanges<Quad>,\n  options?: DatasetRequestOptions,\n): Promise<UpdateResult<SolidLeaf | SolidContainer>>;\nexport async function updateDataResource(\n  resource: SolidLeaf | SolidContainer,\n  datasetChanges: DatasetChanges<Quad>,\n  options?: DatasetRequestOptions,\n): Promise<UpdateResult<SolidLeaf | SolidContainer>> {\n  try {\n    // Optimistically add data\n    options?.dataset?.bulk(datasetChanges);\n    const fetch = guaranteeFetch(options?.fetch);\n\n    // Make request\n    const sparqlUpdate = await changesToSparqlUpdate(datasetChanges);\n    const response = await fetch(resource.uri, {\n      method: \"PATCH\",\n      body: sparqlUpdate,\n      headers: {\n        \"Content-Type\": \"application/sparql-update\",\n      },\n    });\n    const httpError = HttpErrorResult.checkResponse(resource, response);\n    if (httpError) {\n      // Handle error rollback\n      if (options?.dataset) {\n        options.dataset.bulk({\n          added: datasetChanges.removed,\n          removed: datasetChanges.added,\n        });\n      }\n      return httpError;\n    }\n    return new UpdateSuccess(resource);\n  } catch (err) {\n    return UnexpectedResourceError.fromThrown(resource, err);\n  }\n}\n"],"mappings":";;;;;AAmDA,eAAsB,mBACpB,UACA,gBACA,SACmD;AACnD,KAAI;AAEF,WAAS,SAAS,KAAK,eAAe;EACtC,MAAM,QAAQ,eAAe,SAAS,MAAM;EAG5C,MAAM,eAAe,MAAM,sBAAsB,eAAe;EAChE,MAAM,WAAW,MAAM,MAAM,SAAS,KAAK;GACzC,QAAQ;GACR,MAAM;GACN,SAAS,EACP,gBAAgB,6BACjB;GACF,CAAC;EACF,MAAM,YAAY,gBAAgB,cAAc,UAAU,SAAS;AACnE,MAAI,WAAW;AAEb,OAAI,SAAS,QACX,SAAQ,QAAQ,KAAK;IACnB,OAAO,eAAe;IACtB,SAAS,eAAe;IACzB,CAAC;AAEJ,UAAO;;AAET,SAAO,IAAI,cAAc,SAAS;UAC3B,KAAK;AACZ,SAAO,wBAAwB,WAAW,UAAU,IAAI"}