@algolia/client-common 4.14.2 → 5.0.0-alpha.3
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 +803 -91
- package/dist/client-common.esm.node.js +774 -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/createRetryablePromise.d.ts +14 -0
- package/dist/src/createRetryablePromise.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/CreateRetryablePromise.d.ts +19 -0
- package/dist/src/types/CreateRetryablePromise.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/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/dist/src/types/index.d.ts +7 -0
- package/dist/src/types/index.d.ts.map +1 -0
- package/index.ts +9 -0
- package/package.json +24 -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-retryable-promise.test.ts +86 -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/createRetryablePromise.ts +52 -0
- package/src/getAlgoliaAgent.ts +25 -0
- package/src/transporter/createStatefulHost.ts +24 -0
- package/src/transporter/createTransporter.ts +347 -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/CreateRetryablePromise.ts +21 -0
- package/src/types/Host.ts +38 -0
- package/src/types/Requester.ts +72 -0
- package/src/types/Transporter.ts +153 -0
- package/src/types/index.ts +6 -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,51 @@
|
|
|
1
|
+
import type { Response, StackFrame } from '../types';
|
|
2
|
+
|
|
3
|
+
export class AlgoliaError extends Error {
|
|
4
|
+
name: string = 'AlgoliaError';
|
|
5
|
+
|
|
6
|
+
constructor(message: string, name: string) {
|
|
7
|
+
super(message);
|
|
8
|
+
|
|
9
|
+
if (name) {
|
|
10
|
+
this.name = name;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export class ErrorWithStackTrace extends AlgoliaError {
|
|
16
|
+
stackTrace: StackFrame[];
|
|
17
|
+
|
|
18
|
+
constructor(message: string, stackTrace: StackFrame[], name: string) {
|
|
19
|
+
super(message, name);
|
|
20
|
+
// the array and object should be frozen to reflect the stackTrace at the time of the error
|
|
21
|
+
this.stackTrace = stackTrace;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export class RetryError extends ErrorWithStackTrace {
|
|
26
|
+
constructor(stackTrace: StackFrame[]) {
|
|
27
|
+
super(
|
|
28
|
+
'Unreachable hosts - your application id may be incorrect. If the error persists, contact support@algolia.com.',
|
|
29
|
+
stackTrace,
|
|
30
|
+
'RetryError'
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export class ApiError extends ErrorWithStackTrace {
|
|
36
|
+
status: number;
|
|
37
|
+
|
|
38
|
+
constructor(message: string, status: number, stackTrace: StackFrame[]) {
|
|
39
|
+
super(message, stackTrace, 'ApiError');
|
|
40
|
+
this.status = status;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export class DeserializationError extends AlgoliaError {
|
|
45
|
+
response: Response;
|
|
46
|
+
|
|
47
|
+
constructor(message: string, response: Response) {
|
|
48
|
+
super(message, 'DeserializationError');
|
|
49
|
+
this.response = response;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Headers,
|
|
3
|
+
Host,
|
|
4
|
+
QueryParameters,
|
|
5
|
+
Request,
|
|
6
|
+
RequestOptions,
|
|
7
|
+
Response,
|
|
8
|
+
StackFrame,
|
|
9
|
+
} from '../types';
|
|
10
|
+
|
|
11
|
+
import { ApiError, DeserializationError } from './errors';
|
|
12
|
+
|
|
13
|
+
export function shuffle<TData>(array: TData[]): TData[] {
|
|
14
|
+
const shuffledArray = array;
|
|
15
|
+
|
|
16
|
+
for (let c = array.length - 1; c > 0; c--) {
|
|
17
|
+
const b = Math.floor(Math.random() * (c + 1));
|
|
18
|
+
const a = array[c];
|
|
19
|
+
|
|
20
|
+
shuffledArray[c] = array[b];
|
|
21
|
+
shuffledArray[b] = a;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return shuffledArray;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function serializeUrl(
|
|
28
|
+
host: Host,
|
|
29
|
+
path: string,
|
|
30
|
+
queryParameters: QueryParameters
|
|
31
|
+
): string {
|
|
32
|
+
const queryParametersAsString = serializeQueryParameters(queryParameters);
|
|
33
|
+
let url = `${host.protocol}://${host.url}/${
|
|
34
|
+
path.charAt(0) === '/' ? path.substr(1) : path
|
|
35
|
+
}`;
|
|
36
|
+
|
|
37
|
+
if (queryParametersAsString.length) {
|
|
38
|
+
url += `?${queryParametersAsString}`;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return url;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function serializeQueryParameters(parameters: QueryParameters): string {
|
|
45
|
+
const isObjectOrArray = (value: any): boolean =>
|
|
46
|
+
Object.prototype.toString.call(value) === '[object Object]' ||
|
|
47
|
+
Object.prototype.toString.call(value) === '[object Array]';
|
|
48
|
+
|
|
49
|
+
return Object.keys(parameters)
|
|
50
|
+
.map(
|
|
51
|
+
(key) =>
|
|
52
|
+
`${key}=${
|
|
53
|
+
isObjectOrArray(parameters[key])
|
|
54
|
+
? JSON.stringify(parameters[key])
|
|
55
|
+
: parameters[key]
|
|
56
|
+
}`
|
|
57
|
+
)
|
|
58
|
+
.join('&');
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function serializeData(
|
|
62
|
+
request: Request,
|
|
63
|
+
requestOptions: RequestOptions
|
|
64
|
+
): string | undefined {
|
|
65
|
+
if (
|
|
66
|
+
request.method === 'GET' ||
|
|
67
|
+
(request.data === undefined && requestOptions.data === undefined)
|
|
68
|
+
) {
|
|
69
|
+
return undefined;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const data = Array.isArray(request.data)
|
|
73
|
+
? request.data
|
|
74
|
+
: { ...request.data, ...requestOptions.data };
|
|
75
|
+
|
|
76
|
+
return JSON.stringify(data);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function serializeHeaders(
|
|
80
|
+
baseHeaders: Headers,
|
|
81
|
+
requestHeaders: Headers,
|
|
82
|
+
requestOptionsHeaders?: Headers
|
|
83
|
+
): Headers {
|
|
84
|
+
const headers: Headers = {
|
|
85
|
+
Accept: 'application/json',
|
|
86
|
+
...baseHeaders,
|
|
87
|
+
...requestHeaders,
|
|
88
|
+
...requestOptionsHeaders,
|
|
89
|
+
};
|
|
90
|
+
const serializedHeaders: Headers = {};
|
|
91
|
+
|
|
92
|
+
Object.keys(headers).forEach((header) => {
|
|
93
|
+
const value = headers[header];
|
|
94
|
+
serializedHeaders[header.toLowerCase()] = value;
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
return serializedHeaders;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function deserializeSuccess<TObject>(response: Response): TObject {
|
|
101
|
+
try {
|
|
102
|
+
return JSON.parse(response.content);
|
|
103
|
+
} catch (e) {
|
|
104
|
+
throw new DeserializationError((e as Error).message, response);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function deserializeFailure(
|
|
109
|
+
{ content, status }: Response,
|
|
110
|
+
stackFrame: StackFrame[]
|
|
111
|
+
): Error {
|
|
112
|
+
let message = content;
|
|
113
|
+
try {
|
|
114
|
+
message = JSON.parse(content).message;
|
|
115
|
+
} catch (e) {
|
|
116
|
+
// ..
|
|
117
|
+
}
|
|
118
|
+
return new ApiError(message, status, stackFrame);
|
|
119
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Response } from '../types';
|
|
2
|
+
|
|
3
|
+
export function isNetworkError({
|
|
4
|
+
isTimedOut,
|
|
5
|
+
status,
|
|
6
|
+
}: Omit<Response, 'content'>): boolean {
|
|
7
|
+
return !isTimedOut && ~~status === 0;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function isRetryable({
|
|
11
|
+
isTimedOut,
|
|
12
|
+
status,
|
|
13
|
+
}: Omit<Response, 'content'>): boolean {
|
|
14
|
+
return (
|
|
15
|
+
isTimedOut ||
|
|
16
|
+
isNetworkError({ isTimedOut, status }) ||
|
|
17
|
+
(~~(status / 100) !== 2 && ~~(status / 100) !== 4)
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function isSuccess({ status }: Pick<Response, 'status'>): boolean {
|
|
22
|
+
return ~~(status / 100) === 2;
|
|
23
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Headers, StackFrame } from '../types';
|
|
2
|
+
|
|
3
|
+
export function stackTraceWithoutCredentials(
|
|
4
|
+
stackTrace: StackFrame[]
|
|
5
|
+
): StackFrame[] {
|
|
6
|
+
return stackTrace.map((stackFrame) =>
|
|
7
|
+
stackFrameWithoutCredentials(stackFrame)
|
|
8
|
+
);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function stackFrameWithoutCredentials(
|
|
12
|
+
stackFrame: StackFrame
|
|
13
|
+
): StackFrame {
|
|
14
|
+
const modifiedHeaders: Headers = stackFrame.request.headers[
|
|
15
|
+
'x-algolia-api-key'
|
|
16
|
+
]
|
|
17
|
+
? { 'x-algolia-api-key': '*****' }
|
|
18
|
+
: {};
|
|
19
|
+
|
|
20
|
+
return {
|
|
21
|
+
...stackFrame,
|
|
22
|
+
request: {
|
|
23
|
+
...stackFrame.request,
|
|
24
|
+
headers: {
|
|
25
|
+
...stackFrame.request.headers,
|
|
26
|
+
...modifiedHeaders,
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export type Cache = {
|
|
2
|
+
/**
|
|
3
|
+
* Gets the value of the given `key`.
|
|
4
|
+
*/
|
|
5
|
+
get: <TValue>(
|
|
6
|
+
key: Record<string, any> | string,
|
|
7
|
+
defaultValue: () => Promise<TValue>,
|
|
8
|
+
events?: CacheEvents<TValue>
|
|
9
|
+
) => Promise<TValue>;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Sets the given value with the given `key`.
|
|
13
|
+
*/
|
|
14
|
+
set: <TValue>(
|
|
15
|
+
key: Record<string, any> | string,
|
|
16
|
+
value: TValue
|
|
17
|
+
) => Promise<TValue>;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Deletes the given `key`.
|
|
21
|
+
*/
|
|
22
|
+
delete: (key: Record<string, any> | string) => Promise<void>;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Clears the cache.
|
|
26
|
+
*/
|
|
27
|
+
clear: () => Promise<void>;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export type CacheEvents<TValue> = {
|
|
31
|
+
/**
|
|
32
|
+
* The callback when the given `key` is missing from the cache.
|
|
33
|
+
*/
|
|
34
|
+
miss: (value: TValue) => Promise<any>;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export type MemoryCacheOptions = {
|
|
38
|
+
/**
|
|
39
|
+
* If keys and values should be serialized using `JSON.stringify`.
|
|
40
|
+
*/
|
|
41
|
+
serializable?: boolean;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export type BrowserLocalStorageOptions = {
|
|
45
|
+
/**
|
|
46
|
+
* The cache key.
|
|
47
|
+
*/
|
|
48
|
+
key: string;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The native local storage implementation.
|
|
52
|
+
*/
|
|
53
|
+
localStorage?: Storage;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export type FallbackableCacheOptions = {
|
|
57
|
+
/**
|
|
58
|
+
* List of caches order by priority.
|
|
59
|
+
*/
|
|
60
|
+
caches: Cache[];
|
|
61
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { AlgoliaAgentOptions, TransporterOptions } from './Transporter';
|
|
2
|
+
|
|
3
|
+
export type AuthMode = 'WithinHeaders' | 'WithinQueryParameters';
|
|
4
|
+
|
|
5
|
+
type OverriddenTransporterOptions =
|
|
6
|
+
| 'baseHeaders'
|
|
7
|
+
| 'baseQueryParameters'
|
|
8
|
+
| 'hosts';
|
|
9
|
+
|
|
10
|
+
export type CreateClientOptions = Omit<
|
|
11
|
+
TransporterOptions,
|
|
12
|
+
OverriddenTransporterOptions | 'algoliaAgent'
|
|
13
|
+
> &
|
|
14
|
+
Partial<Pick<TransporterOptions, OverriddenTransporterOptions>> & {
|
|
15
|
+
appId: string;
|
|
16
|
+
apiKey: string;
|
|
17
|
+
authMode?: AuthMode;
|
|
18
|
+
algoliaAgents: AlgoliaAgentOptions[];
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type ClientOptions = Partial<
|
|
22
|
+
Omit<CreateClientOptions, 'apiKey' | 'appId'>
|
|
23
|
+
>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export type CreateRetryablePromiseOptions<TResponse> = {
|
|
2
|
+
/**
|
|
3
|
+
* The function to run, which returns a promise.
|
|
4
|
+
*/
|
|
5
|
+
func: () => Promise<TResponse>;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The validator function. It receives the resolved return of `func`.
|
|
9
|
+
*/
|
|
10
|
+
validate: (response: TResponse) => boolean;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The maximum number of retry. 50 by default.
|
|
14
|
+
*/
|
|
15
|
+
maxRetries?: number;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The function to decide how long to wait between retries.
|
|
19
|
+
*/
|
|
20
|
+
timeout?: (retryCount: number) => number;
|
|
21
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export type Host = {
|
|
2
|
+
/**
|
|
3
|
+
* The host URL.
|
|
4
|
+
*/
|
|
5
|
+
url: string;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The accepted transporter.
|
|
9
|
+
*/
|
|
10
|
+
accept: 'read' | 'readWrite' | 'write';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The protocol of the host URL.
|
|
14
|
+
*/
|
|
15
|
+
protocol: 'http' | 'https';
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export type StatefulHost = Host & {
|
|
19
|
+
/**
|
|
20
|
+
* The status of the host.
|
|
21
|
+
*/
|
|
22
|
+
status: 'down' | 'timed out' | 'up';
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The last update of the host status, used to compare with the expiration delay.
|
|
26
|
+
*/
|
|
27
|
+
lastUpdate: number;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Returns whether the host is up or not.
|
|
31
|
+
*/
|
|
32
|
+
isUp: () => boolean;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Returns whether the host is timed out or not.
|
|
36
|
+
*/
|
|
37
|
+
isTimedOut: () => boolean;
|
|
38
|
+
};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { Headers, QueryParameters } from './Transporter';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The method of the request.
|
|
5
|
+
*/
|
|
6
|
+
export type Method = 'DELETE' | 'GET' | 'PATCH' | 'POST' | 'PUT';
|
|
7
|
+
|
|
8
|
+
export type Request = {
|
|
9
|
+
method: Method;
|
|
10
|
+
/**
|
|
11
|
+
* The path of the REST API to send the request to.
|
|
12
|
+
*/
|
|
13
|
+
path: string;
|
|
14
|
+
queryParameters: QueryParameters;
|
|
15
|
+
data?: Array<Record<string, any>> | Record<string, any>;
|
|
16
|
+
headers: Headers;
|
|
17
|
+
/**
|
|
18
|
+
* If the given request should persist on the cache. Keep in mind,
|
|
19
|
+
* that some methods may have this option enabled by default.
|
|
20
|
+
*/
|
|
21
|
+
cacheable?: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Some POST methods in the Algolia REST API uses the `read` transporter.
|
|
24
|
+
* This information is defined at the spec level.
|
|
25
|
+
*/
|
|
26
|
+
useReadTransporter?: boolean;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export type EndRequest = Pick<Request, 'headers' | 'method'> & {
|
|
30
|
+
/**
|
|
31
|
+
* The full URL of the REST API.
|
|
32
|
+
*/
|
|
33
|
+
url: string;
|
|
34
|
+
/**
|
|
35
|
+
* The connection timeout, in milliseconds.
|
|
36
|
+
*/
|
|
37
|
+
connectTimeout: number;
|
|
38
|
+
/**
|
|
39
|
+
* The response timeout, in milliseconds.
|
|
40
|
+
*/
|
|
41
|
+
responseTimeout: number;
|
|
42
|
+
data?: string;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export type Response = {
|
|
46
|
+
/**
|
|
47
|
+
* The body of the response.
|
|
48
|
+
*/
|
|
49
|
+
content: string;
|
|
50
|
+
/**
|
|
51
|
+
* Whether the API call is timed out or not.
|
|
52
|
+
*/
|
|
53
|
+
isTimedOut: boolean;
|
|
54
|
+
/**
|
|
55
|
+
* The HTTP status code of the response.
|
|
56
|
+
*/
|
|
57
|
+
status: number;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export type Requester = {
|
|
61
|
+
/**
|
|
62
|
+
* Sends the given `request` to the server.
|
|
63
|
+
*/
|
|
64
|
+
send: (request: EndRequest) => Promise<Response>;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
export type EchoResponse = Omit<EndRequest, 'data'> &
|
|
68
|
+
Pick<Request, 'data' | 'path'> & {
|
|
69
|
+
host: string;
|
|
70
|
+
algoliaAgent: string;
|
|
71
|
+
searchParams?: Record<string, string>;
|
|
72
|
+
};
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import type { Cache } from './Cache';
|
|
2
|
+
import type { Host } from './Host';
|
|
3
|
+
import type { Request, Requester, EndRequest, Response } from './Requester';
|
|
4
|
+
|
|
5
|
+
export type Headers = Record<string, string>;
|
|
6
|
+
export type QueryParameters = Record<string, any>;
|
|
7
|
+
|
|
8
|
+
export type RequestOptions = Pick<Request, 'cacheable'> & {
|
|
9
|
+
/**
|
|
10
|
+
* Custom timeout for the request. Note that, in normal situations
|
|
11
|
+
* the given timeout will be applied. But the transporter layer may
|
|
12
|
+
* increase this timeout if there is need for it.
|
|
13
|
+
*/
|
|
14
|
+
timeout?: number;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Custom headers for the request. This headers are
|
|
18
|
+
* going to be merged the transporter headers.
|
|
19
|
+
*/
|
|
20
|
+
headers?: Headers;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Custom query parameters for the request. This query parameters are
|
|
24
|
+
* going to be merged the transporter query parameters.
|
|
25
|
+
*/
|
|
26
|
+
queryParameters?: QueryParameters;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Custom data for the request. This data is
|
|
30
|
+
* going to be merged the transporter data.
|
|
31
|
+
*/
|
|
32
|
+
data?: Array<Record<string, any>> | Record<string, any>;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export type StackFrame = {
|
|
36
|
+
request: EndRequest;
|
|
37
|
+
response: Response;
|
|
38
|
+
host: Host;
|
|
39
|
+
triesLeft: number;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export type AlgoliaAgentOptions = {
|
|
43
|
+
/**
|
|
44
|
+
* The segment. Usually the integration name.
|
|
45
|
+
*/
|
|
46
|
+
segment: string;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The version. Usually the integration version.
|
|
50
|
+
*/
|
|
51
|
+
version?: string;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export type AlgoliaAgent = {
|
|
55
|
+
/**
|
|
56
|
+
* The raw value of the user agent.
|
|
57
|
+
*/
|
|
58
|
+
value: string;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Mutates the current user agent adding the given user agent options.
|
|
62
|
+
*/
|
|
63
|
+
add: (options: AlgoliaAgentOptions) => AlgoliaAgent;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export type Timeouts = {
|
|
67
|
+
/**
|
|
68
|
+
* Timeout in milliseconds before the connection is established.
|
|
69
|
+
*/
|
|
70
|
+
connect: number;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Timeout in milliseconds before reading the response on a read request.
|
|
74
|
+
*/
|
|
75
|
+
read: number;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Timeout in milliseconds before reading the response on a write request.
|
|
79
|
+
*/
|
|
80
|
+
write: number;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export type TransporterOptions = {
|
|
84
|
+
/**
|
|
85
|
+
* The cache of the hosts. Usually used to persist
|
|
86
|
+
* the state of the host when its down.
|
|
87
|
+
*/
|
|
88
|
+
hostsCache: Cache;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* The underlying requester used. Should differ
|
|
92
|
+
* depending of the environment where the client
|
|
93
|
+
* will be used.
|
|
94
|
+
*/
|
|
95
|
+
requester: Requester;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* The cache of the requests. When requests are
|
|
99
|
+
* `cacheable`, the returned promised persists
|
|
100
|
+
* in this cache to shared in similar requests
|
|
101
|
+
* before being resolved.
|
|
102
|
+
*/
|
|
103
|
+
requestsCache: Cache;
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* The cache of the responses. When requests are
|
|
107
|
+
* `cacheable`, the returned responses persists
|
|
108
|
+
* in this cache to shared in similar requests.
|
|
109
|
+
*/
|
|
110
|
+
responsesCache: Cache;
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* The timeouts used by the requester. The transporter
|
|
114
|
+
* layer may increase this timeouts as defined on the
|
|
115
|
+
* retry strategy.
|
|
116
|
+
*/
|
|
117
|
+
timeouts: Timeouts;
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* The hosts used by the requester.
|
|
121
|
+
*/
|
|
122
|
+
hosts: Host[];
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* The headers used by the requester. The transporter
|
|
126
|
+
* layer may add some extra headers during the request
|
|
127
|
+
* for the user agent, and others.
|
|
128
|
+
*/
|
|
129
|
+
baseHeaders: Headers;
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* The query parameters used by the requester. The transporter
|
|
133
|
+
* layer may add some extra headers during the request
|
|
134
|
+
* for the user agent, and others.
|
|
135
|
+
*/
|
|
136
|
+
baseQueryParameters: QueryParameters;
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* The user agent used. Sent on query parameters.
|
|
140
|
+
*/
|
|
141
|
+
algoliaAgent: AlgoliaAgent;
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
export type Transporter = TransporterOptions & {
|
|
145
|
+
/**
|
|
146
|
+
* Performs a request.
|
|
147
|
+
* The `baseRequest` and `baseRequestOptions` will be merged accordingly.
|
|
148
|
+
*/
|
|
149
|
+
request: <TResponse>(
|
|
150
|
+
baseRequest: Request,
|
|
151
|
+
baseRequestOptions?: RequestOptions
|
|
152
|
+
) => Promise<TResponse>;
|
|
153
|
+
};
|