import {z} from 'zod' export type GetApiProjectsProjectIdStringsStringIdArgs = {projectId: number, stringId: number, denormalizePlaceholders?: (0 | 1) | undefined}; export type GetApiProjectsProjectIdStringsStringIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const enterpriseAdditionalFields = z.record(z.string(), z.union([z.string(), z.number().int(), z.boolean(), z.object({})])); export const fileBasedString = z.object({id: z.number().int().optional(), projectId: z.number().int().optional(), branchId: z.number().int().nullable().optional(), identifier: z.string().optional(), text: z.union([z.string(), z.object({})]).nullable().optional(), type: z.string().optional(), context: z.string().optional(), maxLength: z.number().int().optional(), isHidden: z.boolean().optional(), isDuplicate: z.boolean().optional(), masterStringId: z.number().int().nullable().optional(), hasPlurals: z.boolean().optional(), isIcu: z.boolean().optional(), labelIds: z.array(z.number().int()).optional(), webUrl: z.string().optional(), createdAt: z.string().nullable().optional(), updatedAt: z.string().nullable().optional(), fields: enterpriseAdditionalFields.optional(), fileId: z.number().int().optional(), directoryId: z.number().int().nullable().optional(), revision: z.number().int().optional()}); export const enterpriseFileBasedStringResource = z.object({data: fileBasedString}); 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 String * * GET /projects/{projectId}/strings/{stringId} */ export const getApiProjectsProjectIdStringsStringId = async (args: GetApiProjectsProjectIdStringsStringIdArgs, options?: GetApiProjectsProjectIdStringsStringIdOptions) => { const {baseUrl = "https://api.crowdin.com/api/v2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.denormalizePlaceholders != null) query.append("denormalizePlaceholders", String(args.denormalizePlaceholders)) const url = `${baseUrl.replace(/\/+$/, '')}/projects/${encodeURIComponent(String(args.projectId))}/strings/${encodeURIComponent(String(args.stringId))}${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(enterpriseFileBasedStringResource, await res.json()) };