import {z} from 'zod' export type GetApiApiMartsIdArgs = {id: number}; export type GetApiApiMartsIdOptions = {baseUrl: string, token?: string | undefined, fetch?: typeof fetch | undefined}; export const attributeType = z.literal(1); export const attribute = z.object({alias: z.string().nullable().optional(), type: attributeType.optional(), id: z.number().int().optional()}); export const martAttribute = z.object({mart: z.lazy(() => mart.nullable().optional()), attribute: attribute.nullable().optional(), columnName: z.string().nullable().optional(), dataDictionaryItemId: z.number().int().optional(), id: z.number().int().optional()}); export const supplier = z.object({alias: z.string().nullable().optional(), name: z.string().nullable().optional(), id: z.number().int().optional()}); export const martSupplier = z.object({mart: z.lazy(() => mart.nullable().optional()), supplier: supplier.nullable().optional(), tableName: z.string().nullable().optional(), id: z.number().int().optional()}); export const mart: z.ZodType = z.object({alias: z.string().nullable().optional(), server: z.string().nullable().optional(), database: z.string().nullable().optional(), username: z.string().nullable().optional(), password: z.string().nullable().optional(), searchTypeId: z.number().int().optional(), audit: z.boolean().optional(), martAttributes: z.array(martAttribute).nullable().optional(), martSuppliers: z.array(martSupplier).nullable().optional(), attributes: z.array(z.string()).nullable().optional(), suppliers: z.array(z.string()).nullable().optional(), id: z.number().int().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 /api/marts/{id} */ export const getApiApiMartsId = async (args: GetApiApiMartsIdArgs, options: GetApiApiMartsIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, token} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (token !== undefined) headers.set("X-Deluxe-Auth", token) const url = `${baseUrl.replace(/\/+$/, '')}/api/marts/${encodeURIComponent(String(args.id))}${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(mart, await res.json()) };