@ovok/core 0.4.49-dev.498 → 0.4.49-dev.499

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.
@@ -48,7 +48,7 @@ exports.OVOK_CORE_REQUIREMENTS = {
48
48
  description: "The FHIR surface a server must provide for the @ovok/core client library's own methods to function.",
49
49
  software: {
50
50
  name: "@ovok/core",
51
- version: "0.4.49-dev.498+92fd957",
51
+ version: "0.4.49-dev.499+db0e927",
52
52
  },
53
53
  fhirVersion: "4.0.1",
54
54
  format: ["json"],
@@ -0,0 +1,4 @@
1
+ import * as React from "react";
2
+ import type { Ovok } from "../ovok";
3
+ export declare const OvokContext: React.Context<Ovok | null>;
4
+ export declare const useOvok: () => Ovok;
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.useOvok = exports.OvokContext = void 0;
37
+ const React = __importStar(require("react"));
38
+ exports.OvokContext = React.createContext(null);
39
+ const useOvok = () => {
40
+ const client = React.useContext(exports.OvokContext);
41
+ if (client === null) {
42
+ throw new Error("useOvok must be used inside an OvokProvider.");
43
+ }
44
+ return client;
45
+ };
46
+ exports.useOvok = useOvok;
@@ -0,0 +1,14 @@
1
+ import * as React from "react";
2
+ import type { Session } from "../auth/session";
3
+ import type { Ovok } from "../ovok";
4
+ import type { OvokQueryState } from "./query-hooks";
5
+ import { useOvok } from "./context";
6
+ export type OvokProviderProps = React.PropsWithChildren<{
7
+ client: Ovok;
8
+ }>;
9
+ export declare const OvokProvider: ({ client, children }: OvokProviderProps) => React.ReactElement;
10
+ export declare const useSession: () => OvokQueryState<Session | null>;
11
+ export declare const useProfile: () => OvokQueryState<Session["profile"] | null>;
12
+ export { useOvokMutation, useOvokQuery, useResource, useSearch, useSubscription } from "./query-hooks";
13
+ export { useOvok };
14
+ export type { OvokMutationState, OvokQueryOptions, OvokQueryState } from "./query-hooks";
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ "use client";
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
16
+ }) : function(o, v) {
17
+ o["default"] = v;
18
+ });
19
+ var __importStar = (this && this.__importStar) || (function () {
20
+ var ownKeys = function(o) {
21
+ ownKeys = Object.getOwnPropertyNames || function (o) {
22
+ var ar = [];
23
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
24
+ return ar;
25
+ };
26
+ return ownKeys(o);
27
+ };
28
+ return function (mod) {
29
+ if (mod && mod.__esModule) return mod;
30
+ var result = {};
31
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
32
+ __setModuleDefault(result, mod);
33
+ return result;
34
+ };
35
+ })();
36
+ Object.defineProperty(exports, "__esModule", { value: true });
37
+ exports.useOvok = exports.useSubscription = exports.useSearch = exports.useResource = exports.useOvokQuery = exports.useOvokMutation = exports.useProfile = exports.useSession = exports.OvokProvider = void 0;
38
+ const React = __importStar(require("react"));
39
+ const context_1 = require("./context");
40
+ Object.defineProperty(exports, "useOvok", { enumerable: true, get: function () { return context_1.useOvok; } });
41
+ const query_hooks_1 = require("./query-hooks");
42
+ const OvokProvider = ({ client, children }) => (React.createElement(context_1.OvokContext.Provider, { value: client }, children));
43
+ exports.OvokProvider = OvokProvider;
44
+ const useSession = () => {
45
+ const client = (0, context_1.useOvok)();
46
+ const subscribe = React.useCallback((listener) => client.session.onChange(listener), [client]);
47
+ const changedSession = (0, query_hooks_1.useSubscription)(subscribe);
48
+ const state = (0, query_hooks_1.useOvokQuery)((ovok) => ovok.session.current(), { queryKey: ["session"] });
49
+ return changedSession === undefined
50
+ ? state
51
+ : { ...state, data: changedSession, loading: false, isRefreshing: false, isStale: false, error: undefined };
52
+ };
53
+ exports.useSession = useSession;
54
+ const useProfile = () => {
55
+ var _a, _b;
56
+ const session = (0, exports.useSession)();
57
+ return {
58
+ ...session,
59
+ data: session.data === undefined ? undefined : (_b = (_a = session.data) === null || _a === void 0 ? void 0 : _a.profile) !== null && _b !== void 0 ? _b : null,
60
+ };
61
+ };
62
+ exports.useProfile = useProfile;
63
+ var query_hooks_2 = require("./query-hooks");
64
+ Object.defineProperty(exports, "useOvokMutation", { enumerable: true, get: function () { return query_hooks_2.useOvokMutation; } });
65
+ Object.defineProperty(exports, "useOvokQuery", { enumerable: true, get: function () { return query_hooks_2.useOvokQuery; } });
66
+ Object.defineProperty(exports, "useResource", { enumerable: true, get: function () { return query_hooks_2.useResource; } });
67
+ Object.defineProperty(exports, "useSearch", { enumerable: true, get: function () { return query_hooks_2.useSearch; } });
68
+ Object.defineProperty(exports, "useSubscription", { enumerable: true, get: function () { return query_hooks_2.useSubscription; } });
@@ -0,0 +1,31 @@
1
+ import type { QueryParams } from "../http/url";
2
+ import type { Ovok } from "../ovok";
3
+ import type { Page } from "../paging/page";
4
+ import type { ExtractResource, ResourceType } from "@medplum/fhirtypes";
5
+ export type OvokQueryOptions = {
6
+ queryKey: readonly unknown[];
7
+ enabled?: boolean;
8
+ };
9
+ export type OvokQueryState<T> = {
10
+ data: T | undefined;
11
+ loading: boolean;
12
+ isRefreshing: boolean;
13
+ isStale: boolean;
14
+ error: Error | undefined;
15
+ refetch: () => void;
16
+ };
17
+ export type OvokMutationState<TVariables, TResult> = {
18
+ data: TResult | undefined;
19
+ loading: boolean;
20
+ error: Error | undefined;
21
+ mutate: (variables: TVariables) => Promise<TResult>;
22
+ reset: () => void;
23
+ };
24
+ type OvokQuery<T> = (client: Ovok, signal: AbortSignal) => Promise<T>;
25
+ type OvokMutation<TVariables, TResult> = (client: Ovok, variables: TVariables) => Promise<TResult>;
26
+ export declare const useOvokQuery: <T>(query: OvokQuery<T>, options: OvokQueryOptions) => OvokQueryState<T>;
27
+ export declare const useOvokMutation: <TVariables, TResult>(mutation: OvokMutation<TVariables, TResult>) => OvokMutationState<TVariables, TResult>;
28
+ export declare const useResource: <K extends ResourceType>(type: K, id: string | undefined, options?: Pick<OvokQueryOptions, "enabled">) => OvokQueryState<ExtractResource<K>>;
29
+ export declare const useSearch: <K extends ResourceType>(type: K, params?: QueryParams, options?: Pick<OvokQueryOptions, "enabled">) => OvokQueryState<Page<ExtractResource<K>>>;
30
+ export declare const useSubscription: <T>(subscribe: (listener: (value: T) => void) => () => void) => T | undefined;
31
+ export {};
@@ -0,0 +1,160 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.useSubscription = exports.useSearch = exports.useResource = exports.useOvokMutation = exports.useOvokQuery = void 0;
37
+ const React = __importStar(require("react"));
38
+ const context_1 = require("./context");
39
+ const useOvokQuery = (query, options) => {
40
+ var _a, _b;
41
+ const client = (0, context_1.useOvok)();
42
+ const key = (_a = JSON.stringify(options.queryKey)) !== null && _a !== void 0 ? _a : "[]";
43
+ const enabled = (_b = options.enabled) !== null && _b !== void 0 ? _b : true;
44
+ const queryRef = React.useRef(query);
45
+ const previousQuery = React.useRef(null);
46
+ const [data, setData] = React.useState();
47
+ const [loading, setLoading] = React.useState(enabled);
48
+ const [error, setError] = React.useState();
49
+ const [revision, setRevision] = React.useState(0);
50
+ queryRef.current = query;
51
+ React.useEffect(() => {
52
+ const previous = previousQuery.current;
53
+ const changed = previous === null || previous.client !== client || previous.key !== key;
54
+ previousQuery.current = { client, key };
55
+ if (changed) {
56
+ setData(undefined);
57
+ }
58
+ if (!enabled) {
59
+ setError(undefined);
60
+ setLoading(false);
61
+ return undefined;
62
+ }
63
+ const controller = new AbortController();
64
+ let disposed = false;
65
+ setLoading(true);
66
+ setError(undefined);
67
+ void Promise.resolve()
68
+ .then(() => queryRef.current(client, controller.signal))
69
+ .then((result) => {
70
+ if (!disposed) {
71
+ setData(result);
72
+ setLoading(false);
73
+ }
74
+ })
75
+ .catch((reason) => {
76
+ if (!disposed) {
77
+ setError(toError(reason));
78
+ setLoading(false);
79
+ }
80
+ });
81
+ return () => {
82
+ disposed = true;
83
+ controller.abort();
84
+ };
85
+ }, [client, enabled, key, revision]);
86
+ const refetch = React.useCallback(() => setRevision((value) => value + 1), []);
87
+ return {
88
+ data,
89
+ loading,
90
+ isRefreshing: data !== undefined && loading,
91
+ isStale: data !== undefined && (loading || error !== undefined),
92
+ error,
93
+ refetch,
94
+ };
95
+ };
96
+ exports.useOvokQuery = useOvokQuery;
97
+ const useOvokMutation = (mutation) => {
98
+ const client = (0, context_1.useOvok)();
99
+ const mutationRef = React.useRef(mutation);
100
+ const pendingCount = React.useRef(0);
101
+ const [data, setData] = React.useState();
102
+ const [loading, setLoading] = React.useState(false);
103
+ const [error, setError] = React.useState();
104
+ mutationRef.current = mutation;
105
+ const mutate = React.useCallback(async (variables) => {
106
+ pendingCount.current += 1;
107
+ setLoading(true);
108
+ setError(undefined);
109
+ try {
110
+ const result = await mutationRef.current(client, variables);
111
+ setData(result);
112
+ return result;
113
+ }
114
+ catch (reason) {
115
+ const mutationError = toError(reason);
116
+ setError(mutationError);
117
+ throw mutationError;
118
+ }
119
+ finally {
120
+ pendingCount.current -= 1;
121
+ setLoading(pendingCount.current > 0);
122
+ }
123
+ }, [client]);
124
+ const reset = React.useCallback(() => {
125
+ setData(undefined);
126
+ setError(undefined);
127
+ }, []);
128
+ return { data, loading, error, mutate, reset };
129
+ };
130
+ exports.useOvokMutation = useOvokMutation;
131
+ const useResource = (type, id, options = {}) => {
132
+ var _a;
133
+ return (0, exports.useOvokQuery)((client, signal) => {
134
+ if (!id) {
135
+ return Promise.reject(new Error("A resource id is required."));
136
+ }
137
+ return client.fhir.read(type, id, { signal });
138
+ }, { queryKey: [type, id], enabled: (_a = options.enabled) !== null && _a !== void 0 ? _a : Boolean(id) });
139
+ };
140
+ exports.useResource = useResource;
141
+ const useSearch = (type, params = {}, options = {}) => (0, exports.useOvokQuery)((client, signal) => client.fhir.search(type, params, { signal }), { queryKey: [type, params], enabled: options.enabled });
142
+ exports.useSearch = useSearch;
143
+ const useSubscription = (subscribe) => {
144
+ const [subscription, setSubscription] = React.useState();
145
+ React.useEffect(() => {
146
+ let active = true;
147
+ const unsubscribe = subscribe((value) => {
148
+ if (active) {
149
+ setSubscription({ subscribe, value });
150
+ }
151
+ });
152
+ return () => {
153
+ active = false;
154
+ unsubscribe();
155
+ };
156
+ }, [subscribe]);
157
+ return (subscription === null || subscription === void 0 ? void 0 : subscription.subscribe) === subscribe ? subscription.value : undefined;
158
+ };
159
+ exports.useSubscription = useSubscription;
160
+ const toError = (reason) => (reason instanceof Error ? reason : new Error(String(reason)));
@@ -7,5 +7,6 @@ export declare class SessionResource {
7
7
  readonly savedAccounts: SavedAccountsResource;
8
8
  constructor(http: HttpClient, session: UserSession | null);
9
9
  onChange(listener: SessionChangeListener): () => void;
10
+ current(): Promise<Session | null>;
10
11
  }
11
12
  export type { Session };
@@ -11,5 +11,9 @@ class SessionResource {
11
11
  var _a, _b;
12
12
  return (_b = (_a = this.session) === null || _a === void 0 ? void 0 : _a.onChange(listener)) !== null && _b !== void 0 ? _b : (() => undefined);
13
13
  }
14
+ current() {
15
+ var _a, _b;
16
+ return (_b = (_a = this.session) === null || _a === void 0 ? void 0 : _a.current()) !== null && _b !== void 0 ? _b : Promise.resolve(null);
17
+ }
14
18
  }
15
19
  exports.SessionResource = SessionResource;
@@ -0,0 +1 @@
1
+ export * from "../../dist/next/react";
@@ -0,0 +1 @@
1
+ module.exports = require("../../dist/next/react/index.js");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ovok/core",
3
- "version": "0.4.49-dev.498+92fd957",
3
+ "version": "0.4.49-dev.499+db0e927",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -59,5 +59,16 @@
59
59
  "@medplum/fhirtypes": ">=3.2.22",
60
60
  "@medplum/react-hooks": ">=3.2.19",
61
61
  "react": "*"
62
+ },
63
+ "peerDependenciesMeta": {
64
+ "react": {
65
+ "optional": true
66
+ },
67
+ "@medplum/core": {
68
+ "optional": true
69
+ },
70
+ "@medplum/react-hooks": {
71
+ "optional": true
72
+ }
62
73
  }
63
74
  }