import {z} from 'zod' export type BatchIdentifiersRequest = {ids: Array}; export type CreateApiV1BatchIdentifiersArgs = {body: BatchIdentifiersRequest}; export type CreateApiV1BatchIdentifiersOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const identifierProvenance = z.object({market_identifiers: z.string(), entity_registry: z.string(), explanation: z.string()}); export const identifiersOut = z.object({edinet_code: z.string().nullable().optional(), securities_code: z.string().nullable().optional(), isin: z.string().nullable().optional(), lei: z.string().nullable().optional(), jpx_code: z.string().nullable().optional(), match_method: z.string().nullable().optional(), confidence: z.number().nullable().optional(), matched_at: z.string().nullable().optional(), entity_id: z.string().nullable().optional(), name: z.string().nullable().optional(), name_en: z.string().nullable().optional(), roles: z.array(z.string()).optional(), links: z.record(z.string(), z.string()).optional(), provenance: identifierProvenance}); export const axioraApiSchemasMeta = z.record(z.string(), z.unknown()); export const listResponseIdentifiersOut = z.object({data: z.array(identifiersOut), meta: axioraApiSchemasMeta}); 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 }; /** * Batch Resolve Identifiers * * POST /v1/batch/identifiers */ export const createApiV1BatchIdentifiers = async (args: CreateApiV1BatchIdentifiersArgs, options: CreateApiV1BatchIdentifiersOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/batch/identifiers${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(listResponseIdentifiersOut, await res.json()) };