All Products
Search
Document Center

EventBridge:SDK for TypeScript

Last Updated:Nov 07, 2023

This topic describes how to install the SDK for TypeScript and use the SDK for TypeScript to publish events. Sample code is also provided in this topic.

Overview

EventBridge SDKs are classified into management API SDKs and data API SDKs. Sample code that is used by different types of SDKs vary.

  • Management API SDKs: the SDKs that are used to perform operations on the EventBridge console.

  • Data API SDKs: the channel for event data. Only the PutEvents API operation is called by using this type of SDK.

Before you start

Perform the following operations:

Environment preparations

  1. Node.js 8.0 or later is installed. For more information, visit the download page of Node.js.

  2. You can run the node -v command to check the version of Node.js.

  3. Run the following command to install TypeScript globally:

  4. npm install -g typescript

Management API SDKs

Install the SDK for TypeScript

Run the following command to install the SDK for TypeScript:

npm install --save @alicloud/eventbridge20200401@1.0.1

Sample code

You can call the corresponding operation in OpenAPI Explorer to automatically generate the sample code. For more information, see Automatic generation of SDK examples.

Data API SDKs

Install the SDK for TypeScript

  1. Run the following command to install the SDK for TypeScript:

    npm install @alicloud/eventbridge -S
  2. Run the following command to install the output library for TypeScript:

    npm install @alicloud/tea-console -S

Sample code

Data API SDKs support only the PutEvents API operation. If you want to use the SDK for TypeScript to publish one or more events, refer to the following sample code:

import EventBridge, * as $EventBridge from '@alicloud/eventbridge';
import Util from '@alicloud/tea-util';
import Console from '@alicloud/tea-console';
import * as $tea from '@alicloud/tea-typescript';

export default class Client {

    /**
     * Use the CreateClient() function to initialize common request parameters. 
     */
    static async createClient(): Promise<EventBridge> {
        let config = new $EventBridge.Config({ });
        SetAccessKeyId(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")).
        SetAccessKeySecret(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")).
        SetEndpoint("<endpoint>")
        return new EventBridge(config);
    }

    /**
     * PutEvents
     */
    static async PutEvents(client: EventBridge): Promise<void> {
        let event = new $EventBridge.CloudEvent({ });
        event.datacontenttype = "application/json";
        event.data = Util.toBytes("test");
        event.id = "a5074581-7e74-4e4c-868f-47e7afdf8445";
        event.source = "acs.oss";
        event.specversion = "1.0";
        event.type = "oss:ObjectCreated:PostObject";
        event.time = "2020-08-24T13:54:05.965Asia/Shanghai";
        event.subject = "1.0";
        event.type = "acs:oss:cn-hangzhou:1234567:xls-papk/game_apk/123.jpg";
        event.extensions = {
            aliyuneventbusname: "demo-bus",
        };
        try {
            let resp = await client.putEvents([
                event
            ]);
            Console.log("--------------------Publish event to the aliyun EventBus--------------------");
            Console.log(Util.toJSONString($tea.toMap(resp)));
        } catch (error) {
            Console.log(error.message);
        }
    }

    static async main(args: string[]): Promise<void> {
        let client = await Client.createClient();
        await Client.PutEvents(client);
    }

}

Client.main(process.argv.slice(2));