import {z} from 'zod' export type GetApiApiV4ExportArgs = {'X-SimpleLocalize-Token': string, downloadFormat?: ('android' | 'android-strings' | 'android-xml' | 'csv-translations' | 'tsv' | 'excel' | 'java-properties' | 'localizable-xcstrings' | 'localizable-strings' | 'localizable-strings-dict' | 'po-pot' | 'php-array' | 'string-resources' | 'simplelocalize-json' | 'single-language-json' | 'multi-language-json' | 'javascript' | 'module-exports' | 'yaml' | 'qt-linguist' | 'resx') | undefined, downloadOptions?: (Array<'SPLIT_BY_LANGUAGES' | 'SPLIT_BY_NAMESPACES' | 'INCLUDE_DESCRIPTIONS' | 'ONLY_NOT_TRANSLATED' | 'ONLY_TRANSLATED' | 'WRITE_NESTED' | 'ESCAPE_NEW_LINES' | 'ESCAPE_SINGLE_QUOTES' | 'ZIP_FILES' | 'SHEETS_AS_NAMESPACES' | 'MULTI_LANGUAGE' | 'LANGUAGES_NESTED'>) | undefined, languageKeys?: Array | undefined, tags?: Array | undefined, languageOrder?: Array | undefined, customerId?: string | undefined, namespace?: string | undefined, sort?: ('DEFAULT' | 'NEWEST_KEYS_FIRST' | 'NEWEST_KEYS_LAST' | 'NAMESPACES' | 'IMPORT_ORDER') | undefined}; export type GetApiApiV4ExportOptions = {baseUrl?: string | undefined, ProjectAPIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const downloadableFile = z.object({url: z.string().optional(), namespace: z.string().optional(), language: z.string().optional()}); export const getExportTranslationsV4 = z.object({files: z.array(downloadableFile).optional()}); export const getApiApiV4ExportResponse = z.object({msg: z.string().optional(), status: z.number().int().optional(), data: getExportTranslationsV4.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 }; /** * Export translations to file(s) * * GET /api/v4/export */ export const getApiApiV4Export = async (args: GetApiApiV4ExportArgs, options?: GetApiApiV4ExportOptions) => { const {baseUrl = "https://api.simplelocalize.io", fetch: fetchFn = fetch, ProjectAPIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['X-SimpleLocalize-Token'] != null) headers.set("X-SimpleLocalize-Token", String(args['X-SimpleLocalize-Token'])) if (args.downloadFormat != null) query.append("downloadFormat", String(args.downloadFormat)) for (const item of args.downloadOptions ?? []) query.append("downloadOptions", String(item)) for (const item of args.languageKeys ?? []) query.append("languageKeys", String(item)) for (const item of args.tags ?? []) query.append("tags", String(item)) for (const item of args.languageOrder ?? []) query.append("languageOrder", String(item)) if (args.customerId != null) query.append("customerId", String(args.customerId)) if (args.namespace != null) query.append("namespace", String(args.namespace)) if (args.sort != null) query.append("sort", String(args.sort)) if (ProjectAPIKey !== undefined) headers.set("X-SimpleLocalize-Token", ProjectAPIKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v4/export${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiApiV4ExportResponse, await res.json()) };