import {z} from 'zod' export type CreateApiTimeOffPoliciesArgs = {body: {company: string, name: string, active?: boolean | undefined, policy_year?: ({basis?: ('fixed_date' | 'assignment') | undefined, date?: (string | null) | undefined}) | undefined, earning_type: 'pto' | 'sick', earning_code?: (string | null) | undefined, accrual_method: 'annual_grant' | 'per_payroll' | 'per_hour_worked' | 'none', annual_grant?: ({hours?: unknown | undefined, partial_year_method?: ('prorate' | 'full') | undefined} | null) | undefined, per_payroll?: ({hours_per_year?: unknown | undefined} | null) | undefined, per_hour_worked?: ({hours?: unknown | undefined, per_hours_worked?: unknown | undefined, include_overtime?: boolean | undefined} | null) | undefined, limits?: ({max_balance?: (unknown | null) | undefined, min_balance?: (unknown | null) | undefined, max_rollover?: (unknown | null) | undefined, max_accrual_per_policy_year?: (unknown | null) | undefined}) | undefined, accrual_waiting_period_days?: number | undefined, usage_waiting_period_days?: number | undefined, metadata?: Record | undefined}}; export type CreateApiTimeOffPoliciesOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const timeOffPolicy = z.object({id: z.string().optional(), company: z.string().optional(), name: z.string().optional(), active: z.boolean().optional(), policy_year: z.object({basis: z.enum(["fixed_date", "assignment"]).optional(), date: z.string().nullable().optional()}).optional(), earning_type: z.enum(["pto", "sick"]).optional(), earning_code: z.string().nullable().optional(), accrual_method: z.enum(["annual_grant", "per_payroll", "per_hour_worked", "none"]).optional(), annual_grant: z.object({hours: z.string().optional(), partial_year_method: z.enum(["prorate", "full"]).optional()}).nullable().optional(), per_payroll: z.object({hours_per_year: z.string().optional()}).nullable().optional(), per_hour_worked: z.object({hours: z.string().optional(), per_hours_worked: z.string().optional(), include_overtime: z.boolean().optional()}).nullable().optional(), limits: z.object({max_balance: z.string().nullable().optional(), min_balance: z.string().nullable().optional(), max_rollover: z.string().nullable().optional(), max_accrual_per_policy_year: z.string().nullable().optional()}).optional(), accrual_waiting_period_days: z.number().int().optional(), usage_waiting_period_days: z.number().int().optional(), metadata: z.object({}).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 }; /** * Create a time off policy * * POST /time-off/policies */ export const createApiTimeOffPolicies = async (args: CreateApiTimeOffPoliciesArgs, options?: CreateApiTimeOffPoliciesOptions) => { const {baseUrl = "https://sandbox.checkhq.com", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (sec0 !== undefined) headers.set("Authorization", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/time-off/policies${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(timeOffPolicy, await res.json()) };