import {z} from 'zod' export type GetApiApiV2ProjectArgs = {'X-SimpleLocalize-Token': string}; export type GetApiApiV2ProjectOptions = {baseUrl?: string | undefined, ProjectAPIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const apiLanguageDetailsV2 = z.object({key: z.string().optional(), name: z.string().optional(), isDefault: z.boolean().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()}); export const apiCustomerDetailsV2 = z.object({key: z.string().optional()}); export const apiNamespaceDetailsV2 = z.object({name: z.string().optional()}); export const apiEnvironmentDetailsV2 = z.object({name: z.string().optional(), key: z.string().optional(), color: z.string().optional()}); export const apiHostingResourceV2 = z.object({path: z.string().optional(), namespace: z.string().optional(), language: z.string().optional(), customerId: z.string().optional(), key: z.string().optional()}); export const apiProjectDetailsV2 = z.object({name: z.string().optional(), projectToken: z.string().optional(), languages: z.array(apiLanguageDetailsV2).optional(), customers: z.array(apiCustomerDetailsV2).optional(), namespaces: z.array(apiNamespaceDetailsV2).optional(), environments: z.array(apiEnvironmentDetailsV2).optional(), keys: z.number().int().optional(), unpublishedChanges: z.number().int().optional(), translatedPercentage: z.number().optional(), lastActivityAt: z.string().optional(), lastEditedAt: z.string().optional(), createdAt: z.string().optional(), translatedKeysByLanguage: z.record(z.string(), z.number().int()).optional(), hostingResources: z.array(apiHostingResourceV2).optional()}); export const getApiApiV2ProjectResponse = z.object({msg: z.string().optional(), status: z.number().int().optional(), data: apiProjectDetailsV2.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 }; /** * Get project details * * GET /api/v2/project */ export const getApiApiV2Project = async (args: GetApiApiV2ProjectArgs, options?: GetApiApiV2ProjectOptions) => { 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 (ProjectAPIKey !== undefined) headers.set("X-SimpleLocalize-Token", ProjectAPIKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/project${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(getApiApiV2ProjectResponse, await res.json()) };