import {z} from 'zod' export type ProjectCreateRequest = {name: string}; export type CreateApiApiV2ProjectsArgs = {Authorization: string, body: ProjectCreateRequest}; export type CreateApiApiV2ProjectsOptions = {baseUrl?: string | undefined, PersonalAccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const language = z.object({key: z.string().optional(), name: z.string().optional(), isDefault: z.boolean().optional(), order: z.number().int().optional(), flag: z.string().optional(), access: z.enum(["WRITE_READ", "READ", "HIDDEN"]).optional(), translationProvider: z.enum(["GOOGLE_TRANSLATE", "DEEPL", "OPEN_AI", "OPEN_ROUTER", "CUSTOM_AI", "ANTHROPIC", "GEMINI", "SIMPLELOCALIZE_AI", "EXTERNAL_API", "SYSTRAN", "PSEUDO_TRANSLATE", "TRANSLATION_MEMORY", "DISABLED"]).optional(), providerLanguage: z.string().optional(), sourceLanguage: z.string().optional(), deeplFormality: z.string().optional(), isLocked: z.boolean().optional(), attributes: z.record(z.string(), z.unknown()).optional(), context: z.string().optional(), fallbackLanguage: z.string().optional()}); export const customerOverride = z.object({key: z.string().optional(), description: z.string().optional(), displayName: z.string().optional()}); export const projectReadDetails = z.object({name: z.string().optional(), languages: z.array(language).optional(), customers: z.array(customerOverride).optional(), projectToken: z.string().optional(), apiKey: z.string().optional(), keys: z.number().int().optional(), unpublishedChanges: z.number().int().optional(), translatedPercentage: z.number().optional(), translatedKeysByLanguage: z.record(z.string(), z.number().int()).optional()}); export const createApiApiV2ProjectsResponse = z.object({msg: z.string().optional(), status: z.number().int().optional(), data: projectReadDetails.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 project * * POST /api/v2/projects */ export const createApiApiV2Projects = async (args: CreateApiApiV2ProjectsArgs, options?: CreateApiApiV2ProjectsOptions) => { const {baseUrl = "https://api.simplelocalize.io", fetch: fetchFn = fetch, PersonalAccessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (PersonalAccessToken !== undefined) headers.set("Authorization", PersonalAccessToken) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/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(createApiApiV2ProjectsResponse, await res.json()) };