import {z} from 'zod' export type GetApiApiV1CommentsArgs = {'X-SimpleLocalize-Token': string, Authorization: string, namespace?: string | undefined, key: string, language: string, page?: number | undefined, size?: number | undefined}; export type GetApiApiV1CommentsOptions = {baseUrl?: string | undefined, ProjectAPIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const commentAuthor = z.object({name: z.string().optional(), email: z.string().optional(), role: z.string().optional(), profileImageUrl: z.string().optional(), isAnonymous: z.boolean().optional()}); export const commentResponse = z.object({id: z.string().optional(), content: z.string().optional(), votes: z.number().int().optional(), userVote: z.number().int().optional(), suggestedTranslation: z.string().optional(), suggestedTranslationApprovedAt: z.string().optional(), author: commentAuthor.optional(), createdAt: z.string().optional(), deletedAt: z.string().optional(), canDelete: z.boolean().optional(), canModify: z.boolean().optional(), canIgnore: z.boolean().optional(), modifiedAt: z.string().optional(), ignoredAt: z.string().optional(), link: z.string().optional(), namespace: z.string().optional(), translationKey: z.string().optional(), languageKey: z.string().optional()}); export const pageMetadata = z.object({size: z.number().int().optional(), number: z.number().int().optional(), totalElements: z.number().int().optional(), totalPages: z.number().int().optional()}); export const pagedModelCommentResponse = z.object({content: z.array(commentResponse).optional(), page: pageMetadata.optional()}); export const getApiApiV1CommentsResponse = z.object({msg: z.string().optional(), status: z.number().int().optional(), data: pagedModelCommentResponse.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 comments * * GET /api/v1/comments * * Also accepts auth: Personal Access Token */ export const getApiApiV1Comments = async (args: GetApiApiV1CommentsArgs, options?: GetApiApiV1CommentsOptions) => { 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 (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (args.namespace != null) query.append("namespace", String(args.namespace)) if (args.key != null) query.append("key", String(args.key)) if (args.language != null) query.append("language", String(args.language)) 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/comments${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(getApiApiV1CommentsResponse, await res.json()) };