gt-sanity 1.0.11 → 1.1.1

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,22 +1,8 @@
1
1
  import { gt, overrideConfig } from '../adapter/core';
2
+ import { FileProperties } from '../adapter/types';
3
+ import { DownloadedFile } from 'generaltranslation/types';
2
4
  import type { Secrets } from '../types';
3
5
 
4
- export type BatchedFiles = Array<{
5
- documentId: string;
6
- versionId: string;
7
- translationId: string;
8
- locale: string;
9
- }>;
10
-
11
- export type DownloadedFile = {
12
- docData: {
13
- documentId: string;
14
- versionId: string;
15
- translationId: string;
16
- locale: string;
17
- };
18
- data: string;
19
- };
20
6
  /**
21
7
  * Downloads multiple translation files in a single batch request
22
8
  * @param files - Array of files to download with their output paths
@@ -25,38 +11,27 @@ export type DownloadedFile = {
25
11
  * @returns Object containing successful and failed file IDs
26
12
  */
27
13
  export async function downloadTranslations(
28
- files: BatchedFiles,
14
+ files: FileProperties[],
29
15
  secrets: Secrets,
30
16
  maxRetries = 3,
31
17
  retryDelay = 1000
32
18
  ): Promise<DownloadedFile[]> {
33
19
  overrideConfig(secrets);
34
20
  let retries = 0;
35
- const fileIds = files.map((file) => file.translationId);
36
-
37
- const map = new Map(files.map((file) => [file.translationId, file]));
38
- const result = [] as DownloadedFile[];
39
21
 
40
22
  while (retries <= maxRetries) {
41
23
  try {
42
24
  // Download the files
43
- const responseData = await gt.downloadFileBatch(fileIds);
25
+ const responseData = await gt.downloadFileBatch(
26
+ files.map((file) => ({
27
+ fileId: file.fileId,
28
+ branchId: file.branchId,
29
+ versionId: file.versionId,
30
+ locale: file.locale,
31
+ }))
32
+ );
44
33
  const downloadedFiles = responseData.files || [];
45
-
46
- // Process each file in the response
47
- for (const file of downloadedFiles) {
48
- const documentData = map.get(file.id);
49
- if (!documentData) {
50
- continue;
51
- }
52
-
53
- result.push({
54
- docData: documentData,
55
- data: file.data,
56
- });
57
- }
58
-
59
- return result;
34
+ return downloadedFiles;
60
35
  } catch (error) {
61
36
  // Increment retry counter and wait before next attempt
62
37
  retries++;
@@ -64,5 +39,5 @@ export async function downloadTranslations(
64
39
  }
65
40
  }
66
41
 
67
- return result;
42
+ return [];
68
43
  }
@@ -27,14 +27,17 @@ export async function initProject(
27
27
  let setupFailedMessage: string | null = null;
28
28
 
29
29
  while (true) {
30
- const status = await gt.checkSetupStatus(setupJobId);
31
-
32
- if (status.status === 'completed') {
30
+ const status = await gt.checkJobStatus([setupJobId]);
31
+ if (!status[0]) {
32
+ setupFailedMessage = 'Unknown error';
33
+ break;
34
+ }
35
+ if (status[0].status === 'completed') {
33
36
  setupCompleted = true;
34
37
  break;
35
38
  }
36
- if (status.status === 'failed') {
37
- setupFailedMessage = status.error?.message || 'Unknown error';
39
+ if (status[0].status === 'failed') {
40
+ setupFailedMessage = status[0].error?.message || 'Unknown error';
38
41
  break;
39
42
  }
40
43
  if (Date.now() - start > setupTimeoutMs) {
@@ -1,16 +1,8 @@
1
1
  import { SanityDocument } from 'sanity';
2
- import { GTFile, Secrets, TranslationFunctionContext } from '../types';
3
- import {
4
- downloadTranslations,
5
- BatchedFiles,
6
- } from '../translation/downloadTranslations';
2
+ import { Secrets, TranslationFunctionContext } from '../types';
3
+ import { downloadTranslations } from '../translation/downloadTranslations';
7
4
  import { processImportBatch, ImportBatchItem } from './batchProcessor';
8
-
9
- export interface TranslationStatus {
10
- progress: number;
11
- isReady: boolean;
12
- translationId?: string;
13
- }
5
+ import type { FileProperties, TranslationStatus } from '../adapter/types';
14
6
 
15
7
  export interface ImportResult {
16
8
  successCount: number;
@@ -25,32 +17,20 @@ export interface ImportOptions {
25
17
  }
26
18
 
27
19
  export async function getReadyFilesForImport(
28
- documents: SanityDocument[],
29
20
  translationStatuses: Map<string, TranslationStatus>,
30
21
  options: ImportOptions = {}
31
- ): Promise<BatchedFiles> {
22
+ ): Promise<FileProperties[]> {
32
23
  const { filterReadyFiles = () => true } = options;
33
- const readyFiles: BatchedFiles = [];
24
+ const readyFiles: FileProperties[] = [];
34
25
 
35
26
  for (const [key, status] of translationStatuses.entries()) {
36
- if (
37
- status.isReady &&
38
- status.translationId &&
39
- filterReadyFiles(key, status)
40
- ) {
41
- const [documentId, locale] = key.split(':');
42
- const document = documents.find(
43
- (doc) => (doc._id?.replace('drafts.', '') || doc._id) === documentId
44
- );
45
-
46
- if (document) {
47
- readyFiles.push({
48
- documentId,
49
- versionId: document._rev,
50
- translationId: status.translationId,
51
- locale,
52
- });
53
- }
27
+ if (status.isReady && filterReadyFiles(key, status)) {
28
+ readyFiles.push({
29
+ fileId: status.fileData.fileId,
30
+ versionId: status.fileData.versionId,
31
+ branchId: status.fileData.branchId,
32
+ locale: status.fileData.locale,
33
+ });
54
34
  }
55
35
  }
56
36
 
@@ -58,7 +38,7 @@ export async function getReadyFilesForImport(
58
38
  }
59
39
 
60
40
  export async function importTranslations(
61
- readyFiles: BatchedFiles,
41
+ readyFiles: FileProperties[],
62
42
  secrets: Secrets,
63
43
  translationContext: TranslationFunctionContext,
64
44
  options: ImportOptions = {}
@@ -71,13 +51,13 @@ export async function importTranslations(
71
51
 
72
52
  const importItems: ImportBatchItem[] = downloadedFiles.map((file) => ({
73
53
  docInfo: {
74
- documentId: file.docData.documentId,
75
- versionId: file.docData.versionId,
54
+ documentId: file.fileId,
55
+ versionId: file.versionId,
76
56
  },
77
- locale: file.docData.locale,
57
+ locale: file.locale!,
78
58
  data: file.data,
79
59
  translationContext,
80
- key: `${file.docData.documentId}:${file.docData.locale}`,
60
+ key: `${file.branchId}:${file.fileId}:${file.versionId}:${file.locale}`,
81
61
  }));
82
62
 
83
63
  const result = await processImportBatch(importItems, {