index.d.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**
  2. * @license
  3. * Copyright 2018 Google LLC
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /// <reference types="node" />
  18. /// <reference types="node" />
  19. /// <reference types="node" />
  20. import { Agent, AgentOptions as HttpsAgentOptions } from 'https';
  21. import { AgentOptions as HttpAgentOptions } from 'http';
  22. import { PassThrough, Readable } from 'stream';
  23. import { TeenyStatistics } from './TeenyStatistics';
  24. export interface CoreOptions {
  25. method?: string;
  26. timeout?: number;
  27. gzip?: boolean;
  28. json?: any;
  29. headers?: Headers;
  30. body?: string | {};
  31. useQuerystring?: boolean;
  32. qs?: any;
  33. proxy?: string;
  34. multipart?: RequestPart[];
  35. forever?: boolean;
  36. pool?: HttpsAgentOptions | HttpAgentOptions;
  37. }
  38. export interface OptionsWithUri extends CoreOptions {
  39. uri: string;
  40. }
  41. export interface OptionsWithUrl extends CoreOptions {
  42. url: string;
  43. }
  44. export type Options = OptionsWithUri | OptionsWithUrl;
  45. export interface Request extends PassThrough {
  46. agent: Agent | false;
  47. headers: Headers;
  48. href?: string;
  49. }
  50. export interface Response<T = any> {
  51. statusCode: number;
  52. headers: Headers;
  53. body: T;
  54. request: Request;
  55. statusMessage?: string;
  56. }
  57. export interface RequestPart {
  58. body: string | Readable;
  59. }
  60. export interface RequestCallback<T = any> {
  61. (err: Error | null, response: Response, body?: T): void;
  62. }
  63. export declare class RequestError extends Error {
  64. code?: number;
  65. }
  66. interface Headers {
  67. [index: string]: any;
  68. }
  69. declare function teenyRequest(reqOpts: Options): Request;
  70. declare function teenyRequest(reqOpts: Options, callback: RequestCallback): void;
  71. declare namespace teenyRequest {
  72. var defaults: (defaults: CoreOptions) => (reqOpts: Options, callback?: RequestCallback<any> | undefined) => void | Request;
  73. var stats: TeenyStatistics;
  74. var resetStats: () => void;
  75. }
  76. export { teenyRequest };