Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: nodejs support by default #134

Merged
merged 2 commits into from Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not if(window && 'fetch' in window)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking if window is truthy can still trigger TS error "window is not defined"

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 {
Tymek marked this conversation as resolved.
Show resolved Hide resolved
private store = new Map();

public async save(name: string, data: any) {
Expand Down