import {z} from 'zod' export type GetApiV1CompaniesCodePeersArgs = {code: string, limit?: number | undefined}; export type GetApiV1CompaniesCodePeersOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const companyOut = z.object({edinet_code: z.string(), securities_code: z.string().nullable().optional(), name_jp: z.string(), name_en: z.string().nullable().optional(), name_en_quality: z.string().nullable().optional(), name_en_resolved_at: z.string().nullable().optional(), sector: z.string().nullable().optional(), sector_en: z.string().nullable().optional(), fiscal_year_end: z.string().nullable().optional(), listing: z.string().nullable().optional(), listing_en: z.string().nullable().optional(), sector_display: z.string().optional(), entity_type: z.string().nullable().optional(), created_at: z.string(), updated_at: z.string()}); export const peerOut = z.object({company: companyOut, fiscal_year: z.number().int(), revenue: z.number().int().nullable().optional(), net_income: z.number().int().nullable().optional(), roe: z.number().nullable().optional(), health_score: z.number().int().nullable().optional()}); export const axioraApiSchemasMeta = z.record(z.string(), z.unknown()); export const listResponsePeerOut = z.object({data: z.array(peerOut), 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 }; /** * Get Peers * * GET /v1/companies/{code}/peers */ export const getApiV1CompaniesCodePeers = async (args: GetApiV1CompaniesCodePeersArgs, options: GetApiV1CompaniesCodePeersOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/companies/${encodeURIComponent(String(args.code))}/peers${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(listResponsePeerOut, await res.json()) };