import {z} from 'zod' export type GetApiRestPaychecksPaycheckIdOptions = {baseUrl?: string | undefined, oauthUserToken?: string | undefined, fetch?: typeof fetch | undefined}; export const currency = z.string(); export const paycheckAttributes = z.object({status: z.enum(["ESTIMATED", "PROCESSING", "IN_TRANSIT", "DEPOSITED"]), pay_period_ends_at: z.string(), pay_period_starts_at: z.string(), deposit_expected_at: z.string(), total_debited: z.number().int().gte(0).nullable(), gross_earnings: z.number().int(), employer_withholdings: z.number().int().nullable(), net_earnings: z.number().int().nullable(), currency: currency}); export const paycheckLink = z.string(); export const paycheckLinks = z.object({self: paycheckLink}); export const personIdentifier = z.object({type: z.literal("people"), id: z.string()}); export const personRelationship = z.object({data: personIdentifier}); export const jobIdentifier = z.object({type: z.literal("jobs"), id: z.string()}); export const jobRelationship = z.object({data: jobIdentifier}); export const paycheckRelationships = z.object({person: personRelationship, job: jobRelationship}); export const paycheckResource = z.object({type: z.literal("paychecks"), id: z.string(), attributes: paycheckAttributes, links: paycheckLinks, relationships: paycheckRelationships}); export const paycheckData = z.object({data: paycheckResource}); 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 Paycheck object * * GET /rest/paychecks/{paycheck_id} */ export const getApiRestPaychecksPaycheckId = async (options?: GetApiRestPaychecksPaycheckIdOptions) => { 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/paychecks/${encodeURIComponent(String(args.paycheck_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(paycheckData, await res.json()) };