@unmap/routing 0.0.1

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.
@@ -0,0 +1,45 @@
1
+ interface RouterOptions {
2
+ key: string;
3
+ gateway?: string;
4
+ fetch?: typeof fetch;
5
+ }
6
+ type TravelMode = "auto" | "bicycle" | "pedestrian";
7
+ /** [longitude, latitude]. */
8
+ type LngLat = [number, number];
9
+ interface RouteResult {
10
+ distanceMeters: number;
11
+ durationSeconds: number;
12
+ geometry: {
13
+ type: "LineString";
14
+ coordinates: LngLat[];
15
+ };
16
+ }
17
+ /** Minimal GeoJSON FeatureCollection (isochrone bands). */
18
+ interface FeatureCollection {
19
+ type: "FeatureCollection";
20
+ features: unknown[];
21
+ }
22
+ interface IsochroneOptions {
23
+ minutes: number[];
24
+ mode?: TravelMode;
25
+ signal?: AbortSignal;
26
+ }
27
+ declare class RouterError extends Error {
28
+ readonly status: number;
29
+ constructor(status: number, message: string);
30
+ }
31
+ /** Typed client for the unmap routing API (gateway `/route`, `/isochrone`). */
32
+ declare class Router {
33
+ private readonly key;
34
+ private readonly base;
35
+ private readonly fetchImpl;
36
+ constructor(opts: RouterOptions);
37
+ route(from: LngLat, to: LngLat, opts?: {
38
+ mode?: TravelMode;
39
+ signal?: AbortSignal;
40
+ }): Promise<RouteResult>;
41
+ isochrone(center: LngLat, opts: IsochroneOptions): Promise<FeatureCollection>;
42
+ private get;
43
+ }
44
+
45
+ export { type FeatureCollection, type IsochroneOptions, type LngLat, type RouteResult, Router, RouterError, type RouterOptions, type TravelMode };
package/dist/index.js ADDED
@@ -0,0 +1,49 @@
1
+ // src/index.ts
2
+ import { DEFAULT_GATEWAY } from "@unmap/core";
3
+ var RouterError = class extends Error {
4
+ constructor(status, message) {
5
+ super(message);
6
+ this.status = status;
7
+ this.name = "RouterError";
8
+ }
9
+ status;
10
+ };
11
+ var Router = class {
12
+ key;
13
+ base;
14
+ fetchImpl;
15
+ constructor(opts) {
16
+ this.key = opts.key;
17
+ this.base = (opts.gateway ?? DEFAULT_GATEWAY).replace(/\/+$/, "");
18
+ this.fetchImpl = opts.fetch ?? globalThis.fetch.bind(globalThis);
19
+ }
20
+ route(from, to, opts = {}) {
21
+ return this.get(
22
+ "/route",
23
+ { from: from.join(","), to: to.join(","), mode: opts.mode },
24
+ opts.signal
25
+ );
26
+ }
27
+ isochrone(center, opts) {
28
+ return this.get(
29
+ "/isochrone",
30
+ { lon: String(center[0]), lat: String(center[1]), minutes: opts.minutes.join(","), mode: opts.mode },
31
+ opts.signal
32
+ );
33
+ }
34
+ async get(path, params, signal) {
35
+ const q = new URLSearchParams({ key: this.key });
36
+ for (const [k, v] of Object.entries(params)) if (v != null && v !== "") q.set(k, v);
37
+ const res = await this.fetchImpl(`${this.base}${path}?${q}`, {
38
+ signal,
39
+ headers: { accept: "application/json" }
40
+ });
41
+ if (!res.ok) throw new RouterError(res.status, `route ${res.status} on ${path}`);
42
+ return res.json();
43
+ }
44
+ };
45
+ export {
46
+ Router,
47
+ RouterError
48
+ };
49
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { DEFAULT_GATEWAY } from \"@unmap/core\";\n\nexport interface RouterOptions {\n key: string;\n gateway?: string;\n fetch?: typeof fetch;\n}\n\nexport type TravelMode = \"auto\" | \"bicycle\" | \"pedestrian\";\n/** [longitude, latitude]. */\nexport type LngLat = [number, number];\n\nexport interface RouteResult {\n distanceMeters: number;\n durationSeconds: number;\n geometry: { type: \"LineString\"; coordinates: LngLat[] };\n}\n\n/** Minimal GeoJSON FeatureCollection (isochrone bands). */\nexport interface FeatureCollection {\n type: \"FeatureCollection\";\n features: unknown[];\n}\n\nexport interface IsochroneOptions {\n minutes: number[];\n mode?: TravelMode;\n signal?: AbortSignal;\n}\n\nexport class RouterError extends Error {\n constructor(\n public readonly status: number,\n message: string,\n ) {\n super(message);\n this.name = \"RouterError\";\n }\n}\n\n/** Typed client for the unmap routing API (gateway `/route`, `/isochrone`). */\nexport class Router {\n private readonly key: string;\n private readonly base: string;\n private readonly fetchImpl: typeof fetch;\n\n constructor(opts: RouterOptions) {\n this.key = opts.key;\n this.base = (opts.gateway ?? DEFAULT_GATEWAY).replace(/\\/+$/, \"\");\n // Bound to globalThis: stored on an instance and called as `this.fetchImpl(...)`, a bare\n // `fetch` reference is invoked with the Router as its receiver, and browsers reject that\n // with \"Illegal invocation\". Node is lenient, so this only ever surfaces in the browser.\n this.fetchImpl = opts.fetch ?? globalThis.fetch.bind(globalThis);\n }\n\n route(\n from: LngLat,\n to: LngLat,\n opts: { mode?: TravelMode; signal?: AbortSignal } = {},\n ): Promise<RouteResult> {\n return this.get<RouteResult>(\n \"/route\",\n { from: from.join(\",\"), to: to.join(\",\"), mode: opts.mode },\n opts.signal,\n );\n }\n\n isochrone(center: LngLat, opts: IsochroneOptions): Promise<FeatureCollection> {\n return this.get<FeatureCollection>(\n \"/isochrone\",\n { lon: String(center[0]), lat: String(center[1]), minutes: opts.minutes.join(\",\"), mode: opts.mode },\n opts.signal,\n );\n }\n\n private async get<T>(\n path: string,\n params: Record<string, string | undefined>,\n signal?: AbortSignal,\n ): Promise<T> {\n const q = new URLSearchParams({ key: this.key });\n for (const [k, v] of Object.entries(params)) if (v != null && v !== \"\") q.set(k, v);\n const res = await this.fetchImpl(`${this.base}${path}?${q}`, {\n signal,\n headers: { accept: \"application/json\" },\n });\n if (!res.ok) throw new RouterError(res.status, `route ${res.status} on ${path}`);\n return res.json() as Promise<T>;\n }\n}\n"],"mappings":";AAAA,SAAS,uBAAuB;AA8BzB,IAAM,cAAN,cAA0B,MAAM;AAAA,EACrC,YACkB,QAChB,SACA;AACA,UAAM,OAAO;AAHG;AAIhB,SAAK,OAAO;AAAA,EACd;AAAA,EALkB;AAMpB;AAGO,IAAM,SAAN,MAAa;AAAA,EACD;AAAA,EACA;AAAA,EACA;AAAA,EAEjB,YAAY,MAAqB;AAC/B,SAAK,MAAM,KAAK;AAChB,SAAK,QAAQ,KAAK,WAAW,iBAAiB,QAAQ,QAAQ,EAAE;AAIhE,SAAK,YAAY,KAAK,SAAS,WAAW,MAAM,KAAK,UAAU;AAAA,EACjE;AAAA,EAEA,MACE,MACA,IACA,OAAoD,CAAC,GAC/B;AACtB,WAAO,KAAK;AAAA,MACV;AAAA,MACA,EAAE,MAAM,KAAK,KAAK,GAAG,GAAG,IAAI,GAAG,KAAK,GAAG,GAAG,MAAM,KAAK,KAAK;AAAA,MAC1D,KAAK;AAAA,IACP;AAAA,EACF;AAAA,EAEA,UAAU,QAAgB,MAAoD;AAC5E,WAAO,KAAK;AAAA,MACV;AAAA,MACA,EAAE,KAAK,OAAO,OAAO,CAAC,CAAC,GAAG,KAAK,OAAO,OAAO,CAAC,CAAC,GAAG,SAAS,KAAK,QAAQ,KAAK,GAAG,GAAG,MAAM,KAAK,KAAK;AAAA,MACnG,KAAK;AAAA,IACP;AAAA,EACF;AAAA,EAEA,MAAc,IACZ,MACA,QACA,QACY;AACZ,UAAM,IAAI,IAAI,gBAAgB,EAAE,KAAK,KAAK,IAAI,CAAC;AAC/C,eAAW,CAAC,GAAG,CAAC,KAAK,OAAO,QAAQ,MAAM,EAAG,KAAI,KAAK,QAAQ,MAAM,GAAI,GAAE,IAAI,GAAG,CAAC;AAClF,UAAM,MAAM,MAAM,KAAK,UAAU,GAAG,KAAK,IAAI,GAAG,IAAI,IAAI,CAAC,IAAI;AAAA,MAC3D;AAAA,MACA,SAAS,EAAE,QAAQ,mBAAmB;AAAA,IACxC,CAAC;AACD,QAAI,CAAC,IAAI,GAAI,OAAM,IAAI,YAAY,IAAI,QAAQ,SAAS,IAAI,MAAM,OAAO,IAAI,EAAE;AAC/E,WAAO,IAAI,KAAK;AAAA,EAClB;AACF;","names":[]}
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@unmap/routing",
3
+ "version": "0.0.1",
4
+ "description": "Typed client for the unmap routing API (route + isochrone)",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "sideEffects": false,
19
+ "publishConfig": {
20
+ "access": "public"
21
+ },
22
+ "dependencies": {
23
+ "@unmap/core": "0.0.1"
24
+ },
25
+ "devDependencies": {
26
+ "tsup": "^8.3.0",
27
+ "typescript": "^5.6.0",
28
+ "vitest": "^2.1.0"
29
+ },
30
+ "scripts": {
31
+ "build": "tsup",
32
+ "test": "vitest run",
33
+ "typecheck": "tsc --noEmit"
34
+ }
35
+ }