import {z} from 'zod' export type GetApiApiV1TranslationKeysArgs = {key?: string | undefined, namespace?: string | undefined, sort?: ('last_seen_at' | 'modified_at' | 'created_at' | 'deprecated_at') | undefined, page?: number | undefined, size?: number | undefined}; export type GetApiApiV1TranslationKeysOptions = {baseUrl?: string | undefined, ProjectAPIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const translationKeyResponseV2 = z.object({key: z.string().optional(), namespace: z.string().optional(), description: z.string().optional(), codeDescription: z.string().optional(), createdAt: z.string().optional(), createdSource: z.string().optional(), lastSeenAt: z.string().optional(), lastSeenSource: z.string().optional(), charactersLimit: z.number().int().optional(), deprecated: z.boolean().optional(), attributes: z.record(z.string(), z.unknown()).optional()}); export const pageDetails = z.object({totalElements: z.number().int().optional(), totalPages: z.number().int().optional(), currentPage: z.number().int().optional(), hasNext: z.boolean().optional(), first: z.boolean().optional(), last: z.boolean().optional()}); export const pagedTranslationKeysV2RestResponse = z.object({msg: z.string().optional(), status: z.number().int().optional(), data: z.array(translationKeyResponseV2).optional(), pageDetails: pageDetails.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 translation keys with metadata * * GET /api/v1/translation-keys */ export const getApiApiV1TranslationKeys = async (args: GetApiApiV1TranslationKeysArgs, options?: GetApiApiV1TranslationKeysOptions) => { 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.key != null) query.append("key", String(args.key)) if (args.namespace != null) query.append("namespace", String(args.namespace)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) if (ProjectAPIKey !== undefined) headers.set("X-SimpleLocalize-Token", ProjectAPIKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/translation-keys${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(pagedTranslationKeysV2RestResponse, await res.json()) };