import {z} from 'zod' export type GetApiAttributesListArgs = {category_name: string, country?: string | undefined, lang?: ('fr' | 'es' | 'it' | 'en' | 'de' | 'pt' | 'pt-BR') | undefined}; export type GetApiAttributesListOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const attributeType = z.enum(["URL", "REPEATABLE_URL", "BOOL", "ENUM", "REPEATABLE_ENUM"]); export const attributeGmbId = z.string(); export const attributeName = z.string(); export const attributeOrder = z.number().int(); export const attribute = z.object({type: attributeType.optional(), gmb_id: attributeGmbId.optional(), name: attributeName.optional(), order: attributeOrder.optional()}); export const attributesGroupName = z.string(); export const attributesGroupOrder = z.number().int(); export const attributesGroup = z.object({attributes: z.array(attribute).optional(), name: attributesGroupName.optional(), order: attributesGroupOrder.optional()}); export const attributesGroupList = z.array(attributesGroup); export const attributesGroupResponse = z.object({groups: attributesGroupList.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 Google Attributes * * GET /attributes/list */ export const getApiAttributesList = async (args: GetApiAttributesListArgs, options?: GetApiAttributesListOptions) => { 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.category_name != null) query.append("category_name", String(args.category_name)) if (args.country != null) query.append("country", String(args.country)) if (args.lang != null) query.append("lang", String(args.lang)) if (ApiKeyAuth !== undefined) headers.set("x-APIKey", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/attributes/list${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(attributesGroupResponse, await res.json()) };