import {z} from 'zod' export type SvhelpModelsTimeWindow = {start?: string | undefined, end?: string | undefined}; export type SvhelpModelsWeeklyHours = {sunday?: Array | undefined, monday?: Array | undefined, tuesday?: Array | undefined, wednesday?: Array | undefined, thursday?: Array | undefined, friday?: Array | undefined, saturday?: Array | undefined}; export type CreateApiV2TeamsTeamIdSchedulesArgs = {team_id: string, body: {name?: string | undefined, timezone?: string | undefined, weeklyHours?: SvhelpModelsWeeklyHours | undefined}}; export type CreateApiV2TeamsTeamIdSchedulesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const svhelpModelsTimeWindow = z.object({start: z.string().optional(), end: z.string().optional()}); export const svhelpModelsWeeklyHours = z.object({sunday: z.array(svhelpModelsTimeWindow).optional(), monday: z.array(svhelpModelsTimeWindow).optional(), tuesday: z.array(svhelpModelsTimeWindow).optional(), wednesday: z.array(svhelpModelsTimeWindow).optional(), thursday: z.array(svhelpModelsTimeWindow).optional(), friday: z.array(svhelpModelsTimeWindow).optional(), saturday: z.array(svhelpModelsTimeWindow).optional()}); export const googleProtobufTimestamp = z.string(); export const svhelpModelsSchedule = z.object({id: z.string().optional(), teamId: z.string().optional(), name: z.string().optional(), timezone: z.string().optional(), weeklyHours: svhelpModelsWeeklyHours.optional(), createdAt: googleProtobufTimestamp.optional(), updatedAt: googleProtobufTimestamp.optional()}); export const svhelpPublicapiCreateScheduleResponse = z.object({data: svhelpModelsSchedule.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 }; /** * Create Schedule * * POST /v2/teams/{team_id}/schedules */ export const createApiV2TeamsTeamIdSchedules = async (args: CreateApiV2TeamsTeamIdSchedulesArgs, options?: CreateApiV2TeamsTeamIdSchedulesOptions) => { const {baseUrl = "https://public.api.serval.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/teams/${encodeURIComponent(String(args.team_id))}/schedules${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(svhelpPublicapiCreateScheduleResponse, await res.json()) };