import {z} from 'zod' export type CreateApiGreendotArgs = {body: {account_id: string, channel_type: string, device_type: string, email_address: string, ip_address: string, latitude: string, longitude: string, retailer_key: string}}; export type CreateApiGreendotOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const createApiGreendotResponse = z.object({account_id: z.string().optional(), amount: z.string().optional(), authorized_at: z.string().optional(), available_at: z.string().optional(), bank_id: z.string().optional(), barcode_number: z.string().optional(), channel_type: z.string().optional(), completed_at: z.string().optional(), device_type: z.string().optional(), email_address: z.string().optional(), error: z.string().optional(), expired_at: z.string().optional(), expires_at: z.string().optional(), id: z.string().optional(), ip_address: z.string().optional(), latitude: z.string().optional(), longitude: z.string().optional(), metadata: z.object({}).optional(), retailer_key: z.string().optional(), status: z.string().optional(), voided_at: z.string().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 }; /** * Create a Green Dot Cash Load * * POST /greendot * * Also accepts auth: apiKey */ export const createApiGreendot = async (args: CreateApiGreendotArgs, options?: CreateApiGreendotOptions) => { const {baseUrl = "https://api.treasuryprime.com", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/greendot${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiGreendotResponse, await res.json()) };