import {z} from 'zod' export type GetApiApiV1LocationsCodesArgs = {codes: string, sort?: string | undefined, format?: string | undefined}; export type GetApiApiV1LocationsCodesOptions = {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 getApiApiV1LocationsCodesResponse = 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 list of Locations filtered by one or more {codes} ('m49', 'iso2' or 'iso3') separated by semi-column (;) or comma (,). This is MORE STRICT (all or none) version of the 2 versions of same endpoint * * GET /api/v1/locations/{codes} */ export const getApiApiV1LocationsCodes = async (args: GetApiApiV1LocationsCodesArgs, options: GetApiApiV1LocationsCodesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") 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/${encodeURIComponent(String(args.codes))}${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(getApiApiV1LocationsCodesResponse, await res.json()) };