import {z} from 'zod' export type GetApiRestJobsOptions = {baseUrl?: string | undefined, oauthClientCredentialsToken?: string | undefined, fetch?: typeof fetch | undefined}; export const amount = z.number().int().gte(0); export const currency = z.string(); export const jobAttributes = z.object({external_identifiers: z.record(z.string(), z.string()).optional(), first_name: z.string().optional(), last_name: z.string().optional(), activation_status: z.enum(["DEACTIVATED", "DEACTIVATION_PENDING", "ACTIVATION_REQUIRED", "ACTIVATION_UNDER_REVIEW", "ACTIVATED"]), wage_rate: z.object({amount: amount, currency: currency, frequency: z.enum(["HOURLY", "WEEKLY", "BIWEEKLY", "TWICE_MONTHLY", "MONTHLY", "ANNUALLY"])}), title: z.string().nullable().optional(), department: z.string().nullable().optional(), location: z.string().optional(), direct_deposit_status: z.enum(["SETUP_REQUIRED", "SETUP_PENDING", "SETUP_COMPLETE"])}); export const jobLink = z.string(); export const jobLinks = z.object({self: jobLink}); export const personIdentifier = z.object({type: z.literal("people"), id: z.string()}); export const personRelationship = z.object({data: personIdentifier}); export const organizationIdentifier = z.object({type: z.literal("organizations"), id: z.string()}); export const organizationRelationship = z.object({data: organizationIdentifier}); export const accountIdentifier = z.object({type: z.literal("accounts"), id: z.string()}); export const accountRelationship = z.object({data: accountIdentifier}); export const jobRelationships = z.object({person: personRelationship, organization: organizationRelationship, direct_deposit_default_depository: accountRelationship.optional(), direct_deposit_default_card: accountRelationship.optional()}); export const jobResource = z.object({type: z.literal("jobs"), id: z.string(), attributes: jobAttributes, links: jobLinks, relationships: jobRelationships}); export const jobsData = z.object({data: z.array(jobResource)}); 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 job objects * * GET /rest/jobs * * Also accepts auth: oauth_user_token */ export const getApiRestJobs = async (options?: GetApiRestJobsOptions) => { const {baseUrl = "https://api.dailypay.com", fetch: fetchFn = fetch, oauthClientCredentialsToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.api+json") if (oauthClientCredentialsToken !== undefined) headers.set('authorization', `Bearer ${oauthClientCredentialsToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/rest/jobs${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(jobsData, await res.json()) };