@mapcreator/api 5.0.0-alpha.84 → 5.0.0-alpha.85

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.
@@ -1,14 +1,6 @@
1
- import { type ApiError, type ApiSuccess, type Flatten, type Revivers, getSearchParams, request } from '../utils.js';
2
-
3
- import type { RequireAtLeastOne } from 'type-fest';
1
+ import { type ApiError, type ApiSuccess, Flatten, Revivers, getSearchParams, request } from '../utils.js';
4
2
  import type { Polygon } from 'geojson';
5
-
6
- export const boundingBoxRevivers: Revivers<
7
- { data: { bounding_box: string } } & Omit<ApiSuccess, 'data'> | ApiError,
8
- { boundingBox: Polygon }
9
- > = {
10
- boundingBox: (data: { bounding_box: string }) => JSON.parse(data.bounding_box) as Polygon,
11
- };
3
+ import { RequireAtLeastOne } from 'type-fest';
12
4
 
13
5
  export type ApiSearchPoint = {
14
6
  lat: number;
@@ -22,6 +14,23 @@ export type ApiSearchBounds = {
22
14
  max_lng: number;
23
15
  };
24
16
 
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
+
25
34
  type SingleOrGroupedAreaBase = {
26
35
  id: number;
27
36
  title: string;
@@ -38,51 +47,14 @@ export type GroupedArea = SingleOrGroupedAreaBase & {
38
47
  // TODO don't export this once search on click is out
39
48
  export type SingleArea = SingleOrGroupedAreaBase & {
40
49
  isGroup: false;
41
- properties: Record<string, string>;
42
50
  vectorSource: string;
43
51
  sourceLayer: string;
44
52
  featureId: number;
53
+ properties: Record<string, string>;
45
54
  };
46
55
 
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
- };
59
-
60
- export type ApiSingleOrGroupedArea = {
61
- data: {
62
- id: number;
63
- title: string;
64
- subtitle: string;
65
- svg_preview: string;
66
- 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;
72
- };
73
- } & Omit<ApiSuccess, 'data'> | ApiError;
74
-
75
- export type ApiSingleOrGroupedAreaData = Flatten<Exclude<ApiSingleOrGroupedArea, ApiError>['data']>;
76
-
77
- export const singleOrGroupedAreaRevivers: Revivers<ApiSingleOrGroupedArea, SingleOrGroupedArea> = {
78
- ...boundingBoxRevivers,
79
- properties: (data: ApiSingleOrGroupedAreaData) =>
80
- (data.properties != null ? JSON.parse(data.properties) as Record<string, string> : null),
81
- };
56
+ export type SingleOrGroupedArea = SingleArea | GroupedArea;
82
57
 
83
- /**
84
- * TODO When SAGA search on click is implemented, remove mode and make searchBounds required
85
- */
86
58
  export async function searchSingleOrGroupedAreas(
87
59
  language: string,
88
60
  search: RequireAtLeastOne<{
@@ -92,22 +64,29 @@ export async function searchSingleOrGroupedAreas(
92
64
  }>,
93
65
  mode: 'polygon' | 'group' | 'both' = 'both',
94
66
  ): Promise<SingleOrGroupedArea[]> {
95
- const pathname = '/v1/choropleth/polygons/search';
96
- const query = getSearchParams({
97
- language,
98
- ...search.searchBounds,
99
- ...(search.query && { query: search.query }),
100
- ...(search.searchPoint && { point: search.searchPoint }),
101
- mode,
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;
102
89
  });
103
- const path = `${pathname}?${query}`;
104
- const options = { revivers: singleOrGroupedAreaRevivers };
105
-
106
- type ApiSingleOrGroupedAreaArray = {
107
- data: ApiSingleOrGroupedAreaData[];
108
- } & Omit<ApiSuccess, 'data'> | ApiError;
109
-
110
- return request<ApiSingleOrGroupedAreaArray, SingleOrGroupedArea>(path, null, null, options);
111
90
  }
112
91
 
113
92
  export type GroupedAreaChild = {
@@ -135,7 +114,7 @@ type ApiGroupedAreaChild = {
135
114
  export type ApiGroupedAreaChildData = Flatten<Exclude<ApiGroupedAreaChild, ApiError>['data']>;
136
115
 
137
116
  export const groupedAreaChildRevivers: Revivers<ApiGroupedAreaChild, GroupedAreaChild> = {
138
- ...boundingBoxRevivers,
117
+ boundingBox: (data: ApiGroupedAreaChildData) => JSON.parse(data.bounding_box) as Polygon,
139
118
  properties: (data: ApiGroupedAreaChildData) => JSON.parse(data.properties) as Record<string, string>,
140
119
  };
141
120
 
@@ -151,78 +130,3 @@ export async function groupedAreaChildren(groupId: number, language: string): Pr
151
130
 
152
131
  return request<ApiApiGroupedAreaChildArray, GroupedAreaChild>(path, null, null, options);
153
132
  }
154
-
155
- export type MatchedGroup = {
156
- id: number;
157
- sml: number;
158
- childrenCount: number;
159
- matchField: string;
160
- boundingBox: Polygon;
161
- property: string;
162
- name: string;
163
- };
164
-
165
- export type ApiMatchedGroup = {
166
- data: {
167
- id: number;
168
- sml: number;
169
- children_count: number;
170
- match_field: string;
171
- bounding_box: string;
172
- property: string;
173
- name: string;
174
- };
175
- } & Omit<ApiSuccess, 'data'> | ApiError;
176
-
177
- export type ApiMatchedGroupData = Flatten<Exclude<ApiMatchedGroup, ApiError>['data']>;
178
-
179
- export async function getGroupsByDataSample(
180
- sample: Record<string, string[]>,
181
- language: string,
182
- rowCount: number,
183
- ): Promise<MatchedGroup[]> {
184
- const path = `/v1/choropleth/groups/sample`;
185
- const options = { revivers: boundingBoxRevivers };
186
-
187
- type ApiMatchedGroupArray = {
188
- data: ApiMatchedGroupData[];
189
- } & Omit<ApiSuccess, 'data'> | ApiError;
190
-
191
- return request<ApiMatchedGroupArray, MatchedGroup>(path, { sample, language, row_count: rowCount }, null, options);
192
- }
193
-
194
- export type BoundPolygon = {
195
- index: number;
196
- inputName: string;
197
- id: number;
198
- sml: number;
199
- name: string;
200
- };
201
-
202
- export type ApiBoundPolygon = {
203
- data: {
204
- index: number;
205
- input_name: string;
206
- id: number;
207
- sml: number;
208
- name: string;
209
- };
210
- } & Omit<ApiSuccess, 'data'> | ApiError;
211
-
212
- export type ApiBoundPolygonData = Flatten<Exclude<ApiBoundPolygon, ApiError>['data']>;
213
-
214
- export async function getBoundPolygons(
215
- groupId: number,
216
- property: string,
217
- data: string[],
218
- language: string,
219
- ): Promise<BoundPolygon[]> {
220
- const path = `/v1/choropleth/groups/bind`;
221
- const body = { group_id: groupId, property, data, language };
222
-
223
- type ApiBoundPolygonArray = {
224
- data: ApiBoundPolygonData[];
225
- } & Omit<ApiSuccess, 'data'> | ApiError;
226
-
227
- return request<ApiBoundPolygonArray, BoundPolygon>(path, body);
228
- }
package/src/api/font.ts CHANGED
@@ -5,6 +5,9 @@ export type Font = {
5
5
  fontFamilyId: number;
6
6
  name: string;
7
7
  label: string;
8
+ weight: number;
9
+ style: string;
10
+ stretch: string;
8
11
  };
9
12
 
10
13
  export type ApiFont = {
@@ -29,9 +32,6 @@ type FontSearchOptions = {
29
32
  };
30
33
 
31
34
  export const fontRevivers: Revivers<ApiFont, Font> = {
32
- style: undefined,
33
- stretch: undefined,
34
- weight: undefined,
35
35
  order: undefined,
36
36
  };
37
37
 
@@ -1,6 +1,7 @@
1
1
  import { type ApiOrganisationData, type Organisation, organisationRevivers } from './organisation.js';
2
2
  import { type ApiDimensionSetData, type DimensionSet, dimensionSetRevivers } from './dimensionSet.js';
3
3
  import { type ApiMapstyleSetData, type MapstyleSet, mapstyleSetRevivers } from './mapstyleSet.js';
4
+ import { type ApiFontFamilyData, type FontFamily, fontFamilyRevivers } from './fontFamily.js';
4
5
  import { type ApiDimensionData, type Dimension, dimensionRevivers } from './dimension.js';
5
6
  import { type ApiFeatureData, type Feature, featureRevivers } from './feature.js';
6
7
  import { type ApiJobTypeData, type JobType, jobTypeRevivers } from './jobType.js';
@@ -10,6 +11,7 @@ import { type ApiSvgSetData, type SvgSet, svgSetRevivers } from './svgSet.js';
10
11
  import { type ApiLayerData, type Layer, layerRevivers } from './layer.js';
11
12
  import { type ApiColorData, type Color, colorRevivers } from './color.js';
12
13
  import { type ApiUserData, type User, userRevivers } from './user.js';
14
+ import { type ApiFontData, type Font, fontRevivers } from './font.js';
13
15
  import type { CamelCasedProperties } from 'type-fest';
14
16
  import {
15
17
  type ApiCommon,
@@ -62,6 +64,8 @@ export interface Resources {
62
64
  messages: Message[];
63
65
  svgSets: SvgSet[];
64
66
  layers: Layer[];
67
+ fonts: Font[];
68
+ fontFamilies: FontFamily[];
65
69
  }
66
70
 
67
71
  type ApiResources = {
@@ -82,6 +86,8 @@ type ApiResources = {
82
86
  job_types: ApiJobTypeData[];
83
87
  svg_sets: ApiSvgSetData[];
84
88
  layers: ApiLayerData[];
89
+ fonts: ApiFontData[];
90
+ font_families: ApiFontFamilyData[];
85
91
  } & ApiCommonData;
86
92
  } & Omit<ApiSuccess, 'data'> | ApiError;
87
93
 
@@ -129,6 +135,8 @@ export async function loadResources(): Promise<Resources> {
129
135
  messages: allMessages,
130
136
  layers: (raw.layers?.map(processData, getContext(layerRevivers)) ?? []) as Layer[],
131
137
  svgSets: (raw.svgSets?.map(processData, getContext(svgSetRevivers)) ?? []) as SvgSet[],
138
+ fonts: (raw.fonts?.map(processData, getContext(fontRevivers)) ?? []) as Font[],
139
+ fontFamilies: (raw.fontFamilies?.map(processData, getContext(fontFamilyRevivers)) ?? []) as FontFamily[],
132
140
  };
133
141
  }
134
142