import {z} from 'zod' export type GetApiApiV1ResearchPwtCountryIso3Args = {iso3: string, start_year?: (number | null) | undefined, end_year?: (number | null) | undefined, variables?: (string | null) | undefined, version?: (string | null) | undefined}; export type GetApiApiV1ResearchPwtCountryIso3Options = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const pwtObservation = z.object({year: z.number().int(), countrycode: z.string().nullable().optional(), country: z.string().nullable().optional(), currency_unit: z.string().nullable().optional()}).and(z.record(z.string(), z.unknown())); export const pwtCountryPayload = z.object({version: z.string(), countrycode: z.string(), country: z.string().nullable().optional(), currency_unit: z.string().nullable().optional(), variables: z.array(z.string()), observations: z.array(pwtObservation), count: z.number().int(), start_year: z.number().int().nullable().optional(), end_year: z.number().int().nullable().optional(), attribution: z.string()}); 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 envelopePwtCountryPayload = z.object({data: pwtCountryPayload, 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 }; /** * Full Penn World Table time series for one country * * GET /api/v1/research/pwt/country/{iso3} */ export const getApiApiV1ResearchPwtCountryIso3 = async (args: GetApiApiV1ResearchPwtCountryIso3Args, options?: GetApiApiV1ResearchPwtCountryIso3Options) => { const {baseUrl = "https://sugra.ai", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.start_year != null) query.append("start_year", String(args.start_year)) if (args.end_year != null) query.append("end_year", String(args.end_year)) if (args.variables != null) query.append("variables", String(args.variables)) if (args.version != null) query.append("version", String(args.version)) if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/research/pwt/country/${encodeURIComponent(String(args.iso3))}${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(envelopePwtCountryPayload, await res.json()) };