UNPKG

4.27 kBTypeScriptView Raw
1import { ImageSource } from '../image-source';
2import { File } from '../file-system';
3
4/**
5 * Downloads the content from the specified URL as a string.
6 * @param url The URL to request from.
7 */
8export function getString(url: string): Promise<string>;
9
10/**
11 * Downloads the content from the specified URL as a string.
12 * @param options An object that specifies various request options.
13 */
14export function getString(options: HttpRequestOptions): Promise<string>;
15
16/**
17 * Downloads the content from the specified URL as a string and returns its JSON.parse representation.
18 * @param url The URL to request from.
19 */
20export function getJSON<T>(url: string): Promise<T>;
21
22/**
23 * Downloads the content from the specified URL as a string and returns its JSON.parse representation.
24 * @param options An object that specifies various request options.
25 */
26export function getJSON<T>(options: HttpRequestOptions): Promise<T>;
27
28/**
29 * Downloads the content from the specified URL and attempts to decode it as an image.
30 * @param url The URL to request from.
31 */
32export function getImage(url: string): Promise<ImageSource>;
33
34/**
35 * Downloads the content from the specified URL and attempts to decode it as an image.
36 * @param options An object that specifies various request options.
37 */
38export function getImage(options: HttpRequestOptions): Promise<ImageSource>;
39
40/**
41 * Downloads the content from the specified URL and attempts to save it as file.
42 * @param url The URL to request from.
43 * @param destinationFilePath Optional. The downloaded file path.
44 */
45export function getFile(url: string, destinationFilePath?: string): Promise<File>;
46
47/**
48 * Downloads the content from the specified URL and attempts to save it as file.
49 * @param options An object that specifies various request options.
50 * @param destinationFilePath Optional. The downloaded file path.
51 */
52export function getFile(options: HttpRequestOptions, destinationFilePath?: string): Promise<File>;
53
54/**
55 * Downloads the content from the specified URL as binary and returns an ArrayBuffer.
56 * @param url The URL to request from.
57 */
58export function getBinary(url: string): Promise<ArrayBuffer>;
59
60/**
61 * Downloads the content from the specified URL as binary and returns an ArrayBuffer.
62 * @param options An object that specifies various request options.
63 */
64export function getBinary(options: HttpRequestOptions): Promise<ArrayBuffer>;
65
66/**
67 * Makes a generic http request using the provided options and returns a HttpResponse Object.
68 * @param options An object that specifies various request options.
69 */
70export function request(options: HttpRequestOptions): Promise<HttpResponse>;
71
72/**
73 * Provides options for the http requests.
74 */
75export interface HttpRequestOptions {
76 /**
77 * Gets or sets the request url.
78 */
79 url: string;
80
81 /**
82 * Gets or sets the request method.
83 */
84 method: string;
85
86 /**
87 * Gets or sets the request headers in JSON format.
88 */
89 headers?: any;
90
91 /**
92 * Gets or sets the request body.
93 */
94 content?: string | FormData | ArrayBuffer;
95
96 /**
97 * Gets or sets the request timeout in milliseconds.
98 */
99 timeout?: number;
100
101 /**
102 * Gets or sets wether to *not* follow server's redirection responses.
103 */
104 dontFollowRedirects?: boolean;
105}
106
107/**
108 * Encapsulates HTTP-response information from an HTTP-request.
109 */
110export interface HttpResponse {
111 /**
112 * Gets the response status code.
113 */
114 statusCode: number;
115
116 /**
117 * Gets the response headers.
118 */
119 headers: Headers;
120
121 /**
122 * Gets the response content.
123 */
124 content?: HttpContent;
125}
126
127export type Headers = { [key: string]: string | string[] };
128
129export enum HttpResponseEncoding {
130 UTF8,
131 GBK,
132}
133/**
134 * Encapsulates the content of an HttpResponse.
135 */
136export interface HttpContent {
137 /**
138 * Gets the response body as raw data.
139 */
140 raw: any;
141
142 /**
143 * Gets the response body as ArrayBuffer
144 */
145 toArrayBuffer: () => ArrayBuffer;
146
147 /**
148 * Gets the response body as string.
149 */
150 toString: (encoding?: HttpResponseEncoding) => string;
151
152 /**
153 * Gets the response body as JSON object.
154 */
155 toJSON: (encoding?: HttpResponseEncoding) => any;
156
157 /**
158 * Gets the response body as ImageSource.
159 */
160 toImage: () => Promise<ImageSource>;
161
162 /**
163 * Gets the response body as file.
164 */
165 toFile: (destinationFilePath?: string) => File;
166}