@mapcreator/api 5.0.0-alpha.73 → 5.0.0-alpha.75

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.
@@ -1,6 +1,7 @@
1
- import { type ApiError, type ApiSuccess, getSearchParams, request } from '../utils.js';
1
+ import { type ApiError, type ApiSuccess, type Flatten, type Revivers, getSearchParams, request } from '../utils.js';
2
+
3
+ import type { RequireAtLeastOne } from 'type-fest';
2
4
  import type { Polygon } from 'geojson';
3
- import { RequireAtLeastOne } from 'type-fest';
4
5
 
5
6
  export type ApiSearchPoint = {
6
7
  lat: number;
@@ -14,47 +15,54 @@ export type ApiSearchBounds = {
14
15
  max_lng: number;
15
16
  };
16
17
 
17
- type ApiSingleOrGroupedArea = {
18
- id: number;
19
- title: string;
20
- subtitle: string;
21
- svg_preview: string;
22
- bounding_box: string;
23
- is_group: boolean;
24
- vector_source: string | null;
25
- source_layer: string | null;
26
- feature_id: number | null;
27
- properties: Record<string, string> | null;
28
- };
29
-
30
- type ApiSingleOrGroupedAreaArray = {
31
- data: ApiSingleOrGroupedArea[];
32
- } & Omit<ApiSuccess, 'data'> | ApiError;
33
-
34
18
  type SingleOrGroupedAreaBase = {
35
19
  id: number;
36
20
  title: string;
37
21
  subtitle: string;
38
22
  svgPreview: string;
39
23
  boundingBox: Polygon;
24
+ isGroup: boolean;
25
+ properties: Record<string, string> | null;
40
26
  };
41
27
 
42
28
  // TODO don't export this once search on click is out
43
- export type GroupedArea = SingleOrGroupedAreaBase & {
44
- isGroup: true;
45
- };
29
+ export type GroupedArea = SingleOrGroupedAreaBase;
46
30
 
47
31
  // TODO don't export this once search on click is out
48
32
  export type SingleArea = SingleOrGroupedAreaBase & {
49
- isGroup: false;
50
33
  vectorSource: string;
51
34
  sourceLayer: string;
52
35
  featureId: number;
53
- properties: Record<string, string>;
54
36
  };
55
37
 
56
38
  export type SingleOrGroupedArea = SingleArea | GroupedArea;
57
39
 
40
+ export type ApiSingleOrGroupedArea = {
41
+ data: {
42
+ id: number;
43
+ title: string;
44
+ subtitle: string;
45
+ svg_preview: string;
46
+ bounding_box: string;
47
+ is_group: boolean;
48
+ vector_source: string | null;
49
+ source_layer: string | null;
50
+ feature_id: number | null;
51
+ properties: string | null;
52
+ };
53
+ } & Omit<ApiSuccess, 'data'> | ApiError;
54
+
55
+ export type ApiSingleOrGroupedAreaData = Flatten<Exclude<ApiSingleOrGroupedArea, ApiError>['data']>;
56
+
57
+ export const singleOrGroupedAreaRevivers: Revivers<ApiSingleOrGroupedArea, SingleOrGroupedArea> = {
58
+ boundingBox: (data: ApiSingleOrGroupedAreaData) => JSON.parse(data.bounding_box) as Polygon,
59
+ properties: (data: ApiSingleOrGroupedAreaData) =>
60
+ (data.properties != null ? JSON.parse(data.properties) as Record<string, string> : null),
61
+ };
62
+
63
+ /**
64
+ * TODO When SAGA search on click is implemented, remove mode and make searchBounds required
65
+ */
58
66
  export async function searchSingleOrGroupedAreas(
59
67
  language: string,
60
68
  search: RequireAtLeastOne<{
@@ -64,43 +72,23 @@ export async function searchSingleOrGroupedAreas(
64
72
  }>,
65
73
  mode: 'polygon' | 'group' | 'both' = 'both',
66
74
  ): Promise<SingleOrGroupedArea[]> {
67
- /**
68
- * TODO When SAGA search on click is implemented, remove mode and make searchBounds required
69
- */
70
- return request<ApiSingleOrGroupedAreaArray, SingleOrGroupedArea>(
71
- `/v1/choropleth/polygons/search?${getSearchParams({
72
- language,
73
- ...search.searchBounds,
74
- ...(search.query && { query: search.query }),
75
- ...(search.searchPoint && { point: search.searchPoint }),
76
- mode,
77
- })}`,
78
- ).then(result => {
79
- result.forEach(
80
- elem => {
81
- elem.boundingBox = JSON.parse(elem.boundingBox as unknown as string) as Polygon;
82
- if (!elem.isGroup) {
83
- elem.properties = JSON.parse(elem.properties as unknown as string) as Record<string, string>;
84
- }
85
- },
86
- );
87
-
88
- return result;
75
+ const pathname = '/v1/choropleth/polygons/search';
76
+ const query = getSearchParams({
77
+ language,
78
+ ...search.searchBounds,
79
+ ...(search.query && { query: search.query }),
80
+ ...(search.searchPoint && { point: search.searchPoint }),
81
+ mode,
89
82
  });
90
- }
83
+ const path = `${pathname}?${query}`;
84
+ const options = { revivers: singleOrGroupedAreaRevivers };
91
85
 
92
- type ApiGroupedAreaChild = {
93
- id: number;
94
- title: string;
95
- vector_source: string;
96
- source_layer: string;
97
- feature_id: number;
98
- properties: Record<string, string>;
99
- };
86
+ type ApiSingleOrGroupedAreaArray = {
87
+ data: ApiSingleOrGroupedAreaData[];
88
+ } & Omit<ApiSuccess, 'data'> | ApiError;
100
89
 
101
- type ApiGroupedAreaChildArray = {
102
- data: ApiGroupedAreaChild[];
103
- } & Omit<ApiSuccess, 'data'> | ApiError;
90
+ return request<ApiSingleOrGroupedAreaArray, SingleOrGroupedArea>(path, null, null, options);
91
+ }
104
92
 
105
93
  export type GroupedAreaChild = {
106
94
  id: number;
@@ -108,19 +96,116 @@ export type GroupedAreaChild = {
108
96
  vectorSource: string;
109
97
  sourceLayer: string;
110
98
  featureId: number;
99
+ boundingBox: Polygon;
111
100
  properties: Record<string, string>;
112
101
  };
113
102
 
103
+ type ApiGroupedAreaChild = {
104
+ data: {
105
+ id: number;
106
+ title: string;
107
+ vector_source: string;
108
+ source_layer: string;
109
+ feature_id: number;
110
+ bounding_box: string;
111
+ properties: string;
112
+ };
113
+ } & Omit<ApiSuccess, 'data'> | ApiError;
114
+
115
+ export type ApiGroupedAreaChildData = Flatten<Exclude<ApiGroupedAreaChild, ApiError>['data']>;
116
+
117
+ export const groupedAreaChildRevivers: Revivers<ApiGroupedAreaChild, GroupedAreaChild> = {
118
+ boundingBox: (data: ApiGroupedAreaChildData) => JSON.parse(data.bounding_box) as Polygon,
119
+ properties: (data: ApiGroupedAreaChildData) => JSON.parse(data.properties) as Record<string, string>,
120
+ };
121
+
114
122
  export async function groupedAreaChildren(groupId: number, language: string): Promise<GroupedAreaChild[]> {
115
- return request<ApiGroupedAreaChildArray, GroupedAreaChild>(
116
- `/v1/choropleth/groups/${groupId}/children-optimized?${getSearchParams({ language })}`,
117
- ).then(result => {
118
- result.forEach(
119
- elem => {
120
- elem.properties = JSON.parse(elem.properties as unknown as string) as Record<string, string>;
121
- }
122
- );
123
-
124
- return result;
125
- });
123
+ const pathname = `/v1/choropleth/groups/${groupId}/children-optimized`;
124
+ const query = getSearchParams({ language });
125
+ const path = `${pathname}?${query}`;
126
+ const options = { revivers: groupedAreaChildRevivers };
127
+
128
+ type ApiApiGroupedAreaChildArray = {
129
+ data: ApiGroupedAreaChildData[];
130
+ } & Omit<ApiSuccess, 'data'> | ApiError;
131
+
132
+ return request<ApiApiGroupedAreaChildArray, GroupedAreaChild>(path, null, null, options);
133
+ }
134
+
135
+ export type MatchedGroup = {
136
+ id: number;
137
+ sml: number;
138
+ childrenCount: number;
139
+ matchField: string;
140
+ property: string;
141
+ name: string;
142
+ };
143
+
144
+ export type ApiMatchedGroup = {
145
+ data: {
146
+ id: number;
147
+ sml: number;
148
+ children_count: number;
149
+ match_field: string;
150
+ property: string;
151
+ name: string;
152
+ };
153
+ } & Omit<ApiSuccess, 'data'> | ApiError;
154
+
155
+ export type ApiMatchedGroupData = Flatten<Exclude<ApiMatchedGroup, ApiError>['data']>;
156
+
157
+ export async function getGroupsByDataSample(
158
+ sample: Record<string, string[]>,
159
+ language: string,
160
+ ): Promise<MatchedGroup[]> {
161
+ const path = `/v1/choropleth/groups/sample`;
162
+
163
+ type ApiMatchedGroupArray = {
164
+ data: ApiMatchedGroupData[];
165
+ } & Omit<ApiSuccess, 'data'> | ApiError;
166
+
167
+ return request<ApiMatchedGroupArray, MatchedGroup>(path, { sample, language });
168
+ }
169
+
170
+ export type BoundPolygon = {
171
+ index: number;
172
+ id: number;
173
+ sml: number;
174
+ name: string;
175
+ inputName: string;
176
+ boundingBox: Polygon;
177
+ };
178
+
179
+ export type ApiBoundPolygon = {
180
+ data: {
181
+ index: number;
182
+ id: number;
183
+ sml: number;
184
+ name: string;
185
+ input_name: string;
186
+ bounding_box: string;
187
+ };
188
+ } & Omit<ApiSuccess, 'data'> | ApiError;
189
+
190
+ export type ApiBoundPolygonData = Flatten<Exclude<ApiBoundPolygon, ApiError>['data']>;
191
+
192
+ export const boundPolygonRevivers: Revivers<ApiBoundPolygon, BoundPolygon> = {
193
+ boundingBox: (data: ApiBoundPolygonData) => JSON.parse(data.bounding_box) as Polygon,
194
+ };
195
+
196
+ export async function getBoundPolygons(
197
+ groupId: number,
198
+ property: string,
199
+ data: string[],
200
+ language: string,
201
+ ): Promise<BoundPolygon[]> {
202
+ const path = `/v1/choropleth/groups/bind`;
203
+ const body = { group_id: groupId, property, data, language };
204
+ const options = { revivers: boundPolygonRevivers };
205
+
206
+ type ApiBoundPolygonArray = {
207
+ data: ApiBoundPolygonData[];
208
+ } & Omit<ApiSuccess, 'data'> | ApiError;
209
+
210
+ return request<ApiBoundPolygonArray, BoundPolygon>(path, body, null, options);
126
211
  }