import {z} from 'zod' export type Lang = 'fr' | 'en' | 'es' | 'it' | 'pt-br' | 'de' | 'ar' | 'nl' | 'pl' | 'cs' | 'ca' | 'sk' | 'pt' | 'lv' | 'ro' | 'bg' | 'hu'; export type GetApiCategoriesArgs = {country?: string | undefined, lang?: Lang | undefined, state?: ('OK' | 'DEPRECATED') | undefined, block_push_long_description_on_gmb?: boolean | undefined, search_query?: string | undefined, order_by?: ('gmb_id' | '-gmb_id' | 'impacted_businesses' | '-impacted_businesses') | undefined, live__isnull?: boolean | undefined, page?: number | undefined, per_page?: number | undefined, with_names?: boolean | undefined, with_countries?: boolean | undefined}; export type GetApiCategoriesOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const currentPage = z.number().int(); export const maxPage = z.number().int(); export const count = z.number().int(); export const categorySqlId = z.number().int(); export const categorySqlGmbId = z.string(); export const categoryName = z.string(); export const categorySqlName = z.object({name: z.string().optional(), lang: z.string().optional()}); export const categorySqlNames = z.array(categorySqlName); export const categorySqlState = z.string(); export const categorySqlBlockPushLong = z.boolean(); export const categoryCountries = z.array(z.string()); export const categorySqlLiveId = z.string().nullable(); export const categorySql = z.object({id: categorySqlId.optional(), gmb_id: categorySqlGmbId.optional(), name: categoryName.optional(), names: categorySqlNames.optional(), state: categorySqlState.optional(), block_push_long_description_on_gmb: categorySqlBlockPushLong.optional(), countries: categoryCountries.optional(), live_id: categorySqlLiveId.optional()}); export const getApiCategoriesResponse = z.object({page: currentPage.optional(), max_page: maxPage.optional(), count: count.optional(), categories: z.array(categorySql).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 }; /** * List Partoo categories * * GET /categories */ export const getApiCategories = async (args: GetApiCategoriesArgs, options?: GetApiCategoriesOptions) => { const {baseUrl = "https://api.partoo.co/v2", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.country != null) query.append("country", String(args.country)) if (args.lang != null) query.append("lang", String(args.lang)) if (args.state != null) query.append("state", String(args.state)) if (args.block_push_long_description_on_gmb != null) query.append("block_push_long_description_on_gmb", String(args.block_push_long_description_on_gmb)) if (args.search_query != null) query.append("search_query", String(args.search_query)) if (args.order_by != null) query.append("order_by", String(args.order_by)) if (args.live__isnull != null) query.append("live__isnull", String(args.live__isnull)) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.with_names != null) query.append("with_names", String(args.with_names)) if (args.with_countries != null) query.append("with_countries", String(args.with_countries)) if (ApiKeyAuth !== undefined) headers.set("x-APIKey", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/categories${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(getApiCategoriesResponse, await res.json()) };