1.0.0 • Published 7 months ago

tencloud-js-sdk v1.0.0

Weekly downloads
-
License
-
Repository
-
Last release
7 months ago

tencloud-js-sdk

tencloud-js-sdk is built on top of Vite 4.x and prepared for writing libraries in TypeScript. It generates a hybrid package - both support for CommonJS and ESM modules.

Features

  • Hybrid support - CommonJS and ESM modules
  • IIFE bundle for direct browser support without bundler
  • Typings bundle
  • ESLint - scripts linter
  • Stylelint - styles linter
  • Prettier - formatter
  • Vitest - test framework
  • Husky + lint-staged - pre-commit git hook set up for formatting

development

The starter contains the following scripts:

  • dev - starts dev server
  • build - generates the following bundles: CommonJS (.cjs) ESM (.mjs) and IIFE (.iife.js). The name of bundle is automatically taken from package.json name property
  • test - starts vitest and runs all tests
  • test:coverage - starts vitest and run all tests with code coverage report
  • lint:scripts - lint .ts files with eslint
  • lint:styles - lint .css and .scss files with stylelint
  • format:scripts - format .ts, .html and .json files with prettier
  • format:styles - format .cs and .scss files with stylelint
  • format - format all with prettier and stylelint
  • prepare - script for setting up husky pre-commit hook
  • uninstall-husky - script for removing husky from repository

install

npm install tencloud-js-sdk

usage

get serviceConfig:

import sdk from "tencloud-js-sdk";

const core = sdk({
  accessKeyId: "ido.dev",
  secretKey: "FVKbXtHuOLD3Gw1W",
  requestConfig: {
    prefixUrl: "http://47.116.107.183:30100/",
  },
});
const response = await core.route.getServiceConf("tencloud", "app-version-svc");
// { host: '47.116.107.183', port: '30100', protocol: 'http' }

mqtt:

core.mqtt.connect().then(async mqttClient => {
  const client = mqttClient;
  client.on("error", error => {
    console.log("Error event: " + error.toString());
  });

  client.on("messageReceived", eventData => {
    console.log(
      "Message Received event: " + JSON.stringify(eventData.message),
      eventData.message.payload?.toString()
    );
    if (eventData.message.payload) {
      // console.log("  with payload: " + toUtf8(eventData.message.payload as Buffer));
      console.log("  with payload: " + eventData.message.payload);
    }
  });

  client.on("attemptingConnect", () => {
    console.log("Attempting Connect event");
  });

  client.on("connectionSuccess", eventData => {
    console.log("Connection Success event");
    console.log("Connack: " + JSON.stringify(eventData.connack));
    console.log("Settings: " + JSON.stringify(eventData.settings));
  });

  client.on("connectionFailure", eventData => {
    console.log("Connection failure event: " + eventData.error.toString());
  });

  client.on("disconnection", eventData => {
    console.log("Disconnection event: " + eventData.error.toString());
    if (eventData.disconnect !== undefined) {
      console.log("Disconnect packet: " + JSON.stringify(eventData.disconnect));
    }
  });

  client.on("stopped", () => {
    console.log("Stopped event");
  });

  client.start();

  const suback = await client.subscribe({
    subscriptions: [
      { qos: 1, topicFilter: "hello/world/qos1" },
      { qos: 0, topicFilter: "hello/world/qos0" },
    ],
  });
  console.log("Suback result: " + JSON.stringify(suback));

  const qos0PublishResult = await client.publish({
    qos: 1,
    topicName: "hello/world/qos0",
    payload: "This is a qos 0 payload-haha",
  });
  console.log("QoS 0 Publish result: " + JSON.stringify(qos0PublishResult));

  const disconnection = once(client, "disconnection");
  const stopped = once(client, "stopped");

  client.stop();

  await disconnection;
  await stopped;

  client.close();
});