common.d.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. import { Agent } from 'http';
  2. import { URL } from 'url';
  3. import { Readable } from 'stream';
  4. /**
  5. * Support `instanceof` operator for `GaxiosError`s in different versions of this library.
  6. *
  7. * @see {@link GaxiosError[Symbol.hasInstance]}
  8. */
  9. export declare const GAXIOS_ERROR_SYMBOL: unique symbol;
  10. export declare class GaxiosError<T = any> extends Error {
  11. config: GaxiosOptions;
  12. response?: GaxiosResponse<T> | undefined;
  13. error?: (Error | NodeJS.ErrnoException) | undefined;
  14. /**
  15. * An Error code.
  16. * See {@link https://nodejs.org/api/errors.html#errorcode error.code}
  17. *
  18. * @example
  19. * 'ECONNRESET'
  20. */
  21. code?: string;
  22. /**
  23. * An HTTP Status code.
  24. * See {@link https://developer.mozilla.org/en-US/docs/Web/API/Response/status Response: status property}
  25. *
  26. * @example
  27. * 500
  28. */
  29. status?: number;
  30. /**
  31. * Support `instanceof` operator for `GaxiosError` across builds/duplicated files.
  32. *
  33. * @see {@link GAXIOS_ERROR_SYMBOL}
  34. * @see {@link GaxiosError[Symbol.hasInstance]}
  35. * @see {@link https://github.com/microsoft/TypeScript/issues/13965#issuecomment-278570200}
  36. * @see {@link https://stackoverflow.com/questions/46618852/require-and-instanceof}
  37. * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/@@hasInstance#reverting_to_default_instanceof_behavior}
  38. */
  39. [GAXIOS_ERROR_SYMBOL]: string;
  40. /**
  41. * Support `instanceof` operator for `GaxiosError` across builds/duplicated files.
  42. *
  43. * @see {@link GAXIOS_ERROR_SYMBOL}
  44. * @see {@link GaxiosError[GAXIOS_ERROR_SYMBOL]}
  45. */
  46. static [Symbol.hasInstance](instance: unknown): boolean;
  47. constructor(message: string, config: GaxiosOptions, response?: GaxiosResponse<T> | undefined, error?: (Error | NodeJS.ErrnoException) | undefined);
  48. }
  49. export interface Headers {
  50. [index: string]: any;
  51. }
  52. export type GaxiosPromise<T = any> = Promise<GaxiosResponse<T>>;
  53. export interface GaxiosXMLHttpRequest {
  54. responseURL: string;
  55. }
  56. export interface GaxiosResponse<T = any> {
  57. config: GaxiosOptions;
  58. data: T;
  59. status: number;
  60. statusText: string;
  61. headers: Headers;
  62. request: GaxiosXMLHttpRequest;
  63. }
  64. export interface GaxiosMultipartOptions {
  65. headers: Headers;
  66. content: string | Readable;
  67. }
  68. /**
  69. * Request options that are used to form the request.
  70. */
  71. export interface GaxiosOptions {
  72. /**
  73. * Optional method to override making the actual HTTP request. Useful
  74. * for writing tests.
  75. */
  76. adapter?: <T = any>(options: GaxiosOptions, defaultAdapter: (options: GaxiosOptions) => GaxiosPromise<T>) => GaxiosPromise<T>;
  77. url?: string | URL;
  78. /**
  79. * @deprecated
  80. */
  81. baseUrl?: string;
  82. baseURL?: string | URL;
  83. method?: 'GET' | 'HEAD' | 'POST' | 'DELETE' | 'PUT' | 'CONNECT' | 'OPTIONS' | 'TRACE' | 'PATCH';
  84. headers?: Headers;
  85. data?: any;
  86. body?: any;
  87. /**
  88. * The maximum size of the http response content in bytes allowed.
  89. */
  90. maxContentLength?: number;
  91. /**
  92. * The maximum number of redirects to follow. Defaults to 20.
  93. */
  94. maxRedirects?: number;
  95. follow?: number;
  96. /**
  97. * A collection of parts to send as a `Content-Type: multipart/related` request.
  98. */
  99. multipart?: GaxiosMultipartOptions[];
  100. params?: any;
  101. paramsSerializer?: (params: {
  102. [index: string]: string | number;
  103. }) => string;
  104. timeout?: number;
  105. /**
  106. * @deprecated ignored
  107. */
  108. onUploadProgress?: (progressEvent: any) => void;
  109. responseType?: 'arraybuffer' | 'blob' | 'json' | 'text' | 'stream' | 'unknown';
  110. agent?: Agent | ((parsedUrl: URL) => Agent);
  111. validateStatus?: (status: number) => boolean;
  112. retryConfig?: RetryConfig;
  113. retry?: boolean;
  114. signal?: any;
  115. size?: number;
  116. /**
  117. * Implementation of `fetch` to use when making the API call. By default,
  118. * will use the browser context if available, and fall back to `node-fetch`
  119. * in node.js otherwise.
  120. */
  121. fetchImplementation?: FetchImplementation;
  122. cert?: string;
  123. key?: string;
  124. /**
  125. * An optional proxy to use for requests.
  126. * Available via `process.env.HTTP_PROXY` and `process.env.HTTPS_PROXY` as well - with a preference for the this config option when multiple are available.
  127. * The {@link GaxiosOptions.agent `agent`} option overrides this.
  128. *
  129. * @see {@link GaxiosOptions.noProxy}
  130. * @see {@link GaxiosOptions.agent}
  131. */
  132. proxy?: string | URL;
  133. /**
  134. * A list for excluding traffic for proxies.
  135. * Available via `process.env.NO_PROXY` as well as a common-separated list of strings - merged with any local `noProxy` rules.
  136. *
  137. * - When provided a string, it is matched by
  138. * - Wildcard `*.` and `.` matching are available. (e.g. `.example.com` or `*.example.com`)
  139. * - When provided a URL, it is matched by the `.origin` property.
  140. * - For example, requesting `https://example.com` with the following `noProxy`s would result in a no proxy use:
  141. * - new URL('https://example.com')
  142. * - new URL('https://example.com:443')
  143. * - The following would be used with a proxy:
  144. * - new URL('http://example.com:80')
  145. * - new URL('https://example.com:8443')
  146. * - When provided a regular expression it is used to match the stringified URL
  147. *
  148. * @see {@link GaxiosOptions.proxy}
  149. */
  150. noProxy?: (string | URL | RegExp)[];
  151. /**
  152. * An experimental error redactor.
  153. *
  154. * @remarks
  155. *
  156. * This does not replace the requirement for an active Data Loss Prevention (DLP) provider. For DLP suggestions, see:
  157. * - https://cloud.google.com/sensitive-data-protection/docs/redacting-sensitive-data#dlp_deidentify_replace_infotype-nodejs
  158. * - https://cloud.google.com/sensitive-data-protection/docs/infotypes-reference#credentials_and_secrets
  159. *
  160. * @experimental
  161. */
  162. errorRedactor?: typeof defaultErrorRedactor | false;
  163. }
  164. /**
  165. * A partial object of `GaxiosResponse` with only redactable keys
  166. *
  167. * @experimental
  168. */
  169. export type RedactableGaxiosOptions = Pick<GaxiosOptions, 'body' | 'data' | 'headers' | 'url'>;
  170. /**
  171. * A partial object of `GaxiosResponse` with only redactable keys
  172. *
  173. * @experimental
  174. */
  175. export type RedactableGaxiosResponse<T = any> = Pick<GaxiosResponse<T>, 'config' | 'data' | 'headers'>;
  176. /**
  177. * Configuration for the Gaxios `request` method.
  178. */
  179. export interface RetryConfig {
  180. /**
  181. * The number of times to retry the request. Defaults to 3.
  182. */
  183. retry?: number;
  184. /**
  185. * The number of retries already attempted.
  186. */
  187. currentRetryAttempt?: number;
  188. /**
  189. * The amount of time to initially delay the retry, in ms. Defaults to 100ms.
  190. */
  191. retryDelay?: number;
  192. /**
  193. * The HTTP Methods that will be automatically retried.
  194. * Defaults to ['GET','PUT','HEAD','OPTIONS','DELETE']
  195. */
  196. httpMethodsToRetry?: string[];
  197. /**
  198. * The HTTP response status codes that will automatically be retried.
  199. * Defaults to: [[100, 199], [408, 408], [429, 429], [500, 599]]
  200. */
  201. statusCodesToRetry?: number[][];
  202. /**
  203. * Function to invoke when a retry attempt is made.
  204. */
  205. onRetryAttempt?: (err: GaxiosError) => Promise<void> | void;
  206. /**
  207. * Function to invoke which determines if you should retry
  208. */
  209. shouldRetry?: (err: GaxiosError) => Promise<boolean> | boolean;
  210. /**
  211. * When there is no response, the number of retries to attempt. Defaults to 2.
  212. */
  213. noResponseRetries?: number;
  214. /**
  215. * Function to invoke which returns a promise. After the promise resolves,
  216. * the retry will be triggered. If provided, this will be used in-place of
  217. * the `retryDelay`
  218. */
  219. retryBackoff?: (err: GaxiosError, defaultBackoffMs: number) => Promise<void>;
  220. /**
  221. * Time that the initial request was made. Users should not set this directly.
  222. */
  223. timeOfFirstRequest?: number;
  224. /**
  225. * The length of time to keep retrying in ms. The last sleep period will
  226. * be shortened as necessary, so that the last retry runs at deadline (and not
  227. * considerably beyond it). The total time starting from when the initial
  228. * request is sent, after which an error will be returned, regardless of the
  229. * retrying attempts made meanwhile. Defaults to Number.MAX_SAFE_INTEGER indicating to effectively
  230. * ignore totalTimeout.
  231. */
  232. totalTimeout?: number;
  233. maxRetryDelay?: number;
  234. retryDelayMultiplier?: number;
  235. }
  236. export type FetchImplementation = (input: FetchRequestInfo, init?: FetchRequestInit) => Promise<FetchResponse>;
  237. export type FetchRequestInfo = any;
  238. export interface FetchResponse {
  239. readonly status: number;
  240. readonly statusText: string;
  241. readonly url: string;
  242. readonly body: unknown | null;
  243. arrayBuffer(): Promise<unknown>;
  244. blob(): Promise<unknown>;
  245. readonly headers: FetchHeaders;
  246. json(): Promise<any>;
  247. text(): Promise<string>;
  248. }
  249. export interface FetchRequestInit {
  250. method?: string;
  251. }
  252. export interface FetchHeaders {
  253. append(name: string, value: string): void;
  254. delete(name: string): void;
  255. get(name: string): string | null;
  256. has(name: string): boolean;
  257. set(name: string, value: string): void;
  258. forEach(callbackfn: (value: string, key: string) => void, thisArg?: any): void;
  259. }
  260. /**
  261. * An experimental error redactor.
  262. *
  263. * @param config Config to potentially redact properties of
  264. * @param response Config to potentially redact properties of
  265. *
  266. * @experimental
  267. */
  268. export declare function defaultErrorRedactor<T = any>(data: {
  269. config?: RedactableGaxiosOptions;
  270. response?: RedactableGaxiosResponse<T>;
  271. }): {
  272. config?: RedactableGaxiosOptions;
  273. response?: RedactableGaxiosResponse<T>;
  274. };