@chaosity/location-client 0.8.0 → 0.10.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 +264 -42
- package/dist/adapters/GeoPlaces.d.ts +30 -3
- package/dist/adapters/GeoPlaces.js +5 -1
- package/dist/cjs/adapters/GeoPlaces.d.ts +30 -3
- package/dist/cjs/adapters/GeoPlaces.js +5 -4
- package/dist/cjs/client/GeoPlacesClient.d.ts +16 -4
- package/dist/cjs/client/GeoPlacesClient.js +24 -10
- package/dist/cjs/client/commands.d.ts +159 -0
- package/dist/cjs/client/commands.js +108 -0
- package/dist/cjs/errors/LocationServiceException.d.ts +28 -1
- package/dist/cjs/errors/LocationServiceException.js +31 -2
- package/dist/cjs/index.d.ts +5 -3
- package/dist/cjs/index.js +17 -1
- package/dist/cjs/maps/mapEnums.d.ts +82 -4
- package/dist/cjs/maps/mapEnums.js +98 -5
- package/dist/cjs/maps/mapStyle.d.ts +87 -10
- package/dist/cjs/maps/mapStyle.js +28 -5
- package/dist/cjs/maps/staticMap.d.ts +40 -0
- package/dist/cjs/maps/staticMap.js +4 -0
- package/dist/cjs/server/LocationServiceConnector.d.ts +23 -5
- package/dist/cjs/server/LocationServiceConnector.js +32 -14
- package/dist/cjs/transport/endpoints.js +3 -0
- package/dist/cjs/transport/http.d.ts +2 -2
- package/dist/cjs/transport/http.js +2 -2
- package/dist/cjs/types/index.d.ts +6 -5
- package/dist/cjs/utils/tokenClaims.d.ts +37 -13
- package/dist/cjs/utils/tokenClaims.js +36 -14
- package/dist/client/GeoPlacesClient.d.ts +16 -4
- package/dist/client/GeoPlacesClient.js +24 -10
- package/dist/client/commands.d.ts +159 -0
- package/dist/client/commands.js +97 -0
- package/dist/errors/LocationServiceException.d.ts +28 -1
- package/dist/errors/LocationServiceException.js +30 -1
- package/dist/index.d.ts +5 -3
- package/dist/index.js +7 -2
- package/dist/maps/mapEnums.d.ts +82 -4
- package/dist/maps/mapEnums.js +97 -4
- package/dist/maps/mapStyle.d.ts +87 -10
- package/dist/maps/mapStyle.js +28 -5
- package/dist/maps/staticMap.d.ts +40 -0
- package/dist/maps/staticMap.js +4 -0
- package/dist/server/LocationServiceConnector.d.ts +23 -5
- package/dist/server/LocationServiceConnector.js +32 -14
- package/dist/transport/endpoints.js +3 -0
- package/dist/transport/http.d.ts +2 -2
- package/dist/transport/http.js +2 -2
- package/dist/types/index.d.ts +6 -5
- package/dist/utils/tokenClaims.d.ts +37 -13
- package/dist/utils/tokenClaims.js +36 -14
- package/package.json +1 -1
- package/dist/cjs/utils/roundPosition.d.ts +0 -66
- package/dist/cjs/utils/roundPosition.js +0 -109
- package/dist/utils/roundPosition.d.ts +0 -66
- package/dist/utils/roundPosition.js +0 -104
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Round coordinate arrays in command inputs so nearby callers share a server
|
|
3
|
-
* cache entry.
|
|
4
|
-
*
|
|
5
|
-
* `BiasPosition` is a hint about where to look, so collapsing it onto a grid
|
|
6
|
-
* lets two users in the same area reuse one upstream answer. `QueryPosition`
|
|
7
|
-
* is not a hint — reverse-geocode resolves an exact point a user clicked or a
|
|
8
|
-
* device reported, and rounding it returns a neighbour's address. It is left
|
|
9
|
-
* alone (RFC-0001 §2).
|
|
10
|
-
*
|
|
11
|
-
* WHY THE DEFAULT MOVED FROM 2 dp TO 3 dp
|
|
12
|
-
*
|
|
13
|
-
* 2 dp is a ~1.1 km grid, and that is not a coarser answer — it is a wrong
|
|
14
|
-
* one. Measured against Amazon Location directly, `QueryText: "cafe"` from
|
|
15
|
-
* Sydney CBD returns a completely different set of places when the bias moves
|
|
16
|
-
* 111 m:
|
|
17
|
-
*
|
|
18
|
-
* base -> Crepe de Paris | Deli Ziosa | CBD Patisserie
|
|
19
|
-
* +111 m -> Cafe Chocolat | Paradiso Cafe | Incanto Coffee
|
|
20
|
-
*
|
|
21
|
-
* So two users a kilometre apart both received whichever places were nearest
|
|
22
|
-
* the FIRST of them, with nothing in the response saying so. The server moved
|
|
23
|
-
* to 3 dp in api#21 — but this library rounds BEFORE sending, on both the
|
|
24
|
-
* browser and server paths, so the precision was already gone by the time the
|
|
25
|
-
* request arrived and that fix never reached anyone using the SDK. This is
|
|
26
|
-
* what makes it reach them.
|
|
27
|
-
*
|
|
28
|
-
* The cost is hit rate: cells shrink 100x in area. RFC-0001 §5 expected only
|
|
29
|
-
* "single-digit to low-double-digit percent" in mixed traffic, so there was
|
|
30
|
-
* little to protect, and a miss costs one upstream call while a wrong answer
|
|
31
|
-
* costs trust.
|
|
32
|
-
*/
|
|
33
|
-
/** The server's floor (api#65). A request for less is clamped up to it. */
|
|
34
|
-
export const DEFAULT_BIAS_DECIMALS = 3;
|
|
35
|
-
/**
|
|
36
|
-
* The band the server enforces (api#65). Mirrored here so the value this
|
|
37
|
-
* library rounds to is the value the server will actually key its cache by —
|
|
38
|
-
* rounding to something outside the band just means being wrong about what
|
|
39
|
-
* was sent.
|
|
40
|
-
*
|
|
41
|
-
* The floor is correctness: below 3 dp the nearest places are not the ones
|
|
42
|
-
* returned. The ceiling is cost: cells shrink 100x in area per decimal, so
|
|
43
|
-
* finer precision collapses the cache hit rate and every miss is a billable
|
|
44
|
-
* upstream call.
|
|
45
|
-
*/
|
|
46
|
-
export const MIN_BIAS_DECIMALS = 3;
|
|
47
|
-
export const MAX_BIAS_DECIMALS = 5;
|
|
48
|
-
/**
|
|
49
|
-
* Clamp a requested precision into the allowed band.
|
|
50
|
-
*
|
|
51
|
-
* Anything absent or malformed becomes the default rather than throwing: this
|
|
52
|
-
* runs on every request, and a surprising token claim must not break geocoding
|
|
53
|
-
* for an application that is otherwise working.
|
|
54
|
-
*/
|
|
55
|
-
export function clampBiasDecimals(requested) {
|
|
56
|
-
const n = Number(requested);
|
|
57
|
-
if ((typeof requested !== 'number' &&
|
|
58
|
-
!(typeof requested === 'string' && requested.trim() !== '')) ||
|
|
59
|
-
!Number.isFinite(n)) {
|
|
60
|
-
return DEFAULT_BIAS_DECIMALS;
|
|
61
|
-
}
|
|
62
|
-
const floored = Math.floor(n);
|
|
63
|
-
if (floored < MIN_BIAS_DECIMALS)
|
|
64
|
-
return MIN_BIAS_DECIMALS;
|
|
65
|
-
if (floored > MAX_BIAS_DECIMALS)
|
|
66
|
-
return MAX_BIAS_DECIMALS;
|
|
67
|
-
return floored;
|
|
68
|
-
}
|
|
69
|
-
/** Known position field names and whether they should be rounded */
|
|
70
|
-
const POSITION_FIELDS = {
|
|
71
|
-
BiasPosition: true, // geocode, autocomplete, search — round for cache
|
|
72
|
-
QueryPosition: false, // reverse geocode — keep full precision
|
|
73
|
-
};
|
|
74
|
-
function roundCoord(value, decimals) {
|
|
75
|
-
const factor = 10 ** decimals;
|
|
76
|
-
return Math.round(value * factor) / factor;
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* Shallow-clone the input and round position arrays that benefit from caching.
|
|
80
|
-
* Returns the original object if no position fields are present.
|
|
81
|
-
*
|
|
82
|
-
* @param input the command input
|
|
83
|
-
* @param decimals precision for this application; defaults to the floor.
|
|
84
|
-
* Comes from the `biasDecimals` JWT claim where the token
|
|
85
|
-
* carries one (api#65) — an application entitled to finer
|
|
86
|
-
* bias gets it without the caller configuring anything.
|
|
87
|
-
*/
|
|
88
|
-
export function roundPositionFields(input, decimals = DEFAULT_BIAS_DECIMALS) {
|
|
89
|
-
if (!input || typeof input !== 'object')
|
|
90
|
-
return input;
|
|
91
|
-
const dp = clampBiasDecimals(decimals);
|
|
92
|
-
let cloned = null;
|
|
93
|
-
for (const [field, shouldRound] of Object.entries(POSITION_FIELDS)) {
|
|
94
|
-
if (!shouldRound)
|
|
95
|
-
continue;
|
|
96
|
-
const value = input[field];
|
|
97
|
-
if (!Array.isArray(value))
|
|
98
|
-
continue;
|
|
99
|
-
if (!cloned)
|
|
100
|
-
cloned = { ...input };
|
|
101
|
-
cloned[field] = value.map((v) => typeof v === 'number' && Number.isFinite(v) ? roundCoord(v, dp) : v);
|
|
102
|
-
}
|
|
103
|
-
return cloned ?? input;
|
|
104
|
-
}
|