configcat / node-sdk

ConfigCat SDK for Node.js. ConfigCat is a hosted feature flag service: https://configcat.com. Manage feature toggles across frontend, backend, mobile, desktop apps. Alternative to LaunchDarkly. Management app + feature flag SDKs.
https://configcat.com/docs/sdk-reference/node
MIT License
19 stars 7 forks source link

Call `createClientWithAutoPoll` directly from `createClient` to avoid undefined `this` in strict mode with destructured require #31

Closed Conrad2134 closed 3 years ago

Conrad2134 commented 3 years ago

Describe the purpose of your pull request

When calling createClient from Node using a destructured require() statement, createClient will throw the following error:

TypeError: Cannot read property 'createClientWithAutoPoll' of undefined

Due to TypeScript invoking "strict mode"; for the compiled code, this is undefined in createClient only when invoked directly (because of the destructuring, in this case). There's a few different ways to work around this, and so it shouldn't block anyone, but this change should fix it for that case.

I tested the fix with Node 14 (plain JavaScript) and TypeScript (via ts-node). I tried multiple styles of imports and didn't have any issues.

Reproduction

I reproduced this issue by creating a test.js file in the repo and executing it with node test.js.

const {createClient} = require('./lib/client');
const client = createClient("SDKKEY");

Requirement checklist (only if applicable)

sonarcloud[bot] commented 3 years ago

Kudos, SonarCloud Quality Gate passed!

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

codecov[bot] commented 3 years ago

Codecov Report

Merging #31 (a304e63) into master (a6c9d4f) will decrease coverage by 0.96%. The diff coverage is 100.00%.

:exclamation: Current head a304e63 differs from pull request most recent head 66cefc5. Consider uploading reports for the commit 66cefc5 to get more accurate results Impacted file tree graph

@@            Coverage Diff             @@
##           master      #31      +/-   ##
==========================================
- Coverage   76.47%   75.51%   -0.97%     
==========================================
  Files           2        2              
  Lines          51       49       -2     
  Branches        9        9              
==========================================
- Hits           39       37       -2     
  Misses         12       12              
Impacted Files Coverage Δ
src/client.ts 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update a6c9d4f...66cefc5. Read the comment docs.