import {z} from 'zod' export type GetApiRestTransfersTransferIdArgs = {transfer_id: string}; export type GetApiRestTransfersTransferIdOptions = {baseUrl?: string | undefined, oauthUserToken?: string | undefined, fetch?: typeof fetch | undefined}; export const amount = z.number().int().gte(0); export const currency = z.string(); export const transferAttributes = z.object({preview: z.boolean(), amount: amount, currency: currency, schedule: z.enum(["WITHIN_THIRTY_MINUTES", "NEXT_BUSINESS_DAY"]), status: z.enum(["PENDING", "SETTLED", "FAILED"]), submitted_at: z.string(), resolved_at: z.string().nullable(), fee: z.number().int().gte(0)}); export const transferLink = z.string(); export const transferLinks = z.object({self: transferLink}); export const accountIdentifier = z.object({type: z.literal("accounts"), id: z.string()}); export const accountRelationship = z.object({data: accountIdentifier}); export const paycheckIdentifier = z.object({type: z.literal("paychecks"), id: z.string()}); export const paycheckRelationship = z.object({data: paycheckIdentifier}); export const personIdentifier = z.object({type: z.literal("people"), id: z.string()}); export const personRelationship = z.object({data: personIdentifier}); export const fundingSourceIdentifier = z.object({type: z.literal("funding_sources"), id: z.string()}); export const fundingSourcesRelationship = z.object({data: z.array(fundingSourceIdentifier)}); export const transferRelationships = z.object({origin: z.union([accountRelationship, paycheckRelationship]), destination: accountRelationship, person: personRelationship, estimated_funding_sources: fundingSourcesRelationship, final_funding_sources: fundingSourcesRelationship}); export const transferResource = z.object({type: z.literal("transfers"), id: z.string(), attributes: transferAttributes, links: transferLinks, relationships: transferRelationships}); export const fundingSourceAttributes = z.object({amount: amount, currency: currency}); export const transferIdentifier = z.object({type: z.literal("transfers"), id: z.string()}); export const transferRelationship = z.object({data: transferIdentifier}); export const fundingSourceRelationships = z.object({source: paycheckRelationship, transfer: transferRelationship}); export const fundingSourceResource = z.object({id: z.string(), type: z.literal("funding_sources"), attributes: fundingSourceAttributes, relationships: fundingSourceRelationships}); export const transferData = z.object({data: transferResource, included: z.array(fundingSourceResource).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 }; /** * Get a transfer object * * GET /rest/transfers/{transfer_id} */ export const getApiRestTransfersTransferId = async (args: GetApiRestTransfersTransferIdArgs, options?: GetApiRestTransfersTransferIdOptions) => { const {baseUrl = "https://api.dailypay.com", fetch: fetchFn = fetch, oauthUserToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.api+json") if (oauthUserToken !== undefined) headers.set('authorization', `Bearer ${oauthUserToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/rest/transfers/${encodeURIComponent(String(args.transfer_id))}${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(transferData, await res.json()) };