@textunited/sanity-plugin 0.0.0-stage → 0.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/CHANGELOG.md +19 -0
- package/LICENSE.md +21 -0
- package/README.md +182 -2
- package/dist/chunk-3UTIN7WE.js +1050 -0
- package/dist/chunk-3UTIN7WE.js.map +1 -0
- package/dist/chunk-JLJTUACB.js +819 -0
- package/dist/chunk-JLJTUACB.js.map +1 -0
- package/dist/chunk-PC5SE6H6.cjs +1057 -0
- package/dist/chunk-PC5SE6H6.cjs.map +1 -0
- package/dist/chunk-RIWABEFI.cjs +855 -0
- package/dist/chunk-RIWABEFI.cjs.map +1 -0
- package/dist/config-Dcl8jU4S.d.cts +43 -0
- package/dist/config-Dcl8jU4S.d.ts +43 -0
- package/dist/next/index.cjs +18 -0
- package/dist/next/index.cjs.map +1 -0
- package/dist/next/index.d.cts +21 -0
- package/dist/next/index.d.ts +21 -0
- package/dist/next/index.js +16 -0
- package/dist/next/index.js.map +1 -0
- package/dist/server/index.cjs +33 -0
- package/dist/server/index.cjs.map +1 -0
- package/dist/server/index.d.cts +329 -0
- package/dist/server/index.d.ts +329 -0
- package/dist/server/index.js +4 -0
- package/dist/server/index.js.map +1 -0
- package/dist/studio/index.cjs +1005 -0
- package/dist/studio/index.cjs.map +1 -0
- package/dist/studio/index.d.cts +41 -0
- package/dist/studio/index.d.ts +41 -0
- package/dist/studio/index.js +1003 -0
- package/dist/studio/index.js.map +1 -0
- package/package.json +89 -4
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
import { T as TranslationConfig } from '../config-Dcl8jU4S.cjs';
|
|
2
|
+
export { D as DocumentTypeConfig, a as TranslationLanguage } from '../config-Dcl8jU4S.cjs';
|
|
3
|
+
|
|
4
|
+
/** One language of `GET /integrations/languages`. */
|
|
5
|
+
type TextUnitedLanguage = {
|
|
6
|
+
id: number;
|
|
7
|
+
systemName: string;
|
|
8
|
+
langCode: string;
|
|
9
|
+
generalLangCode?: string;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
type SegmentStatus = 'untranslated' | 'draft' | 'automatic' | 'pretranslated' | 'translated' | 'proofread';
|
|
13
|
+
type ProjectSegment = {
|
|
14
|
+
customId: string;
|
|
15
|
+
content: string;
|
|
16
|
+
notes?: string;
|
|
17
|
+
taskId?: string;
|
|
18
|
+
};
|
|
19
|
+
/** A person in an in-house project team (TextUnited `ProjectTeamMember`). */
|
|
20
|
+
type ProjectTeamMember = {
|
|
21
|
+
id?: number;
|
|
22
|
+
username: string;
|
|
23
|
+
isProjectManager: boolean;
|
|
24
|
+
isTranslator: boolean;
|
|
25
|
+
isProofreader: boolean;
|
|
26
|
+
isInCountryReviewer: boolean;
|
|
27
|
+
};
|
|
28
|
+
/** A user or collaborator of the TextUnited company who can join an in-house team. */
|
|
29
|
+
type TeamCandidate = {
|
|
30
|
+
id: number;
|
|
31
|
+
username: string;
|
|
32
|
+
name: string;
|
|
33
|
+
kind: 'user' | 'collaborator';
|
|
34
|
+
};
|
|
35
|
+
type ProjectTargetLanguage = {
|
|
36
|
+
targetLanguageCode: string;
|
|
37
|
+
/** In-house projects: the people who work on this language */
|
|
38
|
+
team?: ProjectTeamMember[];
|
|
39
|
+
managedProject: boolean;
|
|
40
|
+
serviceTranslation: boolean;
|
|
41
|
+
serviceProofreading: boolean;
|
|
42
|
+
serviceAutomaticTranslation: boolean;
|
|
43
|
+
serviceAdaptiveTranslation?: boolean;
|
|
44
|
+
serviceScoring: boolean;
|
|
45
|
+
styleGuideId?: number;
|
|
46
|
+
};
|
|
47
|
+
type CreateProjectRequest = {
|
|
48
|
+
customId: string;
|
|
49
|
+
taskId: string;
|
|
50
|
+
name: string;
|
|
51
|
+
sourceLanguageCode: string;
|
|
52
|
+
targetLanguages: ProjectTargetLanguage[];
|
|
53
|
+
segments: ProjectSegment[];
|
|
54
|
+
instantTranslation?: boolean;
|
|
55
|
+
eventUrl?: string;
|
|
56
|
+
referenceNumber?: string;
|
|
57
|
+
customField1?: string;
|
|
58
|
+
customField2?: string;
|
|
59
|
+
};
|
|
60
|
+
type InstantTranslation = {
|
|
61
|
+
source?: {
|
|
62
|
+
customId?: string;
|
|
63
|
+
content?: string;
|
|
64
|
+
};
|
|
65
|
+
translations?: Array<{
|
|
66
|
+
languageCode?: string;
|
|
67
|
+
content?: string;
|
|
68
|
+
status?: SegmentStatus | string;
|
|
69
|
+
}>;
|
|
70
|
+
};
|
|
71
|
+
type ProjectResponse = {
|
|
72
|
+
id?: number;
|
|
73
|
+
customId?: string;
|
|
74
|
+
name?: string;
|
|
75
|
+
sourceLanguageCode?: string;
|
|
76
|
+
targetLanguages?: Array<ProjectTargetLanguage & {
|
|
77
|
+
id?: number;
|
|
78
|
+
progress?: number | null;
|
|
79
|
+
state?: string | null;
|
|
80
|
+
}>;
|
|
81
|
+
eventUrl?: string;
|
|
82
|
+
/** The task an append update added (also when the server generated its id) */
|
|
83
|
+
taskId?: string | null;
|
|
84
|
+
instantTranslationSegments?: InstantTranslation[];
|
|
85
|
+
};
|
|
86
|
+
type SegmentWithTranslations = {
|
|
87
|
+
source?: {
|
|
88
|
+
customId?: string;
|
|
89
|
+
content?: string;
|
|
90
|
+
taskId?: string | null;
|
|
91
|
+
};
|
|
92
|
+
translations?: Array<{
|
|
93
|
+
languageCode?: string;
|
|
94
|
+
content?: string;
|
|
95
|
+
status?: SegmentStatus | string;
|
|
96
|
+
}>;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* How the server authenticates with TextUnited. A personal access token (`tu_pat_...`, scopes
|
|
101
|
+
* `projects:read` and `projects:write`) is the recommended option; company id plus API key is
|
|
102
|
+
* kept for accounts that still use it.
|
|
103
|
+
*/
|
|
104
|
+
type TextUnitedAuth = {
|
|
105
|
+
type: 'pat';
|
|
106
|
+
token: string;
|
|
107
|
+
} | {
|
|
108
|
+
type: 'apiKey';
|
|
109
|
+
companyId: string;
|
|
110
|
+
apiKey: string;
|
|
111
|
+
};
|
|
112
|
+
type TextUnitedClientOptions = {
|
|
113
|
+
auth: TextUnitedAuth;
|
|
114
|
+
/** Defaults to https://api.textunited.com */
|
|
115
|
+
baseUrl?: string;
|
|
116
|
+
/** Injected in tests; defaults to the global fetch. */
|
|
117
|
+
fetch?: typeof fetch;
|
|
118
|
+
/**
|
|
119
|
+
* Longest wait for one TextUnited response. Instant machine translation can be slow, and an
|
|
120
|
+
* update may not answer at all; the content is stored by then, so callers treat a timeout as
|
|
121
|
+
* "sent, translations follow later". Defaults to 90 seconds.
|
|
122
|
+
*/
|
|
123
|
+
timeoutMs?: number;
|
|
124
|
+
};
|
|
125
|
+
declare class TextUnitedTimeoutError extends Error {
|
|
126
|
+
constructor(method: string, path: string, timeoutMs: number);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* What the connected TextUnited API can do.
|
|
130
|
+
*
|
|
131
|
+
* `appendSegments`: `PUT /segments/projects` accepts `appendContent: true`, which adds the sent
|
|
132
|
+
* segments as a new task and leaves the existing tasks (and their work) untouched. With it, an
|
|
133
|
+
* update sends only new and changed texts, as one new task per request. Without it, an update
|
|
134
|
+
* replaces whole tasks, so each changed part of the document is sent as its own task.
|
|
135
|
+
*/
|
|
136
|
+
type TextUnitedCapabilities = {
|
|
137
|
+
appendSegments: boolean;
|
|
138
|
+
};
|
|
139
|
+
declare class TextUnitedApiError extends Error {
|
|
140
|
+
readonly status: number;
|
|
141
|
+
readonly body: string;
|
|
142
|
+
constructor(status: number, body: string);
|
|
143
|
+
}
|
|
144
|
+
declare class TextUnitedClient {
|
|
145
|
+
private readonly baseUrl;
|
|
146
|
+
private readonly authHeader;
|
|
147
|
+
private readonly fetchImpl;
|
|
148
|
+
private readonly timeoutMs;
|
|
149
|
+
constructor(options: TextUnitedClientOptions);
|
|
150
|
+
private request;
|
|
151
|
+
/** Returns the multilingual project with this custom id, or null when it does not exist. */
|
|
152
|
+
getProject(customId: string): Promise<ProjectResponse | null>;
|
|
153
|
+
createProject(request: CreateProjectRequest): Promise<ProjectResponse>;
|
|
154
|
+
/**
|
|
155
|
+
* Replaces the content of one task (virtual file) in every language project of the
|
|
156
|
+
* multilingual project. With `instantTranslation`, unchanged segments get their text back
|
|
157
|
+
* from TextUnited's translation memory, but with the status `automatic`.
|
|
158
|
+
*/
|
|
159
|
+
replaceTask(args: {
|
|
160
|
+
customId: string;
|
|
161
|
+
taskId: string;
|
|
162
|
+
segments: ProjectSegment[];
|
|
163
|
+
eventUrl?: string;
|
|
164
|
+
}): Promise<ProjectResponse>;
|
|
165
|
+
/**
|
|
166
|
+
* People who can join an in-house team: active company users and confirmed collaborators.
|
|
167
|
+
* Needs the same token scopes as projects (projects:read).
|
|
168
|
+
*/
|
|
169
|
+
listTeamCandidates(): Promise<TeamCandidate[]>;
|
|
170
|
+
/**
|
|
171
|
+
* Adds the segments as a new task in every language project of the multilingual project;
|
|
172
|
+
* existing tasks stay as they are. The task id must not exist in the project yet (HTTP 400).
|
|
173
|
+
*/
|
|
174
|
+
appendTask(args: {
|
|
175
|
+
customId: string;
|
|
176
|
+
taskId: string;
|
|
177
|
+
segments: ProjectSegment[];
|
|
178
|
+
eventUrl?: string;
|
|
179
|
+
}): Promise<ProjectResponse>;
|
|
180
|
+
/** Languages TextUnited supports (`GET /integrations/languages`, no token needed). */
|
|
181
|
+
listLanguages(): Promise<TextUnitedLanguage[]>;
|
|
182
|
+
/** Source segments with their translations in every target language. */
|
|
183
|
+
getSegments(customId: string, taskId?: string): Promise<SegmentWithTranslations[]>;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
type PlainObject = Record<string, unknown>;
|
|
187
|
+
|
|
188
|
+
type ApplyOutcome =
|
|
189
|
+
/** Written to the document */
|
|
190
|
+
'applied'
|
|
191
|
+
/** The document already holds this text */
|
|
192
|
+
| 'unchanged'
|
|
193
|
+
/** Someone edited the translation in Sanity after the plugin wrote it; left as it is */
|
|
194
|
+
| 'keptManualEdit'
|
|
195
|
+
/** TextUnited has no translation (yet) for every chunk of the value */
|
|
196
|
+
| 'incomplete';
|
|
197
|
+
|
|
198
|
+
type Mutation = {
|
|
199
|
+
create: PlainObject;
|
|
200
|
+
} | {
|
|
201
|
+
createOrReplace: PlainObject;
|
|
202
|
+
} | {
|
|
203
|
+
createIfNotExists: PlainObject;
|
|
204
|
+
} | {
|
|
205
|
+
delete: {
|
|
206
|
+
id: string;
|
|
207
|
+
};
|
|
208
|
+
} | {
|
|
209
|
+
patch: {
|
|
210
|
+
id: string;
|
|
211
|
+
set?: PlainObject;
|
|
212
|
+
setIfMissing?: PlainObject;
|
|
213
|
+
ifRevisionID?: string;
|
|
214
|
+
};
|
|
215
|
+
};
|
|
216
|
+
/**
|
|
217
|
+
* The few Sanity operations the server needs. The HTTP implementation below talks to the Sanity
|
|
218
|
+
* API directly; tests use an in-memory implementation.
|
|
219
|
+
*/
|
|
220
|
+
interface SanityStore {
|
|
221
|
+
/** Exact document by id (`drafts.` ids included), or null */
|
|
222
|
+
getDocument(id: string): Promise<PlainObject | null>;
|
|
223
|
+
/** Translation metadata (@sanity/document-internationalization) that references the document */
|
|
224
|
+
findTranslationMetadata(baseDocumentId: string): Promise<PlainObject | null>;
|
|
225
|
+
/** Applies mutations in one transaction */
|
|
226
|
+
mutate(mutations: Mutation[]): Promise<void>;
|
|
227
|
+
/** Ids of source documents whose translations TextUnited may still change, oldest check first */
|
|
228
|
+
listPendingDocuments(limit: number): Promise<string[]>;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** Services ordered for every target language of a new project. */
|
|
232
|
+
type ProjectSettings = Omit<ProjectTargetLanguage, 'targetLanguageCode'>;
|
|
233
|
+
type JobSummary = {
|
|
234
|
+
tasksSent: string[];
|
|
235
|
+
/** Sent, but TextUnited did not answer in time; their translations arrive later */
|
|
236
|
+
tasksAwaitingTranslation?: string[];
|
|
237
|
+
unitsSelected: number;
|
|
238
|
+
/** Texts sent to TextUnited (all of them for a new project after a reset) */
|
|
239
|
+
textsSent?: number;
|
|
240
|
+
outcomes: Partial<Record<ApplyOutcome, number>>;
|
|
241
|
+
projectCreated: boolean;
|
|
242
|
+
/** For a stop or resume request: what changed */
|
|
243
|
+
action?: 'stop' | 'resume';
|
|
244
|
+
languages?: string[];
|
|
245
|
+
};
|
|
246
|
+
type JobResult = {
|
|
247
|
+
status: 'ignored';
|
|
248
|
+
reason: string;
|
|
249
|
+
} | {
|
|
250
|
+
status: 'done';
|
|
251
|
+
summary: JobSummary;
|
|
252
|
+
} | {
|
|
253
|
+
status: 'failed';
|
|
254
|
+
error: string;
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
type SyncResult = {
|
|
258
|
+
status: 'ignored';
|
|
259
|
+
reason: string;
|
|
260
|
+
} | {
|
|
261
|
+
status: 'synced';
|
|
262
|
+
outcomes: JobSummary['outcomes'];
|
|
263
|
+
/** Units whose source was edited after it was sent; they wait for a new request */
|
|
264
|
+
sourceChanged: number;
|
|
265
|
+
pending: boolean;
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
declare const REQUEST_ID_PREFIX = "textunited.request.";
|
|
269
|
+
declare const TEAM_QUERY_ID_PREFIX = "textunited.teamQuery.";
|
|
270
|
+
type TextUnitedHandlerOptions = {
|
|
271
|
+
translation: TranslationConfig;
|
|
272
|
+
textUnited: {
|
|
273
|
+
auth: TextUnitedAuth;
|
|
274
|
+
baseUrl?: string;
|
|
275
|
+
capabilities?: TextUnitedCapabilities;
|
|
276
|
+
/** Services ordered for each language of a new outsourced project */
|
|
277
|
+
project?: Partial<ProjectSettings>;
|
|
278
|
+
/** Services of a new in-house project; the team comes from the Studio request */
|
|
279
|
+
inHouse?: Partial<ProjectSettings>;
|
|
280
|
+
/** Longest wait for one TextUnited response; defaults to 90 seconds */
|
|
281
|
+
timeoutMs?: number;
|
|
282
|
+
};
|
|
283
|
+
sanity: {
|
|
284
|
+
projectId: string;
|
|
285
|
+
dataset: string;
|
|
286
|
+
/** Token with write access, used to save translations. Server side only. */
|
|
287
|
+
token: string;
|
|
288
|
+
apiVersion?: string;
|
|
289
|
+
};
|
|
290
|
+
/**
|
|
291
|
+
* Public HTTPS origin of the site that hosts this handler, e.g. https://www.example.com.
|
|
292
|
+
* TextUnited sends its notifications here, so it must be reachable from the internet and
|
|
293
|
+
* should not change: TextUnited caches the address for up to 24 hours.
|
|
294
|
+
*/
|
|
295
|
+
publicBaseUrl: string;
|
|
296
|
+
/** Path the handler is mounted at, e.g. /api/textunited */
|
|
297
|
+
basePath: string;
|
|
298
|
+
/** Random secret used to sign notification URLs */
|
|
299
|
+
secret: string;
|
|
300
|
+
/**
|
|
301
|
+
* Origins of the Studios allowed to trigger requests directly, e.g.
|
|
302
|
+
* ['https://example.sanity.studio', 'http://localhost:3333']
|
|
303
|
+
*/
|
|
304
|
+
studioOrigins?: string[];
|
|
305
|
+
/** Secret of an optional Sanity GROQ webhook on `textunited.translationRequest` documents */
|
|
306
|
+
webhookSecret?: string;
|
|
307
|
+
/**
|
|
308
|
+
* Bearer token for the periodic check (`{basePath}/sync`), e.g. Vercel's CRON_SECRET.
|
|
309
|
+
* Defaults to `secret`.
|
|
310
|
+
*/
|
|
311
|
+
cronSecret?: string;
|
|
312
|
+
/** Replaces the Sanity HTTP client, e.g. in tests */
|
|
313
|
+
store?: SanityStore;
|
|
314
|
+
/** Replaces fetch for TextUnited calls, e.g. in tests */
|
|
315
|
+
fetch?: typeof fetch;
|
|
316
|
+
};
|
|
317
|
+
/**
|
|
318
|
+
* Framework-independent request handler built on the web-standard Request and Response, so it
|
|
319
|
+
* runs in Next.js route handlers, Remix, Hono, Bun, Deno and Node 20+.
|
|
320
|
+
*
|
|
321
|
+
* Routes, relative to `basePath`:
|
|
322
|
+
* - `POST /jobs` process a translation request: `{requestId}` from the Studio, or the body of a
|
|
323
|
+
* signed Sanity webhook
|
|
324
|
+
* - `GET|POST /events` a notification from TextUnited
|
|
325
|
+
* - `GET|POST /sync` periodic check of documents still being translated (Bearer token)
|
|
326
|
+
*/
|
|
327
|
+
declare function createTextUnitedRequestHandler(options: TextUnitedHandlerOptions): (request: Request) => Promise<Response>;
|
|
328
|
+
|
|
329
|
+
export { type JobResult, type JobSummary, type Mutation, type ProjectSettings, REQUEST_ID_PREFIX, type SanityStore, type SyncResult, TEAM_QUERY_ID_PREFIX, TextUnitedApiError, type TextUnitedAuth, type TextUnitedCapabilities, TextUnitedClient, type TextUnitedHandlerOptions, TextUnitedTimeoutError, TranslationConfig, createTextUnitedRequestHandler };
|
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
import { T as TranslationConfig } from '../config-Dcl8jU4S.js';
|
|
2
|
+
export { D as DocumentTypeConfig, a as TranslationLanguage } from '../config-Dcl8jU4S.js';
|
|
3
|
+
|
|
4
|
+
/** One language of `GET /integrations/languages`. */
|
|
5
|
+
type TextUnitedLanguage = {
|
|
6
|
+
id: number;
|
|
7
|
+
systemName: string;
|
|
8
|
+
langCode: string;
|
|
9
|
+
generalLangCode?: string;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
type SegmentStatus = 'untranslated' | 'draft' | 'automatic' | 'pretranslated' | 'translated' | 'proofread';
|
|
13
|
+
type ProjectSegment = {
|
|
14
|
+
customId: string;
|
|
15
|
+
content: string;
|
|
16
|
+
notes?: string;
|
|
17
|
+
taskId?: string;
|
|
18
|
+
};
|
|
19
|
+
/** A person in an in-house project team (TextUnited `ProjectTeamMember`). */
|
|
20
|
+
type ProjectTeamMember = {
|
|
21
|
+
id?: number;
|
|
22
|
+
username: string;
|
|
23
|
+
isProjectManager: boolean;
|
|
24
|
+
isTranslator: boolean;
|
|
25
|
+
isProofreader: boolean;
|
|
26
|
+
isInCountryReviewer: boolean;
|
|
27
|
+
};
|
|
28
|
+
/** A user or collaborator of the TextUnited company who can join an in-house team. */
|
|
29
|
+
type TeamCandidate = {
|
|
30
|
+
id: number;
|
|
31
|
+
username: string;
|
|
32
|
+
name: string;
|
|
33
|
+
kind: 'user' | 'collaborator';
|
|
34
|
+
};
|
|
35
|
+
type ProjectTargetLanguage = {
|
|
36
|
+
targetLanguageCode: string;
|
|
37
|
+
/** In-house projects: the people who work on this language */
|
|
38
|
+
team?: ProjectTeamMember[];
|
|
39
|
+
managedProject: boolean;
|
|
40
|
+
serviceTranslation: boolean;
|
|
41
|
+
serviceProofreading: boolean;
|
|
42
|
+
serviceAutomaticTranslation: boolean;
|
|
43
|
+
serviceAdaptiveTranslation?: boolean;
|
|
44
|
+
serviceScoring: boolean;
|
|
45
|
+
styleGuideId?: number;
|
|
46
|
+
};
|
|
47
|
+
type CreateProjectRequest = {
|
|
48
|
+
customId: string;
|
|
49
|
+
taskId: string;
|
|
50
|
+
name: string;
|
|
51
|
+
sourceLanguageCode: string;
|
|
52
|
+
targetLanguages: ProjectTargetLanguage[];
|
|
53
|
+
segments: ProjectSegment[];
|
|
54
|
+
instantTranslation?: boolean;
|
|
55
|
+
eventUrl?: string;
|
|
56
|
+
referenceNumber?: string;
|
|
57
|
+
customField1?: string;
|
|
58
|
+
customField2?: string;
|
|
59
|
+
};
|
|
60
|
+
type InstantTranslation = {
|
|
61
|
+
source?: {
|
|
62
|
+
customId?: string;
|
|
63
|
+
content?: string;
|
|
64
|
+
};
|
|
65
|
+
translations?: Array<{
|
|
66
|
+
languageCode?: string;
|
|
67
|
+
content?: string;
|
|
68
|
+
status?: SegmentStatus | string;
|
|
69
|
+
}>;
|
|
70
|
+
};
|
|
71
|
+
type ProjectResponse = {
|
|
72
|
+
id?: number;
|
|
73
|
+
customId?: string;
|
|
74
|
+
name?: string;
|
|
75
|
+
sourceLanguageCode?: string;
|
|
76
|
+
targetLanguages?: Array<ProjectTargetLanguage & {
|
|
77
|
+
id?: number;
|
|
78
|
+
progress?: number | null;
|
|
79
|
+
state?: string | null;
|
|
80
|
+
}>;
|
|
81
|
+
eventUrl?: string;
|
|
82
|
+
/** The task an append update added (also when the server generated its id) */
|
|
83
|
+
taskId?: string | null;
|
|
84
|
+
instantTranslationSegments?: InstantTranslation[];
|
|
85
|
+
};
|
|
86
|
+
type SegmentWithTranslations = {
|
|
87
|
+
source?: {
|
|
88
|
+
customId?: string;
|
|
89
|
+
content?: string;
|
|
90
|
+
taskId?: string | null;
|
|
91
|
+
};
|
|
92
|
+
translations?: Array<{
|
|
93
|
+
languageCode?: string;
|
|
94
|
+
content?: string;
|
|
95
|
+
status?: SegmentStatus | string;
|
|
96
|
+
}>;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* How the server authenticates with TextUnited. A personal access token (`tu_pat_...`, scopes
|
|
101
|
+
* `projects:read` and `projects:write`) is the recommended option; company id plus API key is
|
|
102
|
+
* kept for accounts that still use it.
|
|
103
|
+
*/
|
|
104
|
+
type TextUnitedAuth = {
|
|
105
|
+
type: 'pat';
|
|
106
|
+
token: string;
|
|
107
|
+
} | {
|
|
108
|
+
type: 'apiKey';
|
|
109
|
+
companyId: string;
|
|
110
|
+
apiKey: string;
|
|
111
|
+
};
|
|
112
|
+
type TextUnitedClientOptions = {
|
|
113
|
+
auth: TextUnitedAuth;
|
|
114
|
+
/** Defaults to https://api.textunited.com */
|
|
115
|
+
baseUrl?: string;
|
|
116
|
+
/** Injected in tests; defaults to the global fetch. */
|
|
117
|
+
fetch?: typeof fetch;
|
|
118
|
+
/**
|
|
119
|
+
* Longest wait for one TextUnited response. Instant machine translation can be slow, and an
|
|
120
|
+
* update may not answer at all; the content is stored by then, so callers treat a timeout as
|
|
121
|
+
* "sent, translations follow later". Defaults to 90 seconds.
|
|
122
|
+
*/
|
|
123
|
+
timeoutMs?: number;
|
|
124
|
+
};
|
|
125
|
+
declare class TextUnitedTimeoutError extends Error {
|
|
126
|
+
constructor(method: string, path: string, timeoutMs: number);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* What the connected TextUnited API can do.
|
|
130
|
+
*
|
|
131
|
+
* `appendSegments`: `PUT /segments/projects` accepts `appendContent: true`, which adds the sent
|
|
132
|
+
* segments as a new task and leaves the existing tasks (and their work) untouched. With it, an
|
|
133
|
+
* update sends only new and changed texts, as one new task per request. Without it, an update
|
|
134
|
+
* replaces whole tasks, so each changed part of the document is sent as its own task.
|
|
135
|
+
*/
|
|
136
|
+
type TextUnitedCapabilities = {
|
|
137
|
+
appendSegments: boolean;
|
|
138
|
+
};
|
|
139
|
+
declare class TextUnitedApiError extends Error {
|
|
140
|
+
readonly status: number;
|
|
141
|
+
readonly body: string;
|
|
142
|
+
constructor(status: number, body: string);
|
|
143
|
+
}
|
|
144
|
+
declare class TextUnitedClient {
|
|
145
|
+
private readonly baseUrl;
|
|
146
|
+
private readonly authHeader;
|
|
147
|
+
private readonly fetchImpl;
|
|
148
|
+
private readonly timeoutMs;
|
|
149
|
+
constructor(options: TextUnitedClientOptions);
|
|
150
|
+
private request;
|
|
151
|
+
/** Returns the multilingual project with this custom id, or null when it does not exist. */
|
|
152
|
+
getProject(customId: string): Promise<ProjectResponse | null>;
|
|
153
|
+
createProject(request: CreateProjectRequest): Promise<ProjectResponse>;
|
|
154
|
+
/**
|
|
155
|
+
* Replaces the content of one task (virtual file) in every language project of the
|
|
156
|
+
* multilingual project. With `instantTranslation`, unchanged segments get their text back
|
|
157
|
+
* from TextUnited's translation memory, but with the status `automatic`.
|
|
158
|
+
*/
|
|
159
|
+
replaceTask(args: {
|
|
160
|
+
customId: string;
|
|
161
|
+
taskId: string;
|
|
162
|
+
segments: ProjectSegment[];
|
|
163
|
+
eventUrl?: string;
|
|
164
|
+
}): Promise<ProjectResponse>;
|
|
165
|
+
/**
|
|
166
|
+
* People who can join an in-house team: active company users and confirmed collaborators.
|
|
167
|
+
* Needs the same token scopes as projects (projects:read).
|
|
168
|
+
*/
|
|
169
|
+
listTeamCandidates(): Promise<TeamCandidate[]>;
|
|
170
|
+
/**
|
|
171
|
+
* Adds the segments as a new task in every language project of the multilingual project;
|
|
172
|
+
* existing tasks stay as they are. The task id must not exist in the project yet (HTTP 400).
|
|
173
|
+
*/
|
|
174
|
+
appendTask(args: {
|
|
175
|
+
customId: string;
|
|
176
|
+
taskId: string;
|
|
177
|
+
segments: ProjectSegment[];
|
|
178
|
+
eventUrl?: string;
|
|
179
|
+
}): Promise<ProjectResponse>;
|
|
180
|
+
/** Languages TextUnited supports (`GET /integrations/languages`, no token needed). */
|
|
181
|
+
listLanguages(): Promise<TextUnitedLanguage[]>;
|
|
182
|
+
/** Source segments with their translations in every target language. */
|
|
183
|
+
getSegments(customId: string, taskId?: string): Promise<SegmentWithTranslations[]>;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
type PlainObject = Record<string, unknown>;
|
|
187
|
+
|
|
188
|
+
type ApplyOutcome =
|
|
189
|
+
/** Written to the document */
|
|
190
|
+
'applied'
|
|
191
|
+
/** The document already holds this text */
|
|
192
|
+
| 'unchanged'
|
|
193
|
+
/** Someone edited the translation in Sanity after the plugin wrote it; left as it is */
|
|
194
|
+
| 'keptManualEdit'
|
|
195
|
+
/** TextUnited has no translation (yet) for every chunk of the value */
|
|
196
|
+
| 'incomplete';
|
|
197
|
+
|
|
198
|
+
type Mutation = {
|
|
199
|
+
create: PlainObject;
|
|
200
|
+
} | {
|
|
201
|
+
createOrReplace: PlainObject;
|
|
202
|
+
} | {
|
|
203
|
+
createIfNotExists: PlainObject;
|
|
204
|
+
} | {
|
|
205
|
+
delete: {
|
|
206
|
+
id: string;
|
|
207
|
+
};
|
|
208
|
+
} | {
|
|
209
|
+
patch: {
|
|
210
|
+
id: string;
|
|
211
|
+
set?: PlainObject;
|
|
212
|
+
setIfMissing?: PlainObject;
|
|
213
|
+
ifRevisionID?: string;
|
|
214
|
+
};
|
|
215
|
+
};
|
|
216
|
+
/**
|
|
217
|
+
* The few Sanity operations the server needs. The HTTP implementation below talks to the Sanity
|
|
218
|
+
* API directly; tests use an in-memory implementation.
|
|
219
|
+
*/
|
|
220
|
+
interface SanityStore {
|
|
221
|
+
/** Exact document by id (`drafts.` ids included), or null */
|
|
222
|
+
getDocument(id: string): Promise<PlainObject | null>;
|
|
223
|
+
/** Translation metadata (@sanity/document-internationalization) that references the document */
|
|
224
|
+
findTranslationMetadata(baseDocumentId: string): Promise<PlainObject | null>;
|
|
225
|
+
/** Applies mutations in one transaction */
|
|
226
|
+
mutate(mutations: Mutation[]): Promise<void>;
|
|
227
|
+
/** Ids of source documents whose translations TextUnited may still change, oldest check first */
|
|
228
|
+
listPendingDocuments(limit: number): Promise<string[]>;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** Services ordered for every target language of a new project. */
|
|
232
|
+
type ProjectSettings = Omit<ProjectTargetLanguage, 'targetLanguageCode'>;
|
|
233
|
+
type JobSummary = {
|
|
234
|
+
tasksSent: string[];
|
|
235
|
+
/** Sent, but TextUnited did not answer in time; their translations arrive later */
|
|
236
|
+
tasksAwaitingTranslation?: string[];
|
|
237
|
+
unitsSelected: number;
|
|
238
|
+
/** Texts sent to TextUnited (all of them for a new project after a reset) */
|
|
239
|
+
textsSent?: number;
|
|
240
|
+
outcomes: Partial<Record<ApplyOutcome, number>>;
|
|
241
|
+
projectCreated: boolean;
|
|
242
|
+
/** For a stop or resume request: what changed */
|
|
243
|
+
action?: 'stop' | 'resume';
|
|
244
|
+
languages?: string[];
|
|
245
|
+
};
|
|
246
|
+
type JobResult = {
|
|
247
|
+
status: 'ignored';
|
|
248
|
+
reason: string;
|
|
249
|
+
} | {
|
|
250
|
+
status: 'done';
|
|
251
|
+
summary: JobSummary;
|
|
252
|
+
} | {
|
|
253
|
+
status: 'failed';
|
|
254
|
+
error: string;
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
type SyncResult = {
|
|
258
|
+
status: 'ignored';
|
|
259
|
+
reason: string;
|
|
260
|
+
} | {
|
|
261
|
+
status: 'synced';
|
|
262
|
+
outcomes: JobSummary['outcomes'];
|
|
263
|
+
/** Units whose source was edited after it was sent; they wait for a new request */
|
|
264
|
+
sourceChanged: number;
|
|
265
|
+
pending: boolean;
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
declare const REQUEST_ID_PREFIX = "textunited.request.";
|
|
269
|
+
declare const TEAM_QUERY_ID_PREFIX = "textunited.teamQuery.";
|
|
270
|
+
type TextUnitedHandlerOptions = {
|
|
271
|
+
translation: TranslationConfig;
|
|
272
|
+
textUnited: {
|
|
273
|
+
auth: TextUnitedAuth;
|
|
274
|
+
baseUrl?: string;
|
|
275
|
+
capabilities?: TextUnitedCapabilities;
|
|
276
|
+
/** Services ordered for each language of a new outsourced project */
|
|
277
|
+
project?: Partial<ProjectSettings>;
|
|
278
|
+
/** Services of a new in-house project; the team comes from the Studio request */
|
|
279
|
+
inHouse?: Partial<ProjectSettings>;
|
|
280
|
+
/** Longest wait for one TextUnited response; defaults to 90 seconds */
|
|
281
|
+
timeoutMs?: number;
|
|
282
|
+
};
|
|
283
|
+
sanity: {
|
|
284
|
+
projectId: string;
|
|
285
|
+
dataset: string;
|
|
286
|
+
/** Token with write access, used to save translations. Server side only. */
|
|
287
|
+
token: string;
|
|
288
|
+
apiVersion?: string;
|
|
289
|
+
};
|
|
290
|
+
/**
|
|
291
|
+
* Public HTTPS origin of the site that hosts this handler, e.g. https://www.example.com.
|
|
292
|
+
* TextUnited sends its notifications here, so it must be reachable from the internet and
|
|
293
|
+
* should not change: TextUnited caches the address for up to 24 hours.
|
|
294
|
+
*/
|
|
295
|
+
publicBaseUrl: string;
|
|
296
|
+
/** Path the handler is mounted at, e.g. /api/textunited */
|
|
297
|
+
basePath: string;
|
|
298
|
+
/** Random secret used to sign notification URLs */
|
|
299
|
+
secret: string;
|
|
300
|
+
/**
|
|
301
|
+
* Origins of the Studios allowed to trigger requests directly, e.g.
|
|
302
|
+
* ['https://example.sanity.studio', 'http://localhost:3333']
|
|
303
|
+
*/
|
|
304
|
+
studioOrigins?: string[];
|
|
305
|
+
/** Secret of an optional Sanity GROQ webhook on `textunited.translationRequest` documents */
|
|
306
|
+
webhookSecret?: string;
|
|
307
|
+
/**
|
|
308
|
+
* Bearer token for the periodic check (`{basePath}/sync`), e.g. Vercel's CRON_SECRET.
|
|
309
|
+
* Defaults to `secret`.
|
|
310
|
+
*/
|
|
311
|
+
cronSecret?: string;
|
|
312
|
+
/** Replaces the Sanity HTTP client, e.g. in tests */
|
|
313
|
+
store?: SanityStore;
|
|
314
|
+
/** Replaces fetch for TextUnited calls, e.g. in tests */
|
|
315
|
+
fetch?: typeof fetch;
|
|
316
|
+
};
|
|
317
|
+
/**
|
|
318
|
+
* Framework-independent request handler built on the web-standard Request and Response, so it
|
|
319
|
+
* runs in Next.js route handlers, Remix, Hono, Bun, Deno and Node 20+.
|
|
320
|
+
*
|
|
321
|
+
* Routes, relative to `basePath`:
|
|
322
|
+
* - `POST /jobs` process a translation request: `{requestId}` from the Studio, or the body of a
|
|
323
|
+
* signed Sanity webhook
|
|
324
|
+
* - `GET|POST /events` a notification from TextUnited
|
|
325
|
+
* - `GET|POST /sync` periodic check of documents still being translated (Bearer token)
|
|
326
|
+
*/
|
|
327
|
+
declare function createTextUnitedRequestHandler(options: TextUnitedHandlerOptions): (request: Request) => Promise<Response>;
|
|
328
|
+
|
|
329
|
+
export { type JobResult, type JobSummary, type Mutation, type ProjectSettings, REQUEST_ID_PREFIX, type SanityStore, type SyncResult, TEAM_QUERY_ID_PREFIX, TextUnitedApiError, type TextUnitedAuth, type TextUnitedCapabilities, TextUnitedClient, type TextUnitedHandlerOptions, TextUnitedTimeoutError, TranslationConfig, createTextUnitedRequestHandler };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { REQUEST_ID_PREFIX, TEAM_QUERY_ID_PREFIX, TextUnitedApiError, TextUnitedClient, TextUnitedTimeoutError, createTextUnitedRequestHandler } from '../chunk-3UTIN7WE.js';
|
|
2
|
+
import '../chunk-JLJTUACB.js';
|
|
3
|
+
//# sourceMappingURL=index.js.map
|
|
4
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"index.js"}
|