import {z} from 'zod' export type GetApiV2HoldersHolderCodeArgs = {holder_code: string, include?: (string | null) | undefined}; export type GetApiV2HoldersHolderCodeOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const subject = z.object({entity: z.string(), role: z.string()}); export const source = z.object({doc_id: z.string().nullable().optional(), doc_type: z.string().nullable().optional(), filed_at: z.string().nullable().optional(), url: z.string().nullable().optional(), section: z.string().nullable().optional(), quote: z.string().nullable().optional(), read_by: z.literal("kiri").nullable().optional()}); export const fact = z.object({id: z.string(), type: z.string(), subjects: z.array(subject), valid_as_of: z.string().nullable().optional(), known_at: z.string().nullable().optional(), value: z.record(z.string(), z.unknown()), superseded_by: z.string().nullable().optional(), source: source, status: z.enum(["confirmed", "held"]), held_reason: z.string().nullable().optional()}); export const entity = z.object({id: z.string(), type: z.string(), name: z.string().nullable().optional(), name_jp: z.string().nullable().optional(), attributes: z.record(z.string(), z.unknown()).optional(), ids: z.record(z.string(), z.string().nullable()).optional(), facts: z.record(z.string(), z.array(fact)).nullable().optional(), facts_more: z.record(z.string(), z.string()).nullable().optional(), status: z.enum(["confirmed", "held"]).optional(), held_reason: z.string().nullable().optional()}); export const axioraApiV2ModelsMeta = z.object({request_id: z.string(), data_as_of: z.string().nullable().optional(), data_version: z.string().nullable().optional(), next_cursor: z.string().nullable().optional(), resolved_from: z.string().nullable().optional(), deprecations: z.array(z.record(z.string(), z.unknown())).optional(), caveats: z.array(z.string()).optional()}); export const entityOne = z.object({data: entity, meta: axioraApiV2ModelsMeta}); 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 Holder * * GET /v2/holders/{holder_code} */ export const getApiV2HoldersHolderCode = async (args: GetApiV2HoldersHolderCodeArgs, options: GetApiV2HoldersHolderCodeOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.include != null) query.append("include", String(args.include)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/holders/${encodeURIComponent(String(args.holder_code))}${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(entityOne, await res.json()) };