import {z} from 'zod' export type GetApiApiBusinessBusinessIdBranchArgs = {businessId: string, service_id?: string | undefined, service_internet_enabled?: boolean | undefined, size?: number | undefined, page?: number | undefined}; export type GetApiApiBusinessBusinessIdBranchOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const branchResponse = z.object({branchId: z.string().optional(), name: z.string().optional(), timeZone: z.string().optional(), latitude: z.number().optional(), longitude: z.number().optional(), streetAddress1: z.string().optional(), streetAddress2: z.string().optional(), city: z.string().optional(), state: z.string().optional(), postalCode: z.string().optional(), country: z.string().optional(), currencyCode: z.string().optional(), accountId: z.number().int().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 dataPagedModelBranchResponse = z.object({_embedded: z.object({branches: z.array(branchResponse).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 branches * * GET /api/business/{businessId}/branch */ export const getApiApiBusinessBusinessIdBranch = async (args: GetApiApiBusinessBusinessIdBranchArgs, options?: GetApiApiBusinessBusinessIdBranchOptions) => { 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.service_id != null) query.append("service_id", String(args.service_id)) if (args.service_internet_enabled != null) query.append("service_internet_enabled", String(args.service_internet_enabled)) 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${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(dataPagedModelBranchResponse, await res.json()) };