Skip to content

Commit

Permalink
Merge pull request #531 from lifeomic/FLME-639
Browse files Browse the repository at this point in the history
Start using @lifeomic/react-client
  • Loading branch information
swain committed Feb 21, 2024
2 parents dedc3fb + c2f6d56 commit 2e8e8da
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 321 deletions.
2 changes: 1 addition & 1 deletion example/storybook/helpers/DataProviderDecorator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const DataOverrideProvider: React.FC<DataOverrideProviderProps> = ({
builder,
children,
}) => {
const axiosInstance = axios.create();
const axiosInstance = axios.create() as any;
const mock = new MockAdapter(axiosInstance);

if (builder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { HttpClientContextProvider } from '../../../../../src/hooks/useHttpClien
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { DeveloperConfigProvider } from '../../../../../src/components/DeveloperConfigProvider';

const axiosInstance = axios.create();
const axiosInstance = axios.create() as any;
const mock = new MockAdapter(axiosInstance);

export const MockEnvironmentDecorator = ({
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@
"dependencies": {
"@lifeomic/chromicons-native": "^2.16.1",
"@lifeomic/one-query": "^3.3.0",
"@lifeomic/react-client": "^1.3.2",
"@types/fhir": "^0.0.36",
"color": "^4.2.3",
"d3-scale": "3.2.1",
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/apps-api.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { setupServer } from 'msw/node';
import { renderHook } from '@testing-library/react-native';
import { QueryClientProvider, QueryClient } from '@tanstack/react-query';
import { useAppsAPIMutation } from './apps-api';
import { AppsAPIEndpoints } from '../types/apps-rest-types';
import { AuthAPIEndpoints } from '@lifeomic/react-client';

const server = setupServer();
server.listen({ onUnhandledRequest: 'error' });

const adoptAppsAPIMocking = createAPIMockingUtility<AppsAPIEndpoints>({
const adoptAppsAPIMocking = createAPIMockingUtility<AuthAPIEndpoints>({
server,
baseUrl: 'https://apps.us.lifeomic.com',
});
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/apps-api.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { createAPIHooks } from '@lifeomic/one-query';
import axios from 'axios';

import { AppsAPIEndpoints } from '../types/apps-rest-types';
import { AuthAPIEndpoints } from '@lifeomic/react-client';
import { useDeveloperConfig } from './useDeveloperConfig';

export const defaultBaseURL = 'https://apps.us.lifeomic.com';

const hooks = createAPIHooks<AppsAPIEndpoints>({
const hooks = createAPIHooks<AuthAPIEndpoints>({
name: 'lifeomic-apps-react-native-sdk',
client: () => {
// hook will be used according to rules of hooks
Expand Down
42 changes: 38 additions & 4 deletions src/hooks/rest-api.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
import { createAPIHooks } from '@lifeomic/one-query';
import { createRestAPIHooks, RestAPIEndpoints } from '@lifeomic/react-client';
import { useHttpClient } from './useHttpClient';
import { RestAPIEndpoints } from '../types/rest-types';
import { AppConfig } from './useAppConfig';
import { APIQueryHooks, RequestPayloadOf } from '@lifeomic/one-query';
import {
UseQueryOptions,
UseQueryResult,
} from '@tanstack/react-query/build/lib/types';
import { AxiosRequestConfig } from 'axios';

const hooks = createAPIHooks<RestAPIEndpoints>({
export type Overrides = {
'GET /v1/life-research/projects/:projectId/app-config': {
Request: {};
Response: AppConfig;
};
};

declare type RestrictedUseQueryOptions<
Response,
TError = unknown,
Data = Response,
> = Omit<UseQueryOptions<Response, TError, Data>, 'queryKey' | 'queryFn'> & {
axios?: AxiosRequestConfig;
};

export type Endpoints = RestAPIEndpoints & Overrides;

const hooks: APIQueryHooks<Endpoints> = createRestAPIHooks<Overrides>({
name: 'lifeomic-react-native-sdk',
client: () => {
// This function is explicitly documented as being hooks-compatible.
Expand All @@ -13,7 +36,18 @@ const hooks = createAPIHooks<RestAPIEndpoints>({
},
});

export const useRestQuery = hooks.useAPIQuery;
export const useRestQuery: <
Route extends keyof Endpoints & string,
Data = Endpoints[Route]['Response'],
>(
route: Route,
payload: RequestPayloadOf<Endpoints, Route>,
options?: RestrictedUseQueryOptions<
Endpoints[Route]['Response'],
unknown,
Data
>,
) => UseQueryResult<Data> = hooks.useAPIQuery;

export const useInfiniteRestQuery = hooks.useInfiniteAPIQuery;

Expand Down
45 changes: 42 additions & 3 deletions src/hooks/useConsent.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import { useActiveProject } from './useActiveProject';
import { Consent, Questionnaire } from 'fhir/r3';
import { useRestQuery, useRestMutation, useRestCache } from './rest-api';
import { RestAPIEndpoints } from '../types/rest-types';
import {
useRestQuery,
useRestMutation,
useRestCache,
Endpoints,
} from './rest-api';
import { RestAPIEndpoints } from '@lifeomic/react-client';
import { RequestPayloadOf } from '@lifeomic/one-query';
import { useQueryClient } from '@tanstack/react-query';
import {
UseMutationResult,
UseQueryResult,
useQueryClient,
} from '@tanstack/react-query';
import { ACTIVITIES_QUERY_KEY } from './useActivities';
import cloneDeep from 'lodash/cloneDeep';

Expand Down Expand Up @@ -104,6 +113,36 @@ export const useConsent = () => {
useConsentDirectives,
useUpdateProjectConsentDirective,
useShouldRenderConsentScreen,
} as {
useConsentDirectives: () => UseQueryResult<{
items: ConsentAndForm[];
}>;
useUpdateProjectConsentDirective: (
options?:
| {
onSuccess?:
| ((
data: PatchConsentDirectives['Response'],
variables: PatchConsentDirectives['Request'] & {
directiveId: string;
},
) => Promise<void> | void)
| undefined;
}
| undefined,
) => UseMutationResult<
{},
unknown,
RequestPayloadOf<
Endpoints,
'PATCH /v1/consent/directives/me/:directiveId'
>
>;
useShouldRenderConsentScreen: () => {
isLoading: boolean;
consentDirectives: ConsentAndForm[] | undefined;
shouldRenderConsentScreen: boolean;
};
};
};

Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useHttpClient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import axios, {
} from 'axios';
import { useAuth } from './useAuth';
import { APIClient } from '@lifeomic/one-query';
import { RestAPIEndpoints } from '../types/rest-types';
import { useActiveAccount } from './useActiveAccount';
import { RestAPIEndpoints } from '@lifeomic/react-client';

export const defaultBaseURL = 'https://api.us.lifeomic.com';
export const defaultHeaders: RawAxiosRequestHeaders = {
Expand Down
11 changes: 7 additions & 4 deletions src/test-utils/rest-api-mocking.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { createAPIMocker } from '@lifeomic/one-query/test-utils';
import { RestAPIEndpoints } from '../types/rest-types';
import { APIMocker, createAPIMocker } from '@lifeomic/one-query/test-utils';
import { RestAPIEndpoints } from '@lifeomic/react-client';
import { SetupServerApi, setupServer } from 'msw/node';
import { Overrides } from '../hooks/rest-api';

const server = setupServer();
server.listen({ onUnhandledRequest: 'error' });

export const createRestAPIMock = () =>
createAPIMocker<RestAPIEndpoints>(
export const createRestAPIMock: () => APIMocker<
RestAPIEndpoints & Overrides
> = () =>
createAPIMocker<RestAPIEndpoints & Overrides>(
server as SetupServerApi,
'https://api.us.lifeomic.com',
);
50 changes: 0 additions & 50 deletions src/types/apps-rest-types.ts

This file was deleted.

106 changes: 0 additions & 106 deletions src/types/fhir-api-types.ts

This file was deleted.

Loading

0 comments on commit 2e8e8da

Please sign in to comment.