middleware.d.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /// <reference types="node" />
  2. import {AgentOptions} from 'http'
  3. import {IncomingHttpHeaders} from 'http'
  4. import {IncomingMessage} from 'http'
  5. import type {ProgressStream} from 'progress-stream'
  6. import progressStream from 'progress-stream'
  7. import type {UrlWithStringQuery} from 'url'
  8. /**
  9. * Constructs a http.Agent and uses it for all requests.
  10. * This can be used to override settings such as `maxSockets`, `maxTotalSockets` (to limit concurrency) or change the `timeout`.
  11. * @public
  12. */
  13. export declare function agent(opts?: AgentOptions): any
  14. /** @public */
  15. export declare type ApplyMiddleware = <T extends keyof MiddlewareHooks>(
  16. hook: T,
  17. value: MiddlewareHooks[T] extends (defaultValue: infer V, ...rest: any[]) => any ? V : never,
  18. ...args: MiddlewareHooks[T] extends (defaultValue: any, ...rest: infer P) => any ? P : never
  19. ) => ReturnType<MiddlewareHooks[T]>
  20. /** @public */
  21. export declare function base(baseUrl: string): {
  22. processOptions: (options: RequestOptions) => RequestOptions
  23. }
  24. /**
  25. * The cancel token API is based on the [cancelable promises proposal](https://github.com/tc39/proposal-cancelable-promises), which is currently at Stage 1.
  26. *
  27. * Code shamelessly stolen/borrowed from MIT-licensed [axios](https://github.com/mzabriskie/axios). Thanks to [Nick Uraltsev](https://github.com/nickuraltsev), [Matt Zabriskie](https://github.com/mzabriskie) and the other contributors of that project!
  28. */
  29. /** @public */
  30. export declare class Cancel {
  31. __CANCEL__: boolean
  32. message: string | undefined
  33. constructor(message: string | undefined)
  34. toString(): string
  35. }
  36. /** @public */
  37. export declare class CancelToken {
  38. promise: Promise<any>
  39. reason?: Cancel
  40. constructor(executor: (cb: (message?: string) => void) => void)
  41. static source: () => {
  42. token: CancelToken
  43. cancel: (message?: string) => void
  44. }
  45. }
  46. /** @public */
  47. declare function debug_2(opts?: any): {
  48. processOptions: (options: RequestOptions) => RequestOptions
  49. onRequest: (event: HookOnRequestEvent) => HookOnRequestEvent
  50. onResponse: (res: MiddlewareResponse, context: HttpContext) => MiddlewareResponse
  51. onError: (err: Error | null, context: HttpContext) => Error | null
  52. }
  53. export {debug_2 as debug}
  54. /** @public */
  55. export declare type DefineApplyMiddleware = (middleware: MiddlewareReducer) => ApplyMiddleware
  56. /**
  57. * Reports the environment as either "node" or "browser", depending on what entry point was used to aid bundler debugging.
  58. * If 'browser' is used, then the globally available `fetch` class is used. While `node` will always use either `node:https` or `node:http` depending on the protocol.
  59. * @public
  60. */
  61. export declare type ExportEnv = 'node' | 'react-server' | 'browser'
  62. /** @public */
  63. export declare interface FinalizeNodeOptionsPayload extends UrlWithStringQuery {
  64. method: RequestOptions['method']
  65. headers: RequestOptions['headers']
  66. maxRedirects: RequestOptions['maxRedirects']
  67. agent?: any
  68. cert?: any
  69. key?: any
  70. ca?: any
  71. }
  72. /** @public */
  73. export declare function headers(
  74. _headers: any,
  75. opts?: any,
  76. ): {
  77. processOptions: (options: RequestOptions) => RequestOptions
  78. }
  79. /** @public */
  80. export declare type HookOnRequestEvent = HookOnRequestEventNode | HookOnRequestEventBrowser
  81. /** @public */
  82. export declare interface HookOnRequestEventBase {
  83. options: RequestOptions
  84. context: HttpContext
  85. request: any
  86. }
  87. /** @public */
  88. export declare interface HookOnRequestEventBrowser extends HookOnRequestEventBase {
  89. adapter: Omit<RequestAdapter, 'node'>
  90. progress?: undefined
  91. }
  92. /** @public */
  93. export declare interface HookOnRequestEventNode extends HookOnRequestEventBase {
  94. adapter: 'node'
  95. progress: any
  96. }
  97. /** @public */
  98. export declare interface HttpContext {
  99. options: RequestOptions
  100. channels: MiddlewareChannels
  101. applyMiddleware: ApplyMiddleware
  102. }
  103. /** @public */
  104. export declare function httpErrors(): {
  105. onResponse: (res: MiddlewareResponse, ctx: HttpContext) => MiddlewareResponse
  106. }
  107. /**
  108. * request-node in node, browser-request in browsers
  109. * @public
  110. */
  111. export declare type HttpRequest = (
  112. context: HttpContext,
  113. callback: (err: Error | null, response?: MiddlewareResponse) => void,
  114. ) => HttpRequestOngoing
  115. /** @public */
  116. export declare interface HttpRequestOngoing {
  117. abort: () => void
  118. }
  119. /** @public */
  120. export declare function injectResponse(opts?: {
  121. inject: (
  122. event: Parameters<MiddlewareHooks['interceptRequest']>[1],
  123. prevValue: Parameters<MiddlewareHooks['interceptRequest']>[0],
  124. ) => Partial<MiddlewareResponse | undefined | void>
  125. }): {
  126. interceptRequest: (
  127. prevValue: MiddlewareResponse | undefined,
  128. event: {
  129. adapter: RequestAdapter
  130. context: HttpContext
  131. },
  132. ) => MiddlewareResponse | undefined
  133. }
  134. /** @public */
  135. export declare function jsonRequest(): {
  136. processOptions: (options: RequestOptions) => RequestOptions
  137. }
  138. /** @public */
  139. export declare function jsonResponse(opts?: any): {
  140. onResponse: (response: MiddlewareResponse) => MiddlewareResponse
  141. processOptions: (options: RequestOptions) => RequestOptions & {
  142. headers: any
  143. }
  144. }
  145. /** @public */
  146. export declare const keepAlive: (config?: any) => any
  147. /** @public */
  148. export declare type Middleware = Partial<MiddlewareHooks>
  149. /** @public */
  150. export declare interface MiddlewareChannels {
  151. request: PubSub<HttpContext>
  152. response: PubSub<unknown>
  153. progress: PubSub<unknown>
  154. error: PubSub<unknown>
  155. abort: PubSub<void>
  156. }
  157. /** @public */
  158. export declare type MiddlewareHookName = keyof MiddlewareHooks
  159. /** @public */
  160. export declare interface MiddlewareHooks {
  161. processOptions: (options: RequestOptions) => RequestOptions
  162. validateOptions: (options: RequestOptions) => void | undefined
  163. interceptRequest: (
  164. prevValue: MiddlewareResponse | undefined,
  165. event: {
  166. adapter: RequestAdapter
  167. context: HttpContext
  168. },
  169. ) => MiddlewareResponse | undefined | void
  170. finalizeOptions: (
  171. options: FinalizeNodeOptionsPayload | RequestOptions,
  172. ) => FinalizeNodeOptionsPayload | RequestOptions
  173. onRequest: (evt: HookOnRequestEvent) => void
  174. onResponse: (response: MiddlewareResponse, context: HttpContext) => MiddlewareResponse
  175. onError: (err: Error | null, context: HttpContext) => any
  176. onReturn: (channels: MiddlewareChannels, context: HttpContext) => any
  177. onHeaders: (
  178. response: IncomingMessage,
  179. evt: {
  180. headers: IncomingHttpHeaders
  181. adapter: RequestAdapter
  182. context: HttpContext
  183. },
  184. ) => ProgressStream
  185. }
  186. /** @public */
  187. export declare type MiddlewareReducer = {
  188. [T in keyof MiddlewareHooks]: ((
  189. ...args: Parameters<MiddlewareHooks[T]>
  190. ) => ReturnType<MiddlewareHooks[T]>)[]
  191. }
  192. /** @public */
  193. export declare interface MiddlewareRequest {}
  194. /** @public */
  195. export declare interface MiddlewareResponse {
  196. body: any
  197. url: string
  198. method: string
  199. headers: any
  200. statusCode: number
  201. statusMessage: string
  202. }
  203. /** @public */
  204. export declare type Middlewares = Middleware[]
  205. /** @public */
  206. export declare function mtls(config?: any): {
  207. finalizeOptions: (options: RequestOptions | FinalizeNodeOptionsPayload) =>
  208. | RequestOptions
  209. | (FinalizeNodeOptionsPayload & {
  210. cert: any
  211. key: any
  212. ca: any
  213. })
  214. }
  215. /** @public */
  216. export declare function observable(opts?: {implementation?: any}): {
  217. onReturn: (channels: MiddlewareChannels, context: HttpContext) => any
  218. }
  219. /** @public */
  220. export declare const processOptions: (opts: RequestOptions) => {
  221. url: string
  222. body?: any
  223. bodySize?: number | undefined
  224. cancelToken?: any
  225. compress?: boolean | undefined
  226. headers?: any
  227. maxRedirects?: number | undefined
  228. maxRetries?: number | undefined
  229. method?: string | undefined
  230. proxy?: any
  231. query?: any
  232. rawBody?: boolean | undefined
  233. shouldRetry?: any
  234. stream?: boolean | undefined
  235. timeout: any
  236. tunnel?: boolean | undefined
  237. debug?: any
  238. requestId?: number | undefined
  239. attemptNumber?: number | undefined
  240. withCredentials?: boolean | undefined
  241. fetch?: boolean | Omit<RequestInit, 'method'> | undefined
  242. useAbortSignal?: boolean | undefined
  243. }
  244. /** @public */
  245. export declare function progress(): {
  246. onHeaders: (
  247. response: IncomingMessage,
  248. evt: {
  249. headers: IncomingHttpHeaders
  250. adapter: RequestAdapter
  251. context: HttpContext
  252. },
  253. ) => progressStream.ProgressStream
  254. onRequest: (evt: HookOnRequestEvent) => void
  255. }
  256. /** @public */
  257. export declare const promise: {
  258. (options?: {onlyBody?: boolean; implementation?: PromiseConstructor}): {
  259. onReturn: (channels: MiddlewareChannels, context: HttpContext) => Promise<unknown>
  260. }
  261. Cancel: typeof Cancel
  262. CancelToken: typeof CancelToken
  263. isCancel: (value: any) => value is Cancel
  264. }
  265. /** @public */
  266. export declare function proxy(_proxy: any): {
  267. processOptions: (options: RequestOptions) => {
  268. proxy: any
  269. } & RequestOptions
  270. }
  271. /** @public */
  272. export declare interface PubSub<Message> {
  273. publish: (message: Message) => void
  274. subscribe: (subscriber: Subscriber<Message>) => () => void
  275. }
  276. /**
  277. * Reports the request adapter in use. `node` is only available if `ExportEnv` is also `node`.
  278. * When `ExportEnv` is `browser` then the adapter can be either `xhr` or `fetch`.
  279. * In the future `fetch` will be available in `node` as well.
  280. * @public
  281. */
  282. export declare type RequestAdapter = 'node' | 'xhr' | 'fetch'
  283. /** @public */
  284. export declare type Requester = {
  285. use: (middleware: Middleware) => Requester
  286. clone: () => Requester
  287. (options: RequestOptions | string): any
  288. }
  289. /** @public */
  290. export declare interface RequestOptions {
  291. url: string
  292. body?: any
  293. bodySize?: number
  294. cancelToken?: any
  295. compress?: boolean
  296. headers?: any
  297. maxRedirects?: number
  298. maxRetries?: number
  299. method?: string
  300. proxy?: any
  301. query?: any
  302. rawBody?: boolean
  303. shouldRetry?: any
  304. stream?: boolean
  305. timeout?: any
  306. tunnel?: boolean
  307. debug?: any
  308. requestId?: number
  309. attemptNumber?: number
  310. withCredentials?: boolean
  311. /**
  312. * Enables using the native `fetch` API instead of the default `http` module, and allows setting its options like `cache`
  313. */
  314. fetch?: boolean | Omit<RequestInit, 'method'>
  315. /**
  316. * Some frameworks have special behavior for `fetch` when an `AbortSignal` is used, and may want to disable it unless userland specifically opts-in.
  317. */
  318. useAbortSignal?: boolean
  319. }
  320. /** @public */
  321. export declare const retry: {
  322. (opts?: Partial<RetryOptions>): {
  323. onError: (err: Error | null, context: HttpContext) => Error | null
  324. }
  325. shouldRetry: (err: any, num: number, options: any) => boolean
  326. }
  327. /** @public */
  328. export declare interface RetryOptions {
  329. shouldRetry: (err: any, num: number, options: any) => boolean
  330. maxRetries?: number
  331. retryDelay?: (attemptNumber: number) => number
  332. }
  333. /** @public */
  334. export declare interface Subscriber<Event> {
  335. (event: Event): void
  336. }
  337. /** @public */
  338. export declare function urlEncoded(): {
  339. processOptions: (options: RequestOptions) => RequestOptions
  340. }
  341. /** @public */
  342. export declare const validateOptions: (options: RequestOptions) => void
  343. export {}