@farm.js/sanity 0.1.0-beta.104 → 0.1.0-beta.105

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"content.d.ts","sourceRoot":"","sources":["../src/content.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAyB,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AACnF,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAInD,MAAM,WAAW,0BAA0B;IACzC,qDAAqD;IACrD,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACjC,8EAA8E;IAC9E,MAAM,CAAC,EAAE,YAAY,CAAC;IACtB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf;;;;OAIG;IACH,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,gEAAgE;IAChE,IAAI,CAAC,EAAE,MAAM,CAAC;IACd;;;OAGG;IACH,EAAE,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,MAAM,CAAC;IACnD,6EAA6E;IAC7E,IAAI,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,MAAM,GAAG,SAAS,CAAC;IACjE,oDAAoD;IACpD,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB;;;;OAIG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,8EAA8E;IAC9E,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,0BAA0B,GAAG,mBAAmB,CA+HrF"}
1
+ {"version":3,"file":"content.d.ts","sourceRoot":"","sources":["../src/content.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAyB,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AACnF,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAInD,MAAM,WAAW,0BAA0B;IACzC,qDAAqD;IACrD,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACjC,8EAA8E;IAC9E,MAAM,CAAC,EAAE,YAAY,CAAC;IACtB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf;;;;OAIG;IACH,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,gEAAgE;IAChE,IAAI,CAAC,EAAE,MAAM,CAAC;IACd;;;OAGG;IACH,EAAE,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,MAAM,CAAC;IACnD,6EAA6E;IAC7E,IAAI,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,MAAM,GAAG,SAAS,CAAC;IACjE,oDAAoD;IACpD,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB;;;;OAIG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,8EAA8E;IAC9E,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,0BAA0B,GAAG,mBAAmB,CA8IrF"}
package/dist/content.js CHANGED
@@ -44,19 +44,32 @@ export function sanitySource(options) {
44
44
  return writeClient;
45
45
  };
46
46
  // Entry ids are slugs by default, but Sanity mutations address `_id`.
47
- // Remember the mapping from the last fetch and fall back to a lookup.
48
- const documentIds = new Map();
47
+ // Resolve live on every write: a cached mapping goes stale the moment an
48
+ // editor reassigns a slug, and writes are rare enough that one lookup is
49
+ // the right price for addressing the intended document. When `createType`
50
+ // is configured the lookup is constrained to it, so a slug shared with an
51
+ // unrelated document type cannot redirect the mutation.
49
52
  const resolveDocumentId = async (id) => {
50
- const known = documentIds.get(id);
51
- if (known)
52
- return known;
53
- const bySlug = await resolveWriteClient().fetch("*[slug.current == $id][0]._id", { id });
54
- return bySlug ?? id;
53
+ const query = options.createType
54
+ ? "*[slug.current == $id && _type == $type][0]._id"
55
+ : "*[slug.current == $id][0]._id";
56
+ const bySlug = await resolveWriteClient().fetch(query, {
57
+ id,
58
+ ...(options.createType ? { type: options.createType } : {}),
59
+ });
60
+ if (bySlug)
61
+ return bySlug;
62
+ // An id that is itself a document _id (custom `id` option) still works.
63
+ const direct = await resolveWriteClient().fetch("*[_id == $id][0]._id", {
64
+ id,
65
+ });
66
+ if (direct)
67
+ return direct;
68
+ throw new Error(`sanitySource(${JSON.stringify(name)}) could not resolve ${JSON.stringify(id)} to a ` +
69
+ "document; the write was not sent. The entry may have been deleted or its slug changed.");
55
70
  };
56
71
  const toDocument = (record) => {
57
72
  const id = resolveId(record) || record._id;
58
- if (typeof record._id === "string")
59
- documentIds.set(id, record._id);
60
73
  const body = options.body?.(record);
61
74
  return { id, data: record, ...(typeof body === "string" ? { body } : {}) };
62
75
  };
@@ -66,13 +79,17 @@ export function sanitySource(options) {
66
79
  if (!options.createType) {
67
80
  throw new Error(`sanitySource(${JSON.stringify(name)}) needs \`createType\` (the Sanity _type) to create documents`);
68
81
  }
82
+ rejectUnsupportedBody(name, input.body);
83
+ // _type is forced after the spread so request-derived data cannot
84
+ // redirect the document into another type.
69
85
  const created = await resolveWriteClient().create({
70
- _type: options.createType,
71
86
  ...input.data,
87
+ _type: options.createType,
72
88
  });
73
89
  return toDocument(created);
74
90
  },
75
91
  update: async (id, patch) => {
92
+ rejectUnsupportedBody(name, patch.body);
76
93
  const documentId = await resolveDocumentId(id);
77
94
  const updated = await resolveWriteClient()
78
95
  .patch(documentId)
@@ -106,8 +123,6 @@ export function sanitySource(options) {
106
123
  throw new Error(`sanitySource(${JSON.stringify(name)}) could not derive an id for the document at ` +
107
124
  `index ${index}; give documents a slug or pass an \`id\` function`);
108
125
  }
109
- if (typeof record._id === "string")
110
- documentIds.set(id, record._id);
111
126
  const body = options.body?.(record);
112
127
  return {
113
128
  id,
@@ -118,6 +133,12 @@ export function sanitySource(options) {
118
133
  },
119
134
  };
120
135
  }
136
+ function rejectUnsupportedBody(name, body) {
137
+ if (typeof body !== "string")
138
+ return;
139
+ throw new Error(`sanitySource(${JSON.stringify(name)}) does not store \`body\`: Sanity documents keep ` +
140
+ "content in fields. Put the text in `data` under the field your schema expects.");
141
+ }
121
142
  function defaultDocumentId(document) {
122
143
  const slug = document.slug;
123
144
  if (slug &&
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@farm.js/sanity",
3
- "version": "0.1.0-beta.104",
3
+ "version": "0.1.0-beta.105",
4
4
  "description": "Sanity CMS integration for Farm.js",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -27,18 +27,18 @@
27
27
  "dependencies": {
28
28
  "@sanity/image-url": "^2.1.1",
29
29
  "@sanity/webhook": "^4.0.4",
30
- "@farm.js/core": "0.1.0-beta.104",
31
- "@farm.js/integration-utils": "0.1.0-beta.104"
30
+ "@farm.js/core": "0.1.0-beta.105",
31
+ "@farm.js/integration-utils": "0.1.0-beta.105"
32
32
  },
33
33
  "devDependencies": {
34
34
  "@sanity/client": "^8.4.0",
35
35
  "typescript": "^5.3.3",
36
36
  "vitest": "^3.2.7",
37
- "@farm.js/content": "0.1.0-beta.0"
37
+ "@farm.js/content": "0.1.0-beta.1"
38
38
  },
39
39
  "peerDependencies": {
40
40
  "@sanity/client": "^7.0.0 || ^8.0.0",
41
- "@farm.js/content": "^0.1.0-beta.0"
41
+ "@farm.js/content": "^0.1.0-beta.1"
42
42
  },
43
43
  "peerDependenciesMeta": {
44
44
  "@farm.js/content": {