import {z} from 'zod' export type GetApiV2OrganizationsOrganizationIdAttendanceSchedulesArgs = {organization_id: number, page_start_id?: number | undefined, page_limit?: number | undefined, 'date[start]': string, 'date[stop]': string, include?: Array<'users'> | undefined}; export type GetApiV2OrganizationsOrganizationIdAttendanceSchedulesOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const attendanceSchedule = z.object({id: z.number().int(), user_id: z.number().int(), organization_id: z.number().int(), created_at: z.string(), updated_at: z.string(), start_date: z.string(), use_time_zone: z.string(), start_time: z.string(), duration: z.number().int(), minimum_time: z.number().int(), repeat_schedule: z.string(), repeat_until: z.string().optional(), weekdays: z.array(z.string()).optional(), excluded_dates: z.array(z.string()).optional()}); export const getApiV2OrganizationsOrganizationIdAttendanceSchedulesResponse = z.object({attendance_schedules: z.array(attendanceSchedule).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 }; /** * List organization attendance schedules * * GET /v2/organizations/{organization_id}/attendance_schedules */ export const getApiV2OrganizationsOrganizationIdAttendanceSchedules = async (args: GetApiV2OrganizationsOrganizationIdAttendanceSchedulesArgs, options?: GetApiV2OrganizationsOrganizationIdAttendanceSchedulesOptions) => { const {baseUrl = "https://api.hubstaff.com", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page_start_id != null) query.append("page_start_id", String(args.page_start_id)) if (args.page_limit != null) query.append("page_limit", String(args.page_limit)) if (args['date[start]'] != null) query.append("date[start]", String(args['date[start]'])) if (args['date[stop]'] != null) query.append("date[stop]", String(args['date[stop]'])) for (const item of args.include ?? []) query.append("include", String(item)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/organizations/${encodeURIComponent(String(args.organization_id))}/attendance_schedules${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(getApiV2OrganizationsOrganizationIdAttendanceSchedulesResponse, await res.json()) };