import {z} from 'zod' export type GetApiSocialMediaCommentsPostPostIdArgs = {page?: string | undefined, limit?: string | undefined}; export type GetApiSocialMediaCommentsPostPostIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiSocialMediaCommentsPostPostIdResponse = z.object({data: z.object({comments: z.array(z.object({__v: z.number().optional(), _id: z.string().optional(), author: z.object({_id: z.string().optional(), account: z.object({_id: z.string().optional(), avatar: z.object({_id: z.string().optional(), localPath: z.string().optional(), url: z.string().optional()}).optional(), email: z.string().optional(), username: z.string().optional()}).optional(), firstName: z.string().optional(), lastName: z.string().optional()}).optional(), content: z.string().optional(), createdAt: z.string().optional(), isLiked: z.boolean().optional(), likes: z.number().optional(), postId: z.string().optional(), updatedAt: z.string().optional()})).optional(), hasNextPage: z.boolean().optional(), hasPrevPage: z.boolean().optional(), limit: z.number().optional(), nextPage: z.number().optional(), page: z.number().optional(), prevPage: z.unknown(), serialNumberStartFrom: z.number().optional(), totalComments: z.number().optional(), totalPages: z.number().optional()}).optional(), message: z.string().optional(), statusCode: z.number().optional(), success: z.boolean().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 post comments * * GET /social-media/comments/post/{postId} */ export const getApiSocialMediaCommentsPostPostId = async (args: GetApiSocialMediaCommentsPostPostIdArgs, options?: GetApiSocialMediaCommentsPostPostIdOptions) => { const {baseUrl = "https://api.freeapi.app/api/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) const url = `${baseUrl.replace(/\/+$/, '')}/social-media/comments/post/${encodeURIComponent(String(args.postId))}${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(getApiSocialMediaCommentsPostPostIdResponse, await res.json()) };