From 81e7f46881df99ebfe700d09421c22c1956cc9f3 Mon Sep 17 00:00:00 2001 From: Tymoteusz Czech <2625371+Tymek@users.noreply.github.com> Date: Wed, 11 Jan 2023 11:47:35 +0100 Subject: [PATCH] fix: make defaults work with nodejs --- README.md | 12 ++++++------ src/index.ts | 7 +++++-- src/storage-provider-inmemory.test.ts | 2 +- src/storage-provider-inmemory.ts | 2 +- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index eb6dca7..a9d1a7e 100644 --- a/README.md +++ b/README.md @@ -96,13 +96,13 @@ The Unleash SDK takes the following options: | url | yes | n/a | The Unleash Proxy URL to connect to. E.g.: `https://examples.com/proxy` | | clientKey | yes | n/a | The Unleash Proxy Secret to be used | | appName | yes | n/a | The name of the application using this SDK. Will be used as part of the metrics sent to Unleash Proxy. Will also be part of the Unleash Context. | -| refreshInterval | no | 30 | How often, in seconds, the SDK should check for updated toggle configuration. If set to 0 will disable checking for updates | -| disableRefresh | no | false | If set to true, the client will not check for updated toggle configuration | -| metricsInterval | no | 60 | How often, in seconds, the SDK should send usage metrics back to Unleash Proxy | -| disableMetrics | no | false | Set this option to `true` if you want to disable usage metrics | +| refreshInterval | no | `30` | How often, in seconds, the SDK should check for updated toggle configuration. If set to 0 will disable checking for updates | +| disableRefresh | no | `false` | If set to true, the client will not check for updated toggle configuration | +| metricsInterval | no | `60` | How often, in seconds, the SDK should send usage metrics back to Unleash Proxy | +| disableMetrics | no | `false` | Set this option to `true` if you want to disable usage metrics | | storageProvider | no | `LocalStorageProvider` | Allows you to inject a custom storeProvider | -| environment | no | 'default' | Identify the current environment. Will be part of the Unleash Context | -| fetch | no | window.fetch | Allows you to override the fetch implementation to use. Useful in Node.js environments where you can inject `node-fetch` | +| environment | no | `default` | Identify the current environment. Will be part of the Unleash Context | +| fetch | no | `window.fetch` or global `fetch` | Allows you to override the fetch implementation to use. Useful in Node.js environments where you can inject `node-fetch` | | bootstrap | no | `[]` | Allows you to bootstrap the cached feature toggle configuration. | | bootstrapOverride | no| `true` | Should the bootstrap automatically override cached data in the local-storage. Will only be used if bootstrap is not an empty array. | | headerName | no| `Authorization` | Provides possiblity to specify custom header that is passed to Unleash / Unleash Proxy with the `clientKey` | diff --git a/src/index.ts b/src/index.ts index deb65e4..020f787 100644 --- a/src/index.ts +++ b/src/index.ts @@ -76,7 +76,7 @@ const storeKey = 'repo'; export const resolveFetch = () => { try { - if ('fetch' in window) { + if (typeof window !== 'undefined' && 'fetch' in window) { return fetch.bind(window); } else if ('fetch' in globalThis) { return fetch.bind(globalThis); @@ -146,7 +146,10 @@ export class UnleashClient extends TinyEmitter { this.clientKey = clientKey; this.headerName = headerName; this.customHeaders = customHeaders; - this.storage = storageProvider || new LocalStorageProvider(); + this.storage = + storageProvider || typeof window !== 'undefined' + ? new LocalStorageProvider() + : new InMemoryStorageProvider(); this.refreshInterval = disableRefresh ? 0 : refreshInterval * 1000; this.context = { appName, environment, ...context }; this.usePOSTrequests = usePOSTrequests; diff --git a/src/storage-provider-inmemory.test.ts b/src/storage-provider-inmemory.test.ts index 13412da..af06aec 100644 --- a/src/storage-provider-inmemory.test.ts +++ b/src/storage-provider-inmemory.test.ts @@ -1,6 +1,6 @@ import InMemoryStorageProvider from './storage-provider-inmemory'; -describe('ImMemoryStorageProvider', () => { +describe('InMemoryStorageProvider', () => { it('should store and retrieve arbitrary values by key', async () => { const store = new InMemoryStorageProvider(); diff --git a/src/storage-provider-inmemory.ts b/src/storage-provider-inmemory.ts index e76af9b..63c06b1 100644 --- a/src/storage-provider-inmemory.ts +++ b/src/storage-provider-inmemory.ts @@ -1,6 +1,6 @@ import type IStorageProvider from './storage-provider'; -export default class ImMemoryStorageProvider implements IStorageProvider { +export default class InMemoryStorageProvider implements IStorageProvider { private store = new Map(); public async save(name: string, data: any) {