@algolia/client-common 5.0.0-alpha.11 → 5.0.0-alpha.110
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 → client-common.cjs} +190 -213
- package/dist/client-common.esm.node.js +190 -212
- package/dist/index.d.ts +9 -9
- package/dist/src/cache/createBrowserLocalStorageCache.d.ts +2 -2
- package/dist/src/cache/createBrowserLocalStorageCache.d.ts.map +1 -1
- package/dist/src/cache/createFallbackableCache.d.ts +2 -2
- package/dist/src/cache/createMemoryCache.d.ts +2 -2
- package/dist/src/cache/createNullCache.d.ts +2 -2
- package/dist/src/cache/index.d.ts +4 -4
- package/dist/src/constants.d.ts +6 -6
- package/dist/src/createAlgoliaAgent.d.ts +2 -2
- package/dist/src/createAuth.d.ts +5 -5
- package/dist/src/createEchoRequester.d.ts +6 -6
- package/dist/src/createEchoRequester.d.ts.map +1 -1
- package/dist/src/createIterablePromise.d.ts +12 -12
- package/dist/src/getAlgoliaAgent.d.ts +7 -7
- package/dist/src/getAlgoliaAgent.d.ts.map +1 -1
- package/dist/src/transporter/createStatefulHost.d.ts +2 -2
- package/dist/src/transporter/createTransporter.d.ts +2 -2
- package/dist/src/transporter/errors.d.ts +37 -20
- package/dist/src/transporter/errors.d.ts.map +1 -1
- package/dist/src/transporter/helpers.d.ts +8 -8
- package/dist/src/transporter/helpers.d.ts.map +1 -1
- package/dist/src/transporter/index.d.ts +6 -6
- package/dist/src/transporter/responses.d.ts +4 -4
- package/dist/src/transporter/stackTrace.d.ts +3 -3
- package/dist/src/types/cache.d.ts +60 -46
- package/dist/src/types/cache.d.ts.map +1 -1
- package/dist/src/types/createClient.d.ts +11 -11
- package/dist/src/types/createClient.d.ts.map +1 -1
- package/dist/src/types/createIterablePromise.d.ts +35 -35
- package/dist/src/types/createIterablePromise.d.ts.map +1 -1
- package/dist/src/types/host.d.ts +36 -32
- package/dist/src/types/host.d.ts.map +1 -1
- package/dist/src/types/index.d.ts +6 -6
- package/dist/src/types/requester.d.ts +65 -65
- package/dist/src/types/requester.d.ts.map +1 -1
- package/dist/src/types/transporter.d.ts +127 -127
- package/dist/src/types/transporter.d.ts.map +1 -1
- package/package.json +11 -8
- package/src/__tests__/cache/browser-local-storage-cache.test.ts +61 -9
- package/src/cache/createBrowserLocalStorageCache.ts +55 -6
- package/src/createEchoRequester.ts +24 -9
- package/src/transporter/errors.ts +39 -3
- package/src/transporter/helpers.ts +16 -8
- package/src/types/cache.ts +17 -0
- package/src/types/host.ts +5 -0
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export * from './cache';
|
|
2
|
-
export * from './createClient';
|
|
3
|
-
export * from './createIterablePromise';
|
|
4
|
-
export * from './host';
|
|
5
|
-
export * from './requester';
|
|
6
|
-
export * from './transporter';
|
|
1
|
+
export * from './cache';
|
|
2
|
+
export * from './createClient';
|
|
3
|
+
export * from './createIterablePromise';
|
|
4
|
+
export * from './host';
|
|
5
|
+
export * from './requester';
|
|
6
|
+
export * from './transporter';
|
|
7
7
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1,66 +1,66 @@
|
|
|
1
|
-
import type { Headers, QueryParameters } from './transporter';
|
|
2
|
-
/**
|
|
3
|
-
* The method of the request.
|
|
4
|
-
*/
|
|
5
|
-
export
|
|
6
|
-
export
|
|
7
|
-
method: Method;
|
|
8
|
-
/**
|
|
9
|
-
* The path of the REST API to send the request to.
|
|
10
|
-
*/
|
|
11
|
-
path: string;
|
|
12
|
-
queryParameters: QueryParameters;
|
|
13
|
-
data?: Array<Record<string, any>> | Record<string, any>;
|
|
14
|
-
headers: Headers;
|
|
15
|
-
/**
|
|
16
|
-
* If the given request should persist on the cache. Keep in mind,
|
|
17
|
-
* that some methods may have this option enabled by default.
|
|
18
|
-
*/
|
|
19
|
-
cacheable?: boolean;
|
|
20
|
-
/**
|
|
21
|
-
* Some POST methods in the Algolia REST API uses the `read` transporter.
|
|
22
|
-
* This information is defined at the spec level.
|
|
23
|
-
*/
|
|
24
|
-
useReadTransporter?: boolean;
|
|
25
|
-
};
|
|
26
|
-
export
|
|
27
|
-
/**
|
|
28
|
-
* The full URL of the REST API.
|
|
29
|
-
*/
|
|
30
|
-
url: string;
|
|
31
|
-
/**
|
|
32
|
-
* The connection timeout, in milliseconds.
|
|
33
|
-
*/
|
|
34
|
-
connectTimeout: number;
|
|
35
|
-
/**
|
|
36
|
-
* The response timeout, in milliseconds.
|
|
37
|
-
*/
|
|
38
|
-
responseTimeout: number;
|
|
39
|
-
data?: string;
|
|
40
|
-
};
|
|
41
|
-
export
|
|
42
|
-
/**
|
|
43
|
-
* The body of the response.
|
|
44
|
-
*/
|
|
45
|
-
content: string;
|
|
46
|
-
/**
|
|
47
|
-
* Whether the API call is timed out or not.
|
|
48
|
-
*/
|
|
49
|
-
isTimedOut: boolean;
|
|
50
|
-
/**
|
|
51
|
-
* The HTTP status code of the response.
|
|
52
|
-
*/
|
|
53
|
-
status: number;
|
|
54
|
-
};
|
|
55
|
-
export
|
|
56
|
-
/**
|
|
57
|
-
* Sends the given `request` to the server.
|
|
58
|
-
*/
|
|
59
|
-
send: (request: EndRequest) => Promise<Response>;
|
|
60
|
-
};
|
|
61
|
-
export
|
|
62
|
-
host: string;
|
|
63
|
-
algoliaAgent: string;
|
|
64
|
-
searchParams?: Record<string, string>;
|
|
65
|
-
};
|
|
1
|
+
import type { Headers, QueryParameters } from './transporter';
|
|
2
|
+
/**
|
|
3
|
+
* The method of the request.
|
|
4
|
+
*/
|
|
5
|
+
export type Method = 'DELETE' | 'GET' | 'PATCH' | 'POST' | 'PUT';
|
|
6
|
+
export type Request = {
|
|
7
|
+
method: Method;
|
|
8
|
+
/**
|
|
9
|
+
* The path of the REST API to send the request to.
|
|
10
|
+
*/
|
|
11
|
+
path: string;
|
|
12
|
+
queryParameters: QueryParameters;
|
|
13
|
+
data?: Array<Record<string, any>> | Record<string, any>;
|
|
14
|
+
headers: Headers;
|
|
15
|
+
/**
|
|
16
|
+
* If the given request should persist on the cache. Keep in mind,
|
|
17
|
+
* that some methods may have this option enabled by default.
|
|
18
|
+
*/
|
|
19
|
+
cacheable?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Some POST methods in the Algolia REST API uses the `read` transporter.
|
|
22
|
+
* This information is defined at the spec level.
|
|
23
|
+
*/
|
|
24
|
+
useReadTransporter?: boolean;
|
|
25
|
+
};
|
|
26
|
+
export type EndRequest = Pick<Request, 'headers' | 'method'> & {
|
|
27
|
+
/**
|
|
28
|
+
* The full URL of the REST API.
|
|
29
|
+
*/
|
|
30
|
+
url: string;
|
|
31
|
+
/**
|
|
32
|
+
* The connection timeout, in milliseconds.
|
|
33
|
+
*/
|
|
34
|
+
connectTimeout: number;
|
|
35
|
+
/**
|
|
36
|
+
* The response timeout, in milliseconds.
|
|
37
|
+
*/
|
|
38
|
+
responseTimeout: number;
|
|
39
|
+
data?: string;
|
|
40
|
+
};
|
|
41
|
+
export type Response = {
|
|
42
|
+
/**
|
|
43
|
+
* The body of the response.
|
|
44
|
+
*/
|
|
45
|
+
content: string;
|
|
46
|
+
/**
|
|
47
|
+
* Whether the API call is timed out or not.
|
|
48
|
+
*/
|
|
49
|
+
isTimedOut: boolean;
|
|
50
|
+
/**
|
|
51
|
+
* The HTTP status code of the response.
|
|
52
|
+
*/
|
|
53
|
+
status: number;
|
|
54
|
+
};
|
|
55
|
+
export type Requester = {
|
|
56
|
+
/**
|
|
57
|
+
* Sends the given `request` to the server.
|
|
58
|
+
*/
|
|
59
|
+
send: (request: EndRequest) => Promise<Response>;
|
|
60
|
+
};
|
|
61
|
+
export type EchoResponse = Omit<EndRequest, 'data'> & Pick<Request, 'data' | 'path'> & {
|
|
62
|
+
host: string;
|
|
63
|
+
algoliaAgent: string;
|
|
64
|
+
searchParams?: Record<string, string>;
|
|
65
|
+
};
|
|
66
66
|
//# sourceMappingURL=requester.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"requester.d.ts","sourceRoot":"","sources":["../../../src/types/requester.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAE9D;;GAEG;AACH,
|
|
1
|
+
{"version":3,"file":"requester.d.ts","sourceRoot":"","sources":["../../../src/types/requester.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAE9D;;GAEG;AACH,MAAM,MAAM,MAAM,GAAG,QAAQ,GAAG,KAAK,GAAG,OAAO,GAAG,MAAM,GAAG,KAAK,CAAC;AAEjE,MAAM,MAAM,OAAO,GAAG;IACpB,MAAM,EAAE,MAAM,CAAC;IACf;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IACb,eAAe,EAAE,eAAe,CAAC;IACjC,IAAI,CAAC,EAAE,KAAK,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IACxD,OAAO,EAAE,OAAO,CAAC;IACjB;;;OAGG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB;;;OAGG;IACH,kBAAkB,CAAC,EAAE,OAAO,CAAC;CAC9B,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG,IAAI,CAAC,OAAO,EAAE,SAAS,GAAG,QAAQ,CAAC,GAAG;IAC7D;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC;IACZ;;OAEG;IACH,cAAc,EAAE,MAAM,CAAC;IACvB;;OAEG;IACH,eAAe,EAAE,MAAM,CAAC;IACxB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,MAAM,MAAM,QAAQ,GAAG;IACrB;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;IAChB;;OAEG;IACH,UAAU,EAAE,OAAO,CAAC;IACpB;;OAEG;IACH,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,MAAM,MAAM,SAAS,GAAG;IACtB;;OAEG;IACH,IAAI,EAAE,CAAC,OAAO,EAAE,UAAU,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;CAClD,CAAC;AAEF,MAAM,MAAM,YAAY,GAAG,IAAI,CAAC,UAAU,EAAE,MAAM,CAAC,GACjD,IAAI,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC,GAAG;IAC/B,IAAI,EAAE,MAAM,CAAC;IACb,YAAY,EAAE,MAAM,CAAC;IACrB,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACvC,CAAC"}
|
|
@@ -1,128 +1,128 @@
|
|
|
1
|
-
import type { Cache } from './cache';
|
|
2
|
-
import type { Host } from './host';
|
|
3
|
-
import type { Request, Requester, EndRequest, Response } from './requester';
|
|
4
|
-
export
|
|
5
|
-
export
|
|
6
|
-
export
|
|
7
|
-
/**
|
|
8
|
-
* Custom timeout for the request. Note that, in normal situations
|
|
9
|
-
* the given timeout will be applied. But the transporter layer may
|
|
10
|
-
* increase this timeout if there is need for it.
|
|
11
|
-
*/
|
|
12
|
-
timeout?: number;
|
|
13
|
-
/**
|
|
14
|
-
* Custom headers for the request. This headers are
|
|
15
|
-
* going to be merged the transporter headers.
|
|
16
|
-
*/
|
|
17
|
-
headers?: Headers;
|
|
18
|
-
/**
|
|
19
|
-
* Custom query parameters for the request. This query parameters are
|
|
20
|
-
* going to be merged the transporter query parameters.
|
|
21
|
-
*/
|
|
22
|
-
queryParameters?: QueryParameters;
|
|
23
|
-
/**
|
|
24
|
-
* Custom data for the request. This data is
|
|
25
|
-
* going to be merged the transporter data.
|
|
26
|
-
*/
|
|
27
|
-
data?: Array<Record<string, any>> | Record<string, any>;
|
|
28
|
-
};
|
|
29
|
-
export
|
|
30
|
-
request: EndRequest;
|
|
31
|
-
response: Response;
|
|
32
|
-
host: Host;
|
|
33
|
-
triesLeft: number;
|
|
34
|
-
};
|
|
35
|
-
export
|
|
36
|
-
/**
|
|
37
|
-
* The segment. Usually the integration name.
|
|
38
|
-
*/
|
|
39
|
-
segment: string;
|
|
40
|
-
/**
|
|
41
|
-
* The version. Usually the integration version.
|
|
42
|
-
*/
|
|
43
|
-
version?: string;
|
|
44
|
-
};
|
|
45
|
-
export
|
|
46
|
-
/**
|
|
47
|
-
* The raw value of the user agent.
|
|
48
|
-
*/
|
|
49
|
-
value: string;
|
|
50
|
-
/**
|
|
51
|
-
* Mutates the current user agent adding the given user agent options.
|
|
52
|
-
*/
|
|
53
|
-
add: (options: AlgoliaAgentOptions) => AlgoliaAgent;
|
|
54
|
-
};
|
|
55
|
-
export
|
|
56
|
-
/**
|
|
57
|
-
* Timeout in milliseconds before the connection is established.
|
|
58
|
-
*/
|
|
59
|
-
connect: number;
|
|
60
|
-
/**
|
|
61
|
-
* Timeout in milliseconds before reading the response on a read request.
|
|
62
|
-
*/
|
|
63
|
-
read: number;
|
|
64
|
-
/**
|
|
65
|
-
* Timeout in milliseconds before reading the response on a write request.
|
|
66
|
-
*/
|
|
67
|
-
write: number;
|
|
68
|
-
};
|
|
69
|
-
export
|
|
70
|
-
/**
|
|
71
|
-
* The cache of the hosts. Usually used to persist
|
|
72
|
-
* the state of the host when its down.
|
|
73
|
-
*/
|
|
74
|
-
hostsCache: Cache;
|
|
75
|
-
/**
|
|
76
|
-
* The underlying requester used. Should differ
|
|
77
|
-
* depending of the environment where the client
|
|
78
|
-
* will be used.
|
|
79
|
-
*/
|
|
80
|
-
requester: Requester;
|
|
81
|
-
/**
|
|
82
|
-
* The cache of the requests. When requests are
|
|
83
|
-
* `cacheable`, the returned promised persists
|
|
84
|
-
* in this cache to shared in similar requests
|
|
85
|
-
* before being resolved.
|
|
86
|
-
*/
|
|
87
|
-
requestsCache: Cache;
|
|
88
|
-
/**
|
|
89
|
-
* The cache of the responses. When requests are
|
|
90
|
-
* `cacheable`, the returned responses persists
|
|
91
|
-
* in this cache to shared in similar requests.
|
|
92
|
-
*/
|
|
93
|
-
responsesCache: Cache;
|
|
94
|
-
/**
|
|
95
|
-
* The timeouts used by the requester. The transporter
|
|
96
|
-
* layer may increase this timeouts as defined on the
|
|
97
|
-
* retry strategy.
|
|
98
|
-
*/
|
|
99
|
-
timeouts: Timeouts;
|
|
100
|
-
/**
|
|
101
|
-
* The hosts used by the requester.
|
|
102
|
-
*/
|
|
103
|
-
hosts: Host[];
|
|
104
|
-
/**
|
|
105
|
-
* The headers used by the requester. The transporter
|
|
106
|
-
* layer may add some extra headers during the request
|
|
107
|
-
* for the user agent, and others.
|
|
108
|
-
*/
|
|
109
|
-
baseHeaders: Headers;
|
|
110
|
-
/**
|
|
111
|
-
* The query parameters used by the requester. The transporter
|
|
112
|
-
* layer may add some extra headers during the request
|
|
113
|
-
* for the user agent, and others.
|
|
114
|
-
*/
|
|
115
|
-
baseQueryParameters: QueryParameters;
|
|
116
|
-
/**
|
|
117
|
-
* The user agent used. Sent on query parameters.
|
|
118
|
-
*/
|
|
119
|
-
algoliaAgent: AlgoliaAgent;
|
|
120
|
-
};
|
|
121
|
-
export
|
|
122
|
-
/**
|
|
123
|
-
* Performs a request.
|
|
124
|
-
* The `baseRequest` and `baseRequestOptions` will be merged accordingly.
|
|
125
|
-
*/
|
|
126
|
-
request: <TResponse>(baseRequest: Request, baseRequestOptions?: RequestOptions) => Promise<TResponse>;
|
|
127
|
-
};
|
|
1
|
+
import type { Cache } from './cache';
|
|
2
|
+
import type { Host } from './host';
|
|
3
|
+
import type { Request, Requester, EndRequest, Response } from './requester';
|
|
4
|
+
export type Headers = Record<string, string>;
|
|
5
|
+
export type QueryParameters = Record<string, any>;
|
|
6
|
+
export type RequestOptions = Pick<Request, 'cacheable'> & {
|
|
7
|
+
/**
|
|
8
|
+
* Custom timeout for the request. Note that, in normal situations
|
|
9
|
+
* the given timeout will be applied. But the transporter layer may
|
|
10
|
+
* increase this timeout if there is need for it.
|
|
11
|
+
*/
|
|
12
|
+
timeout?: number;
|
|
13
|
+
/**
|
|
14
|
+
* Custom headers for the request. This headers are
|
|
15
|
+
* going to be merged the transporter headers.
|
|
16
|
+
*/
|
|
17
|
+
headers?: Headers;
|
|
18
|
+
/**
|
|
19
|
+
* Custom query parameters for the request. This query parameters are
|
|
20
|
+
* going to be merged the transporter query parameters.
|
|
21
|
+
*/
|
|
22
|
+
queryParameters?: QueryParameters;
|
|
23
|
+
/**
|
|
24
|
+
* Custom data for the request. This data is
|
|
25
|
+
* going to be merged the transporter data.
|
|
26
|
+
*/
|
|
27
|
+
data?: Array<Record<string, any>> | Record<string, any>;
|
|
28
|
+
};
|
|
29
|
+
export type StackFrame = {
|
|
30
|
+
request: EndRequest;
|
|
31
|
+
response: Response;
|
|
32
|
+
host: Host;
|
|
33
|
+
triesLeft: number;
|
|
34
|
+
};
|
|
35
|
+
export type AlgoliaAgentOptions = {
|
|
36
|
+
/**
|
|
37
|
+
* The segment. Usually the integration name.
|
|
38
|
+
*/
|
|
39
|
+
segment: string;
|
|
40
|
+
/**
|
|
41
|
+
* The version. Usually the integration version.
|
|
42
|
+
*/
|
|
43
|
+
version?: string;
|
|
44
|
+
};
|
|
45
|
+
export type AlgoliaAgent = {
|
|
46
|
+
/**
|
|
47
|
+
* The raw value of the user agent.
|
|
48
|
+
*/
|
|
49
|
+
value: string;
|
|
50
|
+
/**
|
|
51
|
+
* Mutates the current user agent adding the given user agent options.
|
|
52
|
+
*/
|
|
53
|
+
add: (options: AlgoliaAgentOptions) => AlgoliaAgent;
|
|
54
|
+
};
|
|
55
|
+
export type Timeouts = {
|
|
56
|
+
/**
|
|
57
|
+
* Timeout in milliseconds before the connection is established.
|
|
58
|
+
*/
|
|
59
|
+
connect: number;
|
|
60
|
+
/**
|
|
61
|
+
* Timeout in milliseconds before reading the response on a read request.
|
|
62
|
+
*/
|
|
63
|
+
read: number;
|
|
64
|
+
/**
|
|
65
|
+
* Timeout in milliseconds before reading the response on a write request.
|
|
66
|
+
*/
|
|
67
|
+
write: number;
|
|
68
|
+
};
|
|
69
|
+
export type TransporterOptions = {
|
|
70
|
+
/**
|
|
71
|
+
* The cache of the hosts. Usually used to persist
|
|
72
|
+
* the state of the host when its down.
|
|
73
|
+
*/
|
|
74
|
+
hostsCache: Cache;
|
|
75
|
+
/**
|
|
76
|
+
* The underlying requester used. Should differ
|
|
77
|
+
* depending of the environment where the client
|
|
78
|
+
* will be used.
|
|
79
|
+
*/
|
|
80
|
+
requester: Requester;
|
|
81
|
+
/**
|
|
82
|
+
* The cache of the requests. When requests are
|
|
83
|
+
* `cacheable`, the returned promised persists
|
|
84
|
+
* in this cache to shared in similar requests
|
|
85
|
+
* before being resolved.
|
|
86
|
+
*/
|
|
87
|
+
requestsCache: Cache;
|
|
88
|
+
/**
|
|
89
|
+
* The cache of the responses. When requests are
|
|
90
|
+
* `cacheable`, the returned responses persists
|
|
91
|
+
* in this cache to shared in similar requests.
|
|
92
|
+
*/
|
|
93
|
+
responsesCache: Cache;
|
|
94
|
+
/**
|
|
95
|
+
* The timeouts used by the requester. The transporter
|
|
96
|
+
* layer may increase this timeouts as defined on the
|
|
97
|
+
* retry strategy.
|
|
98
|
+
*/
|
|
99
|
+
timeouts: Timeouts;
|
|
100
|
+
/**
|
|
101
|
+
* The hosts used by the requester.
|
|
102
|
+
*/
|
|
103
|
+
hosts: Host[];
|
|
104
|
+
/**
|
|
105
|
+
* The headers used by the requester. The transporter
|
|
106
|
+
* layer may add some extra headers during the request
|
|
107
|
+
* for the user agent, and others.
|
|
108
|
+
*/
|
|
109
|
+
baseHeaders: Headers;
|
|
110
|
+
/**
|
|
111
|
+
* The query parameters used by the requester. The transporter
|
|
112
|
+
* layer may add some extra headers during the request
|
|
113
|
+
* for the user agent, and others.
|
|
114
|
+
*/
|
|
115
|
+
baseQueryParameters: QueryParameters;
|
|
116
|
+
/**
|
|
117
|
+
* The user agent used. Sent on query parameters.
|
|
118
|
+
*/
|
|
119
|
+
algoliaAgent: AlgoliaAgent;
|
|
120
|
+
};
|
|
121
|
+
export type Transporter = TransporterOptions & {
|
|
122
|
+
/**
|
|
123
|
+
* Performs a request.
|
|
124
|
+
* The `baseRequest` and `baseRequestOptions` will be merged accordingly.
|
|
125
|
+
*/
|
|
126
|
+
request: <TResponse>(baseRequest: Request, baseRequestOptions?: RequestOptions) => Promise<TResponse>;
|
|
127
|
+
};
|
|
128
128
|
//# sourceMappingURL=transporter.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transporter.d.ts","sourceRoot":"","sources":["../../../src/types/transporter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AACnC,OAAO,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAE5E,
|
|
1
|
+
{"version":3,"file":"transporter.d.ts","sourceRoot":"","sources":["../../../src/types/transporter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AACnC,OAAO,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAE5E,MAAM,MAAM,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAC7C,MAAM,MAAM,eAAe,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;AAElD,MAAM,MAAM,cAAc,GAAG,IAAI,CAAC,OAAO,EAAE,WAAW,CAAC,GAAG;IACxD;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB;;;OAGG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;IAElB;;;OAGG;IACH,eAAe,CAAC,EAAE,eAAe,CAAC;IAElC;;;OAGG;IACH,IAAI,CAAC,EAAE,KAAK,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CACzD,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG;IACvB,OAAO,EAAE,UAAU,CAAC;IACpB,QAAQ,EAAE,QAAQ,CAAC;IACnB,IAAI,EAAE,IAAI,CAAC;IACX,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,mBAAmB,GAAG;IAChC;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;IAEhB;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,MAAM,MAAM,YAAY,GAAG;IACzB;;OAEG;IACH,KAAK,EAAE,MAAM,CAAC;IAEd;;OAEG;IACH,GAAG,EAAE,CAAC,OAAO,EAAE,mBAAmB,KAAK,YAAY,CAAC;CACrD,CAAC;AAEF,MAAM,MAAM,QAAQ,GAAG;IACrB;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;IAEhB;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;OAEG;IACH,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG;IAC/B;;;OAGG;IACH,UAAU,EAAE,KAAK,CAAC;IAElB;;;;OAIG;IACH,SAAS,EAAE,SAAS,CAAC;IAErB;;;;;OAKG;IACH,aAAa,EAAE,KAAK,CAAC;IAErB;;;;OAIG;IACH,cAAc,EAAE,KAAK,CAAC;IAEtB;;;;OAIG;IACH,QAAQ,EAAE,QAAQ,CAAC;IAEnB;;OAEG;IACH,KAAK,EAAE,IAAI,EAAE,CAAC;IAEd;;;;OAIG;IACH,WAAW,EAAE,OAAO,CAAC;IAErB;;;;OAIG;IACH,mBAAmB,EAAE,eAAe,CAAC;IAErC;;OAEG;IACH,YAAY,EAAE,YAAY,CAAC;CAC5B,CAAC;AAEF,MAAM,MAAM,WAAW,GAAG,kBAAkB,GAAG;IAC7C;;;OAGG;IACH,OAAO,EAAE,CAAC,SAAS,EACjB,WAAW,EAAE,OAAO,EACpB,kBAAkB,CAAC,EAAE,cAAc,KAChC,OAAO,CAAC,SAAS,CAAC,CAAC;CACzB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@algolia/client-common",
|
|
3
|
-
"version": "5.0.0-alpha.
|
|
3
|
+
"version": "5.0.0-alpha.110",
|
|
4
4
|
"description": "Common package for the Algolia JavaScript API client.",
|
|
5
5
|
"repository": "algolia/algoliasearch-client-javascript",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"author": "Algolia",
|
|
8
|
-
"
|
|
8
|
+
"type": "module",
|
|
9
|
+
"main": "dist/client-common.cjs",
|
|
9
10
|
"module": "dist/client-common.esm.node.js",
|
|
10
11
|
"types": "dist/index.d.ts",
|
|
11
12
|
"files": [
|
|
@@ -19,12 +20,14 @@
|
|
|
19
20
|
"test": "jest"
|
|
20
21
|
},
|
|
21
22
|
"devDependencies": {
|
|
22
|
-
"@
|
|
23
|
-
"@
|
|
24
|
-
"jest": "
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"
|
|
23
|
+
"@babel/preset-env": "7.24.3",
|
|
24
|
+
"@babel/preset-typescript": "7.24.1",
|
|
25
|
+
"@types/jest": "29.5.12",
|
|
26
|
+
"@types/node": "20.11.30",
|
|
27
|
+
"jest": "29.7.0",
|
|
28
|
+
"jest-environment-jsdom": "29.7.0",
|
|
29
|
+
"ts-jest": "29.1.2",
|
|
30
|
+
"typescript": "5.4.3"
|
|
28
31
|
},
|
|
29
32
|
"engines": {
|
|
30
33
|
"node": ">= 14.0.0"
|
|
@@ -39,6 +39,24 @@ describe('browser local storage cache', () => {
|
|
|
39
39
|
expect(missMock.mock.calls.length).toBe(1);
|
|
40
40
|
});
|
|
41
41
|
|
|
42
|
+
it('reads unexpired timeToLive keys', async () => {
|
|
43
|
+
const cache = createBrowserLocalStorageCache({
|
|
44
|
+
key: version,
|
|
45
|
+
timeToLive: 5,
|
|
46
|
+
});
|
|
47
|
+
await cache.set({ key: 'foo' }, { bar: 1 });
|
|
48
|
+
|
|
49
|
+
const defaultValue = (): DefaultValue => Promise.resolve({ bar: 2 });
|
|
50
|
+
|
|
51
|
+
expect(
|
|
52
|
+
await cache.get({ key: 'foo' }, defaultValue, {
|
|
53
|
+
miss: () => Promise.resolve(missMock()),
|
|
54
|
+
})
|
|
55
|
+
).toMatchObject({ bar: 1 });
|
|
56
|
+
|
|
57
|
+
expect(missMock.mock.calls.length).toBe(0);
|
|
58
|
+
});
|
|
59
|
+
|
|
42
60
|
it('deletes keys', async () => {
|
|
43
61
|
const cache = createBrowserLocalStorageCache({ key: version });
|
|
44
62
|
|
|
@@ -53,19 +71,43 @@ describe('browser local storage cache', () => {
|
|
|
53
71
|
expect(missMock.mock.calls.length).toBe(1);
|
|
54
72
|
});
|
|
55
73
|
|
|
74
|
+
it('deletes expired keys', async () => {
|
|
75
|
+
const cache = createBrowserLocalStorageCache({
|
|
76
|
+
key: version,
|
|
77
|
+
timeToLive: -1,
|
|
78
|
+
});
|
|
79
|
+
await cache.set({ key: 'foo' }, { bar: 1 });
|
|
80
|
+
|
|
81
|
+
const defaultValue = (): DefaultValue => Promise.resolve({ bar: 2 });
|
|
82
|
+
|
|
83
|
+
expect(
|
|
84
|
+
await cache.get({ key: 'foo' }, defaultValue, {
|
|
85
|
+
miss: () => Promise.resolve(missMock()),
|
|
86
|
+
})
|
|
87
|
+
).toMatchObject({ bar: 2 });
|
|
88
|
+
|
|
89
|
+
expect(missMock.mock.calls.length).toBe(1);
|
|
90
|
+
});
|
|
91
|
+
|
|
56
92
|
it('can be cleared', async () => {
|
|
57
93
|
const cache = createBrowserLocalStorageCache({ key: version });
|
|
58
|
-
|
|
59
94
|
await cache.set({ key: 'foo' }, { bar: 1 });
|
|
95
|
+
|
|
60
96
|
await cache.clear();
|
|
61
97
|
|
|
62
|
-
const defaultValue = ():
|
|
98
|
+
const defaultValue = (): Promise<void> => Promise.resolve({ bar: 2 });
|
|
63
99
|
|
|
64
|
-
expect(await cache.get({ key: 'foo' }, defaultValue, events)).toMatchObject(
|
|
65
|
-
{ bar: 2 }
|
|
66
|
-
);
|
|
67
|
-
expect(missMock.mock.calls.length).toBe(1);
|
|
68
100
|
expect(localStorage.length).toBe(0);
|
|
101
|
+
|
|
102
|
+
expect(
|
|
103
|
+
await cache.get({ key: 'foo' }, defaultValue, {
|
|
104
|
+
miss: () => Promise.resolve(missMock()),
|
|
105
|
+
})
|
|
106
|
+
).toMatchObject({ bar: 2 });
|
|
107
|
+
|
|
108
|
+
expect(missMock.mock.calls.length).toBe(1);
|
|
109
|
+
|
|
110
|
+
expect(localStorage.getItem(`algolia-client-js-${version}`)).toEqual('{}');
|
|
69
111
|
});
|
|
70
112
|
|
|
71
113
|
it('do throws localstorage exceptions on access', async () => {
|
|
@@ -122,13 +164,23 @@ describe('browser local storage cache', () => {
|
|
|
122
164
|
});
|
|
123
165
|
const key = { foo: 'bar' };
|
|
124
166
|
const value = 'foo';
|
|
125
|
-
|
|
126
167
|
expect(localStorage.getItem(`algolia-client-js-${version}`)).toBeNull();
|
|
127
168
|
|
|
128
169
|
await cache.set(key, value);
|
|
129
170
|
|
|
130
|
-
expect
|
|
131
|
-
|
|
171
|
+
const expectedValue = expect.objectContaining({
|
|
172
|
+
[JSON.stringify(key)]: {
|
|
173
|
+
timestamp: expect.any(Number),
|
|
174
|
+
value,
|
|
175
|
+
},
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
const localStorageValue = localStorage.getItem(
|
|
179
|
+
`algolia-client-js-${version}`
|
|
180
|
+
);
|
|
181
|
+
|
|
182
|
+
expect(JSON.parse(localStorageValue ? localStorageValue : '{}')).toEqual(
|
|
183
|
+
expectedValue
|
|
132
184
|
);
|
|
133
185
|
});
|
|
134
186
|
});
|
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
BrowserLocalStorageCacheItem,
|
|
3
|
+
BrowserLocalStorageOptions,
|
|
4
|
+
Cache,
|
|
5
|
+
CacheEvents,
|
|
6
|
+
} from '../types';
|
|
2
7
|
|
|
3
8
|
export function createBrowserLocalStorageCache(
|
|
4
9
|
options: BrowserLocalStorageOptions
|
|
@@ -19,20 +24,61 @@ export function createBrowserLocalStorageCache(
|
|
|
19
24
|
return JSON.parse(getStorage().getItem(namespaceKey) || '{}');
|
|
20
25
|
}
|
|
21
26
|
|
|
27
|
+
function setNamespace(namespace: Record<string, any>): void {
|
|
28
|
+
getStorage().setItem(namespaceKey, JSON.stringify(namespace));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function removeOutdatedCacheItems(): void {
|
|
32
|
+
const timeToLive = options.timeToLive ? options.timeToLive * 1000 : null;
|
|
33
|
+
const namespace = getNamespace<BrowserLocalStorageCacheItem>();
|
|
34
|
+
|
|
35
|
+
const filteredNamespaceWithoutOldFormattedCacheItems = Object.fromEntries(
|
|
36
|
+
Object.entries(namespace).filter(([, cacheItem]) => {
|
|
37
|
+
return cacheItem.timestamp !== undefined;
|
|
38
|
+
})
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
setNamespace(filteredNamespaceWithoutOldFormattedCacheItems);
|
|
42
|
+
|
|
43
|
+
if (!timeToLive) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const filteredNamespaceWithoutExpiredItems = Object.fromEntries(
|
|
48
|
+
Object.entries(filteredNamespaceWithoutOldFormattedCacheItems).filter(
|
|
49
|
+
([, cacheItem]) => {
|
|
50
|
+
const currentTimestamp = new Date().getTime();
|
|
51
|
+
const isExpired = cacheItem.timestamp + timeToLive < currentTimestamp;
|
|
52
|
+
|
|
53
|
+
return !isExpired;
|
|
54
|
+
}
|
|
55
|
+
)
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
setNamespace(filteredNamespaceWithoutExpiredItems);
|
|
59
|
+
}
|
|
60
|
+
|
|
22
61
|
return {
|
|
23
62
|
get<TValue>(
|
|
24
63
|
key: Record<string, any> | string,
|
|
25
64
|
defaultValue: () => Promise<TValue>,
|
|
26
65
|
events: CacheEvents<TValue> = {
|
|
27
|
-
miss: ()
|
|
66
|
+
miss: () => Promise.resolve(),
|
|
28
67
|
}
|
|
29
68
|
): Promise<TValue> {
|
|
30
69
|
return Promise.resolve()
|
|
31
70
|
.then(() => {
|
|
32
|
-
|
|
33
|
-
const value = getNamespace<TValue>()[keyAsString];
|
|
71
|
+
removeOutdatedCacheItems();
|
|
34
72
|
|
|
35
|
-
return Promise
|
|
73
|
+
return getNamespace<Promise<BrowserLocalStorageCacheItem>>()[
|
|
74
|
+
JSON.stringify(key)
|
|
75
|
+
];
|
|
76
|
+
})
|
|
77
|
+
.then((value) => {
|
|
78
|
+
return Promise.all([
|
|
79
|
+
value ? value.value : defaultValue(),
|
|
80
|
+
value !== undefined,
|
|
81
|
+
]);
|
|
36
82
|
})
|
|
37
83
|
.then(([value, exists]) => {
|
|
38
84
|
return Promise.all([value, exists || events.miss(value)]);
|
|
@@ -47,7 +93,10 @@ export function createBrowserLocalStorageCache(
|
|
|
47
93
|
return Promise.resolve().then(() => {
|
|
48
94
|
const namespace = getNamespace();
|
|
49
95
|
|
|
50
|
-
namespace[JSON.stringify(key)] =
|
|
96
|
+
namespace[JSON.stringify(key)] = {
|
|
97
|
+
timestamp: new Date().getTime(),
|
|
98
|
+
value,
|
|
99
|
+
};
|
|
51
100
|
|
|
52
101
|
getStorage().setItem(namespaceKey, JSON.stringify(namespace));
|
|
53
102
|
|