import {z} from 'zod' export type GetApiV2TimeOffTimeOffIdArgs = {'x-api-version'?: string | undefined, 'x-company-guid'?: string | undefined, time_off_id: number}; export type GetApiV2TimeOffTimeOffIdOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV2TimeOffTimeOffIdResponse = 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()}))}); 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 }; /** * Retrieve Time Off * * GET /v2/time_off/{time_off_id} * * Also accepts auth: cookieAuth */ export const getApiV2TimeOffTimeOffId = async (args: GetApiV2TimeOffTimeOffIdArgs, options?: GetApiV2TimeOffTimeOffIdOptions) => { 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 (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/time_off/${encodeURIComponent(String(args.time_off_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(getApiV2TimeOffTimeOffIdResponse, await res.json()) };