@comapeo/core-react 11.0.2 → 11.0.3

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.
@@ -2,26 +2,12 @@ import type { AbortMapShareOptions, CancelMapShareOptions, CreateAndSendMapShare
2
2
  /**
3
3
  * Get a URL that points to a StyleJSON resource served by the embedded HTTP server.
4
4
  *
5
- * If `opts.refreshToken` is specified, it will be appended to the returned URL as a search param. This is useful for forcing cache busting
6
- * due to hidden internal details by consuming components (e.g. map component from MapLibre).
7
- *
8
- * @param opts.refreshToken String to append to the returned value as a search param
9
- *
10
5
  * @example
11
6
  * ```tsx
12
7
  * function ExampleWithoutRefreshToken() {
13
8
  * const { data, isRefetching } = useMapStyleUrl()
14
9
  *
15
- * console.log(data) // logs something like 'http://localhost:...'
16
- * }
17
- * ```
18
- *
19
- * ```tsx
20
- * function ExampleWithRefreshToken() {
21
- * const [refreshToken] = useState('foo')
22
- * const { data } = useMapStyleUrl({ refreshToken })
23
- *
24
- * console.log(data) // logs something like 'http://localhost:...?refresh_token=foo'
10
+ * console.log(data) // logs something like 'http://127.0.0.1:...'
25
11
  * }
26
12
  * ```
27
13
  */
@@ -104,15 +90,61 @@ export declare function useRemoveCustomMapFile(): Pick<import("@tanstack/react-q
104
90
  }> & {
105
91
  mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<Response, Error, void, unknown>;
106
92
  }, "error" | "status" | "mutate" | "reset" | "mutateAsync">;
107
- export declare function useGetCustomMapInfo(): {
108
- data: {
109
- name: string;
110
- size: number;
111
- created: number;
112
- } | undefined;
113
- error: Error | null;
114
- isRefetching: boolean;
115
- };
93
+ /**
94
+ * Get the map info for the custom map.
95
+ *
96
+ * Note that this is _not_ a suspenseful hook. The different read states should be explicitly handled at usage.
97
+ *
98
+ * @example
99
+ * ```tsx
100
+ * import { getErrorCode } from '@comapeo/core-react'
101
+ *
102
+ * function Example() {
103
+ * const mapInfo = useGetCustomMapInfo()
104
+ *
105
+ * if (mapInfo.status === 'pending') {
106
+ * // handle pending...
107
+ * }
108
+ *
109
+ * if (mapInfo.status === 'error') {
110
+ * if (getErrorCode(mapInfo.error) === 'MAP_NOT_FOUND') {
111
+ * // handle not found error...
112
+ * } else {
113
+ * // handle all other errors...
114
+ * }
115
+ * }
116
+ *
117
+ * // Handle success state...
118
+ * console.log(mapInfo.data)
119
+ * }
120
+ * ```
121
+ *
122
+ */
123
+ export declare function useGetCustomMapInfo(): Pick<import("@tanstack/react-query").QueryObserverRefetchErrorResult<{
124
+ name: string;
125
+ size: number;
126
+ created: number;
127
+ }, Error>, "data" | "error" | "status" | "isRefetching"> | Pick<import("@tanstack/react-query").QueryObserverSuccessResult<{
128
+ name: string;
129
+ size: number;
130
+ created: number;
131
+ }, Error>, "data" | "error" | "status" | "isRefetching"> | Pick<import("@tanstack/react-query").QueryObserverLoadingErrorResult<{
132
+ name: string;
133
+ size: number;
134
+ created: number;
135
+ }, Error>, "data" | "error" | "status" | "isRefetching"> | Pick<import("@tanstack/react-query").QueryObserverLoadingResult<{
136
+ name: string;
137
+ size: number;
138
+ created: number;
139
+ }, Error>, "data" | "error" | "status" | "isRefetching"> | Pick<import("@tanstack/react-query").QueryObserverPendingResult<{
140
+ name: string;
141
+ size: number;
142
+ created: number;
143
+ }, Error>, "data" | "error" | "status" | "isRefetching"> | Pick<import("@tanstack/react-query").QueryObserverPlaceholderResult<{
144
+ name: string;
145
+ size: number;
146
+ created: number;
147
+ }, Error>, "data" | "error" | "status" | "isRefetching">;
116
148
  /**
117
149
  * Get all map shares that the device has received. Automatically updates when
118
150
  * new shares arrive or share states change.
@@ -22,26 +22,12 @@ const react_query_js_1 = require("../lib/react-query.js");
22
22
  /**
23
23
  * Get a URL that points to a StyleJSON resource served by the embedded HTTP server.
24
24
  *
25
- * If `opts.refreshToken` is specified, it will be appended to the returned URL as a search param. This is useful for forcing cache busting
26
- * due to hidden internal details by consuming components (e.g. map component from MapLibre).
27
- *
28
- * @param opts.refreshToken String to append to the returned value as a search param
29
- *
30
25
  * @example
31
26
  * ```tsx
32
27
  * function ExampleWithoutRefreshToken() {
33
28
  * const { data, isRefetching } = useMapStyleUrl()
34
29
  *
35
- * console.log(data) // logs something like 'http://localhost:...'
36
- * }
37
- * ```
38
- *
39
- * ```tsx
40
- * function ExampleWithRefreshToken() {
41
- * const [refreshToken] = useState('foo')
42
- * const { data } = useMapStyleUrl({ refreshToken })
43
- *
44
- * console.log(data) // logs something like 'http://localhost:...?refresh_token=foo'
30
+ * console.log(data) // logs something like 'http://127.0.0.1:...'
45
31
  * }
46
32
  * ```
47
33
  */
@@ -114,11 +100,41 @@ function useRemoveCustomMapFile() {
114
100
  },
115
101
  }));
116
102
  }
103
+ /**
104
+ * Get the map info for the custom map.
105
+ *
106
+ * Note that this is _not_ a suspenseful hook. The different read states should be explicitly handled at usage.
107
+ *
108
+ * @example
109
+ * ```tsx
110
+ * import { getErrorCode } from '@comapeo/core-react'
111
+ *
112
+ * function Example() {
113
+ * const mapInfo = useGetCustomMapInfo()
114
+ *
115
+ * if (mapInfo.status === 'pending') {
116
+ * // handle pending...
117
+ * }
118
+ *
119
+ * if (mapInfo.status === 'error') {
120
+ * if (getErrorCode(mapInfo.error) === 'MAP_NOT_FOUND') {
121
+ * // handle not found error...
122
+ * } else {
123
+ * // handle all other errors...
124
+ * }
125
+ * }
126
+ *
127
+ * // Handle success state...
128
+ * console.log(mapInfo.data)
129
+ * }
130
+ * ```
131
+ *
132
+ */
117
133
  function useGetCustomMapInfo() {
118
134
  const mapServerApi = (0, MapServer_js_1.useMapServerApi)();
119
135
  // TODO: Support custom maps
120
136
  const mapId = constants_js_1.CUSTOM_MAP_ID;
121
- const { data, error, isRefetching } = (0, react_query_1.useQuery)({
137
+ const result = (0, react_query_1.useQuery)({
122
138
  ...(0, react_query_js_1.baseQueryOptions)(),
123
139
  queryKey: (0, react_query_js_1.getMapInfoQueryKey)({ mapId }),
124
140
  queryFn: async () => {
@@ -128,7 +144,7 @@ function useGetCustomMapInfo() {
128
144
  staleTime: Infinity,
129
145
  gcTime: Infinity,
130
146
  });
131
- return { data, error, isRefetching };
147
+ return (0, react_query_js_1.filterQueryResult)(result);
132
148
  }
133
149
  // ============================================
134
150
  // RECEIVER HOOKS
@@ -1,4 +1,4 @@
1
- import type { QueryClient, UseMutationResult } from '@tanstack/react-query';
1
+ import type { QueryClient, UseMutationResult, UseQueryResult } from '@tanstack/react-query';
2
2
  import { DistributedPick } from 'type-fest';
3
3
  import type { WriteableDocumentType } from './types.js';
4
4
  export declare function baseQueryOptions(): {
@@ -17,6 +17,9 @@ export type FilteredMutationResult<TResult extends UseMutationResult<any, any, a
17
17
  * result based on the `status` property.
18
18
  */
19
19
  export declare function filterMutationResult<TResult extends UseMutationResult<any, any, any, any>>(mutationResult: TResult): DistributedPick<TResult, "error" | "status" | "mutate" | "reset" | "mutateAsync">;
20
+ declare const PICKED_QUERY_RESULT_KEYS: readonly ["data", "error", "isRefetching", "status"];
21
+ export type FilteredQueryResult<TResult extends UseQueryResult<any, any>> = DistributedPick<TResult, (typeof PICKED_QUERY_RESULT_KEYS)[number]>;
22
+ export declare function filterQueryResult<TResult extends UseQueryResult<any, any>>(queryResult: TResult): DistributedPick<TResult, "data" | "error" | "status" | "isRefetching">;
20
23
  export declare function getDeviceInfoQueryKey(): readonly ["@comapeo/core-react", "client", "device_info"];
21
24
  export declare function getIsArchiveDeviceQueryKey(): readonly ["@comapeo/core-react", "client", "is_archive_device"];
22
25
  export declare function getInvitesQueryKey(): readonly ["@comapeo/core-react", "invites"];
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.baseQueryOptions = baseQueryOptions;
4
4
  exports.baseMutationOptions = baseMutationOptions;
5
5
  exports.filterMutationResult = filterMutationResult;
6
+ exports.filterQueryResult = filterQueryResult;
6
7
  exports.getDeviceInfoQueryKey = getDeviceInfoQueryKey;
7
8
  exports.getIsArchiveDeviceQueryKey = getIsArchiveDeviceQueryKey;
8
9
  exports.getInvitesQueryKey = getInvitesQueryKey;
@@ -62,6 +63,19 @@ function filterMutationResult(mutationResult) {
62
63
  }
63
64
  return filteredResult;
64
65
  }
66
+ const PICKED_QUERY_RESULT_KEYS = [
67
+ 'data',
68
+ 'error',
69
+ 'isRefetching',
70
+ 'status',
71
+ ];
72
+ function filterQueryResult(queryResult) {
73
+ const filteredResult = {};
74
+ for (const key of PICKED_QUERY_RESULT_KEYS) {
75
+ filteredResult[key] = queryResult[key];
76
+ }
77
+ return filteredResult;
78
+ }
65
79
  // #endregion
66
80
  // #region Client
67
81
  function getClientQueryKey() {
@@ -2,26 +2,12 @@ import type { AbortMapShareOptions, CancelMapShareOptions, CreateAndSendMapShare
2
2
  /**
3
3
  * Get a URL that points to a StyleJSON resource served by the embedded HTTP server.
4
4
  *
5
- * If `opts.refreshToken` is specified, it will be appended to the returned URL as a search param. This is useful for forcing cache busting
6
- * due to hidden internal details by consuming components (e.g. map component from MapLibre).
7
- *
8
- * @param opts.refreshToken String to append to the returned value as a search param
9
- *
10
5
  * @example
11
6
  * ```tsx
12
7
  * function ExampleWithoutRefreshToken() {
13
8
  * const { data, isRefetching } = useMapStyleUrl()
14
9
  *
15
- * console.log(data) // logs something like 'http://localhost:...'
16
- * }
17
- * ```
18
- *
19
- * ```tsx
20
- * function ExampleWithRefreshToken() {
21
- * const [refreshToken] = useState('foo')
22
- * const { data } = useMapStyleUrl({ refreshToken })
23
- *
24
- * console.log(data) // logs something like 'http://localhost:...?refresh_token=foo'
10
+ * console.log(data) // logs something like 'http://127.0.0.1:...'
25
11
  * }
26
12
  * ```
27
13
  */
@@ -104,15 +90,61 @@ export declare function useRemoveCustomMapFile(): Pick<import("@tanstack/react-q
104
90
  }> & {
105
91
  mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<Response, Error, void, unknown>;
106
92
  }, "error" | "status" | "mutate" | "reset" | "mutateAsync">;
107
- export declare function useGetCustomMapInfo(): {
108
- data: {
109
- name: string;
110
- size: number;
111
- created: number;
112
- } | undefined;
113
- error: Error | null;
114
- isRefetching: boolean;
115
- };
93
+ /**
94
+ * Get the map info for the custom map.
95
+ *
96
+ * Note that this is _not_ a suspenseful hook. The different read states should be explicitly handled at usage.
97
+ *
98
+ * @example
99
+ * ```tsx
100
+ * import { getErrorCode } from '@comapeo/core-react'
101
+ *
102
+ * function Example() {
103
+ * const mapInfo = useGetCustomMapInfo()
104
+ *
105
+ * if (mapInfo.status === 'pending') {
106
+ * // handle pending...
107
+ * }
108
+ *
109
+ * if (mapInfo.status === 'error') {
110
+ * if (getErrorCode(mapInfo.error) === 'MAP_NOT_FOUND') {
111
+ * // handle not found error...
112
+ * } else {
113
+ * // handle all other errors...
114
+ * }
115
+ * }
116
+ *
117
+ * // Handle success state...
118
+ * console.log(mapInfo.data)
119
+ * }
120
+ * ```
121
+ *
122
+ */
123
+ export declare function useGetCustomMapInfo(): Pick<import("@tanstack/react-query").QueryObserverRefetchErrorResult<{
124
+ name: string;
125
+ size: number;
126
+ created: number;
127
+ }, Error>, "data" | "error" | "status" | "isRefetching"> | Pick<import("@tanstack/react-query").QueryObserverSuccessResult<{
128
+ name: string;
129
+ size: number;
130
+ created: number;
131
+ }, Error>, "data" | "error" | "status" | "isRefetching"> | Pick<import("@tanstack/react-query").QueryObserverLoadingErrorResult<{
132
+ name: string;
133
+ size: number;
134
+ created: number;
135
+ }, Error>, "data" | "error" | "status" | "isRefetching"> | Pick<import("@tanstack/react-query").QueryObserverLoadingResult<{
136
+ name: string;
137
+ size: number;
138
+ created: number;
139
+ }, Error>, "data" | "error" | "status" | "isRefetching"> | Pick<import("@tanstack/react-query").QueryObserverPendingResult<{
140
+ name: string;
141
+ size: number;
142
+ created: number;
143
+ }, Error>, "data" | "error" | "status" | "isRefetching"> | Pick<import("@tanstack/react-query").QueryObserverPlaceholderResult<{
144
+ name: string;
145
+ size: number;
146
+ created: number;
147
+ }, Error>, "data" | "error" | "status" | "isRefetching">;
116
148
  /**
117
149
  * Get all map shares that the device has received. Automatically updates when
118
150
  * new shares arrive or share states change.
@@ -4,30 +4,16 @@ import { useMutation, useQuery, useQueryClient, useSuspenseQuery, } from '@tanst
4
4
  import { useCallback } from 'react';
5
5
  import { useMapServerApi } from '../contexts/MapServer.js';
6
6
  import { useReceivedMapSharesActions, useReceivedMapSharesState, useSentMapSharesActions, useSentMapSharesState, } from '../contexts/MapShares.js';
7
- import { baseMutationOptions, baseQueryOptions, filterMutationResult, getMapInfoQueryKey, getStyleJsonUrlQueryKey, invalidateMapQueries, } from '../lib/react-query.js';
7
+ import { baseMutationOptions, baseQueryOptions, filterMutationResult, filterQueryResult, getMapInfoQueryKey, getStyleJsonUrlQueryKey, invalidateMapQueries, } from '../lib/react-query.js';
8
8
  /**
9
9
  * Get a URL that points to a StyleJSON resource served by the embedded HTTP server.
10
10
  *
11
- * If `opts.refreshToken` is specified, it will be appended to the returned URL as a search param. This is useful for forcing cache busting
12
- * due to hidden internal details by consuming components (e.g. map component from MapLibre).
13
- *
14
- * @param opts.refreshToken String to append to the returned value as a search param
15
- *
16
11
  * @example
17
12
  * ```tsx
18
13
  * function ExampleWithoutRefreshToken() {
19
14
  * const { data, isRefetching } = useMapStyleUrl()
20
15
  *
21
- * console.log(data) // logs something like 'http://localhost:...'
22
- * }
23
- * ```
24
- *
25
- * ```tsx
26
- * function ExampleWithRefreshToken() {
27
- * const [refreshToken] = useState('foo')
28
- * const { data } = useMapStyleUrl({ refreshToken })
29
- *
30
- * console.log(data) // logs something like 'http://localhost:...?refresh_token=foo'
16
+ * console.log(data) // logs something like 'http://127.0.0.1:...'
31
17
  * }
32
18
  * ```
33
19
  */
@@ -100,11 +86,41 @@ export function useRemoveCustomMapFile() {
100
86
  },
101
87
  }));
102
88
  }
89
+ /**
90
+ * Get the map info for the custom map.
91
+ *
92
+ * Note that this is _not_ a suspenseful hook. The different read states should be explicitly handled at usage.
93
+ *
94
+ * @example
95
+ * ```tsx
96
+ * import { getErrorCode } from '@comapeo/core-react'
97
+ *
98
+ * function Example() {
99
+ * const mapInfo = useGetCustomMapInfo()
100
+ *
101
+ * if (mapInfo.status === 'pending') {
102
+ * // handle pending...
103
+ * }
104
+ *
105
+ * if (mapInfo.status === 'error') {
106
+ * if (getErrorCode(mapInfo.error) === 'MAP_NOT_FOUND') {
107
+ * // handle not found error...
108
+ * } else {
109
+ * // handle all other errors...
110
+ * }
111
+ * }
112
+ *
113
+ * // Handle success state...
114
+ * console.log(mapInfo.data)
115
+ * }
116
+ * ```
117
+ *
118
+ */
103
119
  export function useGetCustomMapInfo() {
104
120
  const mapServerApi = useMapServerApi();
105
121
  // TODO: Support custom maps
106
122
  const mapId = CUSTOM_MAP_ID;
107
- const { data, error, isRefetching } = useQuery({
123
+ const result = useQuery({
108
124
  ...baseQueryOptions(),
109
125
  queryKey: getMapInfoQueryKey({ mapId }),
110
126
  queryFn: async () => {
@@ -114,7 +130,7 @@ export function useGetCustomMapInfo() {
114
130
  staleTime: Infinity,
115
131
  gcTime: Infinity,
116
132
  });
117
- return { data, error, isRefetching };
133
+ return filterQueryResult(result);
118
134
  }
119
135
  // ============================================
120
136
  // RECEIVER HOOKS
@@ -1,4 +1,4 @@
1
- import type { QueryClient, UseMutationResult } from '@tanstack/react-query';
1
+ import type { QueryClient, UseMutationResult, UseQueryResult } from '@tanstack/react-query';
2
2
  import { DistributedPick } from 'type-fest';
3
3
  import type { WriteableDocumentType } from './types.js';
4
4
  export declare function baseQueryOptions(): {
@@ -17,6 +17,9 @@ export type FilteredMutationResult<TResult extends UseMutationResult<any, any, a
17
17
  * result based on the `status` property.
18
18
  */
19
19
  export declare function filterMutationResult<TResult extends UseMutationResult<any, any, any, any>>(mutationResult: TResult): DistributedPick<TResult, "error" | "status" | "mutate" | "reset" | "mutateAsync">;
20
+ declare const PICKED_QUERY_RESULT_KEYS: readonly ["data", "error", "isRefetching", "status"];
21
+ export type FilteredQueryResult<TResult extends UseQueryResult<any, any>> = DistributedPick<TResult, (typeof PICKED_QUERY_RESULT_KEYS)[number]>;
22
+ export declare function filterQueryResult<TResult extends UseQueryResult<any, any>>(queryResult: TResult): DistributedPick<TResult, "data" | "error" | "status" | "isRefetching">;
20
23
  export declare function getDeviceInfoQueryKey(): readonly ["@comapeo/core-react", "client", "device_info"];
21
24
  export declare function getIsArchiveDeviceQueryKey(): readonly ["@comapeo/core-react", "client", "is_archive_device"];
22
25
  export declare function getInvitesQueryKey(): readonly ["@comapeo/core-react", "invites"];
@@ -37,6 +37,19 @@ export function filterMutationResult(mutationResult) {
37
37
  }
38
38
  return filteredResult;
39
39
  }
40
+ const PICKED_QUERY_RESULT_KEYS = [
41
+ 'data',
42
+ 'error',
43
+ 'isRefetching',
44
+ 'status',
45
+ ];
46
+ export function filterQueryResult(queryResult) {
47
+ const filteredResult = {};
48
+ for (const key of PICKED_QUERY_RESULT_KEYS) {
49
+ filteredResult[key] = queryResult[key];
50
+ }
51
+ return filteredResult;
52
+ }
40
53
  // #endregion
41
54
  // #region Client
42
55
  function getClientQueryKey() {
package/docs/API.md CHANGED
@@ -1111,34 +1111,17 @@ Parameters:
1111
1111
 
1112
1112
  Get a URL that points to a StyleJSON resource served by the embedded HTTP server.
1113
1113
 
1114
- If `opts.refreshToken` is specified, it will be appended to the returned URL as a search param. This is useful for forcing cache busting
1115
- due to hidden internal details by consuming components (e.g. map component from MapLibre).
1116
-
1117
1114
  | Function | Type |
1118
1115
  | ---------- | ---------- |
1119
1116
  | `useMapStyleUrl` | `() => { data: string; error: Error or null; isRefetching: boolean; }` |
1120
1117
 
1121
- Parameters:
1122
-
1123
- * `opts.refreshToken`: String to append to the returned value as a search param
1124
-
1125
-
1126
1118
  Examples:
1127
1119
 
1128
1120
  ```tsx
1129
1121
  function ExampleWithoutRefreshToken() {
1130
1122
  const { data, isRefetching } = useMapStyleUrl()
1131
1123
 
1132
- console.log(data) // logs something like 'http://localhost:...'
1133
- }
1134
- ```
1135
-
1136
- ```tsx
1137
- function ExampleWithRefreshToken() {
1138
- const [refreshToken] = useState('foo')
1139
- const { data } = useMapStyleUrl({ refreshToken })
1140
-
1141
- console.log(data) // logs something like 'http://localhost:...?refresh_token=foo'
1124
+ console.log(data) // logs something like 'http://127.0.0.1:...'
1142
1125
  }
1143
1126
  ```
1144
1127
 
@@ -1170,9 +1153,39 @@ function MapImportExample() {
1170
1153
 
1171
1154
  ### useGetCustomMapInfo
1172
1155
 
1156
+ Get the map info for the custom map.
1157
+
1158
+ Note that this is _not_ a suspenseful hook. The different read states should be explicitly handled at usage.
1159
+
1173
1160
  | Function | Type |
1174
1161
  | ---------- | ---------- |
1175
- | `useGetCustomMapInfo` | `() => { data: any; error: Error or null; isRefetching: boolean; }` |
1162
+ | `useGetCustomMapInfo` | `() => Pick<QueryObserverRefetchErrorResult<MapInfoResponse, Error>, "data" or "error" or "status" or "isRefetching"> or Pick<QueryObserverSuccessResult<MapInfoResponse, Error>, "data" or ... 2 more ... or "isRefetching"> or Pick<...> or Pick<...> or Pick<...> or Pick<...>` |
1163
+
1164
+ Examples:
1165
+
1166
+ ```tsx
1167
+ import { getErrorCode } from '@comapeo/core-react'
1168
+
1169
+ function Example() {
1170
+ const mapInfo = useGetCustomMapInfo()
1171
+
1172
+ if (mapInfo.status === 'pending') {
1173
+ // handle pending...
1174
+ }
1175
+
1176
+ if (mapInfo.status === 'error') {
1177
+ if (getErrorCode(mapInfo.error) === 'MAP_NOT_FOUND') {
1178
+ // handle not found error...
1179
+ } else {
1180
+ // handle all other errors...
1181
+ }
1182
+ }
1183
+
1184
+ // Handle success state...
1185
+ console.log(mapInfo.data)
1186
+ }
1187
+ ```
1188
+
1176
1189
 
1177
1190
  ### useManyReceivedMapShares
1178
1191
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@comapeo/core-react",
3
- "version": "11.0.2",
3
+ "version": "11.0.3",
4
4
  "description": "React wrapper for working with @comapeo/core",
5
5
  "repository": {
6
6
  "type": "git",