Repository navigation
Expand file tree
/
Copy pathindex.ts
More file actions
183 lines (161 loc) · 3.57 KB
/
Copy pathindex.ts
File metadata and controls
183 lines (161 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import { ImageSource } from '../image-source';
import { File } from '../file-system';
import * as httpRequest from './http-request';
export * from './http-request';
/**
* Provides options for the http requests.
*/
export interface HttpRequestOptions {
/**
* Gets or sets the request url.
*/
url: string;
/**
* Gets or sets the request method.
*/
method: string;
/**
* Gets or sets the request headers in JSON format.
*/
headers?: any;
/**
* Gets or sets the request body.
*/
content?: string | FormData | ArrayBuffer;
/**
* Gets or sets the request timeout in milliseconds.
*/
timeout?: number;
/**
* Gets or sets whether to *not* follow server's redirection responses.
*/
dontFollowRedirects?: boolean;
signal?: AbortSignal;
}
/**
* Encapsulates HTTP-response information from an HTTP-request.
*/
export interface HttpResponse {
/**
* Gets the response status code.
*/
statusCode: number;
/**
* Gets the response headers.
*/
headers: Headers;
/**
* Gets the response content.
*/
content?: HttpContent;
}
export type Headers = { [key: string]: string | string[] };
export enum HttpResponseEncoding {
UTF8,
GBK,
}
/**
* Encapsulates the content of an HttpResponse.
*/
export interface HttpContent {
/**
* Gets the response body as raw data.
*/
raw: any;
/**
* Gets the response body as ArrayBuffer
*/
toArrayBuffer: () => ArrayBuffer;
/**
* Gets the response body as string.
*/
toString: (encoding?: HttpResponseEncoding) => string;
/**
* Gets the response body as JSON object.
*/
toJSON: (encoding?: HttpResponseEncoding) => any;
/**
* Gets the response body as ImageSource.
*/
toImage: () => Promise<ImageSource>;
/**
* Gets the response body as file.
*/
toFile: (destinationFilePath?: string) => File;
}
export function getString(arg: any): Promise<string> {
return new Promise<string>((resolve, reject) => {
httpRequest.request(typeof arg === 'string' ? { url: arg, method: 'GET' } : arg).then(
(r) => {
try {
const str = r.content.toString();
resolve(str);
} catch (e) {
reject(e);
}
},
(e) => reject(e)
);
});
}
export function getJSON<T>(arg: any): Promise<T> {
return new Promise<T>((resolve, reject) => {
httpRequest.request(typeof arg === 'string' ? { url: arg, method: 'GET' } : arg).then(
(r) => {
try {
const json = r.content.toJSON();
resolve(json);
} catch (e) {
reject(e);
}
},
(e) => reject(e)
);
});
}
export function getImage(arg: any): Promise<ImageSource> {
return new Promise<any>((resolve, reject) => {
httpRequest.request(typeof arg === 'string' ? { url: arg, method: 'GET' } : arg).then(
(r) => {
try {
resolve(r.content.toImage());
} catch (err) {
reject(err);
}
},
(err) => {
reject(err);
}
);
});
}
export function getFile(arg: any, destinationFilePath?: string): Promise<any> {
return new Promise<any>((resolve, reject) => {
httpRequest.request(typeof arg === 'string' ? { url: arg, method: 'GET' } : arg).then(
(r) => {
try {
const file = r.content.toFile(destinationFilePath);
resolve(file);
} catch (e) {
reject(e);
}
},
(e) => reject(e)
);
});
}
export function getBinary(arg: any): Promise<ArrayBuffer> {
return new Promise<ArrayBuffer>((resolve, reject) => {
httpRequest.request(typeof arg === 'string' ? { url: arg, method: 'GET' } : arg).then(
(r) => {
try {
const arrayBuffer = r.content.toArrayBuffer();
resolve(arrayBuffer);
} catch (e) {
reject(e);
}
},
(e) => reject(e)
);
});
}