flexpa/login

FHIR Hooks

The Flexpa React SDK provides you all of the tools needed to build an application backed by Patient data.

Our React Hooks are your primary way of interacting with Flexpa. These allow you to execute FHIR queries in your application.

Hooks:

  • useFlexpaConfig
    • Connect to the Flexpa React State tree. Returns the state object as well as a dispatch method to make changes to the state.
  • useFHIRRead
    • Send a Read request to a FHIR API.
  • useFHIRSearch
    • Send a Search request to a FHIR API.

Usage of useFHIRRead and useFHIRSearch

These two methods will be your primary way of interacting with FHIR APIs. Read requests query a specific resource by ID, and Search requests return many resources that match some criteria.

  • Both methods expect a type parameter <T>, which must be a FHIR Resource. This type will be used to strongly type the returned data.
  • Both methods will return an object with two properties, data and error.

useFHIRRead

Expects three parameters:

  • fhirResource (string): The FHIR Resource we want to read
  • id (string): The ID of the Resource we want to read
  • options (object): see below

Usage

const options = {
  baseURL: 'https://api.sandbox.flexpa.com/fhir/',
  authorization: 'logged-in-jwt',
  query: { _count: 10 },
}
const { data, error } = useFHIRRead<Patient>('Patient', 'A000123', options);

useFHIRSearch

Expects two parameters:

  • fhirResource (string): The FHIR Resource we want to search
  • options (object): see below

Usage

const options = {
  baseURL: 'https://api.sandbox.flexpa.com/fhir/',
  authorization: 'logged-in-jwt',
  query: { birthdate: '2022-01-01' },
}
const { data, error } = useFHIRSearch<Patient>('Patient', options);

Options Param

const options = {
  baseURL: string,
  authorization: string,
  query?: Record<string, string>,
};

Executing the FHIR Query Yourself

If you know what FHIR Resources you need to query, you can use our React hooks to accomplish this.

If we want to search for a Patient's Coverage, we will need:

  • useFlexpaConfig
    • get the Patient's logged in context (a JWT), as well as the Flexpa URL for the server you will need to query.
  • useFHIRSearch
    • make FHIR queries against our API, returning strongly typed data sets.

Here is an example of trying to find a Dental Coverage Plan:

import { useFlexpaConfig, useFHIRSearch } from '@flexpa/login';

const TestComponent = () => {
  const { state } = useFlexpaConfig();
  const options = {
    baseURL: state.gatewayUrl,
    authorization: state.flexpaJwt,
  };
  const { data: coverages, error } = useFHIRSearch<Coverage>('Coverage', options);

  if (coverages.find(coverage => coverage.type.text === 'Dental Care Policy')) {
    return 'Verified!';
  }

  return 'Unverified';
}

Note: It's important that this component must be rendered inside a FlexpaLogin context (see below), otherwise the useFlexpaConfig hook will throw an error.

<FlexpaLogin>
  <!--
    Your Custom component needs
    to be rendered as a child of <FlexpaLogin />
  -->
  <TestComponent />
</FlexpaLogin>

Constructing the FHIR Query Yourself

If you wish, you can contruct the actual HTTP query yourself with just a few pieces of information. The pieces you will need are:

  • The base URL (obtained from useFlexpaConfig)
  • The JWT for the logged in Patient (obtained from useFlexpaConfig)
  • The FHIR Resource you wish to query (this is simply 'Coverage' for the example above)
  • Any search params necessary (i.e. "patient=Patient/123")

This turns into a request of the form:

$ curl -H "Authorization: Bearer ${JWT}" ${BASE_URL}/${FHIR_RESOURCE}?patient=Patient/123