import {z} from 'zod' export type TimesheetsetupsCreatePayload = {approver_id?: number | undefined, member_ids?: Array | undefined, periodicity?: string | undefined, reminder_day?: string | undefined, reminder_time?: string | undefined, start_date?: string | undefined}; export type CreateApiWorkspacesWorkspaceIdTimesheetSetupsArgs = {workspace_id: number, body: TimesheetsetupsCreatePayload}; export type CreateApiWorkspacesWorkspaceIdTimesheetSetupsOptions = {baseUrl: string, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const modelsTimesheetSetupError = z.object({code: z.string().optional(), message: z.string().optional()}); export const timesheetsetupsAPITimesheetSetup = z.object({approver_id: z.number().int().optional(), approver_name: z.string().optional(), end_date: z.string().optional(), errors: z.array(modelsTimesheetSetupError).optional(), id: z.number().int().optional(), member_id: z.number().int().optional(), member_name: z.string().optional(), periodicity: z.string().optional(), reminder_day: z.string().optional(), reminder_time: z.string().optional(), start_date: z.string().optional(), workspace_id: z.number().int().optional()}); export const createApiWorkspacesWorkspaceIdTimesheetSetupsResponse = z.array(timesheetsetupsAPITimesheetSetup); 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 timesheet setup * * POST /workspaces/{workspace_id}/timesheet_setups */ export const createApiWorkspacesWorkspaceIdTimesheetSetups = async (args: CreateApiWorkspacesWorkspaceIdTimesheetSetupsArgs, options: CreateApiWorkspacesWorkspaceIdTimesheetSetupsOptions) => { const {baseUrl, fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/workspaces/${encodeURIComponent(String(args.workspace_id))}/timesheet_setups${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(createApiWorkspacesWorkspaceIdTimesheetSetupsResponse, await res.json()) };