Skip to content

Commit 384e3d6

Browse files
committed
Make the main entry point's API surface area uniform + expose cache and config fetcher implementations + provide extension points to change request/reponse headers
1 parent bb35f16 commit 384e3d6

25 files changed

+149
-44
lines changed

extra-checks/ts-compat-check/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
import "../../lib/esm/browser";
22
import "../../lib/esm/chromium-extension";
3-
import "../../lib/esm/deno";
43
import "../../lib/esm/node";
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
declare module "http" {
2+
export class IncomingMessage { }
3+
export class OutgoingHttpHeaders { }
4+
}
5+
6+
declare module "@cloudflare/workers-types/2023-03-01" {
7+
8+
}
9+
10+
declare namespace chrome {
11+
export namespace storage {
12+
export class LocalStorageArea { }
13+
}
14+
}

src/browser/LocalStorageConfigCache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ExternalConfigCache } from "../ConfigCatCache";
33
import type { OptionsBase } from "../ConfigCatClientOptions";
44

55
export class LocalStorageConfigCache implements IConfigCatCache {
6-
static tryGetFactory(): ((options: OptionsBase) => IConfigCache) | undefined {
6+
private static tryGetFactory(): ((options: OptionsBase) => IConfigCache) | undefined {
77
const localStorage = getLocalStorage();
88
if (localStorage) {
99
return options => new ExternalConfigCache(new LocalStorageConfigCache(localStorage), options.logger);

src/browser/XmlHttpRequestConfigFetcher.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { FetchRequest, IConfigCatConfigFetcher } from "../ConfigFetcher";
44
import { FetchError, FetchResponse } from "../ConfigFetcher";
55

66
export class XmlHttpRequestConfigFetcher implements IConfigCatConfigFetcher {
7-
static getFactory(): (options: OptionsBase) => IConfigCatConfigFetcher {
7+
private static getFactory(): (options: OptionsBase) => IConfigCatConfigFetcher {
88
return options => {
99
const configFetcher = new XmlHttpRequestConfigFetcher();
1010
configFetcher.logger = options.logger;
@@ -67,13 +67,13 @@ export class XmlHttpRequestConfigFetcher implements IConfigCatConfigFetcher {
6767
});
6868
}
6969

70-
private setRequestHeaders(httpRequest: XMLHttpRequest, headers: ReadonlyArray<[string, string]>) {
70+
protected setRequestHeaders(httpRequest: XMLHttpRequest, headers: ReadonlyArray<[string, string]>): void {
7171
for (const [name, value] of headers) {
7272
httpRequest.setRequestHeader(name, value);
7373
}
7474
}
7575

76-
private getResponseHeaders(httpRequest: XMLHttpRequest): [string, string][] {
76+
protected getResponseHeaders(httpRequest: XMLHttpRequest): [string, string][] {
7777
const headers: [string, string][] = [];
7878
extractHeader("ETag", httpRequest, headers);
7979
extractHeader("CF-RAY", httpRequest, headers);

src/browser/index.root.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,41 @@
1+
import type { IConfigCatClient } from "../ConfigCatClient";
2+
import type { OptionsForPollingMode, PollingMode } from "../ConfigCatClientOptions";
3+
import { getClient as getClientImpl } from ".";
4+
15
/* Package public API for browsers or separate packages that implement a ConfigCat SDK. */
26

7+
/**
8+
* Returns an instance of `ConfigCatClient` for the specified SDK Key.
9+
* @remarks This method returns a single, shared instance per each distinct SDK Key.
10+
* That is, a new client object is created only when there is none available for the specified SDK Key.
11+
* Otherwise, the already created instance is returned (in which case the `pollingMode` and `options` arguments are ignored).
12+
* So, please keep in mind that when you make multiple calls to this method using the same SDK Key, you may end up with multiple references to the same client object.
13+
* @param sdkKey SDK Key to access the ConfigCat config.
14+
* @param pollingMode The polling mode to use.
15+
* @param options Options for the specified polling mode.
16+
*/
17+
export const getClient: <TMode extends PollingMode | undefined>(
18+
sdkKey: string, pollingMode?: TMode, options?: OptionsForPollingMode<TMode>
19+
) => IConfigCatClient = getClientImpl;
20+
21+
export { createConsoleLogger, createFlagOverridesFromMap, disposeAllClients } from "../index.pubternals.core";
22+
23+
export type { INodeAutoPollOptions, INodeLazyLoadingOptions, INodeManualPollOptions } from "../node";
24+
25+
export type { IJSAutoPollOptions, IJSLazyLoadingOptions, IJSManualPollOptions } from ".";
26+
27+
export type { OptionsForPollingMode };
28+
29+
export { LocalStorageConfigCache } from "./LocalStorageConfigCache";
30+
31+
export { IndexedDBConfigCache } from "../shared/IndexedDBConfigCache";
32+
33+
export { XmlHttpRequestConfigFetcher } from "./XmlHttpRequestConfigFetcher";
34+
35+
export { FetchApiConfigFetcher } from "../shared/FetchApiConfigFetcher";
36+
37+
export type { NodeHttpConfigFetcher } from "../node/NodeHttpConfigFetcher";
38+
339
export * as Internals from "../index.pubternals";
4-
export * from ".";
40+
41+
export * from "..";

src/browser/index.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ export function getClient<TMode extends PollingMode | undefined>(sdkKey: string,
3131
defaultCacheFactory: typeof localStorage !== "undefined"
3232
// NOTE: The IndexedDB API is asynchronous, so it's not possible to check here if it actually works. For this reason,
3333
// we'd rather not fall back to IndexedDB if LocalStorage doesn't work. (In that case, IndexedDB is unlikely to work anyway.)
34-
? LocalStorageConfigCache.tryGetFactory()
35-
: IndexedDBConfigCache.tryGetFactory(),
36-
configFetcherFactory: XmlHttpRequestConfigFetcher.getFactory(),
34+
? LocalStorageConfigCache["tryGetFactory"]()
35+
: IndexedDBConfigCache["tryGetFactory"](),
36+
configFetcherFactory: XmlHttpRequestConfigFetcher["getFactory"](),
3737
});
3838
}
3939

@@ -60,4 +60,12 @@ export type OptionsForPollingMode<TMode extends PollingMode | undefined> =
6060
TMode extends undefined ? IJSAutoPollOptions :
6161
never;
6262

63+
export { LocalStorageConfigCache };
64+
65+
export { IndexedDBConfigCache };
66+
67+
export { XmlHttpRequestConfigFetcher };
68+
69+
export { FetchApiConfigFetcher } from "../shared/FetchApiConfigFetcher";
70+
6371
export * from "..";

src/bun/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export function getClient<TMode extends PollingMode | undefined>(sdkKey: string,
2424
sdkType: "ConfigCat-UnifiedJS-Bun",
2525
sdkVersion: CONFIGCAT_SDK_VERSION,
2626
eventEmitterFactory: () => new EventEmitter(),
27-
configFetcherFactory: NodeHttpConfigFetcher.getFactory(),
27+
configFetcherFactory: NodeHttpConfigFetcher["getFactory"](),
2828
});
2929
}
3030

@@ -49,4 +49,8 @@ export type OptionsForPollingMode<TMode extends PollingMode | undefined> =
4949
TMode extends undefined ? IBunAutoPollOptions :
5050
never;
5151

52+
export { NodeHttpConfigFetcher };
53+
54+
export { FetchApiConfigFetcher } from "../shared/FetchApiConfigFetcher";
55+
5256
export * from "..";

src/chromium-extension/ChromeLocalStorageConfigCache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { OptionsBase } from "../ConfigCatClientOptions";
55
/* eslint-disable @typescript-eslint/no-deprecated */
66

77
export class ChromeLocalStorageConfigCache implements IConfigCatCache {
8-
static tryGetFactory(): ((options: OptionsBase) => IConfigCache) | undefined {
8+
private static tryGetFactory(): ((options: OptionsBase) => IConfigCache) | undefined {
99
const localStorage = getChromeLocalStorage();
1010
if (localStorage) {
1111
return options => new ExternalConfigCache(new ChromeLocalStorageConfigCache(localStorage), options.logger);

src/chromium-extension/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ export function getClient<TMode extends PollingMode | undefined>(sdkKey: string,
2828
sdkType: "ConfigCat-UnifiedJS-ChromiumExtension",
2929
sdkVersion: CONFIGCAT_SDK_VERSION,
3030
eventEmitterFactory: () => new DefaultEventEmitter(),
31-
defaultCacheFactory: ChromeLocalStorageConfigCache.tryGetFactory() ?? IndexedDBConfigCache.tryGetFactory(),
32-
configFetcherFactory: FetchApiConfigFetcher.getFactory(),
31+
defaultCacheFactory: ChromeLocalStorageConfigCache["tryGetFactory"]() ?? IndexedDBConfigCache["tryGetFactory"](),
32+
configFetcherFactory: FetchApiConfigFetcher["getFactory"](),
3333
});
3434
}
3535

@@ -56,4 +56,10 @@ export type OptionsForPollingMode<TMode extends PollingMode | undefined> =
5656
TMode extends undefined ? IJSAutoPollOptions :
5757
never;
5858

59+
export { ChromeLocalStorageConfigCache };
60+
61+
export { IndexedDBConfigCache };
62+
63+
export { FetchApiConfigFetcher };
64+
5965
export * from "..";

src/cloudflare-worker/CloudflareConfigCache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ declare const caches: typeof cloudflare.caches;
1111
declare const Response: typeof cloudflare.Response;
1212

1313
export class CloudflareConfigCache implements IConfigCatCache {
14-
static tryGetFactory(): ((options: OptionsBase) => IConfigCache) | undefined {
14+
private static tryGetFactory(): ((options: OptionsBase) => IConfigCache) | undefined {
1515
const cache = getCloudflareCache();
1616
if (cache) {
1717
return options => new ExternalConfigCache(new CloudflareConfigCache(cache), options.logger);

0 commit comments

Comments
 (0)