@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.
Files changed (54) hide show
  1. package/README.md +264 -42
  2. package/dist/adapters/GeoPlaces.d.ts +30 -3
  3. package/dist/adapters/GeoPlaces.js +5 -1
  4. package/dist/cjs/adapters/GeoPlaces.d.ts +30 -3
  5. package/dist/cjs/adapters/GeoPlaces.js +5 -4
  6. package/dist/cjs/client/GeoPlacesClient.d.ts +16 -4
  7. package/dist/cjs/client/GeoPlacesClient.js +24 -10
  8. package/dist/cjs/client/commands.d.ts +159 -0
  9. package/dist/cjs/client/commands.js +108 -0
  10. package/dist/cjs/errors/LocationServiceException.d.ts +28 -1
  11. package/dist/cjs/errors/LocationServiceException.js +31 -2
  12. package/dist/cjs/index.d.ts +5 -3
  13. package/dist/cjs/index.js +17 -1
  14. package/dist/cjs/maps/mapEnums.d.ts +82 -4
  15. package/dist/cjs/maps/mapEnums.js +98 -5
  16. package/dist/cjs/maps/mapStyle.d.ts +87 -10
  17. package/dist/cjs/maps/mapStyle.js +28 -5
  18. package/dist/cjs/maps/staticMap.d.ts +40 -0
  19. package/dist/cjs/maps/staticMap.js +4 -0
  20. package/dist/cjs/server/LocationServiceConnector.d.ts +23 -5
  21. package/dist/cjs/server/LocationServiceConnector.js +32 -14
  22. package/dist/cjs/transport/endpoints.js +3 -0
  23. package/dist/cjs/transport/http.d.ts +2 -2
  24. package/dist/cjs/transport/http.js +2 -2
  25. package/dist/cjs/types/index.d.ts +6 -5
  26. package/dist/cjs/utils/tokenClaims.d.ts +37 -13
  27. package/dist/cjs/utils/tokenClaims.js +36 -14
  28. package/dist/client/GeoPlacesClient.d.ts +16 -4
  29. package/dist/client/GeoPlacesClient.js +24 -10
  30. package/dist/client/commands.d.ts +159 -0
  31. package/dist/client/commands.js +97 -0
  32. package/dist/errors/LocationServiceException.d.ts +28 -1
  33. package/dist/errors/LocationServiceException.js +30 -1
  34. package/dist/index.d.ts +5 -3
  35. package/dist/index.js +7 -2
  36. package/dist/maps/mapEnums.d.ts +82 -4
  37. package/dist/maps/mapEnums.js +97 -4
  38. package/dist/maps/mapStyle.d.ts +87 -10
  39. package/dist/maps/mapStyle.js +28 -5
  40. package/dist/maps/staticMap.d.ts +40 -0
  41. package/dist/maps/staticMap.js +4 -0
  42. package/dist/server/LocationServiceConnector.d.ts +23 -5
  43. package/dist/server/LocationServiceConnector.js +32 -14
  44. package/dist/transport/endpoints.js +3 -0
  45. package/dist/transport/http.d.ts +2 -2
  46. package/dist/transport/http.js +2 -2
  47. package/dist/types/index.d.ts +6 -5
  48. package/dist/utils/tokenClaims.d.ts +37 -13
  49. package/dist/utils/tokenClaims.js +36 -14
  50. package/package.json +1 -1
  51. package/dist/cjs/utils/roundPosition.d.ts +0 -66
  52. package/dist/cjs/utils/roundPosition.js +0 -109
  53. package/dist/utils/roundPosition.d.ts +0 -66
  54. 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
- }