import {z} from 'zod' export type GetApiAccountsAidStoresArgs = {aid: unknown, payout_destination_id?: Array | undefined, terminal_id?: Array | undefined, includes?: Array<'operator_payout_destinations'> | undefined, status?: (Array<'ACTIVE' | 'PENDING' | 'SUSPENDED' | 'DELETED' | 'ERROR'>) | undefined, starting_after?: string | undefined, limit?: number | undefined}; export type GetApiAccountsAidStoresOptions = {baseUrl?: string | undefined, JWT?: string | undefined, fetch?: typeof fetch | undefined}; export const terminalStatus = z.enum(["DELETED", "PENDING", "ACTION_REQUIRED", "ACTIVE", "INACTIVE", "ERROR"]); export const storeTerminalSummary = z.object({status: terminalStatus, count: z.number().int()}); export const payoutDestinationId = z.string().max(40); export const approvalStatus = z.enum(["ACTIVE", "DECLINED", "UNDER_MANUAL_REVIEW", "AUTOMATIC_REVIEW", "WAITING_FOR_SIGNATURE", "WAITING_FOR_DECLARATION", "ERROR", "ARCHIVED", "WAITING_FOR_DETAILS", "TERMINATED"]); export const legalEntityType = z.enum(["company", "individual"]); export const operatorPayoutDestination = z.object({payout_destination_id: z.string(), organization_number: z.string().optional(), name: z.string().optional(), trade_name: z.string().optional(), merchant_category_code: z.string().optional(), status: approvalStatus, type: legalEntityType}); export const storeCollection = z.object({starting_after: z.string().optional(), stores: z.array(z.object({terminals_summary: z.array(storeTerminalSummary).optional(), organization: z.object({organization_number: z.string(), country: z.string().optional(), type: z.enum(["main_unit", "sub_unit"]).optional(), name: z.string().optional(), trade_name: z.string().optional()}), store_name: z.string(), email: z.string().optional(), phone_number: z.string(), address: z.object({address_line: z.string(), address_line_2: z.string().optional(), postal_code: z.string(), postal_place: z.string(), country: z.string()}).optional(), coordinates: z.object({latitude: z.number(), longitude: z.number()}).optional(), website: z.string().optional(), payout_destination: z.object({payout_destination_id: payoutDestinationId, merchant_category_code: z.string().optional(), status: approvalStatus, type: legalEntityType}).optional(), store_id: z.string().max(20), id: z.string(), created_at: z.string(), created_by: z.string(), updated_at: z.string(), updated_by: z.string(), status: z.enum(["ACTIVE", "PENDING", "SUSPENDED", "DELETED", "ERROR"]), verified: z.boolean(), number_of_terminals: z.number().int(), operator_payout_destinations: z.array(operatorPayoutDestination).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 }; /** * aid_account_management_stores_get * * GET /accounts/{aid}/stores */ export const getApiAccountsAidStores = async (args: GetApiAccountsAidStoresArgs, options?: GetApiAccountsAidStoresOptions) => { const {baseUrl = "https://api.dintero.com/v1", fetch: fetchFn = fetch, JWT} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.payout_destination_id ?? []) query.append("payout_destination_id", String(item)) for (const item of args.terminal_id ?? []) query.append("terminal_id", String(item)) if (args.includes != null) query.append("includes", args.includes.join(',')) for (const item of args.status ?? []) query.append("status", String(item)) if (args.starting_after != null) query.append("starting_after", String(args.starting_after)) if (args.limit != null) query.append("limit", String(args.limit)) if (JWT !== undefined) headers.set('authorization', `Bearer ${JWT}`) const url = `${baseUrl.replace(/\/+$/, '')}/accounts/${encodeURIComponent(String(args.aid))}/stores${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(storeCollection, await res.json()) };