import {z} from 'zod' export type Amount = number; export type Currency = string; export type TransferCreateAttributes = {preview?: boolean | undefined, amount: Amount, currency: Currency, schedule: 'WITHIN_THIRTY_MINUTES' | 'NEXT_BUSINESS_DAY'}; export type AccountIdentifier = {type: 'accounts', id: string}; export type AccountRelationship = {data: AccountIdentifier}; export type PersonIdentifier = {type: 'people', id: string}; export type PersonRelationship = {data: PersonIdentifier}; export type TransferCreateRelationships = {origin: AccountRelationship, destination?: AccountRelationship | undefined, person?: PersonRelationship | undefined}; export type TransferCreateResource = {type: 'transfers', id?: string | undefined, attributes: TransferCreateAttributes, relationships: TransferCreateRelationships}; export type TransferCreateData = {data: TransferCreateResource}; export type CreateApiRestTransfersArgs = {'Idempotency-Key': string, body: TransferCreateData}; export type CreateApiRestTransfersOptions = {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 }; /** * Request a transfer * * POST /rest/transfers */ export const createApiRestTransfers = async (args: CreateApiRestTransfersArgs, options?: CreateApiRestTransfersOptions) => { const {baseUrl = "https://api.dailypay.com", fetch: fetchFn = fetch, oauthUserToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/vnd.api+json") headers.set('accept', "application/vnd.api+json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (oauthUserToken !== undefined) headers.set('authorization', `Bearer ${oauthUserToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/rest/transfers${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(transferData, await res.json()) };