import {z} from 'zod' export type GetApiMeasurementUnitArgs = {show?: number | undefined, cursor?: string | undefined, category_id?: string | undefined, name?: string | undefined, active?: boolean | undefined}; export type GetApiMeasurementUnitOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiMeasurementUnitResponse = z.object({success: z.boolean().optional(), message: z.string().optional(), data: z.array(z.object({id: z.number().int().optional(), account_id: z.number().int().optional(), name: z.string().optional(), abbreviation: z.string().optional(), decimal_places: z.number().int().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), active: z.boolean().optional()})).optional(), meta: z.object({cursor: z.object({current: z.unknown(), prev: z.unknown(), next: z.string().optional(), count: z.unknown()}).optional()}).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 }; /** * Listar unidades de medida * * GET /measurementUnit */ export const getApiMeasurementUnit = async (args: GetApiMeasurementUnitArgs, options?: GetApiMeasurementUnitOptions) => { const {baseUrl = "https://api.pipe.run/v1", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.show != null) query.append("show", String(args.show)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.category_id != null) query.append("category_id", String(args.category_id)) if (args.name != null) query.append("name", String(args.name)) if (args.active != null) query.append("active", String(args.active)) if (sec0 !== undefined) headers.set("token", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/measurementUnit${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(getApiMeasurementUnitResponse, await res.json()) };