import {z} from 'zod' export type GetApiV2ReportingTemplatesNameArgs = {name: string}; export type GetApiV2ReportingTemplatesNameOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const metricParamDefinition = z.object({name: z.string().optional(), type: z.enum(["date", "string", "array", "enum", "integer"]).optional(), required: z.boolean().optional(), description: z.string().optional(), options: z.array(z.unknown()).optional(), constraints: z.object({}).optional()}); export const metricTemplate = z.object({name: z.string().optional(), displayName: z.string().optional(), description: z.string().optional(), params: z.array(metricParamDefinition).optional(), supportsBreakdowns: z.boolean().optional(), breakdownOptions: z.array(z.string()).optional()}); export const templateResponse = z.object({status: z.literal("success").optional(), data: metricTemplate.optional(), metadata: z.object({}).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 }; /** * Get Template Details * * GET /v2/reporting/templates/{name} */ export const getApiV2ReportingTemplatesName = async (args: GetApiV2ReportingTemplatesNameArgs, options?: GetApiV2ReportingTemplatesNameOptions) => { const {baseUrl = "https://production.hifibridge.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/reporting/templates/${encodeURIComponent(String(args.name))}${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(templateResponse, await res.json()) };