import {z} from 'zod' export type GetApiApiV1EmpiricalDataArgs = {indicatorId?: number | undefined, locationId?: number | undefined, yearStart?: number | undefined, yearEnd?: number | undefined, sexId?: number | undefined, ageId?: number | undefined, categoryId?: number | undefined, included?: boolean | undefined}; export type GetApiApiV1EmpiricalDataOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const footnote = z.object({noteCountry: z.string().nullable().optional(), noteData: z.string().nullable().optional(), notePopulation: z.string().nullable().optional(), noteContraceptiveUseMethods: z.string().nullable().optional(), noteUnmetNeedIndicator: z.string().nullable().optional()}); export const empiricalDataDto = z.object({timeLabel: z.number().optional(), sexId: z.number().int().optional(), ageId: z.number().int().optional(), ageMid: z.number().optional(), ageLabel: z.string().nullable().optional(), nonStandardAgeLabel: z.string().nullable().optional(), categoryId: z.number().int().optional(), category2Id: z.number().int().nullable().optional(), value: z.number().optional(), dataProcessTypeId: z.number().int().optional(), dataProcessType: z.string().nullable().optional(), dataProcessId: z.number().int().optional(), dataProcess: z.string().nullable().optional(), dataProcessShortName: z.string().nullable().optional(), dataCatalogShortName: z.string().nullable().optional(), dataCatalogName: z.string().nullable().optional(), dataTypeName: z.string().nullable().optional(), dataTypeGroupName: z.string().nullable().optional(), dataTypeGroupShortName: z.string().nullable().optional(), dataProcessTypeShortName: z.string().nullable().optional(), dataTypeGroup2ShortName: z.string().nullable().optional(), populationSubgroupId: z.number().int().nullable().optional(), populationSubgroupName: z.string().nullable().optional(), inclusion: z.boolean().optional(), footnote: footnote.optional(), seriesNameLegend: z.string().nullable().optional(), seriesNameTooltip: z.string().nullable().optional(), seriesNameShort: z.string().nullable().optional()}); export const getApiApiV1EmpiricalDataResponse = z.array(empiricalDataDto); 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 }; /** * Returns a 'ResultData' for a given set of Indicators, Locations, StartYear and EndYear. * * GET /api/v1/empirical/data */ export const getApiApiV1EmpiricalData = async (args: GetApiApiV1EmpiricalDataArgs, options: GetApiApiV1EmpiricalDataOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.indicatorId != null) query.append("indicatorId", String(args.indicatorId)) if (args.locationId != null) query.append("locationId", String(args.locationId)) if (args.yearStart != null) query.append("yearStart", String(args.yearStart)) if (args.yearEnd != null) query.append("yearEnd", String(args.yearEnd)) if (args.sexId != null) query.append("sexId", String(args.sexId)) if (args.ageId != null) query.append("ageId", String(args.ageId)) if (args.categoryId != null) query.append("categoryId", String(args.categoryId)) if (args.included != null) query.append("included", String(args.included)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/empirical/data${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(getApiApiV1EmpiricalDataResponse, await res.json()) };