import {z} from 'zod' export type GetApiApiV1StatisticalAgenciesIbgeAggregatesAggregateIdMetadataArgs = {aggregate_id: number}; export type GetApiApiV1StatisticalAgenciesIbgeAggregatesAggregateIdMetadataOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const ibgePeriodicity = z.object({frequencia: z.string().nullable().optional(), inicio: z.number().int().nullable().optional(), fim: z.number().int().nullable().optional()}); export const ibgeTerritorialLevels = z.object({Administrativo: z.array(z.string()).optional(), Especial: z.array(z.string()).optional(), IBGE: z.array(z.string()).optional()}); export const ibgeVariable = z.object({id: z.number().int(), nome: z.string(), unidade: z.string().nullable().optional(), sumarizacao: z.array(z.string()).optional()}); export const ibgeClassificationCategory = z.object({id: z.number().int(), nome: z.string(), unidade: z.string().nullable().optional(), nivel: z.number().int().nullable().optional()}); export const ibgeClassification = z.object({id: z.number().int(), nome: z.string(), sumarizacao: z.unknown(), categorias: z.array(ibgeClassificationCategory).optional()}); export const ibgeAggregateMetadataPayload = z.object({id: z.number().int(), nome: z.string(), URL: z.string().nullable().optional(), pesquisa: z.string().nullable().optional(), assunto: z.string().nullable().optional(), periodicidade: ibgePeriodicity.nullable().optional(), nivelTerritorial: ibgeTerritorialLevels.nullable().optional(), variaveis: z.array(ibgeVariable).optional(), classificacoes: z.array(ibgeClassification).optional()}); export const sugraMeta = z.object({endpoint: z.string(), data_time: z.string(), response_time: z.string(), provider: z.string(), data_age_days: z.number().nullable().optional(), source: z.string().nullable().optional(), attribution: z.string().nullable().optional(), source_url: z.string().nullable().optional(), citation: z.string().nullable().optional(), fallback_used: z.boolean().nullable().optional(), fallback_chain: z.array(z.string()).nullable().optional(), cached: z.boolean().nullable().optional(), stale: z.boolean().nullable().optional(), stale_since: z.string().nullable().optional(), period: z.string().nullable().optional(), notes: z.string().nullable().optional()}); export const envelopeIbgeAggregateMetadataPayload = z.object({data: ibgeAggregateMetadataPayload, meta: sugraMeta}); 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 }; /** * SIDRA aggregate metadata * * GET /api/v1/statistical-agencies/ibge/aggregates/{aggregate_id}/metadata */ export const getApiApiV1StatisticalAgenciesIbgeAggregatesAggregateIdMetadata = async (args: GetApiApiV1StatisticalAgenciesIbgeAggregatesAggregateIdMetadataArgs, options?: GetApiApiV1StatisticalAgenciesIbgeAggregatesAggregateIdMetadataOptions) => { const {baseUrl = "https://sugra.ai", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/statistical-agencies/ibge/aggregates/${encodeURIComponent(String(args.aggregate_id))}/metadata${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(envelopeIbgeAggregateMetadataPayload, await res.json()) };