import {z} from 'zod' export type GetApiV2TimeClockingPayrollPeriodsArgs = {'x-api-version'?: string | undefined, 'x-company-guid'?: string | undefined, cursor?: string | undefined, limit?: number | undefined, company_id: number, include_punches_status?: boolean | undefined}; export type GetApiV2TimeClockingPayrollPeriodsOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV2TimeClockingPayrollPeriodsResponse = z.object({data: z.array(z.object({id: z.number().int(), company_id: z.number().int(), start: z.string(), end: z.string(), closed: z.boolean(), finalized: z.boolean().nullable(), states: z.array(z.object({id: z.number().int().nullable(), location_id: z.number().int(), closed: z.boolean(), processed: z.string().nullable(), modified_by_user_id: z.number().int(), closed_or_reopened_at: z.string().nullable()})), all_punches_approved: z.boolean().nullable(), employee_punch_approvals: z.enum(["reviewed", "pending"]).nullable(), created: z.string().nullable(), modified: z.string().nullable()})), 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 Payroll Periods * * GET /v2/time_clocking/payroll_periods * * Also accepts auth: cookieAuth */ export const getApiV2TimeClockingPayrollPeriods = async (args: GetApiV2TimeClockingPayrollPeriodsArgs, options?: GetApiV2TimeClockingPayrollPeriodsOptions) => { 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.cursor != null) query.append("cursor", String(args.cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.company_id != null) query.append("company_id", String(args.company_id)) if (args.include_punches_status != null) query.append("include_punches_status", String(args.include_punches_status)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/time_clocking/payroll_periods${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(getApiV2TimeClockingPayrollPeriodsResponse, await res.json()) };