All files / src/components/context APIContext.ts

92.3% Statements 12/13
0% Branches 0/1
100% Functions 2/2
90.9% Lines 10/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60            4x 4x 4x                                       4x 4x                 4x                           4x 367x 367x     367x    
/*---------------------------------------------------------------------------------------------
 * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
 * See LICENSE.md in the project root for license terms and full copyright notice.
 *--------------------------------------------------------------------------------------------*/
import type { AccessToken } from "@itwin/core-bentley";
import type { IEC3ConfigurationsClient, IEC3JobsClient, IOdataClient, IReportsClient } from "@itwin/insights-client";
import { EC3ConfigurationsClient, EC3JobsClient, ODataClient, ReportsClient } from "@itwin/insights-client";
import { createContext, useContext } from "react";
import { EC3Config } from "../EC3/EC3Config";
 
/**
 * Get Access Token callback function
 * @public
 */
export type GetAccessTokenFn = () => Promise<AccessToken>;
 
/**
 * EC3 API Context
 * @beta
 */
export interface EC3ApiContext {
  reportsClient: IReportsClient;
  oDataClient: IOdataClient;
  ec3JobsClient: IEC3JobsClient;
  ec3ConfigurationsClient: IEC3ConfigurationsClient;
  config: EC3Config;
}
 
export const createApiContext = (config: EC3Config) => {
  return {
    reportsClient: config.reportsClient,
    oDataClient: config.oDataClient,
    ec3JobsClient: config.ec3JobsClient,
    ec3ConfigurationsClient: config.ec3ConfigurationsClient,
    config,
  };
};
 
export const ApiContext = createContext<EC3ApiContext>(
  createApiContext(
    new EC3Config({
      iTwinId: "",
      clientId: "",
      redirectUri: "",
      reportsClient: new ReportsClient(),
      oDataClient: new ODataClient(),
      ec3JobsClient: new EC3JobsClient(),
      ec3ConfigurationsClient: new EC3ConfigurationsClient(),
    }),
  ),
);
 
export const useApiContext = () => {
  const context = useContext(ApiContext);
  Iif (!context) {
    throw new Error("useAPIContext should be used within an APIContext provider");
  }
  return context;
};