import {z} from 'zod' export type GetApiRestPaychecksArgs = {'filter[job.id]'?: string | undefined, 'filter[status]'?: ('ESTIMATED' | 'PROCESSING' | 'IN_TRANSIT' | 'DEPOSITED') | undefined, 'filter[deposit_expected_at__gte]'?: string | undefined, 'filter[deposit_expected_at__lt]'?: string | undefined, 'filter[pay_period_ends_at__gte]'?: string | undefined, 'filter[pay_period_ends_at__lt]'?: string | undefined, 'filter[pay_period_starts_at__gte]'?: string | undefined, 'filter[pay_period_starts_at__lt]'?: string | undefined, filter?: string | undefined}; export type GetApiRestPaychecksOptions = {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 paychecksData = z.object({data: z.array(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 list of paycheck objects * * GET /rest/paychecks */ export const getApiRestPaychecks = async (args: GetApiRestPaychecksArgs, options?: GetApiRestPaychecksOptions) => { 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 (args['filter[job.id]'] != null) query.append("filter[job.id]", String(args['filter[job.id]'])) if (args['filter[status]'] != null) query.append("filter[status]", String(args['filter[status]'])) if (args['filter[deposit_expected_at__gte]'] != null) query.append("filter[deposit_expected_at__gte]", String(args['filter[deposit_expected_at__gte]'])) if (args['filter[deposit_expected_at__lt]'] != null) query.append("filter[deposit_expected_at__lt]", String(args['filter[deposit_expected_at__lt]'])) if (args['filter[pay_period_ends_at__gte]'] != null) query.append("filter[pay_period_ends_at__gte]", String(args['filter[pay_period_ends_at__gte]'])) if (args['filter[pay_period_ends_at__lt]'] != null) query.append("filter[pay_period_ends_at__lt]", String(args['filter[pay_period_ends_at__lt]'])) if (args['filter[pay_period_starts_at__gte]'] != null) query.append("filter[pay_period_starts_at__gte]", String(args['filter[pay_period_starts_at__gte]'])) if (args['filter[pay_period_starts_at__lt]'] != null) query.append("filter[pay_period_starts_at__lt]", String(args['filter[pay_period_starts_at__lt]'])) if (args.filter != null) query.append("filter", String(args.filter)) if (oauthUserToken !== undefined) headers.set('authorization', `Bearer ${oauthUserToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/rest/paychecks${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(paychecksData, await res.json()) };