TypeScript Config class

更新时间:
复制 MD 格式

The Config class configures the Open Search TypeScript SDK client. It holds the endpoint, authentication credentials, and settings your application needs to call Open Search API operations.

Prerequisites

Before you begin, ensure that you have:

Important

Use a RAM user's AccessKey pair instead of your Alibaba Cloud account's AccessKey pair. An account-level AccessKey pair grants access to all API operations — if it is leaked, all resources in your account are at risk. For information about creating a RAM user, see Create a RAM user. For information about granting the required permissions, see AliyunServiceRoleForOpenSearch.

Install dependencies

Install the required packages from npmjs.com.

Runtime dependencies:

npm install @alicloud/credentials @alicloud/opensearch-util @alicloud/tea-typescript @alicloud/tea-util

Development dependencies:

npm install --save-dev typescript ts-node

Set environment variables

Store your AccessKey pair as environment variables to avoid hardcoding credentials in your source code.

  • Linux and macOS Run the following commands. Replace <access_key_id> and <access_key_secret> with the AccessKey ID and AccessKey secret of your RAM user.

    export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id>
    export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>
  • Windows

    1. Create an environment variable file, add ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET to the file, and set each to the corresponding value from your RAM user's AccessKey pair.

    2. Restart Windows for the changes to take effect.

Config class reference

The Config class extends $tea.Model and accepts the following fields in its constructor.

FieldTypeRequiredDescription
endpointstringNoThe endpoint URL for the Open Search service in your region.
protocolstringNoThe transfer protocol.
typestringNoThe authentication type.
securityTokenstringNoThe Security Token Service (STS) token for temporary credentials.
accessKeyIdstringNoThe AccessKey ID. Read from the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable.
accessKeySecretstringNoThe AccessKey secret. Read from the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable.
userAgentstringNoA custom user agent string appended to SDK requests.

Config.ts

import * as $tea from '@alicloud/tea-typescript';

class Config extends $tea.Model {
  endpoint?: string;
  protocol?: string;
  type?: string;
  securityToken?: string;
  accessKeyId?: string;
  accessKeySecret?: string;
  userAgent?: string;
  static names(): { [key: string]: string } {
    return {
      endpoint: 'endpoint',
      protocol: 'protocol',
      type: 'type',
      securityToken: 'securityToken',
      // Read credentials from environment variables.
      // Set ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET
      // before running this code. See "Set environment variables" above.
      accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
      accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET,
      userAgent: 'userAgent',
    };
  }

  static types(): { [key: string]: any } {
    return {
      endpoint: 'string',
      protocol: 'string',
      type: 'string',
      securityToken: 'string',
      accessKeyId: 'string',
      accessKeySecret: 'string',
      userAgent: 'string',
    };
  }

  constructor(map?: { [key: string]: any }) {
    super(map);
  }
}

export default Config;

What's next