import {z} from 'zod' export type GetApiSubnationalCountryCodeArgs = {country_code: string, level?: (string | null) | undefined, admin_level?: (number | null) | undefined, income_class?: (string | null) | undefined, year_from?: (number | null) | undefined, year_to?: (number | null) | undefined, include_forecasts?: boolean | undefined, city_name?: (string | null) | undefined, currencies?: (string | null) | undefined}; export type GetApiSubnationalCountryCodeOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiSubnationalCountryCodeResponse = z.unknown(); 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 Subnational * * GET /subnational/{country_code} */ export const getApiSubnationalCountryCode = async (args: GetApiSubnationalCountryCodeArgs, options: GetApiSubnationalCountryCodeOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.level != null) query.append("level", String(args.level)) if (args.admin_level != null) query.append("admin_level", String(args.admin_level)) if (args.income_class != null) query.append("income_class", String(args.income_class)) if (args.year_from != null) query.append("year_from", String(args.year_from)) if (args.year_to != null) query.append("year_to", String(args.year_to)) if (args.include_forecasts != null) query.append("include_forecasts", String(args.include_forecasts)) if (args.city_name != null) query.append("city_name", String(args.city_name)) if (args.currencies != null) query.append("currencies", String(args.currencies)) if (APIKeyHeader !== undefined) headers.set("X-API-Key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/subnational/${encodeURIComponent(String(args.country_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(getApiSubnationalCountryCodeResponse, await res.json()) };