import {z} from 'zod' export type GetApiApiBusinessBusinessIdBranchBranchIdCourseArgs = {businessId: string, branchId: string, includeArchived?: boolean | undefined, size?: number | undefined, page?: number | undefined}; export type GetApiApiBusinessBusinessIdBranchBranchIdCourseOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const courseItem = z.object({courseItemId: z.string().optional(), unitType: z.enum(["SESSIONS", "MINUTES"]).optional(), totalUnits: z.number().int().optional(), totalPrice: z.number().optional(), serviceId: z.string().optional()}); export const course = z.object({courseId: z.string().optional(), courseName: z.string().optional(), availableOnline: z.boolean().optional(), archived: z.boolean().optional(), multiBranchCourse: z.boolean().optional(), minDaysBetweenSessions: z.number().int().optional(), maxDaysBetweenSessions: z.number().int().optional(), totalPrice: z.number().optional(), courseItems: z.array(courseItem).optional()}); export const pageMetadata = z.object({size: z.number().int().optional(), totalElements: z.number().int().optional(), totalPages: z.number().int().optional(), number: z.number().int().optional()}); export const dataPagedModelCourse = z.object({_embedded: z.object({courses: z.array(course).optional()}).optional(), page: pageMetadata.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 courses * * GET /api/business/{businessId}/branch/{branchId}/course */ export const getApiApiBusinessBusinessIdBranchBranchIdCourse = async (args: GetApiApiBusinessBusinessIdBranchBranchIdCourseArgs, options?: GetApiApiBusinessBusinessIdBranchBranchIdCourseOptions) => { const {baseUrl = "http://api-gateway-eu.phorest.com/third-party-api-server", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.includeArchived != null) query.append("includeArchived", String(args.includeArchived)) if (args.size != null) query.append("size", String(args.size)) if (args.page != null) query.append("page", String(args.page)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/business/${encodeURIComponent(String(args.businessId))}/branch/${encodeURIComponent(String(args.branchId))}/course${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(dataPagedModelCourse, await res.json()) };