Skip to content

Commit

Permalink
fix: nodejs support by default (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tymek committed Jan 12, 2023
1 parent d5f0c74 commit 2fd7282
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
14 changes: 7 additions & 7 deletions README.md
Expand Up @@ -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 |
| 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` |
| 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` in browser, `InMemoryStorageProvider` otherwise | 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` 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` |
Expand Down
8 changes: 6 additions & 2 deletions src/index.ts
Expand Up @@ -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);
Expand Down Expand Up @@ -146,7 +146,11 @@ 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;
Expand Down
2 changes: 1 addition & 1 deletion 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();

Expand Down
2 changes: 1 addition & 1 deletion 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) {
Expand Down

0 comments on commit 2fd7282

Please sign in to comment.