import {z} from 'zod' export type BusinessId = string; export type State = 'success' | 'pending' | 'to_configure'; export type GetApiPublisherStatesArgs = {business__in?: Array | undefined, publishers?: Array | undefined, states?: Array | undefined, per_page?: number | undefined, page?: number | undefined}; export type GetApiPublisherStatesOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const errorType = z.enum(["PUBLISHER_CONNEXION", "USER_INPUT", "EXTERNAL", "null"]).nullable(); export const publisherStateState = z.object({error_type: errorType.optional(), state: z.string().optional(), url_on_publisher: z.string().nullable().optional()}); export const publisherStateType = z.object({business_formatted_address: z.string().optional(), business_id: z.string().optional(), business_name: z.string().optional(), publishers: z.record(z.string(), publisherStateState).optional()}); export const getPublisherStatesResponse = z.object({businesses: z.record(z.string(), publisherStateType).optional(), 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 }; /** * PublisherStates * * GET /publisher_states */ export const getApiPublisherStates = async (args: GetApiPublisherStatesArgs, options?: GetApiPublisherStatesOptions) => { 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") for (const item of args.business__in ?? []) query.append("business__in", String(item)) for (const item of args.publishers ?? []) query.append("publishers", String(item)) for (const item of args.states ?? []) query.append("states", String(item)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.page != null) query.append("page", String(args.page)) if (ApiKeyAuth !== undefined) headers.set("x-APIKey", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/publisher_states${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(getPublisherStatesResponse, await res.json()) };