import {z} from 'zod' export type CashFlow = 'cash_in' | 'cash_out'; export type GetApiCashLocationsArgs = {provider: string, latitude: number, longitude: number, radius?: number | undefined, limit?: number | undefined, flow?: CashFlow | undefined}; export type GetApiCashLocationsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const cashLocationCoordinates = z.object({latitude: z.number(), longitude: z.number()}); export const cashLocation = z.object({locId: z.string(), object: z.literal("cashLocation").optional(), cashLocationReference: z.string(), name: z.string(), provider: z.string(), address: z.string(), city: z.string(), state: z.string(), zipCode: z.string(), country: z.string(), coordinates: cashLocationCoordinates, distance: z.number().optional(), distanceUnit: z.string().optional(), buyAllowed: z.boolean(), sellAllowed: z.boolean(), hours: z.string().optional(), supportedAssets: z.array(z.string()).optional()}); export const cashProvider = z.object({provider: z.string(), maxTransaction: z.string(), maxDailyPerCustomer: z.string(), locations: z.array(cashLocation)}); export const cashLocationsResponse = z.object({providers: z.array(cashProvider)}); 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 cash locations * * GET /cash-locations */ export const getApiCashLocations = async (args: GetApiCashLocationsArgs, options?: GetApiCashLocationsOptions) => { const {baseUrl = "https://sandbox-api.polygon.technology/v0.10", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.provider != null) query.append("provider", String(args.provider)) if (args.latitude != null) query.append("latitude", String(args.latitude)) if (args.longitude != null) query.append("longitude", String(args.longitude)) if (args.radius != null) query.append("radius", String(args.radius)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.flow != null) query.append("flow", String(args.flow)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/cash-locations${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(cashLocationsResponse, await res.json()) };