import {z} from 'zod' export type MerqTimestamp = string | string | string; export type GetApiSecurityTypeArgs = {name?: string | undefined, names?: string | undefined, id?: string | undefined, ids?: string | undefined, metrics?: string | undefined, securityfilter?: string | undefined, namespace?: string | undefined, start_date?: MerqTimestamp | undefined, end_date?: MerqTimestamp | undefined, format?: 'csv' | undefined, as_of?: string | undefined, since?: string | undefined, raw?: boolean | undefined, show_full_history?: boolean | undefined, debug?: 'true' | undefined, validated_only?: boolean | undefined, prefer_read_replica?: boolean | undefined}; export type GetApiSecurityTypeOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const errorCodes = z.object({code: z.string().optional(), message: z.string().optional()}); export const merqTimestamp = z.union([z.string(), z.string(), z.string()]); export const securityMetrics = z.object({eff_ts: merqTimestamp, id: z.string(), lineage: z.object({}).optional(), metric: z.string(), prov_ts: merqTimestamp, source: z.string().optional(), value: z.union([z.string(), z.number(), z.number().int(), z.boolean(), z.array(z.unknown()), z.object({})])}); export const getApiSecurityTypeResponse = z.object({error_codes: errorCodes.optional(), results: z.array(securityMetrics).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 one or more metrics for one or more securities * * GET /security/{type} */ export const getApiSecurityType = async (args: GetApiSecurityTypeArgs, options?: GetApiSecurityTypeOptions) => { const {baseUrl = "https://api.merqube.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.name != null) query.append("name", String(args.name)) if (args.names != null) query.append("names", String(args.names)) if (args.id != null) query.append("id", String(args.id)) if (args.ids != null) query.append("ids", String(args.ids)) if (args.metrics != null) query.append("metrics", String(args.metrics)) if (args.securityfilter != null) query.append("securityfilter", String(args.securityfilter)) if (args.namespace != null) query.append("namespace", String(args.namespace)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.format != null) query.append("format", String(args.format)) if (args.as_of != null) query.append("as_of", String(args.as_of)) if (args.since != null) query.append("since", String(args.since)) if (args.raw != null) query.append("raw", String(args.raw)) if (args.show_full_history != null) query.append("show_full_history", String(args.show_full_history)) if (args.debug != null) query.append("debug", String(args.debug)) if (args.validated_only != null) query.append("validated_only", String(args.validated_only)) if (args.prefer_read_replica != null) query.append("prefer_read_replica", String(args.prefer_read_replica)) const url = `${baseUrl.replace(/\/+$/, '')}/security/${encodeURIComponent(String(args.type))}${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(getApiSecurityTypeResponse, await res.json()) };