import {z} from 'zod' export type GetApiV2TimeOffArgs = {'x-api-version'?: string | undefined, 'x-company-guid'?: string | undefined, company_id: number, location_id?: number | undefined, user_id?: number | undefined, status?: number | undefined, category?: ('paid' | 'unpaid' | 'paid_sick') | undefined, to_date_gte?: string | undefined, sort_by?: ('created' | 'from_date') | undefined, sort_dir?: ('asc' | 'desc') | undefined, cursor?: string | undefined, limit?: number | undefined}; export type GetApiV2TimeOffOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV2TimeOffResponse = z.object({data: z.array(z.object({id: z.number().int(), user_id: z.number().int(), company_id: z.number().int(), from_date: z.string(), to_date: z.string(), partial: z.boolean(), partial_from: z.string().nullable(), partial_to: z.string().nullable(), comments: z.string(), status: z.number().int(), status_action_user_id: z.number().int().nullable(), status_action_message: z.string(), category: z.string(), created: z.string(), amount_of_hours: z.number(), hours: z.array(z.object({date: z.string(), hours: z.number()}))})), meta: z.object({cursor: z.object({current: z.string().nullable(), prev: z.string().nullable(), next: z.string().nullable(), count: z.number().int().nullable()})})}); 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 }; /** * List Time Off * * GET /v2/time_off * * Also accepts auth: cookieAuth */ export const getApiV2TimeOff = async (args: GetApiV2TimeOffArgs, options?: GetApiV2TimeOffOptions) => { const {baseUrl = "https://api.7shifts.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['x-api-version'] != null) headers.set("x-api-version", String(args['x-api-version'])) if (args['x-company-guid'] != null) headers.set("x-company-guid", String(args['x-company-guid'])) if (args.company_id != null) query.append("company_id", String(args.company_id)) if (args.location_id != null) query.append("location_id", String(args.location_id)) if (args.user_id != null) query.append("user_id", String(args.user_id)) if (args.status != null) query.append("status", String(args.status)) if (args.category != null) query.append("category", String(args.category)) if (args.to_date_gte != null) query.append("to_date_gte", String(args.to_date_gte)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.sort_dir != null) query.append("sort_dir", String(args.sort_dir)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/time_off${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(getApiV2TimeOffResponse, await res.json()) };