Skip to content

Commit

Permalink
Merge 9effcc9 into 4a54dce
Browse files Browse the repository at this point in the history
  • Loading branch information
jkdowdle committed Jan 24, 2024
2 parents 4a54dce + 9effcc9 commit 907f44f
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/common/DeveloperConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export type DeveloperConfig = {
simpleTheme?: SimpleTheme;
brand?: Brand;
apiBaseURL?: string;
appsAPIBaseURL?: string;
AppNavHeader?: {
headerColors?: RouteColor[];
onHeaderColors?: RouteColor[];
Expand Down
56 changes: 56 additions & 0 deletions src/hooks/apps-api.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import React from 'react';
import { createAPIMockingUtility } from '@lifeomic/one-query/test-utils';
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';

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

const adoptAppsAPIMocking = createAPIMockingUtility<AppsAPIEndpoints>({
server,
baseUrl: 'https://apps.us.lifeomic.com',
});

const renderHookInContext = () => {
return renderHook(() => useAppsAPIMutation('POST /auth/v1/api/signup'), {
wrapper: ({ children }) => (
<QueryClientProvider client={new QueryClient()}>
{children}
</QueryClientProvider>
),
});
};

const api = adoptAppsAPIMocking();

test('makes mutation request', async () => {
const credentials = {
userConfirmed: true,
tokenType: 'Bearer',
accessToken: 'access-token',
identityToken: 'identity-token',
refreshToken: 'refresh-token',
expiresIn: 3600,
originalUrl: 'fountainlife://evc-signup',
} as const;

api.mock('POST /auth/v1/api/signup', {
status: 200,
data: credentials,
});

const { result } = renderHookInContext();

const response = await result.current.mutateAsync({
evc: 'evc-code',
inviteId: 'invite-id',
originalUrl: 'fountainlife://evc-signup',
clientId: 'client-id',
email: 'bat.man@thecave.com',
});

expect(response).toEqual(credentials);
});
20 changes: 20 additions & 0 deletions src/hooks/apps-api.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { createAPIHooks } from '@lifeomic/one-query';
import axios from 'axios';

import { AppsAPIEndpoints } from '../types/apps-rest-types';
import { useDeveloperConfig } from './useDeveloperConfig';

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

const hooks = createAPIHooks<AppsAPIEndpoints>({
name: 'lifeomic-apps-react-native-sdk',
client: () => {
// hook will be used according to rules of hooks
// https://github.com/lifeomic/one-query?tab=readme-ov-file#createapihooks
// eslint-disable-next-line react-hooks/rules-of-hooks
const { appsAPIBaseURL = defaultBaseURL } = useDeveloperConfig();
return axios.create({ baseURL: appsAPIBaseURL });
},
});

export const useAppsAPIMutation = hooks.useAPIMutation;
50 changes: 50 additions & 0 deletions src/types/apps-rest-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* While performing authorization through an external user-agent (browser)
* is recommended and the only way to be OAuth 2.0 compliant, there are
* publicly available endpoints that can be used to build a fully native
* experience.
*/
export type AppsAPIEndpoints = {
'POST /auth/v1/api/invite-validations': {
Request: {
inviteId: string;
evc: string;
};
Response: {
userState: {
isExistingUser: boolean;
};
invitation: {
email: string;
group: string;
project: string;
};
};
};
'POST /auth/v1/api/signup': {
Request: {
clientId: string;
email: string;
/**
* evc, originalUrl, inviteId are not technically required, but it
* is the only way we currently support signup through POST /signup
*/
originalUrl: string;
evc: string;
inviteId: string;
};
Response:
| {
userConfirmed?: false;
}
| {
userConfirmed: true;
accessToken: string;
refreshToken: string;
identityToken: string;
expiresIn: number;
tokenType: 'Bearer';
originalUrl: string;
};
};
};

0 comments on commit 907f44f

Please sign in to comment.