import {z} from 'zod' export type PointGeoJSON = {type: 'Point', coordinates: Array}; export type MultipointGeoJSON = {type: 'MultiPoint', coordinates: Array>}; export type LinestringGeoJSON = {type: 'LineString', coordinates: Array>}; export type MultilinestringGeoJSON = {type: 'MultiLineString', coordinates: Array>>}; export type PolygonGeoJSON = {type: 'Polygon', coordinates: Array>>}; export type MultipolygonGeoJSON = {type: 'MultiPolygon', coordinates: Array>>>}; export type GeometrycollectionGeoJSON = {type: 'GeometryCollection', geometries: Array}; export type GeometryGeoJSON = PointGeoJSON | MultipointGeoJSON | LinestringGeoJSON | MultilinestringGeoJSON | PolygonGeoJSON | MultipolygonGeoJSON | GeometrycollectionGeoJSON; export type Link = {href: string, rel?: string | undefined, type?: string | undefined, hreflang?: string | undefined, title?: string | undefined, length?: number | undefined}; export type FeatureGeoJSON = {type: 'Feature', geometry: GeometryGeoJSON, properties: Record | null, id?: (string | number) | undefined, links?: Array | undefined}; export type CreateApiCollectionsAlternatesArgs = {body: FeatureGeoJSON}; export type CreateApiCollectionsAlternatesOptions = {baseUrl?: string | undefined, JWT?: string | undefined, fetch?: typeof fetch | undefined}; export const pointGeoJSON = z.object({type: z.literal("Point"), coordinates: z.array(z.number())}); export const multipointGeoJSON = z.object({type: z.literal("MultiPoint"), coordinates: z.array(z.array(z.number()))}); export const linestringGeoJSON = z.object({type: z.literal("LineString"), coordinates: z.array(z.array(z.number()))}); export const multilinestringGeoJSON = z.object({type: z.literal("MultiLineString"), coordinates: z.array(z.array(z.array(z.number())))}); export const polygonGeoJSON = z.object({type: z.literal("Polygon"), coordinates: z.array(z.array(z.array(z.number())))}); export const multipolygonGeoJSON = z.object({type: z.literal("MultiPolygon"), coordinates: z.array(z.array(z.array(z.array(z.number()))))}); export const geometrycollectionGeoJSON = z.object({type: z.literal("GeometryCollection"), geometries: z.array(z.lazy(() => geometryGeoJSON))}); export const geometryGeoJSON: z.ZodType = z.union([pointGeoJSON, multipointGeoJSON, linestringGeoJSON, multilinestringGeoJSON, polygonGeoJSON, multipolygonGeoJSON, geometrycollectionGeoJSON]); export const link = z.object({href: z.string(), rel: z.string().optional(), type: z.string().optional(), hreflang: z.string().optional(), title: z.string().optional(), length: z.number().int().optional()}); export const featureGeoJSON = z.object({type: z.literal("Feature"), geometry: geometryGeoJSON, properties: z.object({}).nullable(), id: z.union([z.string(), z.number().int()]).optional(), links: z.array(link).optional()}); export const timeStamp = z.string(); export const numberMatched = z.number().int().gte(0); export const numberReturned = z.number().int().gte(0); export const featureCollectionGeoJSON = z.object({type: z.literal("FeatureCollection"), features: z.array(featureGeoJSON), links: z.array(link).optional(), timeStamp: timeStamp.optional(), numberMatched: numberMatched.optional(), numberReturned: numberReturned.optional()}); 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 Alternates * * POST /collections/alternates * * Also accepts auth: BasicAuth */ export const createApiCollectionsAlternates = async (args: CreateApiCollectionsAlternatesArgs, options?: CreateApiCollectionsAlternatesOptions) => { const {baseUrl = "https://api.planet.com/features/v1/ogc/my", fetch: fetchFn = fetch, JWT} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/geo+json") if (JWT !== undefined) headers.set('authorization', `Bearer ${JWT}`) const url = `${baseUrl.replace(/\/+$/, '')}/collections/alternates${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(featureCollectionGeoJSON, await res.json()) };