import {z} from 'zod' export type UpdateTranslationRequestV2 = {key: string, language: string, text: string, customerId?: string | undefined, namespace?: string | undefined, reviewStatus?: ('REVIEWED' | 'NOT_REVIEWED' | '') | undefined}; export type UpdateTranslationBulkRequestV2 = {translations?: Array | undefined}; export type PatchApiApiV2TranslationsBulkArgs = {body: UpdateTranslationBulkRequestV2}; export type PatchApiApiV2TranslationsBulkOptions = {baseUrl?: string | undefined, ProjectAPIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const updateTranslationRequestV2 = z.object({key: z.string().min(1), language: z.string().min(1), text: z.string().max(30000), customerId: z.string().optional(), namespace: z.string().optional(), reviewStatus: z.enum(["REVIEWED", "NOT_REVIEWED", ""]).optional()}); export const failure = z.object({entry: updateTranslationRequestV2.optional(), message: z.string().optional()}); export const updateTranslationBulkResponseV2 = z.object({failures: z.array(failure).optional()}); export const patchApiApiV2TranslationsBulkResponse = z.object({msg: z.string().optional(), status: z.number().int().optional(), data: updateTranslationBulkResponseV2.optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Update translations in bulk * * PATCH /api/v2/translations/bulk */ export const patchApiApiV2TranslationsBulk = async (args: PatchApiApiV2TranslationsBulkArgs, options?: PatchApiApiV2TranslationsBulkOptions) => { const {baseUrl = "https://api.simplelocalize.io", fetch: fetchFn = fetch, ProjectAPIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ProjectAPIKey !== undefined) headers.set("X-SimpleLocalize-Token", ProjectAPIKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/translations/bulk${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(patchApiApiV2TranslationsBulkResponse, await res.json()) };