import {z} from 'zod' export type AddFormField = {name: unknown, display_name?: unknown | undefined, label?: unknown | undefined, input_type_id?: number | undefined, info?: unknown | undefined, is_required?: number | undefined, depends_on?: unknown | undefined, depend_value?: unknown | undefined, display_order?: number | undefined, is_active?: number | undefined, options?: string | undefined}; export type AddCategory = {name: string, description?: string | undefined, parent_id?: number | undefined, allowed_free_ads_count: number, post_ad_fee: number, is_active: number, is_popular?: number | undefined, image?: unknown | undefined, form_field?: Array | undefined}; export type CreateApiCategoriesArgs = {token: unknown, body: AddCategory}; export type CreateApiCategoriesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const category = z.object({id: z.number().int().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), name: z.string().optional(), slug: z.string().optional(), parent_id: z.number().int().optional(), allowed_free_ads_count: z.number().int().optional(), post_ad_fee: z.number().optional(), is_active: z.number().int().optional(), ad_count: z.number().int().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 }; /** * Creates a new category * * POST /categories */ export const createApiCategories = async (args: CreateApiCategoriesArgs, options: CreateApiCategoriesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.token != null) query.append("token", String(args.token)) const url = `${baseUrl.replace(/\/+$/, '')}/categories${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(category, await res.json()) };