1.0.22 • Published 4 years ago

@bentley/imodel-selector v1.0.22

Weekly downloads
-
License
MIT
Repository
-
Last release
4 years ago

@bentley/imodel-selector

Copyright © Bentley Systems, Incorporated. All rights reserved.

Description

The @bentley/imodel-selector package provides an easy way to sign-in and select the iModel that is to be used by the host app.

Simply call launchIModelSelector() with some optional parameters. Once completed, it will return an access token and selected context/model id.

  export interface IModelSelectorConfig {
                              // defaults:
    client_id?: string;       // imodeljs-electron-samples              (default for testing only - register your app for deployment)
    only_signin?: boolean;    // false                                  (set true if project/iModel selection is not needed)
    select_project?: boolean; // false                                  (set true if iModel selection is not needed)
    redirect_uri?: string;    // http://localhost:3000/signin-callback  (almost always use the default)
    scope?: string;           // openid imodelhub context-registry-service:read-only urlps-third-party  (minimum required scope)
    verbose?: boolean         // false                                  (set true to turn on verbose log messages)
  }

  export class IModelSelectorData {

    public contextId?: string;
    public imodelId?: string;
    public async getAccessToken(): Promise<AccessToken | undefined>
  }

  // Sample Typescript code for using iModelSelector utility to open iModel

  import { AccessToken, AuthorizedClientRequestContext } from "@bentley/imodeljs-clients";
  import { IModelHost, IModelDb } from "@bentley/imodeljs-backend";
  import { launchIModelSelector, IModelSelectorConfig, IModelSelectorData } from "@bentley/imodel-selector";

  export async function main(process: NodeJS.Process): Promise<void> {
    IModelHost.startup();

    // Start the iModelSelector executable to perform sign-in and iModel selection
    const configParams: IModelSelectorConfig = {client_id: "imodeljs-electron-samples"};

    const imodelSelectorData: IModelSelectorData | undefined = await launchIModelSelector(configParams);
    if (!imodelSelectorData)
      return;

    // Get the access token
    const accessToken: AccessToken | undefined = await imodelSelectorData.getAccessToken();
    if (!accessToken)
      return;

    // Use the access token to create the authorization context
    const requestContext: AuthorizedClientRequestContext = new AuthorizedClientRequestContext(accessToken);

    // Open the iModel
    const iModel = await IModelDb.open(requestContext, imodelSelectorData.contextId, imodelSelectorData.imodelId);

    // Close the iModel
    iModel.close(requestContext);
  }