@justanarthur/payload-plugin-translator 3.0.3 → 3.1.0

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.js CHANGED
@@ -1,15 +1,21 @@
1
- import"./shared/chunk-fsveh29m.js";
2
1
  import {
3
- createAutoTranslateCollectionHook,
4
- createAutoTranslateGlobalHook,
5
- createTranslateTask,
6
- createTranslateWorkflow,
7
- translateOperation
8
- } from "./shared/chunk-q51q8rd2.js";
9
- import"./shared/chunk-zhrw0jnz.js";
10
- import"./shared/chunk-97ktc51q.js";
11
- import"./shared/chunk-ecnph6hw.js";
12
- import"./shared/chunk-frnemj69.js";
2
+ findEntityWithConfig,
3
+ translateOperation2,
4
+ createAutoTranslateCollectionHook2,
5
+ createAutoTranslateGlobalHook2,
6
+ recordTranslationStatus,
7
+ createTranslateTask2,
8
+ createTranslateWorkflow2
9
+ } from "./shared/chunk-p03jz82h.js";
10
+ import"./shared/chunk-9d433kmv.js";
11
+ import {
12
+ readLocales,
13
+ parseEntityKey
14
+ } from "./shared/chunk-15n5q7wd.js";
15
+ import {
16
+ TRANSLATION_STATUS_SLUG2,
17
+ REVIEW_VIEW_PATH
18
+ } from "./shared/chunk-31qgv1zk.js";
13
19
 
14
20
  // src/index.ts
15
21
  import { deepMerge } from "payload/shared";
@@ -112,14 +118,115 @@ var translations = {
112
118
  }
113
119
  };
114
120
 
115
- // src/translate/endpoint.ts
121
+ // src/review/collection.ts
122
+ var signedIn = ({ req }) => Boolean(req.user);
123
+ var createTranslationStatusCollection = () => ({
124
+ slug: TRANSLATION_STATUS_SLUG2,
125
+ admin: {
126
+ hidden: true
127
+ },
128
+ access: {
129
+ create: signedIn,
130
+ delete: signedIn,
131
+ read: signedIn,
132
+ update: signedIn
133
+ },
134
+ fields: [
135
+ { name: "entity", type: "text", required: true, index: true },
136
+ { name: "locale", type: "text", required: true, index: true },
137
+ { name: "sourceHash", type: "text" },
138
+ { name: "translatedAt", type: "date" },
139
+ { name: "reviewedHash", type: "text" },
140
+ { name: "reviewedAt", type: "date" },
141
+ { name: "reviewedBy", type: "text" }
142
+ ]
143
+ });
144
+
145
+ // src/review/endpoints.ts
116
146
  import { APIError } from "payload";
117
- var translateEndpoint = async (req) => {
147
+ var readReviewRequest = async (req) => {
148
+ if (!req.user)
149
+ throw new APIError("Unauthorized", 401);
118
150
  if (!req.json)
119
151
  throw new APIError("Content-Type should be json");
152
+ const body = await req.json();
153
+ const { targetLocales } = readLocales(req);
154
+ if (!body.entity || !body.locale || !targetLocales.includes(body.locale))
155
+ throw new APIError("Bad Request", 400);
156
+ const target = parseEntityKey(body.entity);
157
+ const pluginConfig = req.payload.config.custom?.translator;
158
+ const allowed = target.globalSlug ? pluginConfig?.globals.includes(target.globalSlug) : pluginConfig?.collections.includes(target.collectionSlug);
159
+ if (!allowed || !pluginConfig)
160
+ throw new APIError("Forbidden", 403);
161
+ return { ...body, ...target, locale: body.locale, pluginConfig };
162
+ };
163
+ var translateHandler = async (req) => {
164
+ const { collectionSlug, globalSlug, id, locale, mode, pluginConfig } = await readReviewRequest(req);
165
+ const { defaultLocale } = readLocales(req);
166
+ const resolver = pluginConfig.resolvers[0]?.key;
167
+ if (!resolver)
168
+ throw new APIError("No resolver configured", 400);
169
+ const result = await translateOperation2({
170
+ req,
171
+ collectionSlug,
172
+ globalSlug,
173
+ id,
174
+ emptyOnly: mode !== "all",
175
+ locale,
176
+ localeFrom: defaultLocale,
177
+ overrideAccess: false,
178
+ resolver,
179
+ update: true
180
+ });
181
+ if (result.success) {
182
+ const { config } = await findEntityWithConfig({ collectionSlug, globalSlug, id, locale: defaultLocale, req });
183
+ await recordTranslationStatus({ req, collectionSlug, globalSlug, id, locale, config, dataFrom: result.dataFrom });
184
+ }
185
+ return Response.json({ success: result.success });
186
+ };
187
+ var markReviewedHandler = async (req) => {
188
+ const { collectionSlug, globalSlug, id, locale } = await readReviewRequest(req);
189
+ const { defaultLocale } = readLocales(req);
190
+ const { config, doc } = await findEntityWithConfig({
191
+ collectionSlug,
192
+ globalSlug,
193
+ id,
194
+ locale: defaultLocale,
195
+ overrideAccess: false,
196
+ req
197
+ });
198
+ const user = req.user;
199
+ await recordTranslationStatus({
200
+ req,
201
+ collectionSlug,
202
+ globalSlug,
203
+ id,
204
+ locale,
205
+ config,
206
+ dataFrom: doc,
207
+ reviewedBy: user.email ?? String(user.id)
208
+ });
209
+ return Response.json({ success: true });
210
+ };
211
+ var reviewEndpoints = [
212
+ { path: "/translator/review/translate", method: "post", handler: translateHandler },
213
+ { path: "/translator/review/mark-reviewed", method: "post", handler: markReviewedHandler }
214
+ ];
215
+
216
+ // src/translate/endpoint.ts
217
+ import { APIError as APIError2 } from "payload";
218
+ var translateEndpoint = async (req) => {
219
+ if (!req.user)
220
+ throw new APIError2("Unauthorized", 401);
221
+ if (!req.json)
222
+ throw new APIError2("Content-Type should be json");
120
223
  const args = await req.json();
121
224
  const { collectionSlug, data, emptyOnly, globalSlug, id, locale, localeFrom, resolver } = args;
122
- const result = await translateOperation({
225
+ const pluginConfig = req.payload.config.custom?.translator;
226
+ const allowed = globalSlug ? pluginConfig?.globals.includes(globalSlug) : pluginConfig?.collections.includes(collectionSlug);
227
+ if (!allowed)
228
+ throw new APIError2("Forbidden", 403);
229
+ const result = await translateOperation2({
123
230
  collectionSlug,
124
231
  data,
125
232
  emptyOnly,
@@ -141,10 +248,27 @@ var translator = (pluginConfig) => {
141
248
  if (pluginConfig.disabled || !config.localization || config.localization.locales.length < 2)
142
249
  return config;
143
250
  const autoTranslate = pluginConfig.autoTranslate ?? true;
251
+ const review = pluginConfig.review ?? true;
144
252
  const updatedConfig = {
145
253
  ...config,
146
254
  admin: {
147
255
  ...config.admin ?? {},
256
+ ...review ? {
257
+ components: {
258
+ ...config.admin?.components ?? {},
259
+ afterNavLinks: [
260
+ ...config.admin?.components?.afterNavLinks ?? [],
261
+ "@justanarthur/payload-plugin-translator/client#TranslationsNavLink"
262
+ ],
263
+ views: {
264
+ ...config.admin?.components?.views ?? {},
265
+ translations: {
266
+ Component: "@justanarthur/payload-plugin-translator/rsc#TranslationsView",
267
+ path: REVIEW_VIEW_PATH
268
+ }
269
+ }
270
+ }
271
+ } : {},
148
272
  custom: {
149
273
  ...config.admin?.custom ?? {},
150
274
  translator: {
@@ -152,27 +276,30 @@ var translator = (pluginConfig) => {
152
276
  }
153
277
  }
154
278
  },
155
- collections: config.collections?.map((collection) => {
156
- if (!pluginConfig.collections.includes(collection.slug))
157
- return collection;
158
- return {
159
- ...collection,
160
- admin: {
161
- ...collection.admin ?? {},
162
- components: {
163
- ...collection.admin?.components ?? {},
164
- edit: {
165
- ...collection.admin?.components?.edit ?? {},
166
- PublishButton: CustomButton("publish"),
167
- SaveButton: CustomButton("save")
279
+ collections: [
280
+ ...config.collections?.map((collection) => {
281
+ if (!pluginConfig.collections.includes(collection.slug))
282
+ return collection;
283
+ return {
284
+ ...collection,
285
+ admin: {
286
+ ...collection.admin ?? {},
287
+ components: {
288
+ ...collection.admin?.components ?? {},
289
+ edit: {
290
+ ...collection.admin?.components?.edit ?? {},
291
+ PublishButton: CustomButton("publish"),
292
+ SaveButton: CustomButton("save")
293
+ }
168
294
  }
169
- }
170
- },
171
- ...autoTranslate ? {
172
- hooks: attachCollectionHook(collection.hooks, createAutoTranslateCollectionHook({ collectionSlug: String(collection.slug) }).afterChange)
173
- } : {}
174
- };
175
- }) ?? [],
295
+ },
296
+ ...autoTranslate ? {
297
+ hooks: attachCollectionHook(collection.hooks, createAutoTranslateCollectionHook2({ collectionSlug: String(collection.slug) }).afterChange)
298
+ } : {}
299
+ };
300
+ }) ?? [],
301
+ ...review ? [createTranslationStatusCollection()] : []
302
+ ],
176
303
  custom: {
177
304
  ...config.custom ?? {},
178
305
  translator: {
@@ -185,7 +312,8 @@ var translator = (pluginConfig) => {
185
312
  handler: translateEndpoint,
186
313
  method: "post",
187
314
  path: "/translator/translate"
188
- }
315
+ },
316
+ ...review ? reviewEndpoints : []
189
317
  ],
190
318
  globals: config.globals?.map((global) => {
191
319
  if (!pluginConfig.globals.includes(global.slug))
@@ -204,7 +332,7 @@ var translator = (pluginConfig) => {
204
332
  }
205
333
  },
206
334
  ...autoTranslate ? {
207
- hooks: attachGlobalHook(global.hooks, createAutoTranslateGlobalHook({ globalSlug: String(global.slug) }))
335
+ hooks: attachGlobalHook(global.hooks, createAutoTranslateGlobalHook2({ globalSlug: String(global.slug) }))
208
336
  } : {}
209
337
  };
210
338
  }) ?? [],
@@ -217,8 +345,8 @@ var translator = (pluginConfig) => {
217
345
  ...autoTranslate ? {
218
346
  jobs: {
219
347
  ...config.jobs ?? {},
220
- tasks: ensureJobBySlug(config.jobs?.tasks, createTranslateTask()),
221
- workflows: ensureJobBySlug(config.jobs?.workflows, createTranslateWorkflow()),
348
+ tasks: ensureJobBySlug(config.jobs?.tasks, createTranslateTask2()),
349
+ workflows: ensureJobBySlug(config.jobs?.workflows, createTranslateWorkflow2()),
222
350
  deleteJobOnComplete: false,
223
351
  jobsCollectionOverrides: ({ defaultJobsCollection }) => {
224
352
  defaultJobsCollection.admin = {
@@ -270,11 +398,12 @@ function markAutoTranslate(hook) {
270
398
  function isMarkedAutoTranslate(hook) {
271
399
  return Boolean(hook?.[AUTO_TRANSLATE_MARKER]);
272
400
  }
273
-
274
401
  // src/exports/index.ts
275
402
  var exports_default = translator;
276
403
  export {
277
- translator,
278
- translateOperation,
279
- exports_default as default
404
+ TRANSLATION_STATUS_SLUG2 as TRANSLATION_STATUS_SLUG,
405
+ createTranslationStatusCollection,
406
+ exports_default as default,
407
+ translateOperation2 as translateOperation,
408
+ translator
280
409
  };
package/dist/jobs.d.ts CHANGED
@@ -47,4 +47,4 @@ declare const jobs: {
47
47
  createAutoTranslateCollectionHook: typeof createAutoTranslateCollectionHook;
48
48
  createAutoTranslateGlobalHook: typeof createAutoTranslateGlobalHook;
49
49
  };
50
- export { jobs as default, createTranslateWorkflow, createTranslateTask, createAutoTranslateGlobalHook, createAutoTranslateCollectionHook, TranslateWorkflowConfig, TranslateTaskConfig, TRANSLATE_WORKFLOW_SLUG, TRANSLATE_TASK_SLUG, CreateTranslateWorkflowOptions, CreateTranslateTaskOptions, CreateAutoTranslateGlobalHookOptions, CreateAutoTranslateCollectionHookOptions };
50
+ export { CreateAutoTranslateCollectionHookOptions, CreateAutoTranslateGlobalHookOptions, CreateTranslateTaskOptions, CreateTranslateWorkflowOptions, TRANSLATE_TASK_SLUG, TRANSLATE_WORKFLOW_SLUG, TranslateTaskConfig, TranslateWorkflowConfig, createAutoTranslateCollectionHook, createAutoTranslateGlobalHook, createTranslateTask, createTranslateWorkflow, jobs as default };
package/dist/jobs.js CHANGED
@@ -1,28 +1,29 @@
1
1
  import {
2
- TRANSLATE_TASK_SLUG,
3
- TRANSLATE_WORKFLOW_SLUG,
4
- createAutoTranslateCollectionHook,
5
- createAutoTranslateGlobalHook,
6
- createTranslateTask,
7
- createTranslateWorkflow
8
- } from "./shared/chunk-q51q8rd2.js";
2
+ TRANSLATE_TASK_SLUG2,
3
+ TRANSLATE_WORKFLOW_SLUG2,
4
+ createAutoTranslateCollectionHook2,
5
+ createAutoTranslateGlobalHook2,
6
+ createTranslateTask2,
7
+ createTranslateWorkflow2
8
+ } from "./shared/chunk-p03jz82h.js";
9
+ import"./shared/chunk-9d433kmv.js";
9
10
 
10
11
  // src/exports/jobs.ts
11
12
  var jobs = {
12
- TRANSLATE_TASK_SLUG,
13
- TRANSLATE_WORKFLOW_SLUG,
14
- createTranslateTask,
15
- createTranslateWorkflow,
16
- createAutoTranslateCollectionHook,
17
- createAutoTranslateGlobalHook
13
+ TRANSLATE_TASK_SLUG: TRANSLATE_TASK_SLUG2,
14
+ TRANSLATE_WORKFLOW_SLUG: TRANSLATE_WORKFLOW_SLUG2,
15
+ createTranslateTask: createTranslateTask2,
16
+ createTranslateWorkflow: createTranslateWorkflow2,
17
+ createAutoTranslateCollectionHook: createAutoTranslateCollectionHook2,
18
+ createAutoTranslateGlobalHook: createAutoTranslateGlobalHook2
18
19
  };
19
20
  var jobs_default = jobs;
20
21
  export {
21
- jobs_default as default,
22
- createTranslateWorkflow,
23
- createTranslateTask,
24
- createAutoTranslateGlobalHook,
25
- createAutoTranslateCollectionHook,
26
- TRANSLATE_WORKFLOW_SLUG,
27
- TRANSLATE_TASK_SLUG
22
+ TRANSLATE_TASK_SLUG2 as TRANSLATE_TASK_SLUG,
23
+ TRANSLATE_WORKFLOW_SLUG2 as TRANSLATE_WORKFLOW_SLUG,
24
+ createAutoTranslateCollectionHook2 as createAutoTranslateCollectionHook,
25
+ createAutoTranslateGlobalHook2 as createAutoTranslateGlobalHook,
26
+ createTranslateTask2 as createTranslateTask,
27
+ createTranslateWorkflow2 as createTranslateWorkflow,
28
+ jobs_default as default
28
29
  };
@@ -16,4 +16,4 @@ type TranslateResolver = {
16
16
  resolve: (args: TranslateResolverArgs) => Promise<TranslateResolverResponse> | TranslateResolverResponse;
17
17
  };
18
18
  declare const copyResolver: () => TranslateResolver;
19
- export { copyResolver as default, copyResolver };
19
+ export { copyResolver, copyResolver as default };
@@ -1,10 +1,10 @@
1
1
  import {
2
- copyResolver
3
- } from "../shared/chunk-97ktc51q.js";
2
+ copyResolver2
3
+ } from "../shared/chunk-zenq7s47.js";
4
4
 
5
5
  // src/exports/resolvers/copy.ts
6
- var copy_default = copyResolver;
6
+ var copy_default = copyResolver2;
7
7
  export {
8
- copy_default as default,
9
- copyResolver
8
+ copyResolver2 as copyResolver,
9
+ copy_default as default
10
10
  };
@@ -20,4 +20,4 @@ type GoogleResolverConfig = {
20
20
  chunkLength?: number;
21
21
  };
22
22
  declare const googleResolver: ({ apiKey, chunkLength }: GoogleResolverConfig) => TranslateResolver;
23
- export { googleResolver, googleResolver as default, GoogleResolverConfig };
23
+ export { GoogleResolverConfig, googleResolver as default, googleResolver };
@@ -1,11 +1,10 @@
1
1
  import {
2
- googleResolver
3
- } from "../shared/chunk-fsveh29m.js";
4
- import"../shared/chunk-frnemj69.js";
2
+ googleResolver2
3
+ } from "../shared/chunk-zs19h344.js";
5
4
 
6
5
  // src/exports/resolvers/google.ts
7
- var google_default = googleResolver;
6
+ var google_default = googleResolver2;
8
7
  export {
9
- googleResolver,
10
- google_default as default
8
+ google_default as default,
9
+ googleResolver2 as googleResolver
11
10
  };
@@ -21,4 +21,4 @@ type LibreResolverConfig = {
21
21
  url?: string;
22
22
  };
23
23
  declare const libreResolver: ({ apiKey, chunkLength, url }: LibreResolverConfig) => TranslateResolver;
24
- export { libreResolver, libreResolver as default, LibreResolverConfig };
24
+ export { LibreResolverConfig, libreResolver as default, libreResolver };
@@ -1,11 +1,10 @@
1
1
  import {
2
- libreResolver
3
- } from "../shared/chunk-zhrw0jnz.js";
4
- import"../shared/chunk-frnemj69.js";
2
+ libreResolver2
3
+ } from "../shared/chunk-mvqymw6n.js";
5
4
 
6
5
  // src/exports/resolvers/libreTranslate.ts
7
- var libreTranslate_default = libreResolver;
6
+ var libreTranslate_default = libreResolver2;
8
7
  export {
9
- libreResolver,
10
- libreTranslate_default as default
8
+ libreTranslate_default as default,
9
+ libreResolver2 as libreResolver
11
10
  };
@@ -24,8 +24,10 @@ type OpenAIResolverConfig = {
24
24
  apiKey: string;
25
25
  baseUrl?: string;
26
26
  chunkLength?: number;
27
+ /** chunks sent at the same time */
28
+ concurrency?: number;
27
29
  model?: string;
28
30
  prompt?: OpenAIPrompt;
29
31
  };
30
- declare const openAIResolver: ({ apiKey, baseUrl, chunkLength, model, prompt }: OpenAIResolverConfig) => TranslateResolver;
31
- export { openAIResolver, openAIResolver as default, OpenAIResolverConfig, OpenAIPrompt };
32
+ declare const openAIResolver: ({ apiKey, baseUrl, chunkLength, concurrency, model, prompt }: OpenAIResolverConfig) => TranslateResolver;
33
+ export { OpenAIPrompt, OpenAIResolverConfig, openAIResolver as default, openAIResolver };
@@ -1,11 +1,10 @@
1
1
  import {
2
- openAIResolver
3
- } from "../shared/chunk-ecnph6hw.js";
4
- import"../shared/chunk-frnemj69.js";
2
+ openAIResolver2
3
+ } from "../shared/chunk-nxddacte.js";
5
4
 
6
5
  // src/exports/resolvers/openAI.ts
7
- var openAI_default = openAIResolver;
6
+ var openAI_default = openAIResolver2;
8
7
  export {
9
- openAIResolver,
10
- openAI_default as default
8
+ openAI_default as default,
9
+ openAIResolver2 as openAIResolver
11
10
  };
@@ -15,4 +15,4 @@ type TranslateResolver = {
15
15
  key: string;
16
16
  resolve: (args: TranslateResolverArgs) => Promise<TranslateResolverResponse> | TranslateResolverResponse;
17
17
  };
18
- export { TranslateResolverResponse, TranslateResolverArgs, TranslateResolver };
18
+ export { TranslateResolver, TranslateResolverArgs, TranslateResolverResponse };
package/dist/rsc.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ import { AdminViewServerProps } from "payload";
2
+ import { JSX as JSX_1kxb } from "react";
3
+ type Element_12xgc = JSX_1kxb["Element"];
4
+ declare const TranslationsView: ({ initPageResult, params, searchParams }: AdminViewServerProps) => Promise<Element_12xgc>;
5
+ export { TranslationsView, TranslationsView as default };