featurehub-io / featurehub

FeatureHub - cloud native feature flags, A/B testing and remote configuration service. Real-time streaming feature updates. Provided with Java, JavaScript, React, Python, Go, .Net, Ruby, Android, Swift and Flutter SDKs.
https://www.featurehub.io
Other
315 stars 31 forks source link

TypeScript-SDK: Unable to use client in a require-less/nodeless environment with ES6 #301

Closed digital-h closed 3 years ago

digital-h commented 3 years ago

Describe the bug I'm following the examples to include the TypeScript-SDK at https://github.com/featurehub-io/featurehub/tree/master/sdks/typescript/client-typescript-eventsource

I do not have any node.js running, but would like to use it with my lit-element / web-component frontend.

"featurehub-eventsource-sdk": "^1.2.1" "typescript": "4.1.3" "es-dev-server": "1.60.2" "lit-element": "2.4.0"

import { FeatureHubEventSourceClient } from 'featurehub-eventsource-sdk'
import { featureHubRepository } from 'featurehub-repository/dist';

type Constructor<T> = { new (...args: any[]): T };

class ConfigData {
  fhServerBaseUrl: string;
  sdkUrl: string;
}

export function FeatureToggleMixin <B extends Constructor<any>>(base: B) {

  return class extends base {

    _config: ConfigData;
    _client : FeatureHubEventSourceClient;
    _initialized = false;

    constructor(...args: any[]) {
      super(args);

      this._config = {
        fhServerBaseUrl: 'http://localhost:8085',
        sdkUrl: 'default/823a453b-c7d8-4ad8-bb4c-82436c77a531/QDxTTOLCRcT8kAPiURFLpse8kQLbLUnZEy1nokEV2m2E7FSqlfpbXKNSBAIoBsPLW0giqY8CXfJJcAJT'
      }
    }

    async initializeFeatureHub() {

      if (featureHubRepository.readyness === "Ready" || this._client) {
        return;
      }
      featureHubRepository.addReadynessListener((readyness) => {
        if (!this._initialized) {
          console.log('readyness', readyness);
          if (readyness === "Ready") {
            this._initialized = true;
            if (featureHubRepository.getFlag('test_toggle_1')) {
              // do something
            }
          }
        }
      });

      this._client = new FeatureHubEventSourceClient(`${this._config.fhServerBaseUrl}/features/${this._config.sdkUrl}`);
      this._client.init();
    }
  }
}

When opening the html-page, I get the following error: Uncaught SyntaxError: The requested module '../../../featurehub-repository/dist/index.js' does not provide an export named 'featureHubRepository'

Which area does this issue belong to?

To Reproduce Steps to reproduce the behavior:

Expected behavior No error-message, but a functional SDK

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

You can get the version of the FeatureHub container by running docker ps command. Please include the OS and what version of the OS and Docker you're running.

featurehub/party-server:latest

Additional context An ESM wrapper for your CJS named exports might already be a solution to my problem. https://blog.logrocket.com/publishing-node-modules-typescript-es-modules/

Working with https://github.com/postui/esm.sh seems to work

import * as fhe from 'https://esm.sh/featurehub-eventsource-sdk@1.2.1'
import * as fhr from 'https://esm.sh/featurehub-repository@1.2.2';
rvowles commented 3 years ago

Thank you for the reference to this article @digital-h - we will release a new version that is published as both commonjs and modules to resolve the issue. We'll update this ticket once we have done this!

rvowles commented 3 years ago

This was resolved by the move to featurehub-javascript-client-sdk.