diff --git a/plugin/package.json b/plugin/package.json index d4a7a01..69364e5 100644 --- a/plugin/package.json +++ b/plugin/package.json @@ -1,7 +1,7 @@ { "name": "payload-plugin-translator", "private": true, - "version": "1.0.0-beta.1", + "version": "1.0.0", "type": "module", "scripts": { "dev": "vite", @@ -31,6 +31,16 @@ "require": "./src/index.ts", "types": "./src/index.ts" }, + "./types": { + "import": "./src/types.ts", + "require": "./src/types.ts", + "types": "./src/types.ts" + }, + "./resolvers/types": { + "import": "./src/resolvers/types.ts", + "require": "./src/resolvers/types.ts", + "types": "./src/resolvers/types.ts" + }, "./resolvers/google": { "import": "./src/resolvers/google.ts", "require": "./src/resolvers/google.ts", @@ -59,6 +69,16 @@ "require": "./dist/index.js", "types": "./dist/index.d.ts" }, + "./types": { + "import": "./dist/types.js", + "require": "./dist/types.js", + "types": "./dist/types.d.ts" + }, + "./resolvers/types": { + "import": "./dist/resolvers/types.js", + "require": "./dist/resolvers/types.js", + "types": "./dist/resolvers/types.d.ts" + }, "./resolvers/google": { "import": "./dist/resolvers/google.js", "require": "./dist/resolvers/google.js", diff --git a/plugin/src/index.ts b/plugin/src/index.ts index bdcf081..85bccdf 100644 --- a/plugin/src/index.ts +++ b/plugin/src/index.ts @@ -4,9 +4,11 @@ import { deepMerge } from 'payload/utilities'; import { CustomSaveButton } from './client/components/CustomSaveButton'; import { translateEndpoint } from './translate/endpoint'; import { translations } from './translations'; -import type { PluginConfig } from './types'; +import type { TranslatorPluginConfig } from './types'; -export const payloadPluginTranslator: (pluginConfig: PluginConfig) => Plugin = (pluginConfig) => { +export const payloadPluginTranslator: (pluginConfig: TranslatorPluginConfig) => Plugin = ( + pluginConfig, +) => { return (config) => { if (pluginConfig.disabled || !config.localization || config.localization.locales.length < 2) return config; diff --git a/plugin/src/resolvers/google.ts b/plugin/src/resolvers/google.ts index 0c15161..28444b6 100644 --- a/plugin/src/resolvers/google.ts +++ b/plugin/src/resolvers/google.ts @@ -3,11 +3,13 @@ import type { TranslateResolver } from './types'; type GoogleResponse = { data: { - translations: { - detectedSourceLanguage: string; - model: string; - translatedText: string; - }[]; + data: { + translations: { + detectedSourceLanguage: string; + model: string; + translatedText: string; + }[]; + }; }; success: boolean; }; @@ -23,6 +25,10 @@ const mapLocale = (incoming: string) => export type GoogleResolverConfig = { apiKey: string; + /** + * How many texts to include into 1 request + * @default 100 + */ chunkLength?: number; }; @@ -73,7 +79,7 @@ export const googleResolver = ({ } const translatedTexts = responses - .flatMap((chunk) => chunk.data.translations) + .flatMap((chunk) => chunk.data.data.translations) .map((translation) => translation.translatedText); return { diff --git a/plugin/src/resolvers/openAI.ts b/plugin/src/resolvers/openAI.ts index d734b89..a7a52d0 100644 --- a/plugin/src/resolvers/openAI.ts +++ b/plugin/src/resolvers/openAI.ts @@ -9,7 +9,14 @@ export type OpenAIPrompt = (args: { export type OpenAIResolverConfig = { apiKey: string; + /** + * How many texts to include into 1 request + * @default 100 + */ chunkLength?: number; + /** + * @default "gpt-3.5-turbo" + */ model?: string; promt?: OpenAIPrompt; }; diff --git a/plugin/src/resolvers/types.ts b/plugin/src/resolvers/types.ts index 6e7811f..1a2c567 100644 --- a/plugin/src/resolvers/types.ts +++ b/plugin/src/resolvers/types.ts @@ -1,7 +1,9 @@ import type { PayloadRequest } from 'payload/types'; export type TranslateResolverArgs = { + /** Locale to translate from */ localeFrom: string; + /** Locale to translate to */ localeTo: string; req: PayloadRequest; texts: string[]; diff --git a/plugin/src/types.ts b/plugin/src/types.ts index 4e509a0..ad74b4d 100644 --- a/plugin/src/types.ts +++ b/plugin/src/types.ts @@ -2,9 +2,21 @@ import type { GeneratedTypes } from 'payload'; import type { TranslateResolver } from './resolvers/types'; -export type PluginConfig = { +export type TranslatorPluginConfig = { + /** + * Collections with the enabled translator in the admin UI + */ collections: (keyof GeneratedTypes['collections'])[]; + /** + * Disable the plugin + */ disabled?: boolean; + /** + * Globals with the enabled translator in the admin UI + */ globals: (keyof GeneratedTypes['globals'])[]; + /** + * Add resolvers that you want to include, examples on how to write your own in ./plugin/src/resolvers + */ resolvers: TranslateResolver[]; };