import {z} from 'zod' export type GetApiLocationsLocationIdProfileArgs = {locationId: string}; export type GetApiLocationsLocationIdProfileOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const locationProfileDataType = z.enum(["single_select", "multiple_select", "number"]); export const locationProfileSelectedValue = z.object({id: z.string().optional(), value: z.unknown(), from: z.string().nullable().optional()}); export const locationProfilePossibleValue = z.object({value: z.string().nullable().optional(), developer_description: z.string().nullable().optional()}); export const locationPropertyLimit = z.object({min: z.number().optional(), max: z.number().optional(), step: z.number().optional()}); export const locationProfilePropertyRequirement = z.object({key: z.string().nullable().optional(), accepted_values: z.array(z.unknown()).nullable().optional()}); export const locationProfileTag = z.enum(["required_for_sh_to_work", "required_for_euc_to_work"]); export const locationProfileSetting = z.object({key: z.string().min(1), data_type: locationProfileDataType, selected_values: z.array(locationProfileSelectedValue), possible_values: z.array(locationProfilePossibleValue), limits: locationPropertyLimit.optional(), dependent_properties: z.array(locationProfilePropertyRequirement), developer_description: z.string().min(1), tags: z.array(locationProfileTag), group: z.string().min(1)}); export const locationProfileSettings = z.object({settings: z.array(locationProfileSetting), groups: z.array(z.string())}); 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 }; /** * Get location profile * * GET /locations/{locationId}/profile */ export const getApiLocationsLocationIdProfile = async (args: GetApiLocationsLocationIdProfileArgs, options: GetApiLocationsLocationIdProfileOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/locations/${encodeURIComponent(String(args.locationId))}/profile${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(locationProfileSettings, await res.json()) };