import {z} from 'zod' export type GetApiCollectionsCollectionIdItemsFeatureIdArgs = {collectionId: string, featureId: string}; export type GetApiCollectionsCollectionIdItemsFeatureIdOptions = {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 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 Feature * * GET /collections/{collectionId}/items/{featureId} * * Also accepts auth: BasicAuth */ export const getApiCollectionsCollectionIdItemsFeatureId = async (args: GetApiCollectionsCollectionIdItemsFeatureIdArgs, options?: GetApiCollectionsCollectionIdItemsFeatureIdOptions) => { 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('accept', "application/geo+json") if (JWT !== undefined) headers.set('authorization', `Bearer ${JWT}`) const url = `${baseUrl.replace(/\/+$/, '')}/collections/${encodeURIComponent(String(args.collectionId))}/items/${encodeURIComponent(String(args.featureId))}${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(featureGeoJSON, await res.json()) };