import {z} from 'zod' export type GetApiTilesCogBoundsArgs = {data_path?: (string | null) | undefined, carbon_pool?: (string | null) | undefined, product_type?: (string | null) | undefined, product_variant?: (string | null) | undefined, resolution?: (number | null) | undefined, year?: (number | null) | undefined, analysis_completed_at?: (string | null) | undefined, url?: (string | null) | undefined, crs?: (string | null) | undefined}; export type GetApiTilesCogBoundsOptions = {baseUrl: string, APIKeyQuery?: string | undefined, fetch?: typeof fetch | undefined}; export const bounds = z.object({bounds: z.array(z.unknown()), crs: z.string()}); 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 }; /** * Bounds * * GET /tiles/cog/bounds */ export const getApiTilesCogBounds = async (args: GetApiTilesCogBoundsArgs, options: GetApiTilesCogBoundsOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyQuery} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.data_path != null) query.append("data_path", String(args.data_path)) if (args.carbon_pool != null) query.append("carbon_pool", String(args.carbon_pool)) if (args.product_type != null) query.append("product_type", String(args.product_type)) if (args.product_variant != null) query.append("product_variant", String(args.product_variant)) if (args.resolution != null) query.append("resolution", String(args.resolution)) if (args.year != null) query.append("year", String(args.year)) if (args.analysis_completed_at != null) query.append("analysis_completed_at", String(args.analysis_completed_at)) if (args.url != null) query.append("url", String(args.url)) if (args.crs != null) query.append("crs", String(args.crs)) if (APIKeyQuery !== undefined) query.set("access_token", APIKeyQuery) const url = `${baseUrl.replace(/\/+$/, '')}/tiles/cog/bounds${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(bounds, await res.json()) };