@algolia/client-common 4.14.2 → 5.0.0-alpha.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client-common.cjs.js +804 -92
- package/dist/client-common.esm.node.js +776 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/src/cache/createBrowserLocalStorageCache.d.ts +3 -0
- package/dist/src/cache/createBrowserLocalStorageCache.d.ts.map +1 -0
- package/dist/src/cache/createFallbackableCache.d.ts +3 -0
- package/dist/src/cache/createFallbackableCache.d.ts.map +1 -0
- package/dist/src/cache/createMemoryCache.d.ts +3 -0
- package/dist/src/cache/createMemoryCache.d.ts.map +1 -0
- package/dist/src/cache/createNullCache.d.ts +3 -0
- package/dist/src/cache/createNullCache.d.ts.map +1 -0
- package/dist/src/cache/index.d.ts +5 -0
- package/dist/src/cache/index.d.ts.map +1 -0
- package/dist/src/constants.d.ts +7 -0
- package/dist/src/constants.d.ts.map +1 -0
- package/dist/src/createAlgoliaAgent.d.ts +3 -0
- package/dist/src/createAlgoliaAgent.d.ts.map +1 -0
- package/dist/src/createAuth.d.ts +6 -0
- package/dist/src/createAuth.d.ts.map +1 -0
- package/dist/src/createEchoRequester.d.ts +7 -0
- package/dist/src/createEchoRequester.d.ts.map +1 -0
- package/dist/src/createIterablePromise.d.ts +13 -0
- package/dist/src/createIterablePromise.d.ts.map +1 -0
- package/dist/src/getAlgoliaAgent.d.ts +8 -0
- package/dist/src/getAlgoliaAgent.d.ts.map +1 -0
- package/dist/src/transporter/createStatefulHost.d.ts +3 -0
- package/dist/src/transporter/createStatefulHost.d.ts.map +1 -0
- package/dist/src/transporter/createTransporter.d.ts +3 -0
- package/dist/src/transporter/createTransporter.d.ts.map +1 -0
- package/dist/src/transporter/errors.d.ts +21 -0
- package/dist/src/transporter/errors.d.ts.map +1 -0
- package/dist/src/transporter/helpers.d.ts +9 -0
- package/dist/src/transporter/helpers.d.ts.map +1 -0
- package/dist/src/transporter/index.d.ts +7 -0
- package/dist/src/transporter/index.d.ts.map +1 -0
- package/dist/src/transporter/responses.d.ts +5 -0
- package/dist/src/transporter/responses.d.ts.map +1 -0
- package/dist/src/transporter/stackTrace.d.ts +4 -0
- package/dist/src/transporter/stackTrace.d.ts.map +1 -0
- package/dist/src/types/cache.d.ts +47 -0
- package/dist/src/types/cache.d.ts.map +1 -0
- package/dist/src/types/createClient.d.ts +12 -0
- package/dist/src/types/createClient.d.ts.map +1 -0
- package/dist/src/types/createIterablePromise.d.ts +36 -0
- package/dist/src/types/createIterablePromise.d.ts.map +1 -0
- package/dist/src/types/host.d.ts +33 -0
- package/dist/src/types/host.d.ts.map +1 -0
- package/dist/src/types/index.d.ts +7 -0
- package/dist/src/types/index.d.ts.map +1 -0
- package/dist/src/types/requester.d.ts +66 -0
- package/dist/src/types/requester.d.ts.map +1 -0
- package/dist/src/types/transporter.d.ts +128 -0
- package/dist/src/types/transporter.d.ts.map +1 -0
- package/index.ts +9 -0
- package/package.json +25 -15
- package/src/__tests__/cache/browser-local-storage-cache.test.ts +134 -0
- package/src/__tests__/cache/fallbackable-cache.test.ts +126 -0
- package/src/__tests__/cache/memory-cache.test.ts +90 -0
- package/src/__tests__/cache/null-cache.test.ts +49 -0
- package/src/__tests__/create-iterable-promise.test.ts +238 -0
- package/src/cache/createBrowserLocalStorageCache.ts +74 -0
- package/src/cache/createFallbackableCache.ts +53 -0
- package/src/cache/createMemoryCache.ts +56 -0
- package/src/cache/createNullCache.ts +34 -0
- package/src/cache/index.ts +4 -0
- package/src/constants.ts +7 -0
- package/src/createAlgoliaAgent.ts +20 -0
- package/src/createAuth.ts +25 -0
- package/src/createEchoRequester.ts +59 -0
- package/src/createIterablePromise.ts +47 -0
- package/src/getAlgoliaAgent.ts +25 -0
- package/src/transporter/createStatefulHost.ts +24 -0
- package/src/transporter/createTransporter.ts +350 -0
- package/src/transporter/errors.ts +51 -0
- package/src/transporter/helpers.ts +119 -0
- package/src/transporter/index.ts +6 -0
- package/src/transporter/responses.ts +23 -0
- package/src/transporter/stackTrace.ts +30 -0
- package/src/types/cache.ts +61 -0
- package/src/types/createClient.ts +23 -0
- package/src/types/createIterablePromise.ts +40 -0
- package/src/types/host.ts +38 -0
- package/src/types/index.ts +6 -0
- package/src/types/requester.ts +72 -0
- package/src/types/transporter.ts +153 -0
- package/dist/client-common.d.ts +0 -102
- package/dist/client-common.esm.js +0 -89
- package/index.js +0 -2
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { BrowserLocalStorageOptions, Cache, CacheEvents } from '../types';
|
|
2
|
+
|
|
3
|
+
export function createBrowserLocalStorageCache(
|
|
4
|
+
options: BrowserLocalStorageOptions
|
|
5
|
+
): Cache {
|
|
6
|
+
let storage: Storage;
|
|
7
|
+
// We've changed the namespace to avoid conflicts with v4, as this version is a huge breaking change
|
|
8
|
+
const namespaceKey = `algolia-client-js-${options.key}`;
|
|
9
|
+
|
|
10
|
+
function getStorage(): Storage {
|
|
11
|
+
if (storage === undefined) {
|
|
12
|
+
storage = options.localStorage || window.localStorage;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return storage;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function getNamespace<TValue>(): Record<string, TValue> {
|
|
19
|
+
return JSON.parse(getStorage().getItem(namespaceKey) || '{}');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return {
|
|
23
|
+
get<TValue>(
|
|
24
|
+
key: Record<string, any> | string,
|
|
25
|
+
defaultValue: () => Promise<TValue>,
|
|
26
|
+
events: CacheEvents<TValue> = {
|
|
27
|
+
miss: (): Promise<void> => Promise.resolve(),
|
|
28
|
+
}
|
|
29
|
+
): Promise<TValue> {
|
|
30
|
+
return Promise.resolve()
|
|
31
|
+
.then(() => {
|
|
32
|
+
const keyAsString = JSON.stringify(key);
|
|
33
|
+
const value = getNamespace<TValue>()[keyAsString];
|
|
34
|
+
|
|
35
|
+
return Promise.all([value || defaultValue(), value !== undefined]);
|
|
36
|
+
})
|
|
37
|
+
.then(([value, exists]) => {
|
|
38
|
+
return Promise.all([value, exists || events.miss(value)]);
|
|
39
|
+
})
|
|
40
|
+
.then(([value]) => value);
|
|
41
|
+
},
|
|
42
|
+
|
|
43
|
+
set<TValue>(
|
|
44
|
+
key: Record<string, any> | string,
|
|
45
|
+
value: TValue
|
|
46
|
+
): Promise<TValue> {
|
|
47
|
+
return Promise.resolve().then(() => {
|
|
48
|
+
const namespace = getNamespace();
|
|
49
|
+
|
|
50
|
+
namespace[JSON.stringify(key)] = value;
|
|
51
|
+
|
|
52
|
+
getStorage().setItem(namespaceKey, JSON.stringify(namespace));
|
|
53
|
+
|
|
54
|
+
return value;
|
|
55
|
+
});
|
|
56
|
+
},
|
|
57
|
+
|
|
58
|
+
delete(key: Record<string, any> | string): Promise<void> {
|
|
59
|
+
return Promise.resolve().then(() => {
|
|
60
|
+
const namespace = getNamespace();
|
|
61
|
+
|
|
62
|
+
delete namespace[JSON.stringify(key)];
|
|
63
|
+
|
|
64
|
+
getStorage().setItem(namespaceKey, JSON.stringify(namespace));
|
|
65
|
+
});
|
|
66
|
+
},
|
|
67
|
+
|
|
68
|
+
clear(): Promise<void> {
|
|
69
|
+
return Promise.resolve().then(() => {
|
|
70
|
+
getStorage().removeItem(namespaceKey);
|
|
71
|
+
});
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { FallbackableCacheOptions, Cache, CacheEvents } from '../types';
|
|
2
|
+
|
|
3
|
+
import { createNullCache } from './createNullCache';
|
|
4
|
+
|
|
5
|
+
export function createFallbackableCache(
|
|
6
|
+
options: FallbackableCacheOptions
|
|
7
|
+
): Cache {
|
|
8
|
+
const caches = [...options.caches];
|
|
9
|
+
const current = caches.shift();
|
|
10
|
+
|
|
11
|
+
if (current === undefined) {
|
|
12
|
+
return createNullCache();
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return {
|
|
16
|
+
get<TValue>(
|
|
17
|
+
key: Record<string, any> | string,
|
|
18
|
+
defaultValue: () => Promise<TValue>,
|
|
19
|
+
events: CacheEvents<TValue> = {
|
|
20
|
+
miss: (): Promise<void> => Promise.resolve(),
|
|
21
|
+
}
|
|
22
|
+
): Promise<TValue> {
|
|
23
|
+
return current.get(key, defaultValue, events).catch(() => {
|
|
24
|
+
return createFallbackableCache({ caches }).get(
|
|
25
|
+
key,
|
|
26
|
+
defaultValue,
|
|
27
|
+
events
|
|
28
|
+
);
|
|
29
|
+
});
|
|
30
|
+
},
|
|
31
|
+
|
|
32
|
+
set<TValue>(
|
|
33
|
+
key: Record<string, any> | string,
|
|
34
|
+
value: TValue
|
|
35
|
+
): Promise<TValue> {
|
|
36
|
+
return current.set(key, value).catch(() => {
|
|
37
|
+
return createFallbackableCache({ caches }).set(key, value);
|
|
38
|
+
});
|
|
39
|
+
},
|
|
40
|
+
|
|
41
|
+
delete(key: Record<string, any> | string): Promise<void> {
|
|
42
|
+
return current.delete(key).catch(() => {
|
|
43
|
+
return createFallbackableCache({ caches }).delete(key);
|
|
44
|
+
});
|
|
45
|
+
},
|
|
46
|
+
|
|
47
|
+
clear(): Promise<void> {
|
|
48
|
+
return current.clear().catch(() => {
|
|
49
|
+
return createFallbackableCache({ caches }).clear();
|
|
50
|
+
});
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { Cache, CacheEvents, MemoryCacheOptions } from '../types';
|
|
2
|
+
|
|
3
|
+
export function createMemoryCache(
|
|
4
|
+
options: MemoryCacheOptions = { serializable: true }
|
|
5
|
+
): Cache {
|
|
6
|
+
let cache: Record<string, any> = {};
|
|
7
|
+
|
|
8
|
+
return {
|
|
9
|
+
get<TValue>(
|
|
10
|
+
key: Record<string, any> | string,
|
|
11
|
+
defaultValue: () => Promise<TValue>,
|
|
12
|
+
events: CacheEvents<TValue> = {
|
|
13
|
+
miss: (): Promise<void> => Promise.resolve(),
|
|
14
|
+
}
|
|
15
|
+
): Promise<TValue> {
|
|
16
|
+
const keyAsString = JSON.stringify(key);
|
|
17
|
+
|
|
18
|
+
if (keyAsString in cache) {
|
|
19
|
+
return Promise.resolve(
|
|
20
|
+
options.serializable
|
|
21
|
+
? JSON.parse(cache[keyAsString])
|
|
22
|
+
: cache[keyAsString]
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const promise = defaultValue();
|
|
27
|
+
|
|
28
|
+
return promise
|
|
29
|
+
.then((value: TValue) => events.miss(value))
|
|
30
|
+
.then(() => promise);
|
|
31
|
+
},
|
|
32
|
+
|
|
33
|
+
set<TValue>(
|
|
34
|
+
key: Record<string, any> | string,
|
|
35
|
+
value: TValue
|
|
36
|
+
): Promise<TValue> {
|
|
37
|
+
cache[JSON.stringify(key)] = options.serializable
|
|
38
|
+
? JSON.stringify(value)
|
|
39
|
+
: value;
|
|
40
|
+
|
|
41
|
+
return Promise.resolve(value);
|
|
42
|
+
},
|
|
43
|
+
|
|
44
|
+
delete(key: Record<string, unknown> | string): Promise<void> {
|
|
45
|
+
delete cache[JSON.stringify(key)];
|
|
46
|
+
|
|
47
|
+
return Promise.resolve();
|
|
48
|
+
},
|
|
49
|
+
|
|
50
|
+
clear(): Promise<void> {
|
|
51
|
+
cache = {};
|
|
52
|
+
|
|
53
|
+
return Promise.resolve();
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { Cache, CacheEvents } from '../types';
|
|
2
|
+
|
|
3
|
+
export function createNullCache(): Cache {
|
|
4
|
+
return {
|
|
5
|
+
get<TValue>(
|
|
6
|
+
_key: Record<string, any> | string,
|
|
7
|
+
defaultValue: () => Promise<TValue>,
|
|
8
|
+
events: CacheEvents<TValue> = {
|
|
9
|
+
miss: (): Promise<void> => Promise.resolve(),
|
|
10
|
+
}
|
|
11
|
+
): Promise<TValue> {
|
|
12
|
+
const value = defaultValue();
|
|
13
|
+
|
|
14
|
+
return value
|
|
15
|
+
.then((result) => Promise.all([result, events.miss(result)]))
|
|
16
|
+
.then(([result]) => result);
|
|
17
|
+
},
|
|
18
|
+
|
|
19
|
+
set<TValue>(
|
|
20
|
+
_key: Record<string, any> | string,
|
|
21
|
+
value: TValue
|
|
22
|
+
): Promise<TValue> {
|
|
23
|
+
return Promise.resolve(value);
|
|
24
|
+
},
|
|
25
|
+
|
|
26
|
+
delete(_key: Record<string, any> | string): Promise<void> {
|
|
27
|
+
return Promise.resolve();
|
|
28
|
+
},
|
|
29
|
+
|
|
30
|
+
clear(): Promise<void> {
|
|
31
|
+
return Promise.resolve();
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
}
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export const DEFAULT_CONNECT_TIMEOUT_BROWSER = 1000;
|
|
2
|
+
export const DEFAULT_READ_TIMEOUT_BROWSER = 2000;
|
|
3
|
+
export const DEFAULT_WRITE_TIMEOUT_BROWSER = 30000;
|
|
4
|
+
|
|
5
|
+
export const DEFAULT_CONNECT_TIMEOUT_NODE = 2000;
|
|
6
|
+
export const DEFAULT_READ_TIMEOUT_NODE = 5000;
|
|
7
|
+
export const DEFAULT_WRITE_TIMEOUT_NODE = 30000;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { AlgoliaAgentOptions, AlgoliaAgent } from './types';
|
|
2
|
+
|
|
3
|
+
export function createAlgoliaAgent(version: string): AlgoliaAgent {
|
|
4
|
+
const algoliaAgent = {
|
|
5
|
+
value: `Algolia for JavaScript (${version})`,
|
|
6
|
+
add(options: AlgoliaAgentOptions): AlgoliaAgent {
|
|
7
|
+
const addedAlgoliaAgent = `; ${options.segment}${
|
|
8
|
+
options.version !== undefined ? ` (${options.version})` : ''
|
|
9
|
+
}`;
|
|
10
|
+
|
|
11
|
+
if (algoliaAgent.value.indexOf(addedAlgoliaAgent) === -1) {
|
|
12
|
+
algoliaAgent.value = `${algoliaAgent.value}${addedAlgoliaAgent}`;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return algoliaAgent;
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
return algoliaAgent;
|
|
20
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { AuthMode, Headers, QueryParameters } from './types';
|
|
2
|
+
|
|
3
|
+
export function createAuth(
|
|
4
|
+
appId: string,
|
|
5
|
+
apiKey: string,
|
|
6
|
+
authMode: AuthMode = 'WithinHeaders'
|
|
7
|
+
): {
|
|
8
|
+
readonly headers: () => Headers;
|
|
9
|
+
readonly queryParameters: () => QueryParameters;
|
|
10
|
+
} {
|
|
11
|
+
const credentials = {
|
|
12
|
+
'x-algolia-api-key': apiKey,
|
|
13
|
+
'x-algolia-application-id': appId,
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
return {
|
|
17
|
+
headers(): Headers {
|
|
18
|
+
return authMode === 'WithinHeaders' ? credentials : {};
|
|
19
|
+
},
|
|
20
|
+
|
|
21
|
+
queryParameters(): QueryParameters {
|
|
22
|
+
return authMode === 'WithinQueryParameters' ? credentials : {};
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { EchoResponse, EndRequest, Requester, Response } from './types';
|
|
2
|
+
|
|
3
|
+
export type EchoRequesterParams = {
|
|
4
|
+
getURL: (url: string) => URL;
|
|
5
|
+
status?: number;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
function getUrlParams({
|
|
9
|
+
host,
|
|
10
|
+
searchParams: urlSearchParams,
|
|
11
|
+
pathname,
|
|
12
|
+
}: URL): Pick<EchoResponse, 'algoliaAgent' | 'host' | 'path' | 'searchParams'> {
|
|
13
|
+
const algoliaAgent = urlSearchParams.get('x-algolia-agent') || '';
|
|
14
|
+
const searchParams = {};
|
|
15
|
+
|
|
16
|
+
for (const [k, v] of urlSearchParams) {
|
|
17
|
+
if (k === 'x-algolia-agent') {
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
searchParams[k] = v;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return {
|
|
25
|
+
host,
|
|
26
|
+
algoliaAgent,
|
|
27
|
+
searchParams:
|
|
28
|
+
Object.keys(searchParams).length === 0 ? undefined : searchParams,
|
|
29
|
+
path: pathname,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function createEchoRequester({
|
|
34
|
+
getURL,
|
|
35
|
+
status = 200,
|
|
36
|
+
}: EchoRequesterParams): Requester {
|
|
37
|
+
function send(request: EndRequest): Promise<Response> {
|
|
38
|
+
const { host, searchParams, algoliaAgent, path } = getUrlParams(
|
|
39
|
+
getURL(request.url)
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
const content: EchoResponse = {
|
|
43
|
+
...request,
|
|
44
|
+
data: request.data ? JSON.parse(request.data) : undefined,
|
|
45
|
+
path,
|
|
46
|
+
host,
|
|
47
|
+
algoliaAgent: encodeURI(algoliaAgent),
|
|
48
|
+
searchParams,
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
return Promise.resolve({
|
|
52
|
+
content: JSON.stringify(content),
|
|
53
|
+
isTimedOut: false,
|
|
54
|
+
status,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return { send };
|
|
59
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { CreateIterablePromise } from './types/createIterablePromise';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Helper: Returns the promise of a given `func` to iterate on, based on a given `validate` condition.
|
|
5
|
+
*
|
|
6
|
+
* @param createIterator - The createIterator options.
|
|
7
|
+
* @param createIterator.func - The function to run, which returns a promise.
|
|
8
|
+
* @param createIterator.validate - The validator function. It receives the resolved return of `func`.
|
|
9
|
+
* @param createIterator.aggregator - The function that runs right after the `func` method has been executed, allows you to do anything with the response before `validate`.
|
|
10
|
+
* @param createIterator.error - The `validate` condition to throw an error, and its message.
|
|
11
|
+
* @param createIterator.timeout - The function to decide how long to wait between iterations.
|
|
12
|
+
*/
|
|
13
|
+
export function createIterablePromise<TResponse>({
|
|
14
|
+
func,
|
|
15
|
+
validate,
|
|
16
|
+
aggregator,
|
|
17
|
+
error,
|
|
18
|
+
timeout = (): number => 0,
|
|
19
|
+
}: CreateIterablePromise<TResponse>): Promise<TResponse> {
|
|
20
|
+
const retry = (previousResponse?: TResponse): Promise<TResponse> => {
|
|
21
|
+
return new Promise<TResponse>((resolve, reject) => {
|
|
22
|
+
func(previousResponse)
|
|
23
|
+
.then((response) => {
|
|
24
|
+
if (aggregator) {
|
|
25
|
+
aggregator(response);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (validate(response)) {
|
|
29
|
+
return resolve(response);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (error && error.validate(response)) {
|
|
33
|
+
return reject(new Error(error.message(response)));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return setTimeout(() => {
|
|
37
|
+
retry(response).then(resolve).catch(reject);
|
|
38
|
+
}, timeout());
|
|
39
|
+
})
|
|
40
|
+
.catch((err) => {
|
|
41
|
+
reject(err);
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
return retry();
|
|
47
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { createAlgoliaAgent } from './createAlgoliaAgent';
|
|
2
|
+
import type { AlgoliaAgentOptions, AlgoliaAgent } from './types';
|
|
3
|
+
|
|
4
|
+
export type GetAlgoliaAgent = {
|
|
5
|
+
algoliaAgents: AlgoliaAgentOptions[];
|
|
6
|
+
client: string;
|
|
7
|
+
version: string;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export function getAlgoliaAgent({
|
|
11
|
+
algoliaAgents,
|
|
12
|
+
client,
|
|
13
|
+
version,
|
|
14
|
+
}: GetAlgoliaAgent): AlgoliaAgent {
|
|
15
|
+
const defaultAlgoliaAgent = createAlgoliaAgent(version).add({
|
|
16
|
+
segment: client,
|
|
17
|
+
version,
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
algoliaAgents.forEach((algoliaAgent) =>
|
|
21
|
+
defaultAlgoliaAgent.add(algoliaAgent)
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
return defaultAlgoliaAgent;
|
|
25
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Host, StatefulHost } from '../types';
|
|
2
|
+
|
|
3
|
+
// By default, API Clients at Algolia have expiration delay of 5 mins.
|
|
4
|
+
// In the JavaScript client, we have 2 mins.
|
|
5
|
+
const EXPIRATION_DELAY = 2 * 60 * 1000;
|
|
6
|
+
|
|
7
|
+
export function createStatefulHost(
|
|
8
|
+
host: Host,
|
|
9
|
+
status: StatefulHost['status'] = 'up'
|
|
10
|
+
): StatefulHost {
|
|
11
|
+
const lastUpdate = Date.now();
|
|
12
|
+
|
|
13
|
+
function isUp(): boolean {
|
|
14
|
+
return status === 'up' || Date.now() - lastUpdate > EXPIRATION_DELAY;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function isTimedOut(): boolean {
|
|
18
|
+
return (
|
|
19
|
+
status === 'timed out' && Date.now() - lastUpdate <= EXPIRATION_DELAY
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return { ...host, status, lastUpdate, isUp, isTimedOut };
|
|
24
|
+
}
|