@remaply/maps 0.0.0-stage → 0.2.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.
Files changed (52) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +8 -0
  3. package/README.md +240 -2
  4. package/dist/client.d.ts +31 -0
  5. package/dist/client.d.ts.map +1 -0
  6. package/dist/client.js +208 -0
  7. package/dist/client.js.map +1 -0
  8. package/dist/config.d.ts +62 -0
  9. package/dist/config.d.ts.map +1 -0
  10. package/dist/config.js +65 -0
  11. package/dist/config.js.map +1 -0
  12. package/dist/directions.d.ts +68 -0
  13. package/dist/directions.d.ts.map +1 -0
  14. package/dist/directions.js +114 -0
  15. package/dist/directions.js.map +1 -0
  16. package/dist/errors.d.ts +26 -0
  17. package/dist/errors.d.ts.map +1 -0
  18. package/dist/errors.js +85 -0
  19. package/dist/errors.js.map +1 -0
  20. package/dist/geo.d.ts +17 -0
  21. package/dist/geo.d.ts.map +1 -0
  22. package/dist/geo.js +43 -0
  23. package/dist/geo.js.map +1 -0
  24. package/dist/index.d.ts +21 -0
  25. package/dist/index.d.ts.map +1 -0
  26. package/dist/index.js +23 -0
  27. package/dist/index.js.map +1 -0
  28. package/dist/map.d.ts +117 -0
  29. package/dist/map.d.ts.map +1 -0
  30. package/dist/map.js +357 -0
  31. package/dist/map.js.map +1 -0
  32. package/dist/routePreview.d.ts +84 -0
  33. package/dist/routePreview.d.ts.map +1 -0
  34. package/dist/routePreview.js +206 -0
  35. package/dist/routePreview.js.map +1 -0
  36. package/dist/search.d.ts +70 -0
  37. package/dist/search.d.ts.map +1 -0
  38. package/dist/search.js +60 -0
  39. package/dist/search.js.map +1 -0
  40. package/dist/security.d.ts +20 -0
  41. package/dist/security.d.ts.map +1 -0
  42. package/dist/security.js +53 -0
  43. package/dist/security.js.map +1 -0
  44. package/dist/style.d.ts +45 -0
  45. package/dist/style.d.ts.map +1 -0
  46. package/dist/style.js +173 -0
  47. package/dist/style.js.map +1 -0
  48. package/dist/version.d.ts +3 -0
  49. package/dist/version.d.ts.map +1 -0
  50. package/dist/version.js +5 -0
  51. package/dist/version.js.map +1 -0
  52. package/package.json +56 -4
package/dist/client.js ADDED
@@ -0,0 +1,208 @@
1
+ // Copyright 2026 Remaply
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { currentSettings } from "./config.js";
4
+ import { RemaplyApiError } from "./errors.js";
5
+ import { MAX_RESPONSE_BYTES, isSameApiOrigin, isSecretKey } from "./security.js";
6
+ /** Where the docs explain how to set the API key. */
7
+ export const API_KEY_HELP = "https://github.com/Remaply-Fiji/remaply-maps-sdk/blob/main/web/README.md#api-key";
8
+ /** Header the API reads the key from. The API's CORS rules allow only this one and Content-Type. */
9
+ export const API_KEY_HEADER = "X-Api-Key";
10
+ /** Query parameter the API also reads publishable keys from, for map clients that can only set a URL. */
11
+ export const API_KEY_QUERY_PARAM = "key";
12
+ const REQUEST_TIMEOUT_MS = 20000;
13
+ const MAX_RETRIES = 2;
14
+ const MAX_RETRY_DELAY_MS = 4000;
15
+ export function apiBaseUrl() {
16
+ return currentSettings().apiBaseUrl;
17
+ }
18
+ /** True when [url] is on the Remaply API's origin: the only requests that carry the key. */
19
+ export function isApiUrl(url) {
20
+ return isSameApiOrigin(apiBaseUrl(), url);
21
+ }
22
+ /**
23
+ * `${apiBaseUrl}${path}` with [params]. Null and empty values are dropped; an array repeats the
24
+ * parameter (`category=a&category=b`).
25
+ */
26
+ export function apiUrl(path, params = {}) {
27
+ const url = new URL(apiBaseUrl() + path);
28
+ for (const [name, value] of Object.entries(params)) {
29
+ const values = Array.isArray(value) ? value : [value];
30
+ for (const item of values) {
31
+ if (item === null || item === undefined)
32
+ continue;
33
+ const text = String(item);
34
+ if (text !== "")
35
+ url.searchParams.append(name, text);
36
+ }
37
+ }
38
+ return url.toString();
39
+ }
40
+ /** Throws `missing_api_key` or `secret_api_key` before anything is sent. */
41
+ function requireUsableKey() {
42
+ const key = currentSettings().apiKey;
43
+ if (isSecretKey(key)) {
44
+ throw new RemaplyApiError(0, "secret_api_key", `A secret key (rmp_sk_) is set: websites need a publishable rmp_pk_ key. ${API_KEY_HELP}`);
45
+ }
46
+ if (key === "") {
47
+ throw new RemaplyApiError(0, "missing_api_key", `No Remaply API key: call configure({ apiKey }). ${API_KEY_HELP}`);
48
+ }
49
+ return key;
50
+ }
51
+ /**
52
+ * [url] with the publishable key as `?key=`, for the map's own tile, sprite and glyph requests on the
53
+ * API origin. In the URL, not a header, so the browser sends them without a CORS preflight per tile.
54
+ * Any other URL is returned unchanged. Never adds a secret key.
55
+ */
56
+ export function withKeyParam(url) {
57
+ if (!isApiUrl(url))
58
+ return url;
59
+ const key = currentSettings().apiKey;
60
+ if (key === "" || isSecretKey(key))
61
+ return url;
62
+ const parsed = new URL(url);
63
+ parsed.searchParams.set(API_KEY_QUERY_PARAM, key);
64
+ return parsed.toString();
65
+ }
66
+ /** GET [path] on the API and parse the JSON. Throws [RemaplyApiError] for API errors. */
67
+ export async function apiGet(path, params, signal) {
68
+ return JSON.parse(await getText(apiUrl(path, params), signal));
69
+ }
70
+ /**
71
+ * GET an absolute URL and return the body. The key goes only to the API's origin. Retries network
72
+ * errors, 5xx and 429 twice with backoff; never retries after [signal] aborts.
73
+ */
74
+ export async function getText(url, signal) {
75
+ const toApi = isApiUrl(url);
76
+ const headers = { Accept: "application/json" };
77
+ if (toApi)
78
+ headers[API_KEY_HEADER] = requireUsableKey();
79
+ for (let attempt = 0;; attempt++) {
80
+ let response;
81
+ try {
82
+ response = await fetchWithTimeout(url, headers, signal);
83
+ }
84
+ catch (error) {
85
+ if (isAbort(error, signal) || attempt >= MAX_RETRIES)
86
+ throw error;
87
+ await delay(retryDelay(attempt), signal);
88
+ continue;
89
+ }
90
+ if (response.ok)
91
+ return readCapped(response);
92
+ if ((response.status >= 500 || response.status === 429) && attempt < MAX_RETRIES) {
93
+ await response.body?.cancel().catch(() => undefined);
94
+ await delay(retryDelay(attempt), signal);
95
+ continue;
96
+ }
97
+ throw await toApiError(response);
98
+ }
99
+ }
100
+ /** GET [url] and report only whether it answered with success, for the glyph check. */
101
+ export async function isReachable(url, signal) {
102
+ try {
103
+ const response = await fetchWithTimeout(withKeyParam(url), {}, signal);
104
+ await response.body?.cancel().catch(() => undefined);
105
+ return response.ok;
106
+ }
107
+ catch (error) {
108
+ if (isAbort(error, signal))
109
+ throw error;
110
+ return false;
111
+ }
112
+ }
113
+ async function fetchWithTimeout(url, headers, signal) {
114
+ const controller = new AbortController();
115
+ const timer = setTimeout(() => controller.abort(new DOMException("The request timed out.", "TimeoutError")), REQUEST_TIMEOUT_MS);
116
+ const onAbort = () => controller.abort(signal?.reason);
117
+ if (signal?.aborted)
118
+ onAbort();
119
+ else
120
+ signal?.addEventListener("abort", onAbort, { once: true });
121
+ try {
122
+ return await fetch(url, {
123
+ headers,
124
+ signal: controller.signal,
125
+ // No cookies or HTTP auth: the API key is the only credential.
126
+ credentials: "omit",
127
+ // Partner pages should not leak their path to the API.
128
+ referrerPolicy: "strict-origin-when-cross-origin",
129
+ });
130
+ }
131
+ finally {
132
+ clearTimeout(timer);
133
+ signal?.removeEventListener("abort", onAbort);
134
+ }
135
+ }
136
+ /** The body, at most MAX_RESPONSE_BYTES: a larger one fails with `response_too_large`. */
137
+ async function readCapped(response) {
138
+ const tooLarge = () => new RemaplyApiError(response.status, "response_too_large", `Response larger than ${MAX_RESPONSE_BYTES} bytes`);
139
+ const length = Number(response.headers.get("Content-Length"));
140
+ if (Number.isFinite(length) && length > MAX_RESPONSE_BYTES) {
141
+ await response.body?.cancel().catch(() => undefined);
142
+ throw tooLarge();
143
+ }
144
+ if (!response.body) {
145
+ const text = await response.text();
146
+ if (text.length > MAX_RESPONSE_BYTES)
147
+ throw tooLarge();
148
+ return text;
149
+ }
150
+ const reader = response.body.getReader();
151
+ const chunks = [];
152
+ let total = 0;
153
+ for (;;) {
154
+ const { done, value } = await reader.read();
155
+ if (done)
156
+ break;
157
+ total += value.byteLength;
158
+ if (total > MAX_RESPONSE_BYTES) {
159
+ await reader.cancel().catch(() => undefined);
160
+ throw tooLarge();
161
+ }
162
+ chunks.push(value);
163
+ }
164
+ const bytes = new Uint8Array(total);
165
+ let offset = 0;
166
+ for (const chunk of chunks) {
167
+ bytes.set(chunk, offset);
168
+ offset += chunk.byteLength;
169
+ }
170
+ return new TextDecoder().decode(bytes);
171
+ }
172
+ /** `{"error": {"code": ..., "message": ...}}`, else `http_error`. */
173
+ async function toApiError(response) {
174
+ try {
175
+ const body = JSON.parse(await readCapped(response));
176
+ const code = body.error?.code;
177
+ if (typeof code === "string") {
178
+ const message = typeof body.error?.message === "string" ? body.error.message : "";
179
+ return new RemaplyApiError(response.status, code, message);
180
+ }
181
+ }
182
+ catch {
183
+ // Not JSON: fall through.
184
+ }
185
+ return new RemaplyApiError(response.status, "http_error", `HTTP ${response.status}`);
186
+ }
187
+ function retryDelay(attempt) {
188
+ return Math.min(1000 * 2 ** attempt, MAX_RETRY_DELAY_MS);
189
+ }
190
+ function isAbort(error, signal) {
191
+ return signal?.aborted === true || (error instanceof DOMException && error.name === "AbortError");
192
+ }
193
+ function delay(ms, signal) {
194
+ return new Promise((resolve, reject) => {
195
+ if (signal?.aborted)
196
+ return reject(signal.reason);
197
+ const timer = setTimeout(() => {
198
+ signal?.removeEventListener("abort", onAbort);
199
+ resolve();
200
+ }, ms);
201
+ const onAbort = () => {
202
+ clearTimeout(timer);
203
+ reject(signal?.reason);
204
+ };
205
+ signal?.addEventListener("abort", onAbort, { once: true });
206
+ });
207
+ }
208
+ //# sourceMappingURL=client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,yBAAyB;AACzB,sCAAsC;AAEtC,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,EAAE,kBAAkB,EAAE,eAAe,EAAE,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E,MAAoB;IACnD,OAAO,MAAM,EAAE,OAAO,KAAK,IAAI,IAAI,CAAC,KAAK,YAAY,YAAY,IAAI,KAAK,CAAC,IAAI,KAAK,YAAY,CAAC,CAAC;AACpG,CAAC;AAED,SAAS,KAAK,CAAC,EAAU,EAAE,MAAoB;IAC7C,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACrC,IAAI,MAAM,EAAE,OAAO;YAAE,OAAO,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QAClD,MAAM,KAAK,GAAG,UAAU,CAAC,GAAG,EAAE;YAC5B,MAAM,EAAE,mBAAmB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;YAC9C,OAAO,EAAE,CAAC;QACZ,CAAC,EAAE,EAAE,CAAC,CAAC;QACP,MAAM,OAAO,GAAG,GAAG,EAAE;YACnB,YAAY,CAAC,KAAK,CAAC,CAAC;YACpB,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QACzB,CAAC,CAAC;QACF,MAAM,EAAE,gBAAgB,CAAC,OAAO,EAAE,OAAO,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;IAC7D,CAAC,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,62 @@
1
+ /** The Remaply Platform API. */
2
+ export declare const DEFAULT_API_BASE_URL = "https://api.remaply.com";
3
+ /** Default of `buildingMinZoom`: the first zoom the tiles have buildings in. */
4
+ export declare const BUILDING_MIN_ZOOM = 15;
5
+ /** Default of `addressNumberMinZoom`: the first zoom the tiles have building numbers in. */
6
+ export declare const ADDRESS_NUMBER_MIN_ZOOM = 15;
7
+ export interface RemaplyOptions {
8
+ /**
9
+ * A publishable key (`rmp_pk_...`) from Remaply Console with the `styles`, `tiles`, `search`,
10
+ * `geocode` and `routing` scopes. Restrict it to your website's origins in Remaply Console: it
11
+ * ships in your page. A secret key (`rmp_sk_...`) is refused.
12
+ */
13
+ apiKey: string;
14
+ /**
15
+ * Another Remaply API, such as staging. Must be `https://` (`http://` only for localhost).
16
+ * Default `https://api.remaply.com`.
17
+ */
18
+ apiBaseUrl?: string;
19
+ /**
20
+ * A MapLibre style URL to show instead of Remaply's default style. It needs a vector source
21
+ * named `remaply`; the SDK points it at the Remaply tiles. Empty: the default style.
22
+ */
23
+ mapStyleUrl?: string;
24
+ /**
25
+ * Zoom from which the map shows buildings. Default 15, the first zoom the tiles have buildings
26
+ * in. Null: the tiles' own zooms (17 for most buildings).
27
+ */
28
+ buildingMinZoom?: number | null;
29
+ /** Zoom from which the map shows building numbers. Default 15. Null: the tiles' own zooms (18 for most). */
30
+ addressNumberMinZoom?: number | null;
31
+ }
32
+ interface Settings {
33
+ apiKey: string;
34
+ apiBaseUrl: string;
35
+ mapStyleUrl: string;
36
+ tileset: string;
37
+ buildingMinZoom: number | null;
38
+ addressNumberMinZoom: number | null;
39
+ }
40
+ /**
41
+ * Set the API key, once, before the first map or request.
42
+ *
43
+ * ```ts
44
+ * configure({ apiKey: import.meta.env.VITE_REMAPLY_API_KEY });
45
+ * ```
46
+ *
47
+ * Throws when `apiBaseUrl` is not `https://` (a programming error).
48
+ */
49
+ export declare function configure(options: RemaplyOptions): void;
50
+ /**
51
+ * True when a publishable API key is set. Without one, every request fails with
52
+ * `missing_api_key`. A secret key does not count: requests fail with `secret_api_key`.
53
+ */
54
+ export declare function isConfigured(): boolean;
55
+ /** The SDK version, e.g. `0.1.0`. */
56
+ export declare const version: string;
57
+ /** Internal: read by the SDK's modules. Not exported from the package. */
58
+ export declare function currentSettings(): Readonly<Settings>;
59
+ /** Internal, for tests: back to the defaults. */
60
+ export declare function resetForTests(): void;
61
+ export {};
62
+ //# sourceMappingURL=config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAMA,gCAAgC;AAChC,eAAO,MAAM,oBAAoB,4BAA4B,CAAC;AAE9D,gFAAgF;AAChF,eAAO,MAAM,iBAAiB,KAAK,CAAC;AAEpC,4FAA4F;AAC5F,eAAO,MAAM,uBAAuB,KAAK,CAAC;AAE1C,MAAM,WAAW,cAAc;IAC7B;;;;OAIG;IACH,MAAM,EAAE,MAAM,CAAC;IACf;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;OAGG;IACH,eAAe,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAChC,4GAA4G;IAC5G,oBAAoB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CACtC;AAED,UAAU,QAAQ;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;IACpB,OAAO,EAAE,MAAM,CAAC;IAChB,eAAe,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/B,oBAAoB,EAAE,MAAM,GAAG,IAAI,CAAC;CACrC;AAWD;;;;;;;;GAQG;AACH,wBAAgB,SAAS,CAAC,OAAO,EAAE,cAAc,GAAG,IAAI,CAavD;AAED;;;GAGG;AACH,wBAAgB,YAAY,IAAI,OAAO,CAEtC;AAED,qCAAqC;AACrC,eAAO,MAAM,OAAO,EAAE,MAAgB,CAAC;AAEvC,0EAA0E;AAC1E,wBAAgB,eAAe,IAAI,QAAQ,CAAC,QAAQ,CAAC,CAEpD;AAED,iDAAiD;AACjD,wBAAgB,aAAa,IAAI,IAAI,CAOpC"}
package/dist/config.js ADDED
@@ -0,0 +1,65 @@
1
+ // Copyright 2026 Remaply
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { isSecretKey, validatedApiBaseUrl } from "./security.js";
4
+ import { VERSION } from "./version.js";
5
+ /** The Remaply Platform API. */
6
+ export const DEFAULT_API_BASE_URL = "https://api.remaply.com";
7
+ /** Default of `buildingMinZoom`: the first zoom the tiles have buildings in. */
8
+ export const BUILDING_MIN_ZOOM = 15;
9
+ /** Default of `addressNumberMinZoom`: the first zoom the tiles have building numbers in. */
10
+ export const ADDRESS_NUMBER_MIN_ZOOM = 15;
11
+ const settings = {
12
+ apiKey: "",
13
+ apiBaseUrl: DEFAULT_API_BASE_URL,
14
+ mapStyleUrl: "",
15
+ tileset: "remaply",
16
+ buildingMinZoom: BUILDING_MIN_ZOOM,
17
+ addressNumberMinZoom: ADDRESS_NUMBER_MIN_ZOOM,
18
+ };
19
+ /**
20
+ * Set the API key, once, before the first map or request.
21
+ *
22
+ * ```ts
23
+ * configure({ apiKey: import.meta.env.VITE_REMAPLY_API_KEY });
24
+ * ```
25
+ *
26
+ * Throws when `apiBaseUrl` is not `https://` (a programming error).
27
+ */
28
+ export function configure(options) {
29
+ const apiBaseUrl = validatedApiBaseUrl(options.apiBaseUrl ?? DEFAULT_API_BASE_URL);
30
+ settings.apiKey = (options.apiKey ?? "").trim();
31
+ settings.apiBaseUrl = apiBaseUrl;
32
+ if (options.mapStyleUrl !== undefined)
33
+ settings.mapStyleUrl = options.mapStyleUrl.trim();
34
+ if (options.buildingMinZoom !== undefined)
35
+ settings.buildingMinZoom = options.buildingMinZoom;
36
+ if (options.addressNumberMinZoom !== undefined)
37
+ settings.addressNumberMinZoom = options.addressNumberMinZoom;
38
+ if (isSecretKey(settings.apiKey) && typeof console !== "undefined") {
39
+ console.error("[Remaply] A secret API key (rmp_sk_) was passed to configure(). It is never sent from a browser: " +
40
+ "use a publishable rmp_pk_ key, and rotate this secret key in Remaply Console.");
41
+ }
42
+ }
43
+ /**
44
+ * True when a publishable API key is set. Without one, every request fails with
45
+ * `missing_api_key`. A secret key does not count: requests fail with `secret_api_key`.
46
+ */
47
+ export function isConfigured() {
48
+ return settings.apiKey !== "" && !isSecretKey(settings.apiKey);
49
+ }
50
+ /** The SDK version, e.g. `0.1.0`. */
51
+ export const version = VERSION;
52
+ /** Internal: read by the SDK's modules. Not exported from the package. */
53
+ export function currentSettings() {
54
+ return settings;
55
+ }
56
+ /** Internal, for tests: back to the defaults. */
57
+ export function resetForTests() {
58
+ settings.apiKey = "";
59
+ settings.apiBaseUrl = DEFAULT_API_BASE_URL;
60
+ settings.mapStyleUrl = "";
61
+ settings.tileset = "remaply";
62
+ settings.buildingMinZoom = BUILDING_MIN_ZOOM;
63
+ settings.addressNumberMinZoom = ADDRESS_NUMBER_MIN_ZOOM;
64
+ }
65
+ //# sourceMappingURL=config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,yBAAyB;AACzB,sCAAsC;AAEtC,OAAO,EAAE,WAAW,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AACjE,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAEvC,gCAAgC;AAChC,MAAM,CAAC,MAAM,oBAAoB,GAAG,yBAAyB,CAAC;AAE9D,gFAAgF;AAChF,MAAM,CAAC,MAAM,iBAAiB,GAAG,EAAE,CAAC;AAEpC,4FAA4F;AAC5F,MAAM,CAAC,MAAM,uBAAuB,GAAG,EAAE,CAAC;AAqC1C,MAAM,QAAQ,GAAa;IACzB,MAAM,EAAE,EAAE;IACV,UAAU,EAAE,oBAAoB;IAChC,WAAW,EAAE,EAAE;IACf,OAAO,EAAE,SAAS;IAClB,eAAe,EAAE,iBAAiB;IAClC,oBAAoB,EAAE,uBAAuB;CAC9C,CAAC;AAEF;;;;;;;;GAQG;AACH,MAAM,UAAU,SAAS,CAAC,OAAuB;IAC/C,MAAM,UAAU,GAAG,mBAAmB,CAAC,OAAO,CAAC,UAAU,IAAI,oBAAoB,CAAC,CAAC;IACnF,QAAQ,CAAC,MAAM,GAAG,CAAC,OAAO,CAAC,MAAM,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;IAChD,QAAQ,CAAC,UAAU,GAAG,UAAU,CAAC;IACjC,IAAI,OAAO,CAAC,WAAW,KAAK,SAAS;QAAE,QAAQ,CAAC,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC,IAAI,EAAE,CAAC;IACzF,IAAI,OAAO,CAAC,eAAe,KAAK,SAAS;QAAE,QAAQ,CAAC,eAAe,GAAG,OAAO,CAAC,eAAe,CAAC;IAC9F,IAAI,OAAO,CAAC,oBAAoB,KAAK,SAAS;QAAE,QAAQ,CAAC,oBAAoB,GAAG,OAAO,CAAC,oBAAoB,CAAC;IAC7G,IAAI,WAAW,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,OAAO,OAAO,KAAK,WAAW,EAAE,CAAC;QACnE,OAAO,CAAC,KAAK,CACX,mGAAmG;YACjG,+EAA+E,CAClF,CAAC;IACJ,CAAC;AACH,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,YAAY;IAC1B,OAAO,QAAQ,CAAC,MAAM,KAAK,EAAE,IAAI,CAAC,WAAW,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;AACjE,CAAC;AAED,qCAAqC;AACrC,MAAM,CAAC,MAAM,OAAO,GAAW,OAAO,CAAC;AAEvC,0EAA0E;AAC1E,MAAM,UAAU,eAAe;IAC7B,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED,iDAAiD;AACjD,MAAM,UAAU,aAAa;IAC3B,QAAQ,CAAC,MAAM,GAAG,EAAE,CAAC;IACrB,QAAQ,CAAC,UAAU,GAAG,oBAAoB,CAAC;IAC3C,QAAQ,CAAC,WAAW,GAAG,EAAE,CAAC;IAC1B,QAAQ,CAAC,OAAO,GAAG,SAAS,CAAC;IAC7B,QAAQ,CAAC,eAAe,GAAG,iBAAiB,CAAC;IAC7C,QAAQ,CAAC,oBAAoB,GAAG,uBAAuB,CAAC;AAC1D,CAAC"}
@@ -0,0 +1,68 @@
1
+ import type { LatLon } from "./geo.js";
2
+ /** How you travel: the API's `profile`. */
3
+ export type TravelProfile = "car" | "motorcycle" | "foot" | "bicycle" | "truck";
4
+ /** The profiles the Remaply apps offer, with their labels. */
5
+ export declare const TRAVEL_PROFILES: ReadonlyArray<{
6
+ profile: TravelProfile;
7
+ label: string;
8
+ }>;
9
+ /** Points per route allowed by the API (MAX_POINTS in routing/schemas.py). */
10
+ export declare const MAX_ROUTE_POINTS = 10;
11
+ /** Other routes besides the best one (the API maximum). Only for two points. */
12
+ export declare const MAX_ALTERNATIVES = 2;
13
+ export interface RouteStep {
14
+ /** `depart`, `turn_left`, `roundabout_enter`, `arrive`... A string so new values do not break your code. */
15
+ type: string;
16
+ /** "Turn left onto Queens Road". */
17
+ instruction: string;
18
+ distance_m: number;
19
+ duration_s: number;
20
+ road_names: string[];
21
+ /** Where the step starts. */
22
+ location: LatLon;
23
+ }
24
+ export interface RouteLeg {
25
+ distance_m: number;
26
+ duration_s: number;
27
+ steps: RouteStep[];
28
+ }
29
+ export interface Route {
30
+ distance_m: number;
31
+ duration_s: number;
32
+ /** A GeoJSON LineString: `[lon, lat]` pairs. */
33
+ geometry: {
34
+ type: "LineString";
35
+ coordinates: Array<[number, number]>;
36
+ };
37
+ /** One per pair of consecutive points. */
38
+ legs: RouteLeg[];
39
+ }
40
+ export interface RouteOptions {
41
+ /** Default `car`. */
42
+ profile?: TravelProfile;
43
+ /** Other routes besides the best one, 0–2. Only for two points. */
44
+ alternatives?: number;
45
+ signal?: AbortSignal;
46
+ }
47
+ /**
48
+ * Routes through [points] in order (start, stops, destination), the best first, with turn-by-turn
49
+ * steps. The key needs the `routing` scope.
50
+ *
51
+ * ```ts
52
+ * const [best] = await route([from, to], { profile: "car" });
53
+ * ```
54
+ */
55
+ export declare function route(points: readonly LatLon[], options?: RouteOptions): Promise<Route[]>;
56
+ /** The line of a route as points. */
57
+ export declare function routePoints(route: Pick<Route, "geometry">): LatLon[];
58
+ /** The road the route spends the longest distance on, for "via Queens Road". Null without steps. */
59
+ export declare function mainRoad(route: Pick<Route, "legs">): string | null;
60
+ /** The kind of turn of a step, for its icon. */
61
+ export type Maneuver = "depart" | "arrive" | "straight" | "slight_right" | "right" | "sharp_right" | "uturn" | "sharp_left" | "left" | "slight_left" | "ramp_right" | "ramp_left" | "keep_right" | "keep_left" | "merge" | "roundabout" | "ferry" | "other";
62
+ /** The maneuver of a step type (`turn_left` → `left`); `other` for types this version does not know. */
63
+ export declare function maneuverOf(type: string): Maneuver;
64
+ /** `850` → `850 m`, `1234` → `1.2 km`. */
65
+ export declare function formatDistance(metres: number): string;
66
+ /** `3720` → `1 h 2 min`, `90` → `2 min` (at least `1 min`). */
67
+ export declare function formatDuration(seconds: number): string;
68
+ //# sourceMappingURL=directions.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"directions.d.ts","sourceRoot":"","sources":["../src/directions.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAKvC,2CAA2C;AAC3C,MAAM,MAAM,aAAa,GAAG,KAAK,GAAG,YAAY,GAAG,MAAM,GAAG,SAAS,GAAG,OAAO,CAAC;AAEhF,8DAA8D;AAC9D,eAAO,MAAM,eAAe,EAAE,aAAa,CAAC;IAAE,OAAO,EAAE,aAAa,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAKpF,CAAC;AAEF,8EAA8E;AAC9E,eAAO,MAAM,gBAAgB,KAAK,CAAC;AAEnC,gFAAgF;AAChF,eAAO,MAAM,gBAAgB,IAAI,CAAC;AAElC,MAAM,WAAW,SAAS;IACxB,4GAA4G;IAC5G,IAAI,EAAE,MAAM,CAAC;IACb,oCAAoC;IACpC,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,EAAE,MAAM,EAAE,CAAC;IACrB,6BAA6B;IAC7B,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,QAAQ;IACvB,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,EAAE,MAAM,CAAC;IACnB,KAAK,EAAE,SAAS,EAAE,CAAC;CACpB;AAED,MAAM,WAAW,KAAK;IACpB,UAAU,EAAE,MAAM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/D,wBAAgB,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAMtD"}
@@ -0,0 +1,114 @@
1
+ // Copyright 2026 Remaply
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { apiGet } from "./client.js";
4
+ /** The profiles the Remaply apps offer, with their labels. */
5
+ export const TRAVEL_PROFILES = [
6
+ { profile: "car", label: "Drive" },
7
+ { profile: "motorcycle", label: "Motorcycle" },
8
+ { profile: "foot", label: "Walk" },
9
+ { profile: "bicycle", label: "Cycle" },
10
+ ];
11
+ /** Points per route allowed by the API (MAX_POINTS in routing/schemas.py). */
12
+ export const MAX_ROUTE_POINTS = 10;
13
+ /** Other routes besides the best one (the API maximum). Only for two points. */
14
+ export const MAX_ALTERNATIVES = 2;
15
+ /**
16
+ * Routes through [points] in order (start, stops, destination), the best first, with turn-by-turn
17
+ * steps. The key needs the `routing` scope.
18
+ *
19
+ * ```ts
20
+ * const [best] = await route([from, to], { profile: "car" });
21
+ * ```
22
+ */
23
+ export async function route(points, options = {}) {
24
+ const { profile = "car", alternatives = 0, signal } = options;
25
+ if (points.length < 2 || points.length > MAX_ROUTE_POINTS) {
26
+ throw new RangeError(`A route needs 2 to ${MAX_ROUTE_POINTS} points, not ${points.length}.`);
27
+ }
28
+ const body = await apiGet("/v1/route", {
29
+ points: points.map((p) => `${p.lat},${p.lon}`).join(";"),
30
+ profile,
31
+ alternatives: alternatives > 0 && points.length === 2 ? Math.min(alternatives, MAX_ALTERNATIVES) : null,
32
+ steps: true,
33
+ geometry: "geojson",
34
+ }, signal);
35
+ return body.routes.map((r) => ({
36
+ ...r,
37
+ legs: r.legs.map((leg) => ({ ...leg, steps: (leg.steps ?? []).map((s) => ({ ...s, road_names: s.road_names ?? [] })) })),
38
+ }));
39
+ }
40
+ /** The line of a route as points. */
41
+ export function routePoints(route) {
42
+ return route.geometry.coordinates.filter((c) => c.length >= 2).map(([lon, lat]) => ({ lat, lon }));
43
+ }
44
+ /** The road the route spends the longest distance on, for "via Queens Road". Null without steps. */
45
+ export function mainRoad(route) {
46
+ const byRoad = new Map();
47
+ for (const step of route.legs.flatMap((leg) => leg.steps)) {
48
+ const name = step.road_names[0];
49
+ if (!name)
50
+ continue;
51
+ byRoad.set(name, (byRoad.get(name) ?? 0) + step.distance_m);
52
+ }
53
+ let best = null;
54
+ let bestDistance = -1;
55
+ for (const [name, distance] of byRoad) {
56
+ if (distance > bestDistance) {
57
+ best = name;
58
+ bestDistance = distance;
59
+ }
60
+ }
61
+ return best;
62
+ }
63
+ const MANEUVERS = {
64
+ depart: "depart",
65
+ arrive: "arrive",
66
+ continue: "straight",
67
+ keep_straight: "straight",
68
+ turn_slight_right: "slight_right",
69
+ turn_right: "right",
70
+ turn_sharp_right: "sharp_right",
71
+ uturn: "uturn",
72
+ turn_sharp_left: "sharp_left",
73
+ turn_left: "left",
74
+ turn_slight_left: "slight_left",
75
+ ramp: "ramp_right",
76
+ ramp_right: "ramp_right",
77
+ exit_right: "ramp_right",
78
+ ramp_left: "ramp_left",
79
+ exit_left: "ramp_left",
80
+ keep_right: "keep_right",
81
+ keep_left: "keep_left",
82
+ merge: "merge",
83
+ merge_right: "merge",
84
+ merge_left: "merge",
85
+ roundabout_enter: "roundabout",
86
+ roundabout_exit: "roundabout",
87
+ ferry_enter: "ferry",
88
+ ferry_exit: "ferry",
89
+ };
90
+ /** The maneuver of a step type (`turn_left` → `left`); `other` for types this version does not know. */
91
+ export function maneuverOf(type) {
92
+ return Object.prototype.hasOwnProperty.call(MANEUVERS, type) ? MANEUVERS[type] : "other";
93
+ }
94
+ // Formatting as the apps do it (core/.../util/Format.kt).
95
+ /** `1.25` → `1.3`, `2.0` → `2`. */
96
+ function oneDecimal(value) {
97
+ const tenths = Math.round(value * 10);
98
+ return tenths % 10 === 0 ? `${tenths / 10}` : `${Math.trunc(tenths / 10)}.${Math.abs(tenths % 10)}`;
99
+ }
100
+ /** `850` → `850 m`, `1234` → `1.2 km`. */
101
+ export function formatDistance(metres) {
102
+ const m = Math.max(0, metres);
103
+ return m < 1000 ? `${Math.round(m)} m` : `${oneDecimal(m / 1000)} km`;
104
+ }
105
+ /** `3720` → `1 h 2 min`, `90` → `2 min` (at least `1 min`). */
106
+ export function formatDuration(seconds) {
107
+ const minutes = Math.max(1, Math.round(seconds / 60));
108
+ if (minutes < 60)
109
+ return `${minutes} min`;
110
+ const hours = Math.floor(minutes / 60);
111
+ const rest = minutes % 60;
112
+ return rest > 0 ? `${hours} h ${rest} min` : `${hours} h`;
113
+ }
114
+ //# sourceMappingURL=directions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"directions.js","sourceRoot":"","sources":["../src/directions.ts"],"names":[],"mappings":"AAAA,yBAAyB;AACzB,sCAAsC;AAEtC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AASrC,8DAA8D;AAC9D,MAAM,CAAC,MAAM,eAAe,GAA6D;IACvF,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE;IAClC,EAAE,OAAO,EAAE,YAAY,EAAE,KAAK,EAAE,YAAY,EAAE;IAC9C,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE;IAClC,EAAE,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE,OAAO,EAAE;CACvC,CAAC;AAEF,8EAA8E;AAC9E,MAAM,CAAC,MAAM,gBAAgB,GAAG,EAAE,CAAC;AAEnC,gFAAgF;AAChF,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,CAAC;AAqClC;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,KAAK,CAAC,MAAyB,EAAE,UAAwB,EAAE;IAC/E,MAAM,EAAE,OAAO,GAAG,KAAK,EAAE,YAAY,GAAG,CAAC,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IAC9D,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,IAAI,MAAM,CAAC,MAAM,GAAG,gBAAgB,EAAE,CAAC;QAC1D,MAAM,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AACxE,CAAC;AAED,+DAA+D;AAC/D,MAAM,UAAU,cAAc,CAAC,OAAe;IAC5C,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,OAAO,GAAG,EAAE,CAAC,CAAC,CAAC;IACtD,IAAI,OAAO,GAAG,EAAE;QAAE,OAAO,GAAG,OAAO,MAAM,CAAC;IAC1C,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,GAAG,EAAE,CAAC,CAAC;IACvC,MAAM,IAAI,GAAG,OAAO,GAAG,EAAE,CAAC;IAC1B,OAAO,IAAI,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,KAAK,MAAM,IAAI,MAAM,CAAC,CAAC,CAAC,GAAG,KAAK,IAAI,CAAC;AAC5D,CAAC"}
@@ -0,0 +1,26 @@
1
+ /**
2
+ * An error from the Remaply API: `{"error": {"code": ..., "message": ...}}`.
3
+ *
4
+ * `code` is stable, for code to check (`scope_not_allowed`, `route_not_found`...). `message` is
5
+ * written for developers: show `userMessage`, or your own copy for the code, to users.
6
+ */
7
+ export declare class RemaplyApiError extends Error {
8
+ /** HTTP status; 0 when the SDK refused to send the request (`missing_api_key`, `secret_api_key`). */
9
+ readonly status: number;
10
+ readonly code: string;
11
+ readonly name = "RemaplyApiError";
12
+ constructor(
13
+ /** HTTP status; 0 when the SDK refused to send the request (`missing_api_key`, `secret_api_key`). */
14
+ status: number, code: string, message: string);
15
+ /** Copy fit to show to a user. */
16
+ get userMessage(): string;
17
+ }
18
+ /** Copy for the key errors every endpoint can return. Null for any other error. */
19
+ export declare function apiKeyErrorMessage(error: unknown): string | null;
20
+ /** Copy for a failed search. */
21
+ export declare function searchErrorMessage(error: unknown): string;
22
+ /** Copy for a failed route request. */
23
+ export declare function directionsErrorMessage(error: unknown): string;
24
+ /** True for a request cancelled through its AbortSignal: not an error to show. */
25
+ export declare function isCancelled(error: unknown): boolean;
26
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAGA;;;;;GAKG;AACH,qBAAa,eAAgB,SAAQ,KAAK;IAItC,qGAAqG;IACrG,QAAQ,CAAC,MAAM,EAAE,MAAM;IACvB,QAAQ,CAAC,IAAI,EAAE,MAAM;IALvB,SAAkB,IAAI,qBAAqB;;IAGzC,qGAAqG;IAC5F,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,MAAM,EACrB,OAAO,EAAE,MAAM;IAKjB,kCAAkC;IAClC,IAAI,WAAW,IAAI,MAAM,CAExB;CACF;AAED,mFAAmF;AACnF,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,IAAI,CAchE;AAYD,gCAAgC;AAChC,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAOzD;AAED,uCAAuC;AACvC,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAkB7D;AAED,kFAAkF;AAClF,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAEnD"}