transporter.d.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. import { Cache } from '@algolia/cache-common';
  2. import { Logger } from '@algolia/logger-common';
  3. import { MethodType } from '@algolia/requester-common';
  4. import { Request as Request_2 } from '@algolia/requester-common';
  5. import { Requester } from '@algolia/requester-common';
  6. import { Response } from '@algolia/requester-common';
  7. export declare type ApiError = Error & {
  8. /**
  9. * The http status code.
  10. */
  11. readonly status: number;
  12. /**
  13. * Contains report of stack frames of the
  14. * execution of a certain request.
  15. */
  16. readonly transporterStackTrace: readonly StackFrame[];
  17. };
  18. export declare const CallEnum: Readonly<Record<string, CallType>>;
  19. export declare type CallType = 1 | 2 | 3;
  20. export declare function createApiError(message: string, status: number, transporterStackTrace: readonly StackFrame[]): ApiError;
  21. export declare function createDeserializationError(message: string, response: Response): DeserializationError;
  22. export declare function createMappedRequestOptions(requestOptions?: RequestOptions, timeout?: number): MappedRequestOptions;
  23. export declare function createRetryError(transporterStackTrace: readonly StackFrame[]): RetryError;
  24. export declare function createStatefulHost(host: StatelessHost, status?: HostStatusType): StatefulHost;
  25. export declare function createStatelessHost(options: HostOptions): StatelessHost;
  26. export declare function createTransporter(options: TransporterOptions): Transporter;
  27. export declare function createUserAgent(version: string): UserAgent;
  28. export declare type DeserializationError = Error & {
  29. /**
  30. * The raw response from the server.
  31. */
  32. readonly response: Response;
  33. };
  34. export declare function deserializeFailure({ content, status }: Response, stackFrame: readonly StackFrame[]): Error;
  35. export declare function deserializeSuccess<TObject>(response: Response): TObject;
  36. export declare type Headers = Readonly<Record<string, string>>;
  37. export declare type HostOptions = string | {
  38. /**
  39. * The url of the server, without the protocol.
  40. */
  41. readonly url: string;
  42. /**
  43. * The type of host. Defaults to `Any`.
  44. */
  45. readonly accept?: CallType;
  46. /**
  47. * The protocol. Defaults to `https`.
  48. */
  49. readonly protocol?: string;
  50. };
  51. export declare const HostStatusEnum: Readonly<Record<string, HostStatusType>>;
  52. export declare type HostStatusType = 1 | 2 | 3;
  53. export declare function isStatefulHostTimeouted(host: StatefulHost): boolean;
  54. export declare function isStatefulHostUp(host: StatefulHost): boolean;
  55. export declare type MappedRequestOptions = {
  56. /**
  57. * If the request should be cached.
  58. */
  59. readonly cacheable: boolean | undefined;
  60. /**
  61. * The `read` or `write` timeout of the request.
  62. */
  63. readonly timeout: number | undefined;
  64. /**
  65. * The headers of the request.
  66. */
  67. readonly headers: Record<string, string>;
  68. /**
  69. * The query parameters of the request.
  70. */
  71. readonly queryParameters: Record<string, any>;
  72. /**
  73. * The data to be transfered to the server.
  74. */
  75. readonly data?: Record<string, string>;
  76. };
  77. export declare type QueryParameters = Readonly<Record<string, string>>;
  78. export declare type Request = {
  79. /**
  80. * The method of the request. `GET`, etc.
  81. */
  82. readonly method: MethodType;
  83. /**
  84. * The path of the request. i.e: `/1/indexes`.
  85. */
  86. readonly path: string;
  87. /**
  88. * The data to transfer to the server.
  89. */
  90. readonly data?: Record<string, any> | ReadonlyArray<Record<string, any>>;
  91. /**
  92. * If the response should persist on cache.
  93. */
  94. readonly cacheable?: boolean;
  95. };
  96. export declare type RequestOptions = {
  97. /**
  98. * If the given request should persist on the cache. Keep in mind,
  99. * that some methods may have this option enabled by default.
  100. */
  101. readonly cacheable?: boolean;
  102. /**
  103. * Custom timeout for the request. Note that, in normal situacions
  104. * the given timeout will be applied. But the transporter layer may
  105. * increase this timeout if there is need for it.
  106. */
  107. readonly timeout?: number;
  108. /**
  109. * Custom headers for the request. This headers are
  110. * going to be merged the transporter headers.
  111. */
  112. readonly headers?: Readonly<Record<string, string>>;
  113. /**
  114. * Custom query parameters for the request. This query parameters are
  115. * going to be merged the transporter query parameters.
  116. */
  117. readonly queryParameters?: Record<string, any>;
  118. /**
  119. * Custom data for the request. This data are
  120. * going to be merged the transporter data.
  121. */
  122. readonly data?: Record<string, any>;
  123. /**
  124. * Additional request body values. It's only taken in
  125. * consideration in `POST` and `PUT` requests.
  126. */
  127. [key: string]: any;
  128. };
  129. export declare type RetryError = Error & {
  130. /**
  131. * Contains report of stack frames of the
  132. * execution of a certain request.
  133. */
  134. readonly transporterStackTrace: readonly StackFrame[];
  135. };
  136. export declare function serializeData(request: Request, requestOptions: RequestOptions): string | undefined;
  137. export declare function serializeHeaders(transporter: Transporter, requestOptions: RequestOptions): Headers;
  138. export declare function serializeQueryParameters(parameters: Readonly<Record<string, any>>): string;
  139. export declare function serializeUrl(host: StatelessHost, path: string, queryParameters: Readonly<Record<string, string>>): string;
  140. export declare type StackFrame = {
  141. /**
  142. * The request made.
  143. */
  144. readonly request: Request_2;
  145. /**
  146. * The received response.
  147. */
  148. readonly response: Response;
  149. /**
  150. * The host associated with the `request` and the `response`.
  151. */
  152. readonly host: StatelessHost;
  153. /**
  154. * The number of tries left.
  155. */
  156. readonly triesLeft: number;
  157. };
  158. export declare function stackFrameWithoutCredentials(stackFrame: StackFrame): StackFrame;
  159. export declare function stackTraceWithoutCredentials(stackTrace: readonly StackFrame[]): readonly StackFrame[];
  160. export declare type StatefulHost = StatelessHost & {
  161. /**
  162. * Holds the last time this host failed in milliseconds elapsed
  163. * since the UNIX epoch. This failure can be because of an
  164. * timeout error or a because the host is not available.
  165. */
  166. readonly lastUpdate: number;
  167. /**
  168. * Holds the host status. Note that, depending of the `lastUpdate`
  169. * an host may be considered as `Up` on the transporter layer.
  170. */
  171. readonly status: HostStatusType;
  172. };
  173. export declare type StatelessHost = {
  174. /**
  175. * The protocol of the stateless host. Between `http` and `https`.
  176. */
  177. readonly protocol: string;
  178. /**
  179. * The url, without protocol.
  180. */
  181. readonly url: string;
  182. /**
  183. * The type of the host.
  184. */
  185. readonly accept: CallType;
  186. };
  187. export declare type Timeouts = {
  188. /**
  189. * The timeout to stablish a connection with the server.
  190. */
  191. readonly connect: number;
  192. /**
  193. * The timeout to receive the response on read requests.
  194. */
  195. readonly read: number;
  196. /**
  197. * The timeout to receive the response on write requests.
  198. */
  199. readonly write: number;
  200. };
  201. export declare type Transporter = {
  202. /**
  203. * The cache of the hosts. Usually used to persist
  204. * the state of the host when its down.
  205. */
  206. readonly hostsCache: Cache;
  207. /**
  208. * The logger instance to send events of the transporter.
  209. */
  210. readonly logger: Logger;
  211. /**
  212. * The underlying requester used. Should differ
  213. * depending of the enviroment where the client
  214. * will be used.
  215. */
  216. readonly requester: Requester;
  217. /**
  218. * The cache of the requests. When requests are
  219. * `cacheable`, the returned promised persists
  220. * in this cache to shared in similar resquests
  221. * before being resolved.
  222. */
  223. readonly requestsCache: Cache;
  224. /**
  225. * The cache of the responses. When requests are
  226. * `cacheable`, the returned responses persists
  227. * in this cache to shared in similar resquests.
  228. */
  229. readonly responsesCache: Cache;
  230. /**
  231. * The timeouts used by the requester. The transporter
  232. * layer may increase this timeouts as defined on the
  233. * retry strategy.
  234. */
  235. readonly timeouts: Timeouts;
  236. /**
  237. * The user agent used. Sent on query parameters.
  238. */
  239. readonly userAgent: UserAgent;
  240. /**
  241. * The headers used on each request.
  242. */
  243. readonly headers: Headers;
  244. /**
  245. * The query parameters used on each request.
  246. */
  247. readonly queryParameters: QueryParameters;
  248. /**
  249. * The hosts used by the retry strategy.
  250. *
  251. * @readonly
  252. */
  253. hosts: readonly StatelessHost[];
  254. /**
  255. * Performs a read request using read hosts.
  256. */
  257. readonly read: <TResponse>(request: Request, requestOptions?: RequestOptions) => Readonly<Promise<TResponse>>;
  258. /**
  259. * Performs a write request using write hosts.
  260. */
  261. readonly write: <TResponse>(request: Request, requestOptions?: RequestOptions) => Readonly<Promise<TResponse>>;
  262. };
  263. export declare type TransporterOptions = {
  264. /**
  265. * The cache of the hosts. Usually used to persist
  266. * the state of the host when its down.
  267. */
  268. readonly hostsCache: Cache;
  269. /**
  270. * The logger instance to send events of the transporter.
  271. */
  272. readonly logger: Logger;
  273. /**
  274. * The underlying requester used. Should differ
  275. * depending of the enviroment where the client
  276. * will be used.
  277. */
  278. readonly requester: Requester;
  279. /**
  280. * The cache of the requests. When requests are
  281. * `cacheable`, the returned promised persists
  282. * in this cache to shared in similar resquests
  283. * before being resolved.
  284. */
  285. readonly requestsCache: Cache;
  286. /**
  287. * The cache of the responses. When requests are
  288. * `cacheable`, the returned responses persists
  289. * in this cache to shared in similar resquests.
  290. */
  291. readonly responsesCache: Cache;
  292. /**
  293. * The timeouts used by the requester. The transporter
  294. * layer may increase this timeouts as defined on the
  295. * retry strategy.
  296. */
  297. readonly timeouts: Timeouts;
  298. /**
  299. * The hosts used by the requester.
  300. */
  301. readonly hosts: readonly HostOptions[];
  302. /**
  303. * The headers used by the requester. The transporter
  304. * layer may add some extra headers during the request
  305. * for the user agent, and others.
  306. */
  307. readonly headers: Headers;
  308. /**
  309. * The query parameters used by the requester. The transporter
  310. * layer may add some extra headers during the request
  311. * for the user agent, and others.
  312. */
  313. readonly queryParameters: QueryParameters;
  314. /**
  315. * The user agent used. Sent on query parameters.
  316. */
  317. readonly userAgent: UserAgent;
  318. };
  319. export declare type UserAgent = {
  320. /**
  321. * The raw value of the user agent.
  322. *
  323. * @readonly
  324. */
  325. value: string;
  326. /**
  327. * Mutates the current user agent ading the given user agent options.
  328. */
  329. readonly add: (options: UserAgentOptions) => UserAgent;
  330. };
  331. export declare type UserAgentOptions = {
  332. /**
  333. * The segment. Usually the integration name.
  334. */
  335. readonly segment: string;
  336. /**
  337. * The version. Usually the integration version.
  338. */
  339. readonly version?: string;
  340. };
  341. export { }