contentful / contentful-management.js

JavaScript library for Contentful's Management API (node & browser)
https://contentful.github.io/contentful-management.js
MIT License
263 stars 97 forks source link

Updating an entry as per docs doesn't work because of version conflict #2326

Closed codegoblin closed 2 weeks ago

codegoblin commented 2 weeks ago

I have a test entry in Contentful that's published (which is confusing from the outset because it was at 'current version' 14 before publishing, and when I hit publish, it changed published version to 15 and current version to 16). When I query it via the management js library it then says publishedVersion: 15, under the sys obj, and when I do a simple

 await contentfulClient.entry.update({ entryId: entry.sys.id }, {
            sys: entry.sys,
            fields: {
              title: {
                "en-US": "New entry title",
              },
            },
          });

as per the website docs (https://www.contentful.com/developers/docs/references/content-management-api/#/reference/entries/entry/update-an-entry/console/js-plain), I get back

VersionMismatch: {
  "status": 409,
  "statusText": "Conflict",
  "message": "",
  "details": {},
  "request": {
    "url": "/spaces/gcwac57vok7r/environments/test/entries/3muDpvBe17agDc2TsyFZzM",
    "headers": {
      "Accept": "application/json, text/plain, */*",
      "Content-Type": "application/vnd.contentful.management.v1+json",
      "X-Contentful-User-Agent": "sdk contentful-management-plain.js/11.27.2; platform node.js/v20.7.0; os macOS/v20.7.0;",
      "Authorization": "obfuscated",
      "X-Contentful-Version": "0",
      "User-Agent": "axios/1.6.8",
      "Content-Length": "48",
      "Accept-Encoding": "gzip, compress, deflate, br"
    },
    "method": "put",
    "payloadData": "{\"fields\":{\"title\":{\"en-US\":\"New entry title\"}}}"
  },
  "requestId": "4bc09707-6e60-4bdd-a416-2084004ec2b9"
}

I thought this library was supposed to handle this for me? When I look at the source code, it looks like the update function isn't even using a sys field returned from the original await contentfulClient.entry.getPublished query (https://github.com/contentful/contentful-management.js/blob/9655c6ebca0918cf6a0db512059840483da524bd/lib/adapters/REST/endpoints/entry.ts#L80). There is no version field in the sys obj I got returned from the query. What gives?

codegoblin commented 2 weeks ago

I see now that getting the entry via entry.get includes the version field on the sys obj ... why?? Why are there two different representations of the same entry depending on how I fetch it getPublished vs get? This seems ... wrong. Is there a way to fetch all entries in a space/env that are published and also include the version field on the sys obj? I don't want to have to fetch each entry a second time individually ...

codegoblin commented 2 weeks ago

I hacked a workaround, but I'm dissatisfied with this api, and the docs for this repo are basically useless.