@meridian-ts/valhalla 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,22 @@
1
+ # @meridian-ts/valhalla
2
+
3
+ ## 1.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - 21c5e38: Meridian 1.0: maps and locations for Ember.
8
+
9
+ - `@meridian-ts/ember`: a declarative MapLibre map, a port of `@vis.gl/react-maplibre`. Sources,
10
+ layers, markers, popups, all the controls, clusters, place autocomplete, a geocoder control
11
+ and directions.
12
+ - `@meridian-ts/core`: the provider interfaces for geocoding, places, routing, elevation, time
13
+ zone, static maps and IP location, and `defineMeridian`.
14
+ - Eleven provider adapters: Nominatim, Photon, Pelias, Valhalla, Stadia Maps, MapTiler, Mapbox,
15
+ Google, HERE, Esri and AWS.
16
+ - `@meridian-ts/draw`: draw and edit tools on terra-draw.
17
+ - `@meridian-ts/deck`: deck.gl layers and H3 hexagon clusters.
18
+
19
+ ### Patch Changes
20
+
21
+ - Updated dependencies [21c5e38]
22
+ - @meridian-ts/core@1.0.0
package/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Joe Gaudet
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/NOTICE ADDED
@@ -0,0 +1,9 @@
1
+ Meridian
2
+ Copyright (c) 2026 Joe Gaudet. MIT licensed; see LICENSE.md.
3
+
4
+ Meridian is a port of the component API of @vis.gl/react-maplibre (vis.gl contributors, MIT) to
5
+ Ember. It copies the names and the behaviour of that API, and no code.
6
+
7
+ Meridian renders maps with MapLibre GL JS (BSD-3-Clause), which the application installs.
8
+ Each provider adapter calls the public HTTP API of its provider. Meridian depends on no provider
9
+ SDK. The terms of each provider apply to the data that its API returns.
package/README.md CHANGED
@@ -1,3 +1,23 @@
1
- # Temporary Holding Version
1
+ # @meridian-ts/valhalla
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The Meridian adapter for Valhalla. See the [guide](https://joegaudet.com/meridian/).
4
+
5
+ ```ts
6
+ import { defineMeridian } from '@meridian-ts/core'
7
+ import { valhalla } from '@meridian-ts/valhalla'
8
+
9
+ const provider = valhalla({ url: 'https://valhalla.example.com' })
10
+ export const meridian = defineMeridian({ routing: provider, elevation: provider })
11
+
12
+ const routes = await meridian.routing.directions([
13
+ { lat: 49.2827, lng: -123.1207 },
14
+ { lat: 49.2845, lng: -123.1162 },
15
+ ])
16
+ ```
17
+
18
+ The provider needs no key. The default server is the public demo server
19
+ `https://valhalla1.openstreetmap.de`: use it only for tests. To use your own Valhalla server,
20
+ pass its root URL as `url`. To use a proxy, pass `baseUrl`.
21
+
22
+ `routing.isochrone` gives one polygon for each value in `minutes` and `meters`. The default
23
+ is one contour of 10 minutes.
@@ -0,0 +1,20 @@
1
+ import { type AdapterOptions, type Elevation, type Http, type Provider, type Routing } from '@meridian-ts/core';
2
+ export interface ValhallaOptions extends AdapterOptions {
3
+ /** The root URL of the Valhalla server. The default is the public demo server. */
4
+ url?: string;
5
+ }
6
+ /** The path of each routing action on the server. */
7
+ export interface ValhallaPaths {
8
+ route: string;
9
+ isochrone: string;
10
+ matrix: string;
11
+ }
12
+ /**
13
+ * The routing functions of a Valhalla server. `paths` is the path of each action: Stadia Maps
14
+ * hosts Valhalla with other paths.
15
+ */
16
+ export declare function valhallaRouting(http: Http, paths?: ValhallaPaths): Routing;
17
+ /** The elevation function of a Valhalla server. `path` is the path of the height action. */
18
+ export declare function valhallaElevation(http: Http, path?: string): Elevation;
19
+ /** The Valhalla adapter: directions, isochrones, the time-distance matrix and elevation. */
20
+ export declare function valhalla(options?: ValhallaOptions): Provider;
package/dist/index.js ADDED
@@ -0,0 +1,178 @@
1
+ import { createHttp, toLngLat, decodePolyline } from '@meridian-ts/core';
2
+
3
+ // src/index.ts
4
+ var COSTING = { drive: "auto", walk: "pedestrian", bike: "bicycle" };
5
+ var MANEUVERS = [
6
+ "kNone",
7
+ "kStart",
8
+ "kStartRight",
9
+ "kStartLeft",
10
+ "kDestination",
11
+ "kDestinationRight",
12
+ "kDestinationLeft",
13
+ "kBecomes",
14
+ "kContinue",
15
+ "kSlightRight",
16
+ "kRight",
17
+ "kSharpRight",
18
+ "kUturnRight",
19
+ "kUturnLeft",
20
+ "kSharpLeft",
21
+ "kLeft",
22
+ "kSlightLeft",
23
+ "kRampStraight",
24
+ "kRampRight",
25
+ "kRampLeft",
26
+ "kExitRight",
27
+ "kExitLeft",
28
+ "kStayStraight",
29
+ "kStayRight",
30
+ "kStayLeft",
31
+ "kMerge",
32
+ "kRoundaboutEnter",
33
+ "kRoundaboutExit",
34
+ "kFerryEnter",
35
+ "kFerryExit",
36
+ "kTransit",
37
+ "kTransitTransfer",
38
+ "kTransitRemainOn",
39
+ "kTransitConnectionStart",
40
+ "kTransitConnectionTransfer",
41
+ "kTransitConnectionDestination",
42
+ "kPostTransitConnectionDestination",
43
+ "kMergeRight",
44
+ "kMergeLeft",
45
+ "kElevatorEnter",
46
+ "kStepsEnter",
47
+ "kEscalatorEnter",
48
+ "kBuildingEnter",
49
+ "kBuildingExit"
50
+ ];
51
+ var location = (value) => {
52
+ const [lon, lat] = toLngLat(value);
53
+ return { lat, lon };
54
+ };
55
+ var metres = (length, units) => Math.round(length * (units === "miles" ? 1609.344 : 1e3));
56
+ function toRoute(trip) {
57
+ const coordinates = [];
58
+ const legs = trip.legs.map((leg) => {
59
+ const shape = decodePolyline(leg.shape, 6);
60
+ coordinates.push(...coordinates.length ? shape.slice(1) : shape);
61
+ return {
62
+ distance: metres(leg.summary.length, trip.units),
63
+ duration: leg.summary.time,
64
+ steps: leg.maneuvers.map((maneuver) => {
65
+ const [lng, lat] = shape[maneuver.begin_shape_index] ?? [];
66
+ return {
67
+ instruction: maneuver.instruction,
68
+ distance: metres(maneuver.length, trip.units),
69
+ duration: maneuver.time,
70
+ location: lng === void 0 || lat === void 0 ? void 0 : { lat, lng },
71
+ maneuver: MANEUVERS[maneuver.type]
72
+ };
73
+ })
74
+ };
75
+ });
76
+ return {
77
+ geometry: { type: "LineString", coordinates },
78
+ distance: metres(trip.summary.length, trip.units),
79
+ duration: trip.summary.time,
80
+ legs,
81
+ raw: trip
82
+ };
83
+ }
84
+ function valhallaRouting(http, paths = {
85
+ route: "/route",
86
+ isochrone: "/isochrone",
87
+ matrix: "/sources_to_targets"
88
+ }) {
89
+ return {
90
+ directions: async (waypoints, o = {}) => {
91
+ const body = await http.post(
92
+ paths.route,
93
+ {
94
+ locations: waypoints.map(location),
95
+ costing: COSTING[o.mode ?? "drive"],
96
+ units: "km",
97
+ language: o.language,
98
+ alternates: o.alternatives ? 1 : void 0
99
+ },
100
+ void 0,
101
+ o
102
+ );
103
+ return [body.trip, ...(body.alternates ?? []).map((a) => a.trip)].map(toRoute);
104
+ },
105
+ isochrone: async (lngLat, o = {}) => {
106
+ const contours = [
107
+ ...(o.minutes ?? []).map((time) => ({ time })),
108
+ ...(o.meters ?? []).map((distance) => ({ distance: distance / 1e3 }))
109
+ ];
110
+ const body = await http.post(
111
+ paths.isochrone,
112
+ {
113
+ locations: [location(lngLat)],
114
+ costing: COSTING[o.mode ?? "drive"],
115
+ // Valhalla needs one contour at least. The default is 10 minutes.
116
+ contours: contours.length ? contours : [{ time: 10 }],
117
+ polygons: true
118
+ },
119
+ void 0,
120
+ o
121
+ );
122
+ return body.features.filter((f) => f.geometry.type === "Polygon" || f.geometry.type === "MultiPolygon").map((f) => ({
123
+ type: "Feature",
124
+ geometry: f.geometry,
125
+ properties: {
126
+ value: f.properties.metric === "distance" ? Math.round(f.properties.contour * 1e3) : f.properties.contour,
127
+ metric: f.properties.metric
128
+ }
129
+ }));
130
+ },
131
+ matrix: async (origins, destinations, o = {}) => {
132
+ const body = await http.post(
133
+ paths.matrix,
134
+ {
135
+ sources: origins.map(location),
136
+ targets: destinations.map(location),
137
+ costing: COSTING[o.mode ?? "drive"],
138
+ units: "km"
139
+ },
140
+ void 0,
141
+ o
142
+ );
143
+ const rows = body.sources_to_targets;
144
+ return {
145
+ durations: rows.map((row) => row.map((cell) => cell.time)),
146
+ distances: rows.map(
147
+ (row) => row.map((cell) => cell.distance === null ? null : metres(cell.distance, body.units))
148
+ ),
149
+ raw: body
150
+ };
151
+ }
152
+ };
153
+ }
154
+ function valhallaElevation(http, path = "/height") {
155
+ return {
156
+ at: async (lngLats, o = {}) => {
157
+ const body = await http.post(
158
+ path,
159
+ { shape: lngLats.map(location) },
160
+ void 0,
161
+ o
162
+ );
163
+ return body.height.map((height) => height ?? NaN);
164
+ }
165
+ };
166
+ }
167
+ function valhalla(options = {}) {
168
+ const http = createHttp({
169
+ provider: "valhalla",
170
+ defaultBaseUrl: options.url ?? "https://valhalla1.openstreetmap.de",
171
+ options
172
+ });
173
+ return { name: "valhalla", routing: valhallaRouting(http), elevation: valhallaElevation(http) };
174
+ }
175
+
176
+ export { valhalla, valhallaElevation, valhallaRouting };
177
+ //# sourceMappingURL=index.js.map
178
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;AAgCA,IAAM,UAAsC,EAAE,KAAA,EAAO,QAAQ,IAAA,EAAM,YAAA,EAAc,MAAM,SAAA,EAAU;AAGjG,IAAM,SAAA,GAAY;AAAA,EAChB,OAAA;AAAA,EACA,QAAA;AAAA,EACA,aAAA;AAAA,EACA,YAAA;AAAA,EACA,cAAA;AAAA,EACA,mBAAA;AAAA,EACA,kBAAA;AAAA,EACA,UAAA;AAAA,EACA,WAAA;AAAA,EACA,cAAA;AAAA,EACA,QAAA;AAAA,EACA,aAAA;AAAA,EACA,aAAA;AAAA,EACA,YAAA;AAAA,EACA,YAAA;AAAA,EACA,OAAA;AAAA,EACA,aAAA;AAAA,EACA,eAAA;AAAA,EACA,YAAA;AAAA,EACA,WAAA;AAAA,EACA,YAAA;AAAA,EACA,WAAA;AAAA,EACA,eAAA;AAAA,EACA,YAAA;AAAA,EACA,WAAA;AAAA,EACA,QAAA;AAAA,EACA,kBAAA;AAAA,EACA,iBAAA;AAAA,EACA,aAAA;AAAA,EACA,YAAA;AAAA,EACA,UAAA;AAAA,EACA,kBAAA;AAAA,EACA,kBAAA;AAAA,EACA,yBAAA;AAAA,EACA,4BAAA;AAAA,EACA,+BAAA;AAAA,EACA,mCAAA;AAAA,EACA,aAAA;AAAA,EACA,YAAA;AAAA,EACA,gBAAA;AAAA,EACA,aAAA;AAAA,EACA,iBAAA;AAAA,EACA,gBAAA;AAAA,EACA;AACF,CAAA;AA8CA,IAAM,QAAA,GAAW,CAAC,KAAA,KAAsB;AACtC,EAAA,MAAM,CAAC,GAAA,EAAK,GAAG,CAAA,GAAI,SAAS,KAAK,CAAA;AACjC,EAAA,OAAO,EAAE,KAAK,GAAA,EAAI;AACpB,CAAA;AAIA,IAAM,MAAA,GAAS,CAAC,MAAA,EAAgB,KAAA,K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;AAChE,EAAA,MAAM,OAAO,UAAA,CAAW;AAAA,IACtB,QAAA,EAAU,UAAA;AAAA,IACV,cAAA,EAAgB,QAAQ,GAAA,IAAO,oCAAA;AAAA,IAC/B;AAAA,GACD,CAAA;AACD,EAAA,OAAO,EAAE,IAAA,EAAM,UAAA,EAAY,OAAA,EAAS,eAAA,CAAgB,IAAI,CAAA,EAAG,SAAA,EAAW,iBAAA,CAAkB,IAAI,CAAA,EAAE;AAChG","file":"index.js","sourcesContent":["// Valhalla API: https://valhalla.github.io/valhalla/api/\n// Route: https://valhalla.github.io/valhalla/api/turn-by-turn/api-reference/\n// Isochrone: https://valhalla.github.io/valhalla/api/isochrone/api-reference/\n// Matrix: https://valhalla.github.io/valhalla/api/matrix/api-reference/\n// Elevation: https://valhalla.github.io/valhalla/api/elevation/api-reference/\nimport {\n createHttp,\n decodePolyline,\n toLngLat,\n type AdapterOptions,\n type Elevation,\n type Http,\n type Isochrone,\n type LngLatLike,\n type Provider,\n type Route,\n type Routing,\n type TravelMode,\n} from '@meridian-ts/core'\n\nexport interface ValhallaOptions extends AdapterOptions {\n /** The root URL of the Valhalla server. The default is the public demo server. */\n url?: string\n}\n\n/** The path of each routing action on the server. */\nexport interface ValhallaPaths {\n route: string\n isochrone: string\n matrix: string\n}\n\nconst COSTING: Record<TravelMode, string> = { drive: 'auto', walk: 'pedestrian', bike: 'bicycle' }\n\n// The maneuver `type` numbers, from the route API reference. The index is the number.\nconst MANEUVERS = [\n 'kNone',\n 'kStart',\n 'kStartRight',\n 'kStartLeft',\n 'kDestination',\n 'kDestinationRight',\n 'kDestinationLeft',\n 'kBecomes',\n 'kContinue',\n 'kSlightRight',\n 'kRight',\n 'kSharpRight',\n 'kUturnRight',\n 'kUturnLeft',\n 'kSharpLeft',\n 'kLeft',\n 'kSlightLeft',\n 'kRampStraight',\n 'kRampRight',\n 'kRampLeft',\n 'kExitRight',\n 'kExitLeft',\n 'kStayStraight',\n 'kStayRight',\n 'kStayLeft',\n 'kMerge',\n 'kRoundaboutEnter',\n 'kRoundaboutExit',\n 'kFerryEnter',\n 'kFerryExit',\n 'kTransit',\n 'kTransitTransfer',\n 'kTransitRemainOn',\n 'kTransitConnectionStart',\n 'kTransitConnectionTransfer',\n 'kTransitConnectionDestination',\n 'kPostTransitConnectionDestination',\n 'kMergeRight',\n 'kMergeLeft',\n 'kElevatorEnter',\n 'kStepsEnter',\n 'kEscalatorEnter',\n 'kBuildingEnter',\n 'kBuildingExit',\n]\n\ninterface ValhallaSummary {\n /** In the units of the response. */\n length: number\n /** Seconds. */\n time: number\n}\n\ninterface ValhallaTrip {\n /** `kilometers` or `miles` */\n units: string\n summary: ValhallaSummary\n legs: {\n summary: ValhallaSummary\n /** An encoded polyline with precision 6. */\n shape: string\n maneuvers: {\n type: number\n instruction: string\n length: number\n time: number\n begin_shape_index: number\n }[]\n }[]\n}\n\ninterface ValhallaRouteResponse {\n trip: ValhallaTrip\n alternates?: { trip: ValhallaTrip }[]\n}\n\ninterface ValhallaIsochroneResponse {\n features: {\n geometry: { type: string; coordinates: unknown }\n /** `contour` is minutes for `time`, and kilometers for `distance`. */\n properties: { contour: number; metric: 'time' | 'distance' }\n }[]\n}\n\ninterface ValhallaMatrixResponse {\n units: string\n /** One row for each source. Each row has one cell for each target, in the target sequence. */\n sources_to_targets: { time: number | null; distance: number | null }[][]\n}\n\nconst location = (value: LngLatLike) => {\n const [lon, lat] = toLngLat(value)\n return { lat, lon }\n}\n\n// Valhalla gives a length with three decimal places, so a whole number of metres is exact for\n// kilometers.\nconst metres = (length: number, units: string) =>\n Math.round(length * (units === 'miles' ? 1609.344 : 1000))\n\nfunction toRoute(trip: ValhallaTrip): Route {\n const coordinates: [number, number][] = []\n const legs = trip.legs.map(leg => {\n const shape = decodePolyline(leg.shape, 6)\n // A leg starts at the point where the leg before it stops.\n coordinates.push(...(coordinates.length ? shape.slice(1) : shape))\n return {\n distance: metres(leg.summary.length, trip.units),\n duration: leg.summary.time,\n steps: leg.maneuvers.map(maneuver => {\n const [lng, lat] = shape[maneuver.begin_shape_index] ?? []\n return {\n instruction: maneuver.instruction,\n distance: metres(maneuver.length, trip.units),\n duration: maneuver.time,\n location: lng === undefined || lat === undefined ? undefined : { lat, lng },\n maneuver: MANEUVERS[maneuver.type],\n }\n }),\n }\n })\n return {\n geometry: { type: 'LineString', coordinates },\n distance: metres(trip.summary.length, trip.units),\n duration: trip.summary.time,\n legs,\n raw: trip,\n }\n}\n\n/**\n * The routing functions of a Valhalla server. `paths` is the path of each action: Stadia Maps\n * hosts Valhalla with other paths.\n */\nexport function valhallaRouting(\n http: Http,\n paths: ValhallaPaths = {\n route: '/route',\n isochrone: '/isochrone',\n matrix: '/sources_to_targets',\n },\n): Routing {\n return {\n directions: async (waypoints, o = {}) => {\n const body = await http.post<ValhallaRouteResponse>(\n paths.route,\n {\n locations: waypoints.map(location),\n costing: COSTING[o.mode ?? 'drive'],\n units: 'km',\n language: o.language,\n alternates: o.alternatives ? 1 : undefined,\n },\n undefined,\n o,\n )\n return [body.trip, ...(body.alternates ?? []).map(a => a.trip)].map(toRoute)\n },\n isochrone: async (lngLat, o = {}) => {\n const contours = [\n ...(o.minutes ?? []).map(time => ({ time })),\n ...(o.meters ?? []).map(distance => ({ distance: distance / 1000 })),\n ]\n const body = await http.post<ValhallaIsochroneResponse>(\n paths.isochrone,\n {\n locations: [location(lngLat)],\n costing: COSTING[o.mode ?? 'drive'],\n // Valhalla needs one contour at least. The default is 10 minutes.\n contours: contours.length ? contours : [{ time: 10 }],\n polygons: true,\n },\n undefined,\n o,\n )\n return body.features\n .filter(f => f.geometry.type === 'Polygon' || f.geometry.type === 'MultiPolygon')\n .map(f => ({\n type: 'Feature',\n geometry: f.geometry as Isochrone['geometry'],\n properties: {\n value:\n f.properties.metric === 'distance'\n ? Math.round(f.properties.contour * 1000)\n : f.properties.contour,\n metric: f.properties.metric,\n },\n }))\n },\n matrix: async (origins, destinations, o = {}) => {\n const body = await http.post<ValhallaMatrixResponse>(\n paths.matrix,\n {\n sources: origins.map(location),\n targets: destinations.map(location),\n costing: COSTING[o.mode ?? 'drive'],\n units: 'km',\n },\n undefined,\n o,\n )\n const rows = body.sources_to_targets\n return {\n durations: rows.map(row => row.map(cell => cell.time)),\n distances: rows.map(row =>\n row.map(cell => (cell.distance === null ? null : metres(cell.distance, body.units))),\n ),\n raw: body,\n }\n },\n }\n}\n\n/** The elevation function of a Valhalla server. `path` is the path of the height action. */\nexport function valhallaElevation(http: Http, path = '/height'): Elevation {\n return {\n at: async (lngLats, o = {}) => {\n const body = await http.post<{ height: (number | null)[] }>(\n path,\n { shape: lngLats.map(location) },\n undefined,\n o,\n )\n // `null` means that the server has no elevation data for the location.\n return body.height.map(height => height ?? NaN)\n },\n }\n}\n\n/** The Valhalla adapter: directions, isochrones, the time-distance matrix and elevation. */\nexport function valhalla(options: ValhallaOptions = {}): Provider {\n const http = createHttp({\n provider: 'valhalla',\n defaultBaseUrl: options.url ?? 'https://valhalla1.openstreetmap.de',\n options,\n })\n return { name: 'valhalla', routing: valhallaRouting(http), elevation: valhallaElevation(http) }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,52 @@
1
1
  {
2
2
  "name": "@meridian-ts/valhalla",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "1.0.0",
4
+ "description": "Meridian adapter for Valhalla: directions, isochrones, matrices and elevation.",
5
+ "keywords": [
6
+ "meridian",
7
+ "valhalla",
8
+ "routing isochrone directions",
9
+ "gis"
10
+ ],
11
+ "license": "MIT",
12
+ "author": "Joe Gaudet joe@joegaudet.com",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/joegaudet/meridian.git",
16
+ "directory": "packages/valhalla"
17
+ },
18
+ "homepage": "https://joegaudet.com/meridian/",
19
+ "bugs": "https://github.com/joegaudet/meridian/issues",
20
+ "type": "module",
21
+ "types": "./dist/index.d.ts",
22
+ "exports": {
23
+ ".": {
24
+ "types": "./dist/index.d.ts",
25
+ "default": "./dist/index.js"
26
+ },
27
+ "./package.json": "./package.json"
28
+ },
29
+ "files": [
30
+ "dist",
31
+ "CHANGELOG.md",
32
+ "README.md",
33
+ "LICENSE.md",
34
+ "NOTICE"
35
+ ],
36
+ "sideEffects": false,
37
+ "dependencies": {
38
+ "@meridian-ts/core": "^1.0.0"
39
+ },
40
+ "devDependencies": {
41
+ "@meridian-ts/conformance": "0.0.0"
42
+ },
43
+ "publishConfig": {
44
+ "access": "public"
45
+ },
46
+ "engines": {
47
+ "node": ">=22.12.0"
48
+ },
49
+ "scripts": {
50
+ "build": "tsup"
51
+ }
6
52
  }