@dromney/mapthis 0.1.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/LICENSE +201 -0
- package/README.md +56 -0
- package/dist/ai/index.cjs +474 -0
- package/dist/ai/index.cjs.map +1 -0
- package/dist/ai/index.d.cts +117 -0
- package/dist/ai/index.d.ts +117 -0
- package/dist/ai/index.js +447 -0
- package/dist/ai/index.js.map +1 -0
- package/dist/domain-CZ-L-ntu.d.ts +163 -0
- package/dist/domain-Dc1wSTkf.d.cts +163 -0
- package/dist/errors-Bw97z_4m.d.cts +12 -0
- package/dist/errors-Bw97z_4m.d.ts +12 -0
- package/dist/generate/index.cjs +222 -0
- package/dist/generate/index.cjs.map +1 -0
- package/dist/generate/index.d.cts +140 -0
- package/dist/generate/index.d.ts +140 -0
- package/dist/generate/index.js +220 -0
- package/dist/generate/index.js.map +1 -0
- package/dist/geocoding/index.cjs +90 -0
- package/dist/geocoding/index.cjs.map +1 -0
- package/dist/geocoding/index.d.cts +36 -0
- package/dist/geocoding/index.d.ts +36 -0
- package/dist/geocoding/index.js +86 -0
- package/dist/geocoding/index.js.map +1 -0
- package/dist/index.cjs +546 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +5 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +469 -0
- package/dist/index.js.map +1 -0
- package/dist/parser-CzXzpmVv.d.cts +111 -0
- package/dist/parser-N7-fNxeu.d.ts +111 -0
- package/dist/react/index.cjs +394 -0
- package/dist/react/index.cjs.map +1 -0
- package/dist/react/index.js +383 -0
- package/dist/react/index.js.map +1 -0
- package/dist/schemas-Dy5coqXo.d.cts +484 -0
- package/dist/schemas-Dy5coqXo.d.ts +484 -0
- package/dist/scrape/index.cjs +133 -0
- package/dist/scrape/index.cjs.map +1 -0
- package/dist/scrape/index.d.cts +60 -0
- package/dist/scrape/index.d.ts +60 -0
- package/dist/scrape/index.js +125 -0
- package/dist/scrape/index.js.map +1 -0
- package/dist/search/index.cjs +76 -0
- package/dist/search/index.cjs.map +1 -0
- package/dist/search/index.d.cts +75 -0
- package/dist/search/index.d.ts +75 -0
- package/dist/search/index.js +71 -0
- package/dist/search/index.js.map +1 -0
- package/dist/types/index.cjs +215 -0
- package/dist/types/index.cjs.map +1 -0
- package/dist/types/index.d.cts +4 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/index.js +171 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types-BhqKlq0k.d.ts +31 -0
- package/dist/types-rFjK5YcJ.d.cts +31 -0
- package/dist/utils/index.cjs +335 -0
- package/dist/utils/index.cjs.map +1 -0
- package/dist/utils/index.d.cts +363 -0
- package/dist/utils/index.d.ts +363 -0
- package/dist/utils/index.js +301 -0
- package/dist/utils/index.js.map +1 -0
- package/package.json +150 -0
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var googleMapsServicesJs = require('@googlemaps/google-maps-services-js');
|
|
4
|
+
|
|
5
|
+
// src/types/errors.ts
|
|
6
|
+
var MapthisError = class extends Error {
|
|
7
|
+
constructor(message, options) {
|
|
8
|
+
super(message, options);
|
|
9
|
+
this.name = "MapthisError";
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
// src/geocoding/errors.ts
|
|
14
|
+
var GeocodingError = class extends MapthisError {
|
|
15
|
+
constructor(message, options) {
|
|
16
|
+
super(message, options);
|
|
17
|
+
this.name = "GeocodingError";
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
function createGoogleGeocoder(config) {
|
|
21
|
+
const client = new googleMapsServicesJs.Client({});
|
|
22
|
+
const apiKey = config.apiKey;
|
|
23
|
+
const geocode = (input) => new Promise((resolve) => {
|
|
24
|
+
client.findPlaceFromText({
|
|
25
|
+
params: {
|
|
26
|
+
key: apiKey,
|
|
27
|
+
input: input.address,
|
|
28
|
+
inputtype: googleMapsServicesJs.PlaceInputType.textQuery,
|
|
29
|
+
fields: ["name", "geometry", "formatted_address", "place_id"],
|
|
30
|
+
locationbias: "circle:20000000@0,0"
|
|
31
|
+
}
|
|
32
|
+
}).then((res) => {
|
|
33
|
+
if (res.status !== 200) {
|
|
34
|
+
return resolve({
|
|
35
|
+
input,
|
|
36
|
+
data: null,
|
|
37
|
+
error: `${res.data.error_message ?? res.statusText}`
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
const place = res.data.candidates[0];
|
|
41
|
+
if (!place) {
|
|
42
|
+
return resolve({ input, data: null, error: "no_results" });
|
|
43
|
+
}
|
|
44
|
+
if (!place.formatted_address || !place.geometry?.location?.lat || !place.geometry?.location?.lng || !place.name || !place.place_id) {
|
|
45
|
+
return resolve({
|
|
46
|
+
input,
|
|
47
|
+
data: null,
|
|
48
|
+
error: "invalid_place_data"
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
return resolve({
|
|
52
|
+
input,
|
|
53
|
+
error: null,
|
|
54
|
+
data: {
|
|
55
|
+
provider: "google",
|
|
56
|
+
name: place.name,
|
|
57
|
+
address: place.formatted_address,
|
|
58
|
+
lat: place.geometry.location.lat,
|
|
59
|
+
lng: place.geometry.location.lng,
|
|
60
|
+
providerId: place.place_id
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
}).catch(() => {
|
|
64
|
+
resolve({ input, data: null, error: "unknown" });
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
return {
|
|
68
|
+
name: "google",
|
|
69
|
+
geocode,
|
|
70
|
+
geocodeMany: (inputs) => Promise.all(inputs.map(geocode))
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// src/geocoding/factory.ts
|
|
75
|
+
function createGeocoder(config) {
|
|
76
|
+
switch (config.provider) {
|
|
77
|
+
case "google":
|
|
78
|
+
return createGoogleGeocoder(config);
|
|
79
|
+
default: {
|
|
80
|
+
const _exhaustive = config.provider;
|
|
81
|
+
throw new Error(`Unsupported geocoding provider: ${_exhaustive}`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
exports.GeocodingError = GeocodingError;
|
|
87
|
+
exports.createGeocoder = createGeocoder;
|
|
88
|
+
exports.createGoogleGeocoder = createGoogleGeocoder;
|
|
89
|
+
//# sourceMappingURL=index.cjs.map
|
|
90
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/types/errors.ts","../../src/geocoding/errors.ts","../../src/geocoding/google.ts","../../src/geocoding/factory.ts"],"names":["Client","PlaceInputType"],"mappings":";;;;;AAOO,IAAM,YAAA,GAAN,cAA2B,KAAA,CAAM;AAAA,EACpC,WAAA,CAAY,SAAkB,OAAA,EAAwB;AAClD,IAAA,KAAA,CAAM,SAAS,OAAO,CAAA;AACtB,IAAA,IAAA,CAAK,IAAA,GAAO,cAAA;AAAA,EAChB;AACJ,CAAA;;;ACVO,IAAM,cAAA,GAAN,cAA6B,YAAA,CAAa;AAAA,EAC7C,WAAA,CAAY,SAAkB,OAAA,EAAwB;AAClD,IAAA,KAAA,CAAM,SAAS,OAAO,CAAA;AACtB,IAAA,IAAA,CAAK,IAAA,GAAO,gBAAA;AAAA,EAChB;AACJ;ACIO,SAAS,qBAAqB,MAAA,EAAiD;AAClF,EAAA,MAAM,MAAA,GAAS,IAAIA,2BAAA,CAAO,EAAE,CAAA;AAC5B,EAAA,MAAM,SAAS,MAAA,CAAO,MAAA;AAEtB,EAAA,MAAM,UAAU,CAAC,KAAA,KACb,IAAI,OAAA,CAAQ,CAAC,OAAA,KAAY;AACrB,IAAA,MAAA,CACK,iBAAA,CAAkB;AAAA,MACf,MAAA,EAAQ;AAAA,QACJ,GAAA,EAAK,MAAA;AAAA,QACL,OAAO,KAAA,CAAM,OAAA;AAAA,QACb,WAAWC,mCAAA,CAAe,SAAA;AAAA,QAC1B,MAAA,EAAQ,CAAC,MAAA,EAAQ,UAAA,EAAY,qBAAqB,UA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B,OACH,CAAA;AAAA,IACL,CAAC,CAAA,CACA,KAAA,CAAM,MAAM;AACT,MAAA,OAAA,CAAQ,EAAE,KAAA,EAAO,IAAA,EAAM,IAAA,EAAM,KAAA,EAAO,WAAW,CAAA;AAAA,IACnD,CAAC,CAAA;AAAA,EACT,CAAC,CAAA;AAEL,EAAA,OAAO;AAAA,IACH,IAAA,EAAM,QAAA;AAAA,IACN,OAAA;AAAA,IACA,WAAA,EAAa,CAAC,MAAA,KAAW,OAAA,CAAQ,IAAI,MAAA,CAAO,GAAA,CAAI,OAAO,CAAC;AAAA,GAC5D;AACJ;;;AC1DO,SAAS,eAAe,MAAA,EAA2C;AACtE,EAAA,QAAQ,OAAO,QAAA;AAAU,IACrB,KAAK,QAAA;AACD,MAAA,OAAO,qBAAqB,MAAM,CAAA;AAAA,IACtC,SAAS;AACL,MAAA,MAAM,cAAqB,MAAA,CAAO,QAAA;AAClC,MAAA,MAAM,IAAI,KAAA,CAAM,CAAA,gCAAA,EAAmC,WAAqB,CAAA,CAAE,CAAA;AAAA,IAC9E;AAAA;AAER","file":"index.cjs","sourcesContent":["/**\n * Base error class for every error thrown by the mapthis package.\n *\n * All feature-specific error classes (scrape, search, ai, geocoding) extend\n * this, so consumers can do `catch (e) { if (e instanceof MapthisError) ... }`\n * to distinguish library errors from other exceptions.\n */\nexport class MapthisError extends Error {\n constructor(message?: string, options?: ErrorOptions) {\n super(message, options)\n this.name = \"MapthisError\"\n }\n}\n","import { MapthisError } from \"../types/errors\"\n\nexport class GeocodingError extends MapthisError {\n constructor(message?: string, options?: ErrorOptions) {\n super(message, options)\n this.name = \"GeocodingError\"\n }\n}\n","import { Client, PlaceInputType } from \"@googlemaps/google-maps-services-js\"\nimport type { ParsedLocation, PlaceQueryResult } from \"../types/domain\"\nimport type { GeocodingProvider, GoogleGeocoderConfig } from \"./types\"\n\n/**\n * Create a Google Maps-backed {@link GeocodingProvider}.\n *\n * Under the hood this uses `findPlaceFromText` with a global location bias,\n * which is a low-cost single-candidate lookup. For multi-candidate ranked\n * searches, consumers should reach for the Places library directly.\n */\nexport function createGoogleGeocoder(config: GoogleGeocoderConfig): GeocodingProvider {\n const client = new Client({})\n const apiKey = config.apiKey\n\n const geocode = (input: ParsedLocation): Promise<PlaceQueryResult> =>\n new Promise((resolve) => {\n client\n .findPlaceFromText({\n params: {\n key: apiKey,\n input: input.address,\n inputtype: PlaceInputType.textQuery,\n fields: [\"name\", \"geometry\", \"formatted_address\", \"place_id\"],\n locationbias: \"circle:20000000@0,0\",\n },\n })\n .then((res) => {\n if (res.status !== 200) {\n return resolve({\n input,\n data: null,\n error: `${res.data.error_message ?? res.statusText}`,\n })\n }\n const place = res.data.candidates[0]\n if (!place) {\n return resolve({ input, data: null, error: \"no_results\" })\n }\n if (\n !place.formatted_address ||\n !place.geometry?.location?.lat ||\n !place.geometry?.location?.lng ||\n !place.name ||\n !place.place_id\n ) {\n return resolve({\n input,\n data: null,\n error: \"invalid_place_data\",\n })\n }\n return resolve({\n input,\n error: null,\n data: {\n provider: \"google\",\n name: place.name,\n address: place.formatted_address,\n lat: place.geometry.location.lat,\n lng: place.geometry.location.lng,\n providerId: place.place_id,\n },\n })\n })\n .catch(() => {\n resolve({ input, data: null, error: \"unknown\" })\n })\n })\n\n return {\n name: \"google\",\n geocode,\n geocodeMany: (inputs) => Promise.all(inputs.map(geocode)),\n }\n}\n","import { createGoogleGeocoder } from \"./google\"\nimport type { GeocoderConfig, GeocodingProvider } from \"./types\"\n\n/**\n * Create a {@link GeocodingProvider} from a config object. The provider field\n * on the config discriminates which implementation is returned.\n *\n * @example\n * ```ts\n * const geocoder = createGeocoder({\n * provider: \"google\",\n * apiKey: process.env.GOOGLE_MAPS_KEY!,\n * })\n * const result = await geocoder.geocode({ address: \"Acropolis, Athens, Greece\" })\n * if (result.data) console.log(result.data.lat, result.data.lng)\n * ```\n */\nexport function createGeocoder(config: GeocoderConfig): GeocodingProvider {\n switch (config.provider) {\n case \"google\":\n return createGoogleGeocoder(config)\n default: {\n const _exhaustive: never = config.provider\n throw new Error(`Unsupported geocoding provider: ${_exhaustive as string}`)\n }\n }\n}\n"]}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { M as MapthisError } from '../errors-Bw97z_4m.cjs';
|
|
2
|
+
import { a as GeocoderConfig, G as GeocodingProvider, b as GoogleGeocoderConfig } from '../types-rFjK5YcJ.cjs';
|
|
3
|
+
import '../domain-Dc1wSTkf.cjs';
|
|
4
|
+
import '../schemas-Dy5coqXo.cjs';
|
|
5
|
+
import 'zod';
|
|
6
|
+
|
|
7
|
+
declare class GeocodingError extends MapthisError {
|
|
8
|
+
constructor(message?: string, options?: ErrorOptions);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Create a {@link GeocodingProvider} from a config object. The provider field
|
|
13
|
+
* on the config discriminates which implementation is returned.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* const geocoder = createGeocoder({
|
|
18
|
+
* provider: "google",
|
|
19
|
+
* apiKey: process.env.GOOGLE_MAPS_KEY!,
|
|
20
|
+
* })
|
|
21
|
+
* const result = await geocoder.geocode({ address: "Acropolis, Athens, Greece" })
|
|
22
|
+
* if (result.data) console.log(result.data.lat, result.data.lng)
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
declare function createGeocoder(config: GeocoderConfig): GeocodingProvider;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Create a Google Maps-backed {@link GeocodingProvider}.
|
|
29
|
+
*
|
|
30
|
+
* Under the hood this uses `findPlaceFromText` with a global location bias,
|
|
31
|
+
* which is a low-cost single-candidate lookup. For multi-candidate ranked
|
|
32
|
+
* searches, consumers should reach for the Places library directly.
|
|
33
|
+
*/
|
|
34
|
+
declare function createGoogleGeocoder(config: GoogleGeocoderConfig): GeocodingProvider;
|
|
35
|
+
|
|
36
|
+
export { GeocoderConfig, GeocodingError, GeocodingProvider, GoogleGeocoderConfig, createGeocoder, createGoogleGeocoder };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { M as MapthisError } from '../errors-Bw97z_4m.js';
|
|
2
|
+
import { a as GeocoderConfig, G as GeocodingProvider, b as GoogleGeocoderConfig } from '../types-BhqKlq0k.js';
|
|
3
|
+
import '../domain-CZ-L-ntu.js';
|
|
4
|
+
import '../schemas-Dy5coqXo.js';
|
|
5
|
+
import 'zod';
|
|
6
|
+
|
|
7
|
+
declare class GeocodingError extends MapthisError {
|
|
8
|
+
constructor(message?: string, options?: ErrorOptions);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Create a {@link GeocodingProvider} from a config object. The provider field
|
|
13
|
+
* on the config discriminates which implementation is returned.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* const geocoder = createGeocoder({
|
|
18
|
+
* provider: "google",
|
|
19
|
+
* apiKey: process.env.GOOGLE_MAPS_KEY!,
|
|
20
|
+
* })
|
|
21
|
+
* const result = await geocoder.geocode({ address: "Acropolis, Athens, Greece" })
|
|
22
|
+
* if (result.data) console.log(result.data.lat, result.data.lng)
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
declare function createGeocoder(config: GeocoderConfig): GeocodingProvider;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Create a Google Maps-backed {@link GeocodingProvider}.
|
|
29
|
+
*
|
|
30
|
+
* Under the hood this uses `findPlaceFromText` with a global location bias,
|
|
31
|
+
* which is a low-cost single-candidate lookup. For multi-candidate ranked
|
|
32
|
+
* searches, consumers should reach for the Places library directly.
|
|
33
|
+
*/
|
|
34
|
+
declare function createGoogleGeocoder(config: GoogleGeocoderConfig): GeocodingProvider;
|
|
35
|
+
|
|
36
|
+
export { GeocoderConfig, GeocodingError, GeocodingProvider, GoogleGeocoderConfig, createGeocoder, createGoogleGeocoder };
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { Client, PlaceInputType } from '@googlemaps/google-maps-services-js';
|
|
2
|
+
|
|
3
|
+
// src/types/errors.ts
|
|
4
|
+
var MapthisError = class extends Error {
|
|
5
|
+
constructor(message, options) {
|
|
6
|
+
super(message, options);
|
|
7
|
+
this.name = "MapthisError";
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
// src/geocoding/errors.ts
|
|
12
|
+
var GeocodingError = class extends MapthisError {
|
|
13
|
+
constructor(message, options) {
|
|
14
|
+
super(message, options);
|
|
15
|
+
this.name = "GeocodingError";
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
function createGoogleGeocoder(config) {
|
|
19
|
+
const client = new Client({});
|
|
20
|
+
const apiKey = config.apiKey;
|
|
21
|
+
const geocode = (input) => new Promise((resolve) => {
|
|
22
|
+
client.findPlaceFromText({
|
|
23
|
+
params: {
|
|
24
|
+
key: apiKey,
|
|
25
|
+
input: input.address,
|
|
26
|
+
inputtype: PlaceInputType.textQuery,
|
|
27
|
+
fields: ["name", "geometry", "formatted_address", "place_id"],
|
|
28
|
+
locationbias: "circle:20000000@0,0"
|
|
29
|
+
}
|
|
30
|
+
}).then((res) => {
|
|
31
|
+
if (res.status !== 200) {
|
|
32
|
+
return resolve({
|
|
33
|
+
input,
|
|
34
|
+
data: null,
|
|
35
|
+
error: `${res.data.error_message ?? res.statusText}`
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
const place = res.data.candidates[0];
|
|
39
|
+
if (!place) {
|
|
40
|
+
return resolve({ input, data: null, error: "no_results" });
|
|
41
|
+
}
|
|
42
|
+
if (!place.formatted_address || !place.geometry?.location?.lat || !place.geometry?.location?.lng || !place.name || !place.place_id) {
|
|
43
|
+
return resolve({
|
|
44
|
+
input,
|
|
45
|
+
data: null,
|
|
46
|
+
error: "invalid_place_data"
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
return resolve({
|
|
50
|
+
input,
|
|
51
|
+
error: null,
|
|
52
|
+
data: {
|
|
53
|
+
provider: "google",
|
|
54
|
+
name: place.name,
|
|
55
|
+
address: place.formatted_address,
|
|
56
|
+
lat: place.geometry.location.lat,
|
|
57
|
+
lng: place.geometry.location.lng,
|
|
58
|
+
providerId: place.place_id
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}).catch(() => {
|
|
62
|
+
resolve({ input, data: null, error: "unknown" });
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
return {
|
|
66
|
+
name: "google",
|
|
67
|
+
geocode,
|
|
68
|
+
geocodeMany: (inputs) => Promise.all(inputs.map(geocode))
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// src/geocoding/factory.ts
|
|
73
|
+
function createGeocoder(config) {
|
|
74
|
+
switch (config.provider) {
|
|
75
|
+
case "google":
|
|
76
|
+
return createGoogleGeocoder(config);
|
|
77
|
+
default: {
|
|
78
|
+
const _exhaustive = config.provider;
|
|
79
|
+
throw new Error(`Unsupported geocoding provider: ${_exhaustive}`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export { GeocodingError, createGeocoder, createGoogleGeocoder };
|
|
85
|
+
//# sourceMappingURL=index.js.map
|
|
86
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/types/errors.ts","../../src/geocoding/errors.ts","../../src/geocoding/google.ts","../../src/geocoding/factory.ts"],"names":[],"mappings":";;;AAOO,IAAM,YAAA,GAAN,cAA2B,KAAA,CAAM;AAAA,EACpC,WAAA,CAAY,SAAkB,OAAA,EAAwB;AAClD,IAAA,KAAA,CAAM,SAAS,OAAO,CAAA;AACtB,IAAA,IAAA,CAAK,IAAA,GAAO,cAAA;AAAA,EAChB;AACJ,CAAA;;;ACVO,IAAM,cAAA,GAAN,cAA6B,YAAA,CAAa;AAAA,EAC7C,WAAA,CAAY,SAAkB,OAAA,EAAwB;AAClD,IAA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* Base error class for every error thrown by the mapthis package.\n *\n * All feature-specific error classes (scrape, search, ai, geocoding) extend\n * this, so consumers can do `catch (e) { if (e instanceof MapthisError) ... }`\n * to distinguish library errors from other exceptions.\n */\nexport class MapthisError extends Error {\n constructor(message?: string, options?: ErrorOptions) {\n super(message, options)\n this.name = \"MapthisError\"\n }\n}\n","import { MapthisError } from \"../types/errors\"\n\nexport class GeocodingError extends MapthisError {\n constructor(message?: string, options?: ErrorOptions) {\n super(message, options)\n this.name = \"GeocodingError\"\n }\n}\n","import { Client, PlaceInputType } from \"@googlemaps/google-maps-services-js\"\nimport type { ParsedLocation, PlaceQueryResult } from \"../types/domain\"\nimport type { GeocodingProvider, GoogleGeocoderConfig } from \"./types\"\n\n/**\n * Create a Google Maps-backed {@link GeocodingProvider}.\n *\n * Under the hood this uses `findPlaceFromText` with a global location bias,\n * which is a low-cost single-candidate lookup. For multi-candidate ranked\n * searches, consumers should reach for the Places library directly.\n */\nexport function createGoogleGeocoder(config: GoogleGeocoderConfig): GeocodingProvider {\n const client = new Client({})\n const apiKey = config.apiKey\n\n const geocode = (input: ParsedLocation): Promise<PlaceQueryResult> =>\n new Promise((resolve) => {\n client\n .findPlaceFromText({\n params: {\n key: apiKey,\n input: input.address,\n inputtype: PlaceInputType.textQuery,\n fields: [\"name\", \"geometry\", \"formatted_address\", \"place_id\"],\n locationbias: \"circle:20000000@0,0\",\n },\n })\n .then((res) => {\n if (res.status !== 200) {\n return resolve({\n input,\n data: null,\n error: `${res.data.error_message ?? res.statusText}`,\n })\n }\n const place = res.data.candidates[0]\n if (!place) {\n return resolve({ input, data: null, error: \"no_results\" })\n }\n if (\n !place.formatted_address ||\n !place.geometry?.location?.lat ||\n !place.geometry?.location?.lng ||\n !place.name ||\n !place.place_id\n ) {\n return resolve({\n input,\n data: null,\n error: \"invalid_place_data\",\n })\n }\n return resolve({\n input,\n error: null,\n data: {\n provider: \"google\",\n name: place.name,\n address: place.formatted_address,\n lat: place.geometry.location.lat,\n lng: place.geometry.location.lng,\n providerId: place.place_id,\n },\n })\n })\n .catch(() => {\n resolve({ input, data: null, error: \"unknown\" })\n })\n })\n\n return {\n name: \"google\",\n geocode,\n geocodeMany: (inputs) => Promise.all(inputs.map(geocode)),\n }\n}\n","import { createGoogleGeocoder } from \"./google\"\nimport type { GeocoderConfig, GeocodingProvider } from \"./types\"\n\n/**\n * Create a {@link GeocodingProvider} from a config object. The provider field\n * on the config discriminates which implementation is returned.\n *\n * @example\n * ```ts\n * const geocoder = createGeocoder({\n * provider: \"google\",\n * apiKey: process.env.GOOGLE_MAPS_KEY!,\n * })\n * const result = await geocoder.geocode({ address: \"Acropolis, Athens, Greece\" })\n * if (result.data) console.log(result.data.lat, result.data.lng)\n * ```\n */\nexport function createGeocoder(config: GeocoderConfig): GeocodingProvider {\n switch (config.provider) {\n case \"google\":\n return createGoogleGeocoder(config)\n default: {\n const _exhaustive: never = config.provider\n throw new Error(`Unsupported geocoding provider: ${_exhaustive as string}`)\n }\n }\n}\n"]}
|