import {z} from 'zod' export type CreateApiProjectsArgs = {'X-PhraseApp-OTP'?: string | undefined, body: {name: string, main_format?: string | undefined, media?: string | undefined, shares_translation_memory?: boolean | undefined, project_image?: string | undefined, remove_project_image?: boolean | undefined, account_id?: string | undefined, point_of_contact?: string | undefined, source_project_id?: string | undefined, workflow?: string | undefined, machine_translation_enabled?: boolean | undefined, enable_branching?: boolean | undefined, protect_master_branch?: boolean | undefined, enable_all_data_type_translation_keys_for_translators?: boolean | undefined, enable_icu_message_format?: boolean | undefined, zero_plural_form_enabled?: boolean | undefined, autotranslate_enabled?: boolean | undefined, autotranslate_check_new_translation_keys?: boolean | undefined, autotranslate_check_new_uploads?: boolean | undefined, autotranslate_check_new_locales?: boolean | undefined, autotranslate_mark_as_unverified?: boolean | undefined, autotranslate_use_machine_translation?: boolean | undefined, autotranslate_use_translation_memory?: boolean | undefined, autotranslate_overwrite_unverified_translations?: boolean | undefined, autocomplete_job_enabled?: boolean | undefined, job_locking_enabled?: boolean | undefined, smart_suggest_enabled?: boolean | undefined, smart_suggest_use_glossary?: boolean | undefined, smart_suggest_use_machine_translation?: boolean | undefined, translation_keys_sort_collation?: string | undefined, default_encoding?: ('UTF-8' | 'UTF-16' | 'UTF-16BE' | 'UTF-16LE' | 'ISO-8859-1') | undefined, cldr_version?: string | undefined, placeholder_styles?: Array | undefined}}; export type CreateApiProjectsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const account = z.object({id: z.string().optional(), name: z.string().optional(), slug: z.string().optional(), company: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), company_logo_url: z.string().optional()}); export const userPreview = z.object({id: z.string().optional(), username: z.string().optional(), name: z.string().optional(), gravatar_uid: z.string().optional()}); export const branch = z.object({base_project_id: z.string().optional(), branch_project_id: z.string().optional(), name: z.string().optional(), base: z.string().nullable().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), merged_at: z.string().optional(), merged_by: userPreview.optional(), created_by: userPreview.optional(), state: z.enum(["creating_branch", "merging_branch", "syncing_branch", "merged", "success", "branch_error", "merge_conflict"]).optional(), child_branches: z.array(z.string()).optional()}); export const projectDetails = z.object({id: z.string().optional(), name: z.string().optional(), slug: z.string().optional(), main_format: z.string().optional(), project_image_url: z.string().optional(), media: z.string().optional(), account: account.optional(), space: z.object({id: z.string().optional(), name: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), projects_count: z.number().int().optional()}).optional(), point_of_contact: userPreview.optional(), created_at: z.string().optional(), updated_at: z.string().optional(), shares_translation_memory: z.boolean().optional(), machine_translation_enabled: z.boolean().optional(), zero_plural_form_enabled: z.boolean().optional(), enable_all_data_type_translation_keys_for_translators: z.boolean().optional(), enable_icu_message_format: z.boolean().optional(), enable_branching: z.boolean().optional(), protect_master_branch: z.boolean().optional(), autotranslate_enabled: z.boolean().optional(), autotranslate_check_new_translation_keys: z.boolean().optional(), autotranslate_check_new_uploads: z.boolean().optional(), autotranslate_check_new_locales: z.boolean().optional(), autotranslate_mark_as_unverified: z.boolean().optional(), autotranslate_use_machine_translation: z.boolean().optional(), autotranslate_use_translation_memory: z.boolean().optional(), autotranslate_overwrite_unverified_translations: z.boolean().optional(), autocomplete_job_enabled: z.boolean().optional(), default_encoding: z.string().optional(), cldr_version: z.string().optional(), job_locking_enabled: z.boolean().optional(), placeholder_styles: z.array(z.string()).optional(), branch: branch.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 }; /** * Create a project * * POST /projects */ export const createApiProjects = async (args: CreateApiProjectsArgs, options?: CreateApiProjectsOptions) => { const {baseUrl = "https://eu.phrase.com/control-hub", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-PhraseApp-OTP'] != null) headers.set("X-PhraseApp-OTP", String(args['X-PhraseApp-OTP'])) const url = `${baseUrl.replace(/\/+$/, '')}/projects${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(projectDetails, await res.json()) };