import {z} from 'zod' export type GetApiApiV1ExportTrackerOffersArgs = {tracker_id?: (string | null) | undefined, offer_id?: (string | null) | undefined, created_at_from?: (string | null) | undefined, created_at_to?: (string | null) | undefined, cursor?: (string | null) | undefined, size?: number | undefined}; export type GetApiApiV1ExportTrackerOffersOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const trackerOfferExportDTO = z.object({id: z.string(), trackerId: z.string(), offerId: z.string(), weight: z.number().int()}); export const cursorPageTrackerOfferExportDTO = z.object({items: z.array(trackerOfferExportDTO), total: z.number().int().gte(0), current_page: z.string().nullable().optional(), current_page_backwards: z.string().nullable().optional(), previous_page: z.string().nullable().optional(), next_page: z.string().nullable().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 Tracker Offers * * GET /api/v1/export/tracker-offers */ export const getApiApiV1ExportTrackerOffers = async (args: GetApiApiV1ExportTrackerOffersArgs, options: GetApiApiV1ExportTrackerOffersOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.tracker_id != null) query.append("tracker_id", String(args.tracker_id)) if (args.offer_id != null) query.append("offer_id", String(args.offer_id)) if (args.created_at_from != null) query.append("created_at_from", String(args.created_at_from)) if (args.created_at_to != null) query.append("created_at_to", String(args.created_at_to)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.size != null) query.append("size", String(args.size)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/export/tracker-offers${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(cursorPageTrackerOfferExportDTO, await res.json()) };