Version not found. Please check the version and try again.
@comapeo/core-react 8.0.0 → 9.0.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/README.md +38 -0
- package/dist/commonjs/contexts/ClientApi.d.ts +5 -5
- package/dist/commonjs/contexts/ClientApi.js +6 -5
- package/dist/commonjs/contexts/ComapeoCore.d.ts +8 -0
- package/dist/commonjs/contexts/ComapeoCore.js +9 -0
- package/dist/commonjs/contexts/MapServer.d.ts +69 -0
- package/dist/commonjs/contexts/MapServer.js +92 -0
- package/dist/commonjs/contexts/MapShares.d.ts +52 -0
- package/dist/commonjs/contexts/MapShares.js +74 -0
- package/dist/commonjs/hooks/maps.d.ts +460 -3
- package/dist/commonjs/hooks/maps.js +261 -4
- package/dist/commonjs/index.d.ts +5 -2
- package/dist/commonjs/index.js +20 -3
- package/dist/commonjs/lib/http.d.ts +45 -0
- package/dist/commonjs/lib/http.js +103 -0
- package/dist/commonjs/lib/map-shares-stores.d.ts +80 -0
- package/dist/commonjs/lib/map-shares-stores.js +299 -0
- package/dist/commonjs/lib/react-query/maps.d.ts +66 -20
- package/dist/commonjs/lib/react-query/maps.js +113 -11
- package/dist/commonjs/lib/react-query/mutation-result.d.ts +8 -0
- package/dist/commonjs/lib/react-query/mutation-result.js +22 -0
- package/dist/esm/contexts/ClientApi.d.ts +5 -5
- package/dist/esm/contexts/ClientApi.js +6 -5
- package/dist/esm/contexts/ComapeoCore.d.ts +8 -0
- package/dist/esm/contexts/ComapeoCore.js +6 -0
- package/dist/esm/contexts/MapServer.d.ts +69 -0
- package/dist/esm/contexts/MapServer.js +86 -0
- package/dist/esm/contexts/MapShares.d.ts +52 -0
- package/dist/esm/contexts/MapShares.js +65 -0
- package/dist/esm/hooks/maps.d.ts +460 -3
- package/dist/esm/hooks/maps.js +252 -6
- package/dist/esm/index.d.ts +5 -2
- package/dist/esm/index.js +4 -2
- package/dist/esm/lib/http.d.ts +45 -0
- package/dist/esm/lib/http.js +98 -0
- package/dist/esm/lib/map-shares-stores.d.ts +80 -0
- package/dist/esm/lib/map-shares-stores.js +291 -0
- package/dist/esm/lib/react-query/maps.d.ts +66 -20
- package/dist/esm/lib/react-query/maps.js +109 -12
- package/dist/esm/lib/react-query/mutation-result.d.ts +8 -0
- package/dist/esm/lib/react-query/mutation-result.js +19 -0
- package/docs/API.md +567 -60
- package/package.json +15 -4
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { type QueryClient } from '@tanstack/react-query';
|
|
2
|
+
import { type EventSourceClient, type EventSourceOptions } from 'eventsource-client';
|
|
3
|
+
import { type Context, type JSX, type PropsWithChildren } from 'react';
|
|
4
|
+
import { createHttp } from '../lib/http.js';
|
|
5
|
+
export type MapServerApiOptions = {
|
|
6
|
+
getBaseUrl(): Promise<URL>;
|
|
7
|
+
/**
|
|
8
|
+
* We assume the passed fetch implementation will only accept a `string` as
|
|
9
|
+
* input, not a `URL` or `Request`, because right now the expo/fetch
|
|
10
|
+
* implementation will only accept a `string`. Adding this restriction will
|
|
11
|
+
* catch potential issues if we try to pass a URL in our code. Can be relaxed
|
|
12
|
+
* when https://github.com/expo/expo/issues/43193 is fixed upstream.
|
|
13
|
+
*/
|
|
14
|
+
fetch?(input: string, options?: RequestInit): Promise<Response>;
|
|
15
|
+
};
|
|
16
|
+
export type MapServerApi = ReturnType<typeof createHttp> & {
|
|
17
|
+
createEventSource(options: EventSourceOptions): EventSourceClient;
|
|
18
|
+
getMapStyleJsonUrl(mapId: string): Promise<string>;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Utility function to create a MapServerApi instance.
|
|
22
|
+
* Only exported for unit testing purposes.
|
|
23
|
+
* @private
|
|
24
|
+
*
|
|
25
|
+
* @param opts.getBaseUrl A function that returns a promise that resolves to the base URL of the map server.
|
|
26
|
+
* @param opts.fetch An optional custom fetch function to use for making requests to the map server. If not provided, the global `fetch` will be used.
|
|
27
|
+
*/
|
|
28
|
+
export declare function createMapServerApi({ getBaseUrl, fetch, }: MapServerApiOptions): MapServerApi;
|
|
29
|
+
export declare const MapServerContext: Context<MapServerApi | null>;
|
|
30
|
+
export type MapServerProviderProps = PropsWithChildren<MapServerApiOptions & {
|
|
31
|
+
queryClient: QueryClient;
|
|
32
|
+
}>;
|
|
33
|
+
/**
|
|
34
|
+
* Create a context provider that holds a `MapServerFetch` function, which waits
|
|
35
|
+
* for the map server to be ready before making requests.
|
|
36
|
+
*
|
|
37
|
+
* @param opts.children React children node
|
|
38
|
+
* @param opts.getBaseUrl A function that returns a promise that resolves to the base URL of the map server.
|
|
39
|
+
* @param opts.fetch An optional custom fetch function to use for making requests to the map server. If not provided, the global `fetch` will be used.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```tsx
|
|
43
|
+
* import { createServer } from '@comapeo/map-server'
|
|
44
|
+
*
|
|
45
|
+
* const server = createServer()
|
|
46
|
+
* const listenPromise = server.listen()
|
|
47
|
+
*
|
|
48
|
+
* const getBaseUrl = async () => {
|
|
49
|
+
* const { localPort } = await listenPromise
|
|
50
|
+
* return new URL(`http://localhost:${localPort}/`)
|
|
51
|
+
* }
|
|
52
|
+
*
|
|
53
|
+
* const mapServerApi = createMapServerApi({ getBaseUrl })
|
|
54
|
+
*
|
|
55
|
+
* function App() {
|
|
56
|
+
* return (
|
|
57
|
+
* <MapServerProvider mapServerApi={mapServerApi}>
|
|
58
|
+
* <MyApp />
|
|
59
|
+
* </MapServerProvider>
|
|
60
|
+
* )
|
|
61
|
+
* }
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
export declare function MapServerProvider({ children, getBaseUrl, fetch, queryClient, }: MapServerProviderProps): JSX.Element;
|
|
65
|
+
/**
|
|
66
|
+
* Internal hook to get the MapServerApi (instance of ky) from context.
|
|
67
|
+
* Throws if used outside of MapServerProvider.
|
|
68
|
+
*/
|
|
69
|
+
export declare function useMapServerApi(): MapServerApi;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { createEventSource, } from 'eventsource-client';
|
|
2
|
+
import { createContext, createElement, useContext, useMemo, } from 'react';
|
|
3
|
+
import { useClientApi } from '../hooks/client.js';
|
|
4
|
+
import { createHttp } from '../lib/http.js';
|
|
5
|
+
import { ReceivedMapSharesProvider, SentMapSharesProvider, } from './MapShares.js';
|
|
6
|
+
/**
|
|
7
|
+
* Utility function to create a MapServerApi instance.
|
|
8
|
+
* Only exported for unit testing purposes.
|
|
9
|
+
* @private
|
|
10
|
+
*
|
|
11
|
+
* @param opts.getBaseUrl A function that returns a promise that resolves to the base URL of the map server.
|
|
12
|
+
* @param opts.fetch An optional custom fetch function to use for making requests to the map server. If not provided, the global `fetch` will be used.
|
|
13
|
+
*/
|
|
14
|
+
export function createMapServerApi({ getBaseUrl, fetch = globalThis.fetch, }) {
|
|
15
|
+
const wrappedFetch = async (input, init) => {
|
|
16
|
+
const baseUrl = await getBaseUrl();
|
|
17
|
+
return fetch(new URL(input, baseUrl).href, init);
|
|
18
|
+
};
|
|
19
|
+
const api = createHttp(wrappedFetch);
|
|
20
|
+
Object.defineProperty(api, 'createEventSource', {
|
|
21
|
+
value: (options) => {
|
|
22
|
+
return createEventSource({
|
|
23
|
+
...options,
|
|
24
|
+
fetch: async (input, init) => {
|
|
25
|
+
const baseUrl = await getBaseUrl();
|
|
26
|
+
return fetch(new URL(input, baseUrl).href, init);
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
Object.defineProperty(api, 'getMapStyleJsonUrl', {
|
|
32
|
+
value: async (mapId) => {
|
|
33
|
+
const baseUrl = await getBaseUrl();
|
|
34
|
+
return new URL(`/maps/${mapId}/style.json`, baseUrl).href;
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
return api;
|
|
38
|
+
}
|
|
39
|
+
export const MapServerContext = createContext(null);
|
|
40
|
+
/**
|
|
41
|
+
* Create a context provider that holds a `MapServerFetch` function, which waits
|
|
42
|
+
* for the map server to be ready before making requests.
|
|
43
|
+
*
|
|
44
|
+
* @param opts.children React children node
|
|
45
|
+
* @param opts.getBaseUrl A function that returns a promise that resolves to the base URL of the map server.
|
|
46
|
+
* @param opts.fetch An optional custom fetch function to use for making requests to the map server. If not provided, the global `fetch` will be used.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```tsx
|
|
50
|
+
* import { createServer } from '@comapeo/map-server'
|
|
51
|
+
*
|
|
52
|
+
* const server = createServer()
|
|
53
|
+
* const listenPromise = server.listen()
|
|
54
|
+
*
|
|
55
|
+
* const getBaseUrl = async () => {
|
|
56
|
+
* const { localPort } = await listenPromise
|
|
57
|
+
* return new URL(`http://localhost:${localPort}/`)
|
|
58
|
+
* }
|
|
59
|
+
*
|
|
60
|
+
* const mapServerApi = createMapServerApi({ getBaseUrl })
|
|
61
|
+
*
|
|
62
|
+
* function App() {
|
|
63
|
+
* return (
|
|
64
|
+
* <MapServerProvider mapServerApi={mapServerApi}>
|
|
65
|
+
* <MyApp />
|
|
66
|
+
* </MapServerProvider>
|
|
67
|
+
* )
|
|
68
|
+
* }
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
export function MapServerProvider({ children, getBaseUrl, fetch, queryClient, }) {
|
|
72
|
+
const clientApi = useClientApi();
|
|
73
|
+
const mapServerApi = useMemo(() => createMapServerApi({ getBaseUrl, fetch }), [getBaseUrl, fetch]);
|
|
74
|
+
return createElement(MapServerContext.Provider, { value: mapServerApi }, createElement(SentMapSharesProvider, { clientApi, mapServerApi }, createElement(ReceivedMapSharesProvider, { clientApi, mapServerApi, queryClient }, children)));
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Internal hook to get the MapServerApi (instance of ky) from context.
|
|
78
|
+
* Throws if used outside of MapServerProvider.
|
|
79
|
+
*/
|
|
80
|
+
export function useMapServerApi() {
|
|
81
|
+
const api = useContext(MapServerContext);
|
|
82
|
+
if (!api) {
|
|
83
|
+
throw new Error('useMapServerApi must be used within a MapServerProvider');
|
|
84
|
+
}
|
|
85
|
+
return api;
|
|
86
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { MapeoClientApi } from '@comapeo/ipc';
|
|
2
|
+
import type { QueryClient } from '@tanstack/react-query';
|
|
3
|
+
import { type Context, type JSX, type PropsWithChildren } from 'react';
|
|
4
|
+
import { type ReceivedMapSharesStore, type ReceivedMapShareState, type SentMapSharesStore, type SentMapShareState } from '../lib/map-shares-stores.js';
|
|
5
|
+
import type { MapServerApi } from './MapServer.js';
|
|
6
|
+
/**
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
export declare const ReceivedMapSharesContext: Context<ReceivedMapSharesStore | null>;
|
|
10
|
+
/**
|
|
11
|
+
* @internal
|
|
12
|
+
*/
|
|
13
|
+
export declare const SentMapSharesContext: Context<SentMapSharesStore | null>;
|
|
14
|
+
type MapSharesProviderProps = PropsWithChildren<{
|
|
15
|
+
clientApi: MapeoClientApi;
|
|
16
|
+
mapServerApi: MapServerApi;
|
|
17
|
+
}>;
|
|
18
|
+
/**
|
|
19
|
+
* @internal
|
|
20
|
+
*/
|
|
21
|
+
export declare function ReceivedMapSharesProvider({ children, clientApi, mapServerApi, queryClient, }: MapSharesProviderProps & {
|
|
22
|
+
queryClient: QueryClient;
|
|
23
|
+
}): JSX.Element;
|
|
24
|
+
/**
|
|
25
|
+
* @internal
|
|
26
|
+
*/
|
|
27
|
+
export declare function SentMapSharesProvider({ children, clientApi, mapServerApi, }: MapSharesProviderProps): JSX.Element;
|
|
28
|
+
/**
|
|
29
|
+
* @internal
|
|
30
|
+
*/
|
|
31
|
+
export declare function useReceivedMapSharesActions(): {
|
|
32
|
+
download({ shareId }: import("../lib/map-shares-stores.js").DownloadMapShareOptions): Promise<void>;
|
|
33
|
+
decline({ shareId, reason }: import("../lib/map-shares-stores.js").DeclineMapShareOptions): Promise<void>;
|
|
34
|
+
abort({ shareId }: import("../lib/map-shares-stores.js").AbortMapShareOptions): Promise<void>;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* @internal
|
|
38
|
+
*/
|
|
39
|
+
export declare function useReceivedMapSharesState(): Array<ReceivedMapShareState>;
|
|
40
|
+
export declare function useReceivedMapSharesState<T>(selector: (state: Array<ReceivedMapShareState>) => T): T;
|
|
41
|
+
/**
|
|
42
|
+
* @internal
|
|
43
|
+
*/
|
|
44
|
+
export declare function useSentMapSharesActions(): {
|
|
45
|
+
createAndSend({ projectId, receiverDeviceId, mapId, }: import("../lib/map-shares-stores.js").CreateAndSendMapShareOptions): Promise<import("@comapeo/map-server").MapShareState>;
|
|
46
|
+
cancel({ shareId }: import("../lib/map-shares-stores.js").CancelMapShareOptions): Promise<void>;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* @internal
|
|
50
|
+
*/
|
|
51
|
+
export declare function useSentMapSharesState<T>(selector?: (state: Array<SentMapShareState>) => T): T;
|
|
52
|
+
export {};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { createContext, createElement, useCallback, useContext, useMemo, useSyncExternalStore, } from 'react';
|
|
2
|
+
import { createReceivedMapSharesStore, createSentMapSharesStore, } from '../lib/map-shares-stores.js';
|
|
3
|
+
/**
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
6
|
+
export const ReceivedMapSharesContext = createContext(null);
|
|
7
|
+
/**
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
export const SentMapSharesContext = createContext(null);
|
|
11
|
+
/**
|
|
12
|
+
* @internal
|
|
13
|
+
*/
|
|
14
|
+
export function ReceivedMapSharesProvider({ children, clientApi, mapServerApi, queryClient, }) {
|
|
15
|
+
const mapSharesStore = useMemo(() => createReceivedMapSharesStore({ clientApi, mapServerApi, queryClient }), [clientApi, mapServerApi, queryClient]);
|
|
16
|
+
return createElement(ReceivedMapSharesContext.Provider, { value: mapSharesStore }, children);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* @internal
|
|
20
|
+
*/
|
|
21
|
+
export function SentMapSharesProvider({ children, clientApi, mapServerApi, }) {
|
|
22
|
+
const mapSharesStore = useMemo(() => createSentMapSharesStore({ clientApi, mapServerApi }), [clientApi, mapServerApi]);
|
|
23
|
+
return createElement(SentMapSharesContext.Provider, { value: mapSharesStore }, children);
|
|
24
|
+
}
|
|
25
|
+
const identity = (arg) => arg;
|
|
26
|
+
/**
|
|
27
|
+
* Internal hook to get the MapSharesStore from context.
|
|
28
|
+
*/
|
|
29
|
+
function useMapSharesActions(context) {
|
|
30
|
+
const store = useContext(context);
|
|
31
|
+
if (!store) {
|
|
32
|
+
throw new Error('useMapSharesActions must be used within MapSharesProvider');
|
|
33
|
+
}
|
|
34
|
+
return store.actions;
|
|
35
|
+
}
|
|
36
|
+
function useMapSharesState(context, selector = identity) {
|
|
37
|
+
const store = useContext(context);
|
|
38
|
+
if (!store) {
|
|
39
|
+
throw new Error('useMapSharesState must be used within MapSharesProvider');
|
|
40
|
+
}
|
|
41
|
+
return useSyncExternalStore(store.subscribe, useCallback(
|
|
42
|
+
// Casting here because of TS not resolving generic conditional types correctly
|
|
43
|
+
() => selector(store.getSnapshot()), [selector, store]));
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* @internal
|
|
47
|
+
*/
|
|
48
|
+
export function useReceivedMapSharesActions() {
|
|
49
|
+
return useMapSharesActions(ReceivedMapSharesContext);
|
|
50
|
+
}
|
|
51
|
+
export function useReceivedMapSharesState(selector) {
|
|
52
|
+
return useMapSharesState(ReceivedMapSharesContext, selector);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* @internal
|
|
56
|
+
*/
|
|
57
|
+
export function useSentMapSharesActions() {
|
|
58
|
+
return useMapSharesActions(SentMapSharesContext);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* @internal
|
|
62
|
+
*/
|
|
63
|
+
export function useSentMapSharesState(selector) {
|
|
64
|
+
return useMapSharesState(SentMapSharesContext, selector);
|
|
65
|
+
}
|