import {z} from 'zod' export type GetApiGreendotLocationArgs = {ip_address?: string | undefined, latitude?: string | undefined, longitude?: string | undefined, zip_code?: string | undefined, page_cursor?: string | undefined, page_size?: number | undefined, from_date?: string | undefined, to_date?: string | undefined}; export type GetApiGreendotLocationOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const getApiGreendotLocationResponse = z.object({data: z.array(z.object({address: z.string().optional(), city: z.string().optional(), customer_fee: z.string().optional(), latitude: z.string().optional(), longitude: z.string().optional(), phone: z.string().optional(), retailer_key: z.string().optional(), retailer_name: z.string().optional(), state: z.string().optional(), store_id: z.string().optional(), zip_code: z.string().optional()})).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 }; /** * Fetch Green Dot Retail Locations * * GET /greendot/location * * Also accepts auth: apiKey */ export const getApiGreendotLocation = async (args: GetApiGreendotLocationArgs, options?: GetApiGreendotLocationOptions) => { const {baseUrl = "https://api.treasuryprime.com", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.ip_address != null) query.append("ip_address", String(args.ip_address)) if (args.latitude != null) query.append("latitude", String(args.latitude)) if (args.longitude != null) query.append("longitude", String(args.longitude)) if (args.zip_code != null) query.append("zip_code", String(args.zip_code)) if (args.page_cursor != null) query.append("page_cursor", String(args.page_cursor)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.from_date != null) query.append("from_date", String(args.from_date)) if (args.to_date != null) query.append("to_date", String(args.to_date)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/greendot/location${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(getApiGreendotLocationResponse, await res.json()) };