From aee3f44584e60f9c78e1220b719bac21dbb059bb Mon Sep 17 00:00:00 2001 From: Reinert Lemmens Date: Tue, 22 Oct 2024 20:49:45 +0200 Subject: [PATCH] Remove barrel files + export only relevant schemas & types --- src/feature.ts | 2 +- src/geojson.ts | 21 ++++ src/geometry/{index.ts => geometry.ts} | 9 -- src/index.ts | 162 ++++++++++++++++++++----- src/type.ts | 2 +- 5 files changed, 154 insertions(+), 42 deletions(-) create mode 100644 src/geojson.ts rename src/geometry/{index.ts => geometry.ts} (85%) diff --git a/src/feature.ts b/src/feature.ts index 66091ab..2b0ff71 100644 --- a/src/feature.ts +++ b/src/feature.ts @@ -1,6 +1,6 @@ import { z } from "zod"; import { GeoJSONBaseSchema } from "./base"; -import { GeoJSONGeometryGenericSchema } from "./geometry"; +import { GeoJSONGeometryGenericSchema } from "./geometry/geometry"; import { INVALID_BBOX_ISSUE } from "./geometry/validation/bbox"; import { GeoJSON2DPositionSchema, GeoJSON3DPositionSchema, GeoJSONPosition, GeoJSONPositionSchema } from "./position"; import { GeoJSONTypeSchema } from "./type"; diff --git a/src/geojson.ts b/src/geojson.ts new file mode 100644 index 0000000..dcb6441 --- /dev/null +++ b/src/geojson.ts @@ -0,0 +1,21 @@ +import { z } from "zod"; +import { GeoJSONFeatureGenericSchema } from "./feature"; +import { GeoJSONFeatureCollectionGenericSchema } from "./feature_collection"; +import { GeoJSONGeometryGenericSchema } from "./geometry/geometry"; +import { GeoJSON2DPositionSchema, GeoJSON3DPositionSchema, GeoJSONPosition, GeoJSONPositionSchema } from "./position"; + +export const GeoJSONGenericSchema =

(positionSchema: z.ZodSchema

) => + z.union([ + GeoJSONGeometryGenericSchema(positionSchema), + GeoJSONFeatureGenericSchema(positionSchema), + GeoJSONFeatureCollectionGenericSchema(positionSchema), + ]); + +export const GeoJSONSchema = GeoJSONGenericSchema(GeoJSONPositionSchema); +export type GeoJSON = z.infer; + +export const GeoJSON2DSchema = GeoJSONGenericSchema(GeoJSON2DPositionSchema); +export type GeoJSON2D = z.infer; + +export const GeoJSON3DSchema = GeoJSONGenericSchema(GeoJSON3DPositionSchema); +export type GeoJSON3D = z.infer; diff --git a/src/geometry/index.ts b/src/geometry/geometry.ts similarity index 85% rename from src/geometry/index.ts rename to src/geometry/geometry.ts index 7c2a80a..aa22b2a 100644 --- a/src/geometry/index.ts +++ b/src/geometry/geometry.ts @@ -6,15 +6,6 @@ import { GeoJSONGeometryCollectionGenericSchemaType, } from "./geometry_collection"; -export * from "./geometry_collection"; -export * from "./line_string"; -export * from "./multi_line_string"; -export * from "./multi_point"; -export * from "./multi_polygon"; -export * from "./point"; -export * from "./polygon"; -export * from "./helper/type"; - export type GeoJSONGeometryGenericSchemaType

= z.ZodUnion< [GeoJSONSimpleGeometryGenericSchemaType

, GeoJSONGeometryCollectionGenericSchemaType

] >; diff --git a/src/index.ts b/src/index.ts index a7a63aa..b7f55db 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,32 +1,132 @@ // Derived from the GeoJSON spec: https://datatracker.ietf.org/doc/html/rfc7946 -import { z } from "zod"; -import { GeoJSONFeatureGenericSchema } from "./feature"; -import { GeoJSONFeatureCollectionGenericSchema } from "./feature_collection"; -import { GeoJSONGeometryGenericSchema } from "./geometry"; -import { GeoJSON2DPositionSchema, GeoJSON3DPositionSchema, GeoJSONPosition, GeoJSONPositionSchema } from "./position"; - -export * from "./feature"; -export * from "./feature_collection"; -export * from "./geometry"; -export * from "./bbox"; -export * from "./position"; -export * from "./type"; - -export const GeoJSONGenericSchema =

(positionSchema: z.ZodSchema

) => - z.union([ - GeoJSONGeometryGenericSchema(positionSchema), - GeoJSONFeatureGenericSchema(positionSchema), - GeoJSONFeatureCollectionGenericSchema(positionSchema), - ]); - -export const GeoJSONSchema = GeoJSONGenericSchema(GeoJSONPositionSchema); -export type GeoJSON = z.infer; - -export const GeoJSON2DSchema = GeoJSONGenericSchema(GeoJSON2DPositionSchema); -export type GeoJSON2D = z.infer; - -export const GeoJSON3DSchema = GeoJSONGenericSchema(GeoJSON3DPositionSchema); -export type GeoJSON3D = z.infer; - -// TODO: Do not expose every single schema & inner type, only the useful ones -// TODO: Add negative typing examples for all types (like with point) +// TODO: Add negative typing examples for all types (like in point tests) +// TODO: Make sure each exposed type & schema is tested + +export { + GeoJSONGeometryTypeSchema, + type GeoJSONGeometryEnumType, + type GeoJSONGeometryType, +} from "./geometry/helper/type"; + +export { + GeoJSONGeometryGenericSchema, + GeoJSONGeometrySchema, + type GeoJSONGeometry, + GeoJSON2DGeometrySchema, + type GeoJSON2DGeometry, + GeoJSON3DGeometrySchema, + type GeoJSON3DGeometry, +} from "./geometry/geometry"; + +export { + GeoJSONGeometryCollectionGenericSchema, + GeoJSONGeometryCollectionSchema, + type GeoJSONGeometryCollection, + GeoJSON2DGeometryCollectionSchema, + type GeoJSON2DGeometryCollection, + GeoJSON3DGeometryCollectionSchema, + type GeoJSON3DGeometryCollection, +} from "./geometry/geometry_collection"; + +export { + GeoJSONLineStringGenericSchema, + GeoJSONLineStringSchema, + type GeoJSONLineString, + GeoJSON2DLineStringSchema, + type GeoJSON2DLineString, + GeoJSON3DLineStringSchema, + type GeoJSON3DLineString, +} from "./geometry/line_string"; + +export { + GeoJSONMultiLineStringGenericSchema, + GeoJSONMultiLineStringSchema, + type GeoJSONMultiLineString, + GeoJSON2DMultiLineStringSchema, + type GeoJSON2DMultiLineString, + GeoJSON3DMultiLineStringSchema, + type GeoJSON3DMultiLineString, +} from "./geometry/multi_line_string"; + +export { + GeoJSONMultiPointGenericSchema, + GeoJSONMultiPointSchema, + type GeoJSONMultiPoint, + GeoJSON2DMultiPointSchema, + type GeoJSON2DMultiPoint, + GeoJSON3DMultiPointSchema, + type GeoJSON3DMultiPoint, +} from "./geometry/multi_point"; + +export { + GeoJSONMultiPolygonGenericSchema, + GeoJSONMultiPolygonSchema, + type GeoJSONMultiPolygon, + GeoJSON2DMultiPolygonSchema, + type GeoJSON2DMultiPolygon, + GeoJSON3DMultiPolygonSchema, + type GeoJSON3DMultiPolygon, +} from "./geometry/multi_polygon"; + +export { + GeoJSONPointGenericSchema, + GeoJSONPointSchema, + type GeoJSONPoint, + GeoJSON2DPointSchema, + type GeoJSON2DPoint, + GeoJSON3DPointSchema, + type GeoJSON3DPoint, +} from "./geometry/point"; + +export { + GeoJSONPolygonGenericSchema, + GeoJSONPolygonSchema, + type GeoJSONPolygon, + GeoJSON2DPolygonSchema, + type GeoJSON2DPolygon, + GeoJSON3DPolygonSchema, + type GeoJSON3DPolygon, +} from "./geometry/polygon"; + +export { GeoJSONBboxSchema, type GeoJSONBbox } from "./bbox"; + +export { + GeoJSONFeatureGenericSchema, + GeoJSONFeatureSchema, + type GeoJSONFeature, + GeoJSON2DFeatureSchema, + type GeoJSON2DFeature, + GeoJSON3DFeatureSchema, + type GeoJSON3DFeature, +} from "./feature"; + +export { + GeoJSONFeatureCollectionGenericSchema, + GeoJSONFeatureCollectionSchema, + type GeoJSONFeatureCollection, + GeoJSON2DFeatureCollectionSchema, + type GeoJSON2DFeatureCollection, + GeoJSON3DFeatureCollectionSchema, + type GeoJSON3DFeatureCollection, +} from "./feature_collection"; + +export { + GeoJSONGenericSchema, + GeoJSONSchema, + type GeoJSON, + GeoJSON2DSchema, + type GeoJSON2D, + GeoJSON3DSchema, + type GeoJSON3D, +} from "./geojson"; + +export { + GeoJSONPositionSchema, + type GeoJSONPosition, + GeoJSON2DPositionSchema, + type GeoJSON2DPosition, + GeoJSON3DPositionSchema, + type GeoJSON3DPosition, +} from "./position"; + +export { GeoJSONTypeSchema, type GeoJSONType } from "./type"; diff --git a/src/type.ts b/src/type.ts index 48d2c9e..1c2d87c 100644 --- a/src/type.ts +++ b/src/type.ts @@ -1,6 +1,6 @@ // GeoJSON types and Geometry type (see 1.4) import { z } from "zod"; -import { GeoJSONGeometryTypeSchema } from "./geometry"; +import { GeoJSONGeometryTypeSchema } from "./geometry/helper/type"; export const GeoJSONTypeSchema = z.enum(["Feature", "FeatureCollection", ...GeoJSONGeometryTypeSchema.options]);