import {z} from 'zod' export type GetApiV1CompaniesCodeSectionsArgs = {code: string, fiscal_year?: (number | null) | undefined, section?: (string | null) | undefined}; export type GetApiV1CompaniesCodeSectionsOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const sectionOut = z.object({section: z.string(), label_en: z.string(), label_jp: z.string(), text_jp: z.string(), text_en: z.string().nullable().optional(), char_count: z.number().int()}); export const axioraApiSchemasMeta = z.record(z.string(), z.unknown()); export const listResponseSectionOut = z.object({data: z.array(sectionOut), 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 Company Sections * * GET /v1/companies/{code}/sections */ export const getApiV1CompaniesCodeSections = async (args: GetApiV1CompaniesCodeSectionsArgs, options: GetApiV1CompaniesCodeSectionsOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.fiscal_year != null) query.append("fiscal_year", String(args.fiscal_year)) if (args.section != null) query.append("section", String(args.section)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/companies/${encodeURIComponent(String(args.code))}/sections${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(listResponseSectionOut, await res.json()) };