gt-sanity 4.0.9 → 4.0.11

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.
package/dist/index.d.ts CHANGED
@@ -175,10 +175,14 @@ declare type FieldMatcher = {
175
175
  }[];
176
176
  };
177
177
 
178
+ /**
179
+ * Resolves the draft for a document id, falling back to the published copy.
180
+ * Returns `undefined` when neither exists.
181
+ */
178
182
  export declare const findLatestDraft: (
179
183
  documentId: string,
180
184
  client: SanityClient,
181
- ) => Promise<SanityDocument>;
185
+ ) => Promise<SanityDocument | undefined>;
182
186
 
183
187
  declare const gt: GT;
184
188
 
package/dist/index.js CHANGED
@@ -297,6 +297,22 @@ const findLatestDraft = (documentId, client) => {
297
297
  const baseUrl = `/data/history/${client.config().dataset}/documents/${documentId}?revision=${rev}`, url = client.getUrl(baseUrl);
298
298
  return await fetch(url, { credentials: "include" }).then((req) => req.json()).then((req) => req.documents && req.documents.length ? req.documents[0] : null);
299
299
  };
300
+ async function requireSourceDocument(documentId, versionId, client) {
301
+ let doc = null;
302
+ if (documentId && versionId && (doc = await findDocumentAtRevision(documentId, versionId, client)), doc || (doc = await findLatestDraft(documentId, client)), !doc)
303
+ throw new Error(missingDocumentDiagnostic(documentId));
304
+ return doc;
305
+ }
306
+ function missingDocumentDiagnostic(documentId) {
307
+ return createDiagnosticMessage({
308
+ source: "gt-sanity",
309
+ severity: "Error",
310
+ whatHappened: "Could not find the document to translate",
311
+ why: "no draft or published version of it exists in this dataset",
312
+ fix: "Check that the document was not deleted, and that the Studio is pointed at the dataset that holds it",
313
+ details: [`Document ID: ${documentId}`]
314
+ });
315
+ }
300
316
  function forEachMatchingField(documentId, document2, fields, callback) {
301
317
  const applicable = fields.filter(
302
318
  (field) => field.documentId === documentId || field.documentId === void 0 || field.documentId === null
@@ -519,12 +535,11 @@ async function patchI18nDoc(sourceDocumentId, i18nDocId, sourceDocument, mergedD
519
535
  }
520
536
  const documentLevelPatch = async (docInfo, translatedFields, localeId, client, languageField = "language", mergeWithTargetLocale = !1) => {
521
537
  const baseLanguage = pluginConfig.getSourceLocale();
522
- let baseDoc = null, i18nDoc = null;
523
- docInfo.documentId && docInfo.versionId && (baseDoc = await findDocumentAtRevision(
538
+ let i18nDoc, baseDoc = await requireSourceDocument(
524
539
  docInfo.documentId,
525
540
  docInfo.versionId,
526
541
  client
527
- )), baseDoc || (baseDoc = await findLatestDraft(docInfo.documentId, client));
542
+ );
528
543
  const i18nDocId = (await getOrCreateTranslationMetadata(
529
544
  docInfo.documentId,
530
545
  baseDoc,
@@ -533,11 +548,11 @@ const documentLevelPatch = async (docInfo, translatedFields, localeId, client, l
533
548
  )).translations.find(
534
549
  (translation) => translation.language === localeId
535
550
  )?.value?._ref;
536
- i18nDocId && (i18nDoc = await findLatestDraft(i18nDocId, client)), mergeWithTargetLocale && i18nDoc ? baseDoc = i18nDoc : docInfo.documentId && docInfo.versionId && (baseDoc = await findDocumentAtRevision(
551
+ i18nDocId && (i18nDoc = await findLatestDraft(i18nDocId, client)), mergeWithTargetLocale && i18nDoc ? baseDoc = i18nDoc : docInfo.documentId && docInfo.versionId && (baseDoc = await requireSourceDocument(
537
552
  docInfo.documentId,
538
553
  docInfo.versionId,
539
554
  client
540
- )), baseDoc || (baseDoc = await findLatestDraft(docInfo.documentId, client));
555
+ ));
541
556
  const merged = BaseDocumentMerger.documentLevelMerge(
542
557
  translatedFields,
543
558
  baseDoc
@@ -560,9 +575,8 @@ const documentLevelPatch = async (docInfo, translatedFields, localeId, client, l
560
575
  baseLanguage
561
576
  ), freshI18nDocId = freshTranslationMetadata.translations.find(
562
577
  (translation) => translation.language === localeId
563
- )?.value?._ref;
564
- if (freshI18nDocId) {
565
- const freshI18nDoc = await findLatestDraft(freshI18nDocId, client);
578
+ )?.value?._ref, freshI18nDoc = freshI18nDocId ? await findLatestDraft(freshI18nDocId, client) : void 0;
579
+ if (freshI18nDoc) {
566
580
  await patchI18nDoc(
567
581
  docInfo.documentId,
568
582
  freshI18nDoc._id,
@@ -2719,7 +2733,7 @@ const useTranslations = () => {
2719
2733
  ] }) })
2720
2734
  }
2721
2735
  ) : null;
2722
- var version = "4.0.9";
2736
+ var version = "4.0.11";
2723
2737
  function buildDebugInfo({
2724
2738
  secrets,
2725
2739
  preferences,