-
Notifications
You must be signed in to change notification settings - Fork 215
/
transporter.ts
153 lines (130 loc) · 3.58 KB
/
transporter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import type { Cache } from './cache';
import type { Host } from './host';
import type { Request, Requester, EndRequest, Response } from './requester';
export type Headers = Record<string, string>;
export type QueryParameters = Record<string, any>;
export type RequestOptions = Pick<Request, 'cacheable'> & {
/**
* Custom timeout for the request. Note that, in normal situations
* the given timeout will be applied. But the transporter layer may
* increase this timeout if there is need for it.
*/
timeouts?: Partial<Timeouts>;
/**
* Custom headers for the request. This headers are
* going to be merged the transporter headers.
*/
headers?: Headers;
/**
* Custom query parameters for the request. This query parameters are
* going to be merged the transporter query parameters.
*/
queryParameters?: QueryParameters;
/**
* Custom data for the request. This data is
* going to be merged the transporter data.
*/
data?: Array<Record<string, any>> | Record<string, any>;
};
export type StackFrame = {
request: EndRequest;
response: Response;
host: Host;
triesLeft: number;
};
export type AlgoliaAgentOptions = {
/**
* The segment. Usually the integration name.
*/
segment: string;
/**
* The version. Usually the integration version.
*/
version?: string;
};
export type AlgoliaAgent = {
/**
* The raw value of the user agent.
*/
value: string;
/**
* Mutates the current user agent adding the given user agent options.
*/
add: (options: AlgoliaAgentOptions) => AlgoliaAgent;
};
export type Timeouts = {
/**
* Timeout in milliseconds before the connection is established.
*/
connect: number;
/**
* Timeout in milliseconds before reading the response on a read request.
*/
read: number;
/**
* Timeout in milliseconds before reading the response on a write request.
*/
write: number;
};
export type TransporterOptions = {
/**
* The cache of the hosts. Usually used to persist
* the state of the host when its down.
*/
hostsCache: Cache;
/**
* The underlying requester used. Should differ
* depending of the environment where the client
* will be used.
*/
requester: Requester;
/**
* The cache of the requests. When requests are
* `cacheable`, the returned promised persists
* in this cache to shared in similar requests
* before being resolved.
*/
requestsCache: Cache;
/**
* The cache of the responses. When requests are
* `cacheable`, the returned responses persists
* in this cache to shared in similar requests.
*/
responsesCache: Cache;
/**
* The timeouts used by the requester. The transporter
* layer may increase this timeouts as defined on the
* retry strategy.
*/
timeouts: Timeouts;
/**
* The hosts used by the requester.
*/
hosts: Host[];
/**
* The headers used by the requester. The transporter
* layer may add some extra headers during the request
* for the user agent, and others.
*/
baseHeaders: Headers;
/**
* The query parameters used by the requester. The transporter
* layer may add some extra headers during the request
* for the user agent, and others.
*/
baseQueryParameters: QueryParameters;
/**
* The user agent used. Sent on query parameters.
*/
algoliaAgent: AlgoliaAgent;
};
export type Transporter = TransporterOptions & {
/**
* Performs a request.
* The `baseRequest` and `baseRequestOptions` will be merged accordingly.
*/
request: <TResponse>(
baseRequest: Request,
baseRequestOptions?: RequestOptions
) => Promise<TResponse>;
};