IBM Cloud Docs
App ConfigurationReactクライアントSDK

App ConfigurationReactクライアントSDK

ibm-appconfiguration-react-client-sdk 使用するアプリケーションのセキュリティを強化するために、initメソッドではプレーンなAPIKeyの代わりに暗号化されたAPIKey を使用することを強く推奨します。 この変更は、ユーザがウェブ・アプリケーションを検査する際に、機密性の高い認証情報の漏洩を防ぐために不可欠です。 すでにプレーンAPIKeyを使用している場合は、ここ に記載されている手順に従って、暗号化APIKeyを生成して使用するようにアプリケーションを更新してください。

概要

IBM Cloud App ConfigurationReact Client SDKは、IBM Cloud上の設定に基づいて、Webアプリケーションで機能フラグとプロパティ評価を実行し、Experimentationのカスタムメトリクスを追跡するために使用されます。{サービスに基づいて、実験用のカスタムメトリクスのトラッキングを行いますApp Configuration

IBM Cloud App Configuration は、一元化された機能管理とコンフィギュレーション・サービスです。 サービスです。 IBM Cloud Webアプリケーションやモバイル・アプリケーション、マイクロサービス、分散環境で使用される で使用されます。

App ConfigurationReactクライアントSDKでウェブアプリケーションをインスツルメンテーションし、「App Configurationダッシュボード、CLI、またはAPIを使用して、コレクションに整理され、セグメントにターゲット化された機能フラグやプロパティを定義します。 クラウド上で機能フラグの状態を切り替え、必要に応じてアプリケーションや環境の機能をアクティブまたは非アクティブにします。 カスタムメトリクスを追跡することで、実験を行い、機能フラグがエンドユーザーに与える影響を測定します。 また、分散アプリケーションのプロパティーを一元的に管理することもできます。

互換性:本SDKはReactバージョン16.8.0以降と互換性があります。 このSDKはApp Configurationて構築されています。JavaScriptクライアントSDKを構築し、Reactアプリケーションで使用するためのより良い統合を提供します。 その結果、'App Configuration'JavaScriptClient SDKの機能の多くは、React Client SDKでも使用できる。 詳しくはApp Configurationをご覧ください。JavaScriptクライアントSDKについては こちら をご覧ください。

Client SDK for React の統合

インストール

SDK をインストールします。

npm install ibm-appconfiguration-react-client-sdk

SDK の初期化

次の例のように、 App Configuration サービスインスタンスに接続するためにSDKを初期化します。 アプリ・コンポーネントを AppConfigProvider でラップすると、任意のレベルのコンポーネント階層から機能の & プロパティーにアクセスできます。

import { withAppConfigProvider } from 'ibm-appconfiguration-react-client-sdk';

(async () => {
  const AppConfigProvider = await withAppConfigProvider({
    region: 'us-south',
    guid: '<guid>',
    apikey: '<encrypted_apikey>',
    collectionId: 'airlines-webapp',
    environmentId: 'dev'
  })

  ReactDOM.render(
    <AppConfigProvider>
        <YourApp />
    </AppConfigProvider>,
    document.getElementById('root')
  );
})();
  • region : App Configuration サービスインスタンスが作成されるリージョン名。 対応拠点のリストは こちら。 例: us-southau-syd など。
  • guid : App Configuration サービスのインスタンス ID。 App Configuration ダッシュボードのサービス資格情報セクションから取得してください。
  • apikey :ここ で説明されているように生成された暗号化されたAPIKey。
  • collectionId: Collections セクションの サービスインスタンスで作成されたコレクションのID。 App Configuration
  • environmentId: App Configuration サービスインスタンスの「 環境」 セクションで作成された環境の ID。

機密情報の漏洩を避けるため、常に暗号化されたAPIKeyを使用してください。
ブラウザベースのアプリケーションで使用するのに適した最小限のアクセス権限を持っているので、「 Client SDK ロールでサービス資格情報を作成するようにしてください。

機能関連 API およびプロパティー関連 API の使用例

フィーチャー関連 API の使用については、以下の例を参照してください。

単一の機能を取得する

import { useFeature } from 'ibm-appconfiguration-react-client-sdk';

const feature = useFeature('featureId'); // returns undefined incase the featureId is invalid or doesn't exist

if (feature !== undefined) {
  console.log(`Feature Name ${feature.getFeatureName()} `);
  console.log(`Feature Id ${feature.getFeatureId()} `);
  console.log(`Feature Type ${feature.getFeatureDataType()} `);
  console.log(`Is feature enabled? ${feature.isEnabled()} `);
}

すべての機能を取得する

import { useFeatures } from 'ibm-appconfiguration-react-client-sdk';

const features = useFeatures();
const feature = features['featureId'];

if (feature !== undefined) {
  console.log(`Feature Name ${feature.getFeatureName()} `);
  console.log(`Feature Id ${feature.getFeatureId()} `);
  console.log(`Feature Type ${feature.getFeatureDataType()} `);
  console.log(`Is feature enabled? ${feature.isEnabled()} `);
}

フィーチャーの評価

feature.getCurrentValue(entityId, entityAttributes) メソッドを使用して、機能フラグの値を評価できます。 このメソッドは、評価に基づいて、有効/無効/オーバーライドされた値のいずれかを返します。 戻り値のデータ・タイプは、フィーチャー・フラグのデータ・タイプと一致します。 機能フラグの評価を実行するためのパラメーターとして、固有の entityId を渡します。

const entityId = 'john_doe';
const entityAttributes = {
  city: 'Bangalore',
  country: 'India',
};

const feature = useFeature('featureId');
const featureValue = feature.getCurrentValue(entityId, entityAttributes);

ここで、

  • entityId: エンティティーの ID。 これは、フィーチャーが評価されるエンティティーに関連するストリング ID になります。 例えば、エンティティはモバイルデバイス上で実行されるアプリのインスタンスかもしれないし、ウェブアプリケーションにアクセスするユーザーかもしれない。 いずれかのエンティティーが App Configuration と対話するには、固有のエンティティー ID を提供する必要があります。
  • entityAttributes: 指定されたエンティティーを定義する属性名とその値で構成される JSON オブジェクト。 フィーチャー・フラグがターゲット定義で構成されていない場合、これはオプション・パラメーターです。 ターゲットが構成されている場合は、ルール評価のために entityAttributes を指定する必要があります。 属性は、セグメントを定義するために使用されるパラメーターです。 SDK は、属性値を使用して、指定されたエンティティーがターゲット・ルールを満たしているかどうかを判別し、適切なフィーチャー・フラグ値を返します。

カスタムメトリクスの送信

実験では「useTrack フックを使用してカスタムメトリクスを記録します。

import { useTrack } from 'ibm-appconfiguration-react-client-sdk';

export default MyComponent = function () {
    const trackEvent = useTrack();
    return (
        <button onClick={() => trackEvent('clicked', 'user123')}>Buy</button>
    )
}

単一のプロパティーを取得する

import { useProperty } from 'ibm-appconfiguration-react-client-sdk';

const property = useProperty('propertyId'); // returns undefined incase the propertyId is invalid or doesn't exist

if (property !== undefined) {
  console.log(`Property Name ${property.getPropertyName()} `);
  console.log(`Property Id ${property.getPropertyId()} `);
  console.log(`Property Type ${property.getPropertyDataType()} `);
}

すべてのプロパティーを取得する

import { useProperties } from 'ibm-appconfiguration-react-client-sdk';

const properties = useProperties();
const property = properties['propertyId'];

if (property !== undefined) {
  console.log(`Property Name ${property.getPropertyName()} `);
  console.log(`Property Id ${property.getPropertyId()} `);
  console.log(`Property Type ${property.getPropertyDataType()} `);
}

プロパティーの評価

不動産の価値を評価するには、 property.getCurrentValue(entityId, entityAttributes) メソッドを使用する。 このメソッドは、評価に基づいて、デフォルトのプロパティー値またはオーバーライドされた値を返します。 戻り値のデータ・タイプは、プロパティーのデータ・タイプと一致します。

const entityId = 'john_doe';
const entityAttributes = {
  city: 'Bangalore',
  country: 'India',
};

const property = useProperty('propertyId');
const propertyValue = property.getCurrentValue(entityId, entityAttributes);

ここで、

  • entityId: エンティティーの ID。 これは、プロパティーの評価対象となるエンティティーに関連するストリング ID になります。 いずれかのエンティティーが App Configuration と対話するには、固有のエンティティー ID を提供する必要があります。
  • entityAttributes: 指定されたエンティティーを定義する属性名とその値で構成される JSON オブジェクト。 ターゲット定義でプロパティーが構成されていない場合、これはオプション・パラメーターです。 ターゲットが構成されている場合は、ルール評価のために entityAttributes を指定する必要があります。 属性は、セグメントを定義するために使用されるパラメーターです。 SDK は、属性値を使用して、指定されたエンティティーがターゲティング・ルールを満たしているかどうかを判別し、適切なプロパティー値を返します。

React Client SDKでフォールバック値を使用する

App Configurationで接続エラーが発生した場合、SDKはメモリに保持されている直近に評価されたフラグ値に依存する。 しかし、メモリ上に事前値が存在しない場合は、ユーザーがコード内でフォールバック値を確立し、スムーズな動作を保証することが望ましい。 このフォールバックアプローチを示す例を以下に示します。


import { useFeatures } from 'ibm-appconfiguration-react-client-sdk';

export default function App {
  const features = useFeatures();
  const defaultFlagValues = {
    'flight-booking': false
  }
  const entityId = 'john_doe';
  const entityAttributes = {
    city: 'Bangalore',
    country: 'India',
  };

  const getAppConfigurationFlags = (featureID, features) => {
    if (Object.keys(features).length === 0 && features.constructor === Object) {
      return defaultFlagValues[featureID];
    }

    return feature[featureID]
      ? feature[featureID].getCurrentValue(entityId, entityAttributes)
      : defaultFlagValues[featureID];
  };

  return getAppConfigurationFlags('flight-booking', features) ? <div>Flight Booking</div> : '';
}

サポート対象データ・タイプ

App Configuration サービスでは、以下のデータタイプで機能フラグとプロパティを設定できます:Boolean、 数値、文字列。 「文字列」データ・タイプは、テキスト文字列、JSON、YAML のフォーマットにできます。 SDKは各 SDKは各フォーマットを次の表のように処理します。

テーブルの表示
機能またはプロパティーの値 DataType DataFormat
getCurrentValue() によって返されるデータのタイプ
出力例
true BOOLEAN 適用外 boolean true
25 数値 適用外 number 25
「文字列テキスト」 STRING テキスト string a string text
{
"firefox": {
"name": "Firefox",
"pref_url": "about:config"
}
}
STRING JSON JSON object {"firefox":{"name":"Firefox","pref_url":"about:config"}}
men:
- John Smith
- Bill Jones
women:
- Mary Smith
- Susan Williams
STRING YAML string

`"men:

  • John Smith
  • Bill Jones
    women:
  • Mary Smith
  • Susan Williams"`
機能フラグの使用例
const feature = useFeature('json-feature');
feature.getFeatureDataType(); // STRING
feature.getFeatureDataFormat(); // JSON

// Example (traversing the returned JSON)
let result = feature.getCurrentValue(entityId, entityAttributes);
console.log(result.key) // prints the value of the key

const feature = useFeature('yaml-feature');
feature.getFeatureDataType(); // STRING
feature.getFeatureDataFormat(); // YAML
feature.getCurrentValue(entityId, entityAttributes); // returns the stringified yaml (check above table)
プロパティーの使用例
const property = useProperty('json-property');
property.getPropertyDataType(); // STRING
property.getPropertyDataFormat(); // JSON

// Example (traversing the returned JSON)
let result = property.getCurrentValue(entityId, entityAttributes);
console.log(result.key) // prints the value of the key

const property = useProperty('yaml-property');
property.getPropertyDataType(); // STRING
property.getPropertyDataFormat(); // YAML
property.getCurrentValue(entityId, entityAttributes); // returns the stringified yaml (check above table)

ライセンス

このプロジェクトはApache 2.0ライセンスでリリースされています。 ライセンスの全文は でご覧いただけます

機能またはプロパティー変更を listen する

SDK は、自動的にイベント・ベースのメカニズムをサブスクライブし、フィーチャー・フラグまたはプロパティーの構成が変更されたときに、囲まれたコンポーネントを再レンダリングします。