payload-translate 1.0.3 → 1.0.5
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.
|
@@ -91,8 +91,8 @@ export const translateHandler = async (req)=>{
|
|
|
91
91
|
});
|
|
92
92
|
// Apply translations to document
|
|
93
93
|
const updatedData = applyTranslations(document, translatableFields, translations);
|
|
94
|
-
// Remove
|
|
95
|
-
const
|
|
94
|
+
// Remove all system/immutable fields recursively
|
|
95
|
+
const dataToUpdate = removeSystemFields(updatedData);
|
|
96
96
|
// Update document in target locale
|
|
97
97
|
await payload.update({
|
|
98
98
|
id: documentId,
|
|
@@ -117,5 +117,31 @@ export const translateHandler = async (req)=>{
|
|
|
117
117
|
});
|
|
118
118
|
}
|
|
119
119
|
};
|
|
120
|
+
/**
|
|
121
|
+
* Recursively removes system fields (id, createdAt, updatedAt) from an object
|
|
122
|
+
*/ function removeSystemFields(obj) {
|
|
123
|
+
const result = {};
|
|
124
|
+
for (const [key, value] of Object.entries(obj)){
|
|
125
|
+
// Skip system fields at any level
|
|
126
|
+
if (key === 'id' || key === 'createdAt' || key === 'updatedAt') {
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
if (Array.isArray(value)) {
|
|
130
|
+
// Process array items
|
|
131
|
+
result[key] = value.map((item)=>{
|
|
132
|
+
if (item && typeof item === 'object' && !Array.isArray(item)) {
|
|
133
|
+
return removeSystemFields(item);
|
|
134
|
+
}
|
|
135
|
+
return item;
|
|
136
|
+
});
|
|
137
|
+
} else if (value && typeof value === 'object') {
|
|
138
|
+
// Recursively process nested objects
|
|
139
|
+
result[key] = removeSystemFields(value);
|
|
140
|
+
} else {
|
|
141
|
+
result[key] = value;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return result;
|
|
145
|
+
}
|
|
120
146
|
|
|
121
147
|
//# sourceMappingURL=translateHandler.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/endpoints/translateHandler.ts"],"sourcesContent":["import type { PayloadHandler } from 'payload'\n\nimport type { TranslateRequestBody, TranslateResponse } from '../types.js'\n\nimport { translateWithGemini } from '../services/gemini.js'\nimport { applyTranslations } from '../utils/applyTranslations.js'\nimport { extractTranslatableFields } from '../utils/extractTranslatableFields.js'\n\nexport const translateHandler: PayloadHandler = async (req) => {\n try {\n const { payload, user } = req\n\n // Check authentication\n if (!user) {\n return Response.json({ error: 'Unauthorized', success: false } as TranslateResponse, {\n status: 401,\n })\n }\n\n // Parse request body\n const body = (await req.json?.()) as TranslateRequestBody | undefined\n if (!body) {\n return Response.json(\n { error: 'Invalid request body', success: false } as TranslateResponse,\n { status: 400 },\n )\n }\n const { collection, documentId, sourceLocale, targetLocales } = body\n\n // Validate request\n if (!collection || !documentId || !sourceLocale || !targetLocales || targetLocales.length === 0) {\n return Response.json(\n { error: 'Missing required fields', success: false } as TranslateResponse,\n { status: 400 },\n )\n }\n\n // Get API key from config\n const apiKey = (payload.config.custom as Record<string, unknown>)?.translateApiKey as\n | string\n | undefined\n if (!apiKey) {\n return Response.json(\n { error: 'Translation API key not configured', success: false } as TranslateResponse,\n { status: 500 },\n )\n }\n\n // Fetch document in source locale\n const document = await payload.findByID({\n id: documentId,\n collection,\n depth: 0,\n locale: sourceLocale,\n })\n\n if (!document) {\n return Response.json({ error: 'Document not found', success: false } as TranslateResponse, {\n status: 404,\n })\n }\n\n // Get collection config to find localized fields\n const collectionConfig = payload.collections[collection]?.config\n if (!collectionConfig) {\n return Response.json({ error: 'Collection not found', success: false } as TranslateResponse, {\n status: 404,\n })\n }\n\n // Extract translatable fields\n const translatableFields = extractTranslatableFields(\n document as Record<string, unknown>,\n collectionConfig.fields,\n )\n\n if (translatableFields.length === 0) {\n return Response.json({\n message: 'No translatable fields found',\n success: true,\n translatedFields: 0,\n translatedLocales: 0,\n } as TranslateResponse)\n }\n\n // Prepare texts for translation\n const texts = translatableFields.map((f) => f.value)\n\n // Translate to each target locale\n for (const targetLocale of targetLocales) {\n // Translate with Gemini\n const translations = await translateWithGemini({\n apiKey,\n sourceLocale,\n targetLocale,\n texts,\n })\n\n // Apply translations to document\n const updatedData = applyTranslations(\n document as Record<string, unknown>,\n translatableFields,\n translations,\n )\n\n // Remove
|
|
1
|
+
{"version":3,"sources":["../../src/endpoints/translateHandler.ts"],"sourcesContent":["import type { PayloadHandler } from 'payload'\n\nimport type { TranslateRequestBody, TranslateResponse } from '../types.js'\n\nimport { translateWithGemini } from '../services/gemini.js'\nimport { applyTranslations } from '../utils/applyTranslations.js'\nimport { extractTranslatableFields } from '../utils/extractTranslatableFields.js'\n\nexport const translateHandler: PayloadHandler = async (req) => {\n try {\n const { payload, user } = req\n\n // Check authentication\n if (!user) {\n return Response.json({ error: 'Unauthorized', success: false } as TranslateResponse, {\n status: 401,\n })\n }\n\n // Parse request body\n const body = (await req.json?.()) as TranslateRequestBody | undefined\n if (!body) {\n return Response.json(\n { error: 'Invalid request body', success: false } as TranslateResponse,\n { status: 400 },\n )\n }\n const { collection, documentId, sourceLocale, targetLocales } = body\n\n // Validate request\n if (!collection || !documentId || !sourceLocale || !targetLocales || targetLocales.length === 0) {\n return Response.json(\n { error: 'Missing required fields', success: false } as TranslateResponse,\n { status: 400 },\n )\n }\n\n // Get API key from config\n const apiKey = (payload.config.custom as Record<string, unknown>)?.translateApiKey as\n | string\n | undefined\n if (!apiKey) {\n return Response.json(\n { error: 'Translation API key not configured', success: false } as TranslateResponse,\n { status: 500 },\n )\n }\n\n // Fetch document in source locale\n const document = await payload.findByID({\n id: documentId,\n collection,\n depth: 0,\n locale: sourceLocale,\n })\n\n if (!document) {\n return Response.json({ error: 'Document not found', success: false } as TranslateResponse, {\n status: 404,\n })\n }\n\n // Get collection config to find localized fields\n const collectionConfig = payload.collections[collection]?.config\n if (!collectionConfig) {\n return Response.json({ error: 'Collection not found', success: false } as TranslateResponse, {\n status: 404,\n })\n }\n\n // Extract translatable fields\n const translatableFields = extractTranslatableFields(\n document as Record<string, unknown>,\n collectionConfig.fields,\n )\n\n if (translatableFields.length === 0) {\n return Response.json({\n message: 'No translatable fields found',\n success: true,\n translatedFields: 0,\n translatedLocales: 0,\n } as TranslateResponse)\n }\n\n // Prepare texts for translation\n const texts = translatableFields.map((f) => f.value)\n\n // Translate to each target locale\n for (const targetLocale of targetLocales) {\n // Translate with Gemini\n const translations = await translateWithGemini({\n apiKey,\n sourceLocale,\n targetLocale,\n texts,\n })\n\n // Apply translations to document\n const updatedData = applyTranslations(\n document as Record<string, unknown>,\n translatableFields,\n translations,\n )\n\n // Remove all system/immutable fields recursively\n const dataToUpdate = removeSystemFields(updatedData)\n\n // Update document in target locale\n await payload.update({\n id: documentId,\n collection,\n data: dataToUpdate,\n locale: targetLocale,\n })\n }\n\n return Response.json({\n message: `Successfully translated ${translatableFields.length} field(s) to ${targetLocales.length} locale(s)`,\n success: true,\n translatedFields: translatableFields.length,\n translatedLocales: targetLocales.length,\n } as TranslateResponse)\n } catch (error) {\n console.error('Translation error:', error)\n return Response.json(\n {\n error: error instanceof Error ? error.message : 'Translation failed',\n success: false,\n } as TranslateResponse,\n { status: 500 },\n )\n }\n}\n\n/**\n * Recursively removes system fields (id, createdAt, updatedAt) from an object\n */\nfunction removeSystemFields(obj: Record<string, unknown>): Record<string, unknown> {\n const result: Record<string, unknown> = {}\n\n for (const [key, value] of Object.entries(obj)) {\n // Skip system fields at any level\n if (key === 'id' || key === 'createdAt' || key === 'updatedAt') {\n continue\n }\n\n if (Array.isArray(value)) {\n // Process array items\n result[key] = value.map((item) => {\n if (item && typeof item === 'object' && !Array.isArray(item)) {\n return removeSystemFields(item as Record<string, unknown>)\n }\n return item\n })\n } else if (value && typeof value === 'object') {\n // Recursively process nested objects\n result[key] = removeSystemFields(value as Record<string, unknown>)\n } else {\n result[key] = value\n }\n }\n\n return result\n}\n"],"names":["translateWithGemini","applyTranslations","extractTranslatableFields","translateHandler","req","payload","user","Response","json","error","success","status","body","collection","documentId","sourceLocale","targetLocales","length","apiKey","config","custom","translateApiKey","document","findByID","id","depth","locale","collectionConfig","collections","translatableFields","fields","message","translatedFields","translatedLocales","texts","map","f","value","targetLocale","translations","updatedData","dataToUpdate","removeSystemFields","update","data","console","Error","obj","result","key","Object","entries","Array","isArray","item"],"mappings":"AAIA,SAASA,mBAAmB,QAAQ,wBAAuB;AAC3D,SAASC,iBAAiB,QAAQ,gCAA+B;AACjE,SAASC,yBAAyB,QAAQ,wCAAuC;AAEjF,OAAO,MAAMC,mBAAmC,OAAOC;IACrD,IAAI;QACF,MAAM,EAAEC,OAAO,EAAEC,IAAI,EAAE,GAAGF;QAE1B,uBAAuB;QACvB,IAAI,CAACE,MAAM;YACT,OAAOC,SAASC,IAAI,CAAC;gBAAEC,OAAO;gBAAgBC,SAAS;YAAM,GAAwB;gBACnFC,QAAQ;YACV;QACF;QAEA,qBAAqB;QACrB,MAAMC,OAAQ,MAAMR,IAAII,IAAI;QAC5B,IAAI,CAACI,MAAM;YACT,OAAOL,SAASC,IAAI,CAClB;gBAAEC,OAAO;gBAAwBC,SAAS;YAAM,GAChD;gBAAEC,QAAQ;YAAI;QAElB;QACA,MAAM,EAAEE,UAAU,EAAEC,UAAU,EAAEC,YAAY,EAAEC,aAAa,EAAE,GAAGJ;QAEhE,mBAAmB;QACnB,IAAI,CAACC,cAAc,CAACC,cAAc,CAACC,gBAAgB,CAACC,iBAAiBA,cAAcC,MAAM,KAAK,GAAG;YAC/F,OAAOV,SAASC,IAAI,CAClB;gBAAEC,OAAO;gBAA2BC,SAAS;YAAM,GACnD;gBAAEC,QAAQ;YAAI;QAElB;QAEA,0BAA0B;QAC1B,MAAMO,SAAUb,QAAQc,MAAM,CAACC,MAAM,EAA8BC;QAGnE,IAAI,CAACH,QAAQ;YACX,OAAOX,SAASC,IAAI,CAClB;gBAAEC,OAAO;gBAAsCC,SAAS;YAAM,GAC9D;gBAAEC,QAAQ;YAAI;QAElB;QAEA,kCAAkC;QAClC,MAAMW,WAAW,MAAMjB,QAAQkB,QAAQ,CAAC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