import {z} from 'zod' export type CreateApiHmdsScannerArgs = {body: {instrument?: string | undefined, locations?: string | undefined, scanCode?: string | undefined, secType?: string | undefined, filters?: (Array<{code?: string | undefined, value?: boolean | undefined}>) | undefined}}; export type CreateApiHmdsScannerOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const scannerResult = z.object({total: z.number().int().optional(), size: z.number().int().optional(), offset: z.number().int().optional(), scanTime: z.string().optional(), id: z.number().optional(), position: z.string().optional(), Contracts: z.object({Contract: z.array(z.object({inScanTime: z.string().optional(), distance: z.number().int().optional(), contractID: z.number().int().optional()})).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 }; /** * Run Scanner (Beta) * * POST /hmds/scanner */ export const createApiHmdsScanner = async (args: CreateApiHmdsScannerArgs, options?: CreateApiHmdsScannerOptions) => { const {baseUrl = "http://localhost:5000/v1/api", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/hmds/scanner${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(scannerResult, await res.json()) };