import {z} from 'zod' export type GetApiV1MarketplaceRepresentativesArgs = {'integration-key': string, 'x-token': string, filters?: string | undefined, search?: string | undefined, perPage?: number | undefined, page?: number | undefined, sorters?: string | undefined}; export type GetApiV1MarketplaceRepresentativesOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const representativeStates = z.object({id: z.number(), initials: z.string(), name: z.string()}); export const establishmentOwner = z.object({first_name: z.string(), last_name: z.string(), document: z.string()}); export const establishmentRepresentativeResponse = z.object({first_name: z.string(), last_name: z.string(), document: z.string(), owner: establishmentOwner}); export const representativeRoyalties = z.object({pix: z.number(), ted: z.number(), debit: z.number(), billet: z.number(), credit: z.number()}); export const representativeResponseDto = z.object({id: z.number(), active: z.boolean(), created_at: z.string(), updated_at: z.string(), deleted_at: z.string(), states: z.array(representativeStates), establishment: establishmentRepresentativeResponse, royalties: representativeRoyalties}); export const getApiV1MarketplaceRepresentativesResponse = z.object({total: z.number(), page: z.number(), perPage: z.number(), lastPage: z.number(), data: z.array(representativeResponseDto).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 representantes * * GET /v1/marketplace/representatives */ export const getApiV1MarketplaceRepresentatives = async (args: GetApiV1MarketplaceRepresentativesArgs, options?: GetApiV1MarketplaceRepresentativesOptions) => { 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/representatives${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(getApiV1MarketplaceRepresentativesResponse, await res.json()) };