mapricot 0.6.0 → 0.8.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/README.md CHANGED
@@ -17,7 +17,7 @@ import { Mapricot } from "mapricot";
17
17
 
18
18
  const client = new Mapricot({ apiKey: process.env.MAPRICOT_KEY });
19
19
 
20
- const { results } = await client.search("128 SW 3rd Ave, Portland", { limit: 1 });
20
+ const { results } = await client.geocoding.search("128 SW 3rd Ave, Portland", { limit: 1 });
21
21
  console.log(results[0].label, results[0].lat, results[0].lon);
22
22
  ```
23
23
 
@@ -25,24 +25,25 @@ In a web page, use a browser key, which works only from the origins you allow, a
25
25
 
26
26
  ## Methods
27
27
 
28
- Each method returns the API's JSON, typed, with the same field names as the [API reference](https://mapricot.com/api-reference/).
28
+ Methods are grouped by product, the same way as the API's paths. Each returns the API's JSON, typed, with the same field names as the [API reference](https://mapricot.com/api-reference/).
29
29
 
30
30
  | Method | Endpoint |
31
31
  | --- | --- |
32
- | `client.search(q, { focus, country, limit, lang })` | [`GET /geocoding/v1/search`](https://mapricot.com/docs/geocoding/#find-an-address) |
33
- | `client.autocomplete(q, { focus, country, limit, lang })` | [`GET /geocoding/v1/autocomplete`](https://mapricot.com/docs/geocoding/#suggest-as-people-type) |
34
- | `client.reverse(lat, lon, { limit, lang })` | [`GET /geocoding/v1/reverse`](https://mapricot.com/docs/geocoding/#find-the-address-at-a-point) |
35
- | `client.nearby(lat, lon, { category, radius, limit, lang })` | [`GET /places/v1/nearby`](https://mapricot.com/docs/places/#list-places-around-a-point) |
36
- | `client.place(id, { lang })` | [`GET /places/v1/{id}`](https://mapricot.com/docs/places/#look-up-a-saved-place) |
37
- | `client.route(waypoints, { mode, geometries })` | [`POST /routing/v1/route`](https://mapricot.com/docs/routing/#get-a-route) |
38
- | `client.ip(address)` | [`GET /ip-geolocation/v1/{ip}`](https://mapricot.com/docs/ip-geolocation/#look-up-an-address) |
32
+ | `client.geocoding.search(q, { focus, country, limit, lang })` | [`GET /geocoding/v1/search`](https://mapricot.com/docs/geocoding/#find-an-address) |
33
+ | `client.geocoding.autocomplete(q, { focus, country, limit, lang })` | [`GET /geocoding/v1/autocomplete`](https://mapricot.com/docs/geocoding/#suggest-as-people-type) |
34
+ | `client.geocoding.reverse(lat, lon, { limit, lang })` | [`GET /geocoding/v1/reverse`](https://mapricot.com/docs/geocoding/#find-the-address-at-a-point) |
35
+ | `client.places.nearby(lat, lon, { category, radius, limit, lang })` | [`GET /places/v1/nearby`](https://mapricot.com/docs/places/#list-places-around-a-point) |
36
+ | `client.places.get(id, { lang })` | [`GET /places/v1/{id}`](https://mapricot.com/docs/places/#look-up-a-saved-place) |
37
+ | `client.routing.route(waypoints, { mode, geometries })` | [`POST /routing/v1/route`](https://mapricot.com/docs/routing/#get-a-route) |
38
+ | `client.routing.isochrone(location, minutes, { mode })` | [`POST /routing/v1/isochrone`](https://mapricot.com/docs/routing/#get-an-isochrone) |
39
+ | `client.ipGeolocation.lookup(ip)` | [`GET /ip-geolocation/v1/{ip}`](https://mapricot.com/docs/ip-geolocation/#look-up-an-address) |
39
40
  | `client.account()` | `GET /account`, which checks a key and is never billed |
40
41
 
41
42
  `focus` and each waypoint are `{ lat, lon }`. `country` and `category` take an array or a comma-separated string.
42
43
 
43
44
  ```js
44
- const nearby = await client.nearby(45.52, -122.68, { category: ["food:coffee_shop"], radius: 1000 });
45
- const { routes } = await client.route([{ lat: 45.5189, lon: -122.6793 }, { lat: 45.5214, lon: -122.6828 }], { mode: "walk" });
45
+ const nearby = await client.places.nearby(45.52, -122.68, { category: ["food:coffee_shop"], radius: 1000 });
46
+ const { routes } = await client.routing.route([{ lat: 45.5189, lon: -122.6793 }, { lat: 45.5214, lon: -122.6828 }], { mode: "walk" });
46
47
  console.log(routes[0].durationSeconds);
47
48
  ```
48
49
 
@@ -50,12 +51,19 @@ To cancel a request, such as an autocomplete request for text that has since cha
50
51
 
51
52
  ```js
52
53
  const controller = new AbortController();
53
- const request = client.autocomplete("1005 W Burn", { signal: controller.signal });
54
+ const request = client.geocoding.autocomplete("1005 W Burn", { signal: controller.signal });
54
55
  controller.abort();
55
56
  ```
56
57
 
57
58
  A route's path comes as an encoded `polyline` with five decimal places, which polyline decoders assume. Pass `geometries: "geojson"` to get it as a GeoJSON `LineString` in `geometry` instead, which maps draw without decoding, or `geometries: "polyline6"` for six decimal places.
58
59
 
60
+ An isochrone is a GeoJSON `FeatureCollection` with one polygon per time, largest first, so a map that draws them in order shows the smaller ones on top:
61
+
62
+ ```js
63
+ const area = await client.routing.isochrone({ lat: 45.5189, lon: -122.6793 }, [5, 10], { mode: "walk" });
64
+ map.addSource("reach", { type: "geojson", data: area });
65
+ ```
66
+
59
67
  If your app shows results, show the credits in each response's `attribution`.
60
68
 
61
69
  ## Errors and retries
@@ -66,7 +74,7 @@ An error response throws `MapricotError`, with the HTTP `status`, the API's erro
66
74
  import { MapricotError } from "mapricot";
67
75
 
68
76
  try {
69
- await client.search("");
77
+ await client.geocoding.search("");
70
78
  } catch (error) {
71
79
  if (!(error instanceof MapricotError)) throw error;
72
80
  console.log(error.status, error.code, error.message); // 400 invalid_request The service rejected the request.
package/dist/index.d.ts CHANGED
@@ -55,6 +55,26 @@ export interface Routes {
55
55
  routes: Route[];
56
56
  attribution?: Attribution[];
57
57
  }
58
+ export interface Isochrone {
59
+ type: "Feature";
60
+ properties: {
61
+ minutes: number;
62
+ };
63
+ /** [longitude, latitude] rings, as GeoJSON. */
64
+ geometry: {
65
+ type: "Polygon";
66
+ coordinates: [number, number][][];
67
+ } | {
68
+ type: "MultiPolygon";
69
+ coordinates: [number, number][][][];
70
+ };
71
+ }
72
+ export interface Isochrones {
73
+ type: "FeatureCollection";
74
+ /** One area per time, largest first. Empty when there is no road near the location. */
75
+ features: Isochrone[];
76
+ attribution?: Attribution[];
77
+ }
58
78
  export interface IpLocation {
59
79
  ip: string;
60
80
  found: boolean;
@@ -117,25 +137,37 @@ export declare class MapricotError extends Error {
117
137
  export declare class Mapricot {
118
138
  #private;
119
139
  constructor(options: ClientOptions);
120
- /** Find addresses and places matching free text. */
121
- search(q: string, { signal, ...options }?: SearchOptions): Promise<Places>;
122
- /** Suggest addresses and places as someone types. */
123
- autocomplete(q: string, { signal, ...options }?: SearchOptions): Promise<Places>;
124
- /** Find the address nearest a coordinate, or the area around it. */
125
- reverse(lat: number, lon: number, { signal, ...options }?: ReverseOptions): Promise<Places>;
126
- /** List places around a coordinate, optionally by category. */
127
- nearby(lat: number, lon: number, { signal, ...options }?: NearbyOptions): Promise<Places>;
128
- /** Look up one place by its Mapricot ID. */
129
- place(id: string, { signal, ...options }?: RequestOptions & {
130
- lang?: string;
131
- }): Promise<PlaceResult>;
132
- /** Find a route through 2 to 20 waypoints. */
133
- route(waypoints: Point[], { signal, ...options }?: RequestOptions & {
134
- mode?: "drive" | "walk" | "bicycle";
135
- geometries?: "polyline" | "polyline6" | "geojson";
136
- }): Promise<Routes>;
137
- /** Approximate location of a public IP address. */
138
- ip(address: string, { signal }?: RequestOptions): Promise<IpLocation>;
140
+ readonly geocoding: {
141
+ /** Find addresses and places matching free text. */
142
+ search: (q: string, { signal, ...options }?: SearchOptions) => Promise<Places>;
143
+ /** Suggest addresses and places as someone types. */
144
+ autocomplete: (q: string, { signal, ...options }?: SearchOptions) => Promise<Places>;
145
+ /** Find the address nearest a coordinate, or the area around it. */
146
+ reverse: (lat: number, lon: number, { signal, ...options }?: ReverseOptions) => Promise<Places>;
147
+ };
148
+ readonly places: {
149
+ /** List places around a coordinate, optionally by category. */
150
+ nearby: (lat: number, lon: number, { signal, ...options }?: NearbyOptions) => Promise<Places>;
151
+ /** Look up one place by its Mapricot ID. */
152
+ get: (id: string, { signal, ...options }?: RequestOptions & {
153
+ lang?: string;
154
+ }) => Promise<PlaceResult>;
155
+ };
156
+ readonly routing: {
157
+ /** Find a route through 2 to 20 waypoints. */
158
+ route: (waypoints: Point[], { signal, ...options }?: RequestOptions & {
159
+ mode?: "drive" | "walk" | "bicycle";
160
+ geometries?: "polyline" | "polyline6" | "geojson";
161
+ }) => Promise<Routes>;
162
+ /** The areas reachable from a location within 1 to 4 times, each 1 to 60 minutes. */
163
+ isochrone: (location: Point, minutes: number[], { signal, ...options }?: RequestOptions & {
164
+ mode?: "drive" | "walk" | "bicycle";
165
+ }) => Promise<Isochrones>;
166
+ };
167
+ readonly ipGeolocation: {
168
+ /** Approximate location of a public IP address. */
169
+ lookup: (ip: string, { signal }?: RequestOptions) => Promise<IpLocation>;
170
+ };
139
171
  /** The key's account. Not billed, so it checks a key for free. */
140
172
  account({ signal }?: RequestOptions): Promise<Account>;
141
173
  }
package/dist/index.js CHANGED
@@ -1,14 +1,14 @@
1
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
2
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
3
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
4
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
5
+ };
1
6
  var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
7
  if (kind === "m") throw new TypeError("Private method is not writable");
3
8
  if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
9
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
10
  return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
11
  };
7
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
- };
12
12
  var _Mapricot_instances, _Mapricot_apiKey, _Mapricot_baseUrl, _Mapricot_timeout, _Mapricot_maxRetries, _Mapricot_request;
13
13
  export class MapricotError extends Error {
14
14
  constructor(status, code, message, requestId) {
@@ -27,6 +27,30 @@ export class Mapricot {
27
27
  _Mapricot_baseUrl.set(this, void 0);
28
28
  _Mapricot_timeout.set(this, void 0);
29
29
  _Mapricot_maxRetries.set(this, void 0);
30
+ this.geocoding = {
31
+ /** Find addresses and places matching free text. */
32
+ search: (q, { signal, ...options } = {}) => __classPrivateFieldGet(this, _Mapricot_instances, "m", _Mapricot_request).call(this, "GET", "/geocoding/v1/search", { q, ...options }, signal),
33
+ /** Suggest addresses and places as someone types. */
34
+ autocomplete: (q, { signal, ...options } = {}) => __classPrivateFieldGet(this, _Mapricot_instances, "m", _Mapricot_request).call(this, "GET", "/geocoding/v1/autocomplete", { q, ...options }, signal),
35
+ /** Find the address nearest a coordinate, or the area around it. */
36
+ reverse: (lat, lon, { signal, ...options } = {}) => __classPrivateFieldGet(this, _Mapricot_instances, "m", _Mapricot_request).call(this, "GET", "/geocoding/v1/reverse", { lat, lon, ...options }, signal),
37
+ };
38
+ this.places = {
39
+ /** List places around a coordinate, optionally by category. */
40
+ nearby: (lat, lon, { signal, ...options } = {}) => __classPrivateFieldGet(this, _Mapricot_instances, "m", _Mapricot_request).call(this, "GET", "/places/v1/nearby", { lat, lon, ...options }, signal),
41
+ /** Look up one place by its Mapricot ID. */
42
+ get: (id, { signal, ...options } = {}) => __classPrivateFieldGet(this, _Mapricot_instances, "m", _Mapricot_request).call(this, "GET", `/places/v1/${encodeURIComponent(id)}`, options, signal),
43
+ };
44
+ this.routing = {
45
+ /** Find a route through 2 to 20 waypoints. */
46
+ route: (waypoints, { signal, ...options } = {}) => __classPrivateFieldGet(this, _Mapricot_instances, "m", _Mapricot_request).call(this, "POST", "/routing/v1/route", {}, signal, { waypoints, ...options }),
47
+ /** The areas reachable from a location within 1 to 4 times, each 1 to 60 minutes. */
48
+ isochrone: (location, minutes, { signal, ...options } = {}) => __classPrivateFieldGet(this, _Mapricot_instances, "m", _Mapricot_request).call(this, "POST", "/routing/v1/isochrone", {}, signal, { location, minutes, ...options }),
49
+ };
50
+ this.ipGeolocation = {
51
+ /** Approximate location of a public IP address. */
52
+ lookup: (ip, { signal } = {}) => __classPrivateFieldGet(this, _Mapricot_instances, "m", _Mapricot_request).call(this, "GET", `/ip-geolocation/v1/${encodeURIComponent(ip)}`, {}, signal),
53
+ };
30
54
  if (!(options === null || options === void 0 ? void 0 : options.apiKey))
31
55
  throw new Error("Pass apiKey, a Mapricot key.");
32
56
  __classPrivateFieldSet(this, _Mapricot_apiKey, options.apiKey, "f");
@@ -34,34 +58,6 @@ export class Mapricot {
34
58
  __classPrivateFieldSet(this, _Mapricot_timeout, (_b = options.timeout) !== null && _b !== void 0 ? _b : 30000, "f");
35
59
  __classPrivateFieldSet(this, _Mapricot_maxRetries, (_c = options.maxRetries) !== null && _c !== void 0 ? _c : 2, "f");
36
60
  }
37
- /** Find addresses and places matching free text. */
38
- search(q, { signal, ...options } = {}) {
39
- return __classPrivateFieldGet(this, _Mapricot_instances, "m", _Mapricot_request).call(this, "GET", "/geocoding/v1/search", { q, ...options }, signal);
40
- }
41
- /** Suggest addresses and places as someone types. */
42
- autocomplete(q, { signal, ...options } = {}) {
43
- return __classPrivateFieldGet(this, _Mapricot_instances, "m", _Mapricot_request).call(this, "GET", "/geocoding/v1/autocomplete", { q, ...options }, signal);
44
- }
45
- /** Find the address nearest a coordinate, or the area around it. */
46
- reverse(lat, lon, { signal, ...options } = {}) {
47
- return __classPrivateFieldGet(this, _Mapricot_instances, "m", _Mapricot_request).call(this, "GET", "/geocoding/v1/reverse", { lat, lon, ...options }, signal);
48
- }
49
- /** List places around a coordinate, optionally by category. */
50
- nearby(lat, lon, { signal, ...options } = {}) {
51
- return __classPrivateFieldGet(this, _Mapricot_instances, "m", _Mapricot_request).call(this, "GET", "/places/v1/nearby", { lat, lon, ...options }, signal);
52
- }
53
- /** Look up one place by its Mapricot ID. */
54
- place(id, { signal, ...options } = {}) {
55
- return __classPrivateFieldGet(this, _Mapricot_instances, "m", _Mapricot_request).call(this, "GET", `/places/v1/${encodeURIComponent(id)}`, options, signal);
56
- }
57
- /** Find a route through 2 to 20 waypoints. */
58
- route(waypoints, { signal, ...options } = {}) {
59
- return __classPrivateFieldGet(this, _Mapricot_instances, "m", _Mapricot_request).call(this, "POST", "/routing/v1/route", {}, signal, { waypoints, ...options });
60
- }
61
- /** Approximate location of a public IP address. */
62
- ip(address, { signal } = {}) {
63
- return __classPrivateFieldGet(this, _Mapricot_instances, "m", _Mapricot_request).call(this, "GET", `/ip-geolocation/v1/${encodeURIComponent(address)}`, {}, signal);
64
- }
65
61
  /** The key's account. Not billed, so it checks a key for free. */
66
62
  account({ signal } = {}) {
67
63
  return __classPrivateFieldGet(this, _Mapricot_instances, "m", _Mapricot_request).call(this, "GET", "/account", {}, signal);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mapricot",
3
- "version": "0.6.0",
3
+ "version": "0.8.0",
4
4
  "description": "Official client for the Mapricot API: geocoding, places, routing, and IP geolocation.",
5
5
  "keywords": [
6
6
  "geocoding",