import {z} from 'zod' export type GetApiV1MarketplacePlansArgs = {'integration-key': string, 'x-token': string, filters?: string | undefined, search?: string | undefined, perPage?: number | undefined, page?: number | undefined, sorters?: string | undefined}; export type GetApiV1MarketplacePlansOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const plansMarketplaceResponse = z.object({id: z.number(), name: z.string(), active: z.boolean(), gateway_id: z.number(), description: z.string().nullable(), type: z.enum(["COMMERCIAL", "STANDARD"]), modality: z.enum(["ONLINE", "PHYSICAL"]), allow_anticipation: z.boolean(), days_anticipation: z.number(), created_at: z.string(), updated_at: z.string()}); export const getApiV1MarketplacePlansResponse = z.object({total: z.number(), page: z.number(), perPage: z.number(), lastPage: z.number(), data: z.array(plansMarketplaceResponse).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 }; /** * Listar planos comerciais * * GET /v1/marketplace/plans */ export const getApiV1MarketplacePlans = async (args: GetApiV1MarketplacePlansArgs, options?: GetApiV1MarketplacePlansOptions) => { const {baseUrl = "https://api.sandbox.paytime.com.br", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['integration-key'] != null) headers.set("integration-key", String(args['integration-key'])) if (args['x-token'] != null) headers.set("x-token", String(args['x-token'])) if (args.filters != null) query.append("filters", String(args.filters)) if (args.search != null) query.append("search", String(args.search)) if (args.perPage != null) query.append("perPage", String(args.perPage)) if (args.page != null) query.append("page", String(args.page)) if (args.sorters != null) query.append("sorters", String(args.sorters)) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/marketplace/plans${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(getApiV1MarketplacePlansResponse, await res.json()) };