@mapcreator/api 5.1.0 → 5.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.
package/src/README.md CHANGED
@@ -1,126 +1,126 @@
1
- ### Used type system
2
-
3
- We use type declarations for both the data coming over the wire and the data used by the application. In general, these types differ only in the presence of additional fields in the data arriving over the network (like `created_at`), and used naming convention. Data over the network uses the so-called snake case.
4
-
5
- All in all, we can use TypeScript's native mechanisms to convert one type to another and fully define just one type:
6
-
7
- ```typescript
8
- import type { CamelCasedProperties } from 'type-fest';
9
-
10
- type ApiType =
11
- | ({
12
- data: {
13
- prop: unknown;
14
- } & ApiCommonData;
15
- } & Omit<ApiSuccess, 'data'>)
16
- | ApiError;
17
-
18
- type AppType = CamelCasedProperties<Omit<Exclude<ApiType, ApiError>['data'], keyof ApiCommonData>>;
19
- ```
20
-
21
- or in reverse order:
22
-
23
- ```typescript
24
- import type { SnakeCasedProperties } from 'type-fest';
25
-
26
- type AppType = {
27
- prop: unknown;
28
- };
29
-
30
- type ApiType =
31
- | ({
32
- data: SnakeCasedProperties<AppType> & ApiCommonData;
33
- } & Omit<ApiSuccess, 'data'>)
34
- | ApiError;
35
- ```
36
-
37
- But the decision was made not to do this, since it may be more difficult for the developer to make the conversion in their head than to see it in front of their eyes.
38
-
39
- ### Using a `request()`
40
-
41
- The function has the following signature:
42
-
43
- ```typescript
44
- async function request<I extends ApiCommon, O extends Record<string, unknown> | string>(
45
- path: string,
46
- body?: XMLHttpRequestBodyInit | Record<string | number, unknown> | null,
47
- extraHeaders?: Record<string, string> | null,
48
- extraOptions?: ExtraOptions<I, O>,
49
- ): Promise<O | O[]> { /* ... */ }
50
- ```
51
-
52
- Let's take it step by step.
53
-
54
- `I extends ApiCommon` - represents the type of data we receive over the network.
55
-
56
- `O extends Record<string, unknown> | string` - represents the data type that will be used in the application.
57
-
58
- Ideally you should describe and convey both types. This will help to check the data types in the arguments passed.
59
- See current data types for an example.
60
-
61
- `path: string` - the path to the resource, must include the API version, but must not include the schema or authority.
62
- Example: `/v1/jobs/12345`
63
-
64
- `body?: XMLHttpRequestBodyInit | Record<string | number, unknown> | null` - any meaningful body type. In general,
65
- the presence of an JSON object is assumed (or the absence of one for methods that only request data), but you can
66
- also pass `Blob`, `FormData`, `URLSearchParams` or just `ArrayBuffer`. The required content type will be added to
67
- the headers automatically.
68
-
69
- `extraHeaders?: Record<string, string> | null` - the object with additional headers.
70
-
71
- `extraOptions?: ExtraOptions<I, O>` - where `ExtraOptions<I, O>` is defined like this:
72
-
73
- ```typescript
74
- interface ExtraOptions<I extends ApiCommon, O extends Record<string, unknown> | string> {
75
- method?: 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
76
- revivers?: O extends Record<string, unknown> ? Revivers<I, O> : never;
77
- sendNull?: boolean;
78
- withMeta?: boolean;
79
- }
80
- ```
81
-
82
- Most fields are self-explanatory.
83
-
84
- `sendNull` can be used if you really want to pass `null` as body content.
85
-
86
- `revivers` is used to specify an object that can modify the behavior of the internal handler of data coming over
87
- the network. Let's take a closer look at this moment.
88
-
89
- #### Revivers
90
-
91
- By default, the `request()` function does the following things with data coming over the network:
92
-
93
- - It removes `created_at`, `updated_at`, `deleted_at` fields from the output objects.
94
- - It preserves all the remaining fields but converts their names into camelCase.
95
-
96
- When passing an object with revivers you can a couple of things:
97
-
98
- - You can list the fields that you want **to exclude** from the result object. To do this, the field must be assigned an
99
- `undefined` value.
100
- - You can **add** new fields or **modify** the type of existing ones. To do this, you need to pass a function as a field
101
- value, which will receive the original object as input.
102
-
103
- Example:
104
-
105
- ```typescript
106
- const jobRevivers: Revivers<ApiJob, Job> = {
107
- user_id: undefined,
108
- description: undefined,
109
- share_token: undefined,
110
- autosave_preview_path: undefined,
111
- job_folder_id: undefined,
112
-
113
- jobTypeId: () => 9,
114
- createdAt: (data: ApiJobData) => data.created_at as string,
115
- previewPath: (data: ApiJobData) => data.autosave_preview_path ?? undefined,
116
- };
117
- ```
118
-
119
- `user_id`, `description`, `share_token`, `autosave_preview_path`, `job_folder_id` fields will be excluded from the
120
- result object.
121
-
122
- `jobTypeId` will be always **9**.
123
-
124
- `createdAt` will be returned (please note that that field is excluded by default)
125
-
126
- `previewPath` - some actions will be performed with the source data.
1
+ ### Used type system
2
+
3
+ We use type declarations for both the data coming over the wire and the data used by the application. In general, these types differ only in the presence of additional fields in the data arriving over the network (like `created_at`), and used naming convention. Data over the network uses the so-called snake case.
4
+
5
+ All in all, we can use TypeScript's native mechanisms to convert one type to another and fully define just one type:
6
+
7
+ ```typescript
8
+ import type { CamelCasedProperties } from 'type-fest';
9
+
10
+ type ApiType =
11
+ | ({
12
+ data: {
13
+ prop: unknown;
14
+ } & ApiCommonData;
15
+ } & Omit<ApiSuccess, 'data'>)
16
+ | ApiError;
17
+
18
+ type AppType = CamelCasedProperties<Omit<Exclude<ApiType, ApiError>['data'], keyof ApiCommonData>>;
19
+ ```
20
+
21
+ or in reverse order:
22
+
23
+ ```typescript
24
+ import type { SnakeCasedProperties } from 'type-fest';
25
+
26
+ type AppType = {
27
+ prop: unknown;
28
+ };
29
+
30
+ type ApiType =
31
+ | ({
32
+ data: SnakeCasedProperties<AppType> & ApiCommonData;
33
+ } & Omit<ApiSuccess, 'data'>)
34
+ | ApiError;
35
+ ```
36
+
37
+ But the decision was made not to do this, since it may be more difficult for the developer to make the conversion in their head than to see it in front of their eyes.
38
+
39
+ ### Using a `request()`
40
+
41
+ The function has the following signature:
42
+
43
+ ```typescript
44
+ async function request<I extends ApiCommon, O extends Record<string, unknown> | string>(
45
+ path: string,
46
+ body?: XMLHttpRequestBodyInit | Record<string | number, unknown> | null,
47
+ extraHeaders?: Record<string, string> | null,
48
+ extraOptions?: ExtraOptions<I, O>,
49
+ ): Promise<O | O[]> { /* ... */ }
50
+ ```
51
+
52
+ Let's take it step by step.
53
+
54
+ `I extends ApiCommon` - represents the type of data we receive over the network.
55
+
56
+ `O extends Record<string, unknown> | string` - represents the data type that will be used in the application.
57
+
58
+ Ideally you should describe and convey both types. This will help to check the data types in the arguments passed.
59
+ See current data types for an example.
60
+
61
+ `path: string` - the path to the resource, must include the API version, but must not include the schema or authority.
62
+ Example: `/v1/jobs/12345`
63
+
64
+ `body?: XMLHttpRequestBodyInit | Record<string | number, unknown> | null` - any meaningful body type. In general,
65
+ the presence of an JSON object is assumed (or the absence of one for methods that only request data), but you can
66
+ also pass `Blob`, `FormData`, `URLSearchParams` or just `ArrayBuffer`. The required content type will be added to
67
+ the headers automatically.
68
+
69
+ `extraHeaders?: Record<string, string> | null` - the object with additional headers.
70
+
71
+ `extraOptions?: ExtraOptions<I, O>` - where `ExtraOptions<I, O>` is defined like this:
72
+
73
+ ```typescript
74
+ interface ExtraOptions<I extends ApiCommon, O extends Record<string, unknown> | string> {
75
+ method?: 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
76
+ revivers?: O extends Record<string, unknown> ? Revivers<I, O> : never;
77
+ sendNull?: boolean;
78
+ withMeta?: boolean;
79
+ }
80
+ ```
81
+
82
+ Most fields are self-explanatory.
83
+
84
+ `sendNull` can be used if you really want to pass `null` as body content.
85
+
86
+ `revivers` is used to specify an object that can modify the behavior of the internal handler of data coming over
87
+ the network. Let's take a closer look at this moment.
88
+
89
+ #### Revivers
90
+
91
+ By default, the `request()` function does the following things with data coming over the network:
92
+
93
+ - It removes `created_at`, `updated_at`, `deleted_at` fields from the output objects.
94
+ - It preserves all the remaining fields but converts their names into camelCase.
95
+
96
+ When passing an object with revivers you can a couple of things:
97
+
98
+ - You can list the fields that you want **to exclude** from the result object. To do this, the field must be assigned an
99
+ `undefined` value.
100
+ - You can **add** new fields or **modify** the type of existing ones. To do this, you need to pass a function as a field
101
+ value, which will receive the original object as input.
102
+
103
+ Example:
104
+
105
+ ```typescript
106
+ const jobRevivers: Revivers<ApiJob, Job> = {
107
+ user_id: undefined,
108
+ description: undefined,
109
+ share_token: undefined,
110
+ autosave_preview_path: undefined,
111
+ job_folder_id: undefined,
112
+
113
+ jobTypeId: () => 9,
114
+ createdAt: (data: ApiJobData) => data.created_at as string,
115
+ previewPath: (data: ApiJobData) => data.autosave_preview_path ?? undefined,
116
+ };
117
+ ```
118
+
119
+ `user_id`, `description`, `share_token`, `autosave_preview_path`, `job_folder_id` fields will be excluded from the
120
+ result object.
121
+
122
+ `jobTypeId` will be always **9**.
123
+
124
+ `createdAt` will be returned (please note that that field is excluded by default)
125
+
126
+ `previewPath` - some actions will be performed with the source data.
@@ -10,6 +10,8 @@ export const boundingBoxRevivers: Revivers<
10
10
  boundingBox: (data: { bounding_box: string }) => JSON.parse(data.bounding_box) as Polygon,
11
11
  };
12
12
 
13
+ export type SAGALanguage = 'en' | 'da' | 'nl' | 'fi' | 'fr' | 'de' | 'it' | 'no' | 'pl' | 'pt' | 'es' | 'sv' | 'ja' | 'zh' | 'ru' | 'uk' | 'lv' | 'kk' | '';
14
+
13
15
  export type ApiSearchPoint = {
14
16
  lat: number;
15
17
  lng: number;
@@ -30,12 +32,10 @@ type SingleOrGroupedAreaBase = {
30
32
  boundingBox: Polygon;
31
33
  };
32
34
 
33
- // TODO don't export this once search on click is out
34
35
  export type GroupedArea = SingleOrGroupedAreaBase & {
35
36
  isGroup: true;
36
37
  };
37
38
 
38
- // TODO don't export this once search on click is out
39
39
  export type SingleArea = SingleOrGroupedAreaBase & {
40
40
  isGroup: false;
41
41
  properties: Record<string, string>;
@@ -44,70 +44,100 @@ export type SingleArea = SingleOrGroupedAreaBase & {
44
44
  featureId: number;
45
45
  };
46
46
 
47
- export type SingleOrGroupedArea = {
48
- id: number;
49
- title: string;
50
- subtitle: string;
51
- svgPreview: string;
52
- boundingBox: Polygon;
53
- isGroup: boolean;
54
- properties: Record<string, string> | null;
55
- vectorSource: string | null;
56
- sourceLayer: string | null;
57
- featureId: number | null;
58
- };
47
+ type ApiSingleArea = {
48
+ data: {
49
+ id: number;
50
+ title: string;
51
+ subtitle: string;
52
+ svg_preview: string;
53
+ bounding_box: string;
54
+ is_group: false;
55
+ properties: string;
56
+ vector_source: string;
57
+ source_layer: string;
58
+ feature_id: number;
59
+ };
60
+ } & Omit<ApiSuccess, 'data'> | ApiError;
61
+
62
+ export type ApiSingleAreaData = Flatten<Exclude<ApiSingleArea, ApiError>['data']>;
63
+
64
+ export type ApiSingleAreaArray = {
65
+ data: ApiSingleAreaData[];
66
+ } & Omit<ApiSuccess, 'data'> | ApiError;
59
67
 
60
- export type ApiSingleOrGroupedArea = {
68
+ type ApiGroupedArea = {
61
69
  data: {
62
70
  id: number;
63
71
  title: string;
64
72
  subtitle: string;
65
73
  svg_preview: string;
66
74
  bounding_box: string;
67
- is_group: boolean;
68
- properties: string | null;
69
- vector_source: string | null;
70
- source_layer: string | null;
71
- feature_id: number | null;
75
+ is_group: true;
76
+ properties: null;
77
+ vector_source: null;
78
+ source_layer: null;
79
+ feature_id: null;
72
80
  };
73
81
  } & Omit<ApiSuccess, 'data'> | ApiError;
74
82
 
75
- export type ApiSingleOrGroupedAreaData = Flatten<Exclude<ApiSingleOrGroupedArea, ApiError>['data']>;
83
+ export type ApiGroupedAreaData = Flatten<Exclude<ApiGroupedArea, ApiError>['data']>;
84
+
85
+ export type ApiGroupedAreaArray = {
86
+ data: ApiGroupedAreaData[];
87
+ } & Omit<ApiSuccess, 'data'> | ApiError;
88
+
89
+ export const singleAreaRevivers: Revivers<ApiSingleArea, SingleArea> = {
90
+ ...boundingBoxRevivers,
91
+ properties: (data: ApiSingleAreaData) =>
92
+ JSON.parse(data.properties) as Record<string, string>,
93
+ };
76
94
 
77
- export const singleOrGroupedAreaRevivers: Revivers<ApiSingleOrGroupedArea, SingleOrGroupedArea> = {
95
+ export const groupedAreaRevivers: Revivers<ApiGroupedArea, GroupedArea> = {
78
96
  ...boundingBoxRevivers,
79
- properties: (data: ApiSingleOrGroupedAreaData) =>
80
- (data.properties != null ? JSON.parse(data.properties) as Record<string, string> : null),
81
97
  };
82
98
 
83
- /**
84
- * TODO When SAGA search on click is implemented, remove mode and make searchBounds required
85
- */
86
- export async function searchSingleOrGroupedAreas(
87
- language: string,
99
+ export async function searchSingleAreas(
100
+ language: SAGALanguage,
88
101
  search: RequireAtLeastOne<{
89
- searchBounds?: ApiSearchBounds;
102
+ searchBounds: ApiSearchBounds;
90
103
  query?: string;
91
104
  searchPoint?: ApiSearchPoint;
92
105
  }>,
93
- mode: 'polygon' | 'group' | 'both' = 'both',
94
- ): Promise<SingleOrGroupedArea[]> {
106
+ ): Promise<SingleArea[]> {
95
107
  const pathname = '/v1/choropleth/polygons/search';
96
108
  const query = getSearchParams({
97
109
  language,
98
110
  ...search.searchBounds,
99
111
  ...(search.query && { query: search.query }),
100
112
  ...(search.searchPoint && { point: search.searchPoint }),
101
- mode,
113
+ mode: 'polygon',
102
114
  });
103
115
  const path = `${pathname}?${query}`;
104
- const options = { revivers: singleOrGroupedAreaRevivers };
116
+ const options = { revivers: singleAreaRevivers };
105
117
 
106
- type ApiSingleOrGroupedAreaArray = {
107
- data: ApiSingleOrGroupedAreaData[];
108
- } & Omit<ApiSuccess, 'data'> | ApiError;
118
+ return request<ApiSingleAreaArray, SingleArea>(path, null, null, options);
119
+ }
120
+
121
+ export async function searchGroupedAreas(
122
+ language: SAGALanguage,
123
+ search: RequireAtLeastOne<{
124
+ searchBounds: ApiSearchBounds;
125
+ query?: string;
126
+ searchPoint?: ApiSearchPoint;
127
+ }>,
128
+ ): Promise<GroupedArea[]> {
129
+ const pathname = '/v1/choropleth/polygons/search';
130
+ const query = getSearchParams({
131
+ language,
132
+ ...search.searchBounds,
133
+ ...(search.query && { query: search.query }),
134
+ ...(search.searchPoint && { point: search.searchPoint }),
135
+ mode: 'group',
136
+ });
137
+ const path = `${pathname}?${query}`;
138
+ const options = { revivers: groupedAreaRevivers };
109
139
 
110
- return request<ApiSingleOrGroupedAreaArray, SingleOrGroupedArea>(path, null, null, options);
140
+ return request<ApiGroupedAreaArray, GroupedArea>(path, null, null, options);
111
141
  }
112
142
 
113
143
  export type GroupedAreaChild = {
@@ -139,7 +169,7 @@ export const groupedAreaChildRevivers: Revivers<ApiGroupedAreaChild, GroupedArea
139
169
  properties: (data: ApiGroupedAreaChildData) => JSON.parse(data.properties) as Record<string, string>,
140
170
  };
141
171
 
142
- export async function groupedAreaChildren(groupId: number, language: string): Promise<GroupedAreaChild[]> {
172
+ export async function groupedAreaChildren(groupId: number, language: SAGALanguage): Promise<GroupedAreaChild[]> {
143
173
  const pathname = `/v1/choropleth/groups/${groupId}/children-optimized`;
144
174
  const query = getSearchParams({ language });
145
175
  const path = `${pathname}?${query}`;
@@ -178,7 +208,7 @@ export type ApiMatchedGroupData = Flatten<Exclude<ApiMatchedGroup, ApiError>['da
178
208
 
179
209
  export async function getGroupsByDataSample(
180
210
  sample: Record<string, string[]>,
181
- language: string,
211
+ language: SAGALanguage,
182
212
  rowCount: number,
183
213
  ): Promise<MatchedGroup[]> {
184
214
  const path = `/v1/choropleth/groups/sample`;
@@ -215,7 +245,7 @@ export async function getBoundPolygons(
215
245
  groupId: number,
216
246
  property: string,
217
247
  data: string[],
218
- language: string,
248
+ language: SAGALanguage,
219
249
  ): Promise<BoundPolygon[]> {
220
250
  const path = `/v1/choropleth/groups/bind`;
221
251
  const body = { group_id: groupId, property, data, language };
@@ -6,19 +6,18 @@ import { type ApiDimensionData, type Dimension, dimensionRevivers } from './dime
6
6
  import { type ApiFeatureData, type Feature, featureRevivers } from './feature.js';
7
7
  import { type ApiJobTypeData, type JobType, jobTypeRevivers } from './jobType.js';
8
8
  import { type ApiMessageData, type Message, messageRevivers } from './message.js';
9
- import { type ApiSvg, type ApiSvgData, type Svg, svgRevivers } from './svg.js';
10
9
  import { type ApiSvgSetData, type SvgSet, svgSetRevivers } from './svgSet.js';
11
10
  import { type ApiLayerData, type Layer, layerRevivers } from './layer.js';
12
11
  import { type ApiColorData, type Color, colorRevivers } from './color.js';
13
12
  import { type ApiUserData, type User, userRevivers } from './user.js';
14
13
  import { type ApiFontData, type Font, fontRevivers } from './font.js';
14
+ import { type ApiSvgData, type Svg, svgRevivers } from './svg.js';
15
15
  import type { CamelCasedProperties } from 'type-fest';
16
16
  import {
17
17
  type ApiCommon,
18
18
  type ApiCommonData,
19
19
  type ApiError,
20
20
  type ApiSuccess,
21
- type Revivers,
22
21
  getContext,
23
22
  myUser,
24
23
  processData,
@@ -112,11 +111,6 @@ export async function loadResources(): Promise<Resources> {
112
111
 
113
112
  allMessages.sort((m1, m2) => m2.date.localeCompare(m1.date));
114
113
 
115
- const mcSvgRevivers: Revivers<ApiSvg, Svg> = {
116
- ...svgRevivers,
117
- name: (data: ApiSvgData) => `mc-image-${data.name}`,
118
- };
119
-
120
114
  return {
121
115
  // @ts-expect-error TS2345
122
116
  user: processData.call(getContext(userRevivers), user) as User,
@@ -127,7 +121,7 @@ export async function loadResources(): Promise<Resources> {
127
121
  feature => feature.name,
128
122
  ) as CustomFeature[],
129
123
  colors: raw.colors.map(processData, getContext(colorRevivers)) as Color[],
130
- svgs: raw.svgs.map(processData, getContext(mcSvgRevivers)) as Svg[],
124
+ svgs: raw.svgs.map(processData, getContext(svgRevivers)) as Svg[],
131
125
  dimensions: raw.dimensions.map(processData, getContext(dimensionRevivers)) as Dimension[],
132
126
  dimensionSets: raw.dimensionSets.map(processData, getContext(dimensionSetRevivers)) as DimensionSet[],
133
127
  mapstyleSets: raw.mapstyleSets.map(processData, getContext(mapstyleSetRevivers)) as MapstyleSet[],