import {z} from 'zod' export type GetApiApiV1LocationsArgs = {pageNumber?: number | undefined, pageSize?: number | undefined, pagingInHeader?: boolean | undefined, sort?: string | undefined, format?: string | undefined}; export type GetApiApiV1LocationsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const locationFlatDto = z.object({id: z.number().int().optional(), name: z.string().nullable().optional(), iso3: z.string().nullable().optional(), iso2: z.string().nullable().optional(), longitude: z.number().nullable().optional(), latitude: z.number().nullable().optional()}); export const getApiApiV1LocationsResponse = z.array(locationFlatDto); 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 paged list of all Locations * * GET /api/v1/locations */ export const getApiApiV1Locations = async (args: GetApiApiV1LocationsArgs, options: GetApiApiV1LocationsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.pageNumber != null) query.append("pageNumber", String(args.pageNumber)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.pagingInHeader != null) query.append("pagingInHeader", String(args.pagingInHeader)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.format != null) query.append("format", String(args.format)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/locations${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(getApiApiV1LocationsResponse, await res.json()) };