Skip to content

Commit 2c1bd3f

Browse files
committed
chore: cleanup and improvements
1 parent b23b7ca commit 2c1bd3f

File tree

2 files changed

+30
-36
lines changed

2 files changed

+30
-36
lines changed

src/https/request.android.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,16 @@ class HttpsResponseLegacy implements IHttpsResponseLegacy {
108108
return null;
109109
}
110110
}
111-
toArrayBufferAsync(): Promise<ArrayBuffer> {
111+
async toArrayBufferAsync() {
112112
if (this.arrayBuffer) {
113113
return Promise.resolve(this.arrayBuffer);
114114
}
115-
return new Promise((resolve, reject) => {
115+
const r = await new Promise<ArrayBuffer>((resolve, reject) => {
116116
this.getOrCreateCloseCallback();
117117
this.response.toByteArrayAsync(this.getCallback(resolve, reject));
118-
}).then((r: ArrayBuffer) => {
119-
this.arrayBuffer = r;
120-
return this.arrayBuffer;
121118
});
119+
this.arrayBuffer = r;
120+
return r;
122121
}
123122

124123
// cache it because asking it again wont work as the socket is closed
@@ -152,7 +151,7 @@ class HttpsResponseLegacy implements IHttpsResponseLegacy {
152151
return this.jsonResponse;
153152
}
154153

155-
async toJSONAsync() {
154+
async toJSONAsync<T>() {
156155
if (this.jsonResponse !== undefined) {
157156
return this.jsonResponse;
158157
}
@@ -163,7 +162,7 @@ class HttpsResponseLegacy implements IHttpsResponseLegacy {
163162
// TODO: handle arraybuffer already stored
164163
const r = await this.toStringAsync();
165164
this.jsonResponse = r ? parseJSON(r) : null;
166-
return this.jsonResponse;
165+
return this.jsonResponse as T;
167166
}
168167

169168
// cache it because asking it again wont work as the socket is closed
@@ -172,13 +171,12 @@ class HttpsResponseLegacy implements IHttpsResponseLegacy {
172171
if (this.imageSource) {
173172
return this.imageSource;
174173
}
175-
return new Promise<ImageSource>((resolve, reject) => {
174+
const r = await new Promise<ImageSource>((resolve, reject) => {
176175
this.getOrCreateCloseCallback();
177-
this.response.toImageAsync(this.getCallback(resolve, reject)).then((r) => {
178-
this.imageSource = r;
179-
return r;
180-
});
176+
this.response.toImageAsync(this.getCallback(resolve, reject));
181177
});
178+
this.imageSource = r;
179+
return r;
182180
}
183181
// toFile(destinationFilePath: string): File {
184182
// if (!destinationFilePath) {

src/https/request.ios.ts

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ class HttpsResponseLegacy implements IHttpsResponseLegacy {
149149
return Promise.resolve(this.toString(encoding));
150150
}
151151
jsonResponse: any;
152-
toJSON(encoding?: any) {
152+
toJSON<T>(encoding?: any) {
153153
if (!this.data) {
154154
return null;
155155
}
@@ -165,48 +165,43 @@ class HttpsResponseLegacy implements IHttpsResponseLegacy {
165165
this.jsonResponse = data;
166166
return data;
167167
}
168-
try {
169-
this.stringResponse = data;
170-
this.jsonResponse = parseJSON(data);
171-
return this.jsonResponse;
172-
} catch (err) {
173-
console.error('HttpsResponse.toJSON', err);
174-
return null;
175-
}
168+
this.stringResponse = data;
169+
this.jsonResponse = parseJSON(data);
170+
return this.jsonResponse as T;
171+
176172
}
177-
toJSONAsync(): Promise<any> {
178-
return Promise.resolve(this.toJSON());
173+
toJSONAsync<T>() {
174+
return Promise.resolve<T>(this.toJSON());
179175
}
180176
imageSource: ImageSource;
181-
toImage(): Promise<ImageSource> {
177+
async toImage(): Promise<ImageSource> {
182178
if (!this.data) {
183179
return Promise.resolve(null);
184180
}
185181
if (this.imageSource) {
186182
return Promise.resolve(this.imageSource);
187183
}
188-
return new Promise<ImageSource>((resolve, reject) => {
184+
const r = await new Promise<ImageSource>((resolve, reject) => {
189185
(UIImage as any).tns_decodeImageWithDataCompletion(this.data, (image) => {
190186
if (image) {
191187
resolve(new ImageSource(image));
192188
} else {
193189
reject(new Error('Response content may not be converted to an Image'));
194190
}
195191
});
196-
}).then((r) => {
197-
this.imageSource = r;
198-
return r;
199192
});
193+
this.imageSource = r;
194+
return r;
200195
}
201196
file: File;
202-
toFile(destinationFilePath?: string): Promise<File> {
197+
async toFile(destinationFilePath?: string): Promise<File> {
203198
if (!this.data) {
204199
return Promise.resolve(null);
205200
}
206201
if (this.file) {
207202
return Promise.resolve(this.file);
208203
}
209-
return new Promise<File>((resolve, reject) => {
204+
const r = await new Promise<File>((resolve, reject) => {
210205
if (!destinationFilePath) {
211206
destinationFilePath = getFilenameFromUrl(this.url);
212207
}
@@ -215,24 +210,23 @@ class HttpsResponseLegacy implements IHttpsResponseLegacy {
215210
const file = File.fromPath(destinationFilePath);
216211

217212
const result = this.data.writeToFileAtomically(destinationFilePath, true);
218-
if (resolve) {
213+
if (result) {
219214
resolve(file);
220215
} else {
221216
reject(new Error(`Cannot save file with path: ${destinationFilePath}.`));
222217
}
223218
} else {
224219
reject(new Error(`Cannot save file with path: ${destinationFilePath}.`));
225220
}
226-
}).then((f) => {
227-
this.file = f;
228-
return f;
229-
});
221+
})
222+
this.file = r;
223+
return r;
230224
}
231225
}
232226

233227
function AFFailure(resolve, reject, task: NSURLSessionDataTask, error: NSError, useLegacy: boolean, url) {
234228
if (error.code === -999) {
235-
return reject(new Error(error.localizedDescription));
229+
return reject(error);
236230
}
237231
let getHeaders = () => ({});
238232
const sendi = {
@@ -265,6 +259,7 @@ function AFFailure(resolve, reject, task: NSURLSessionDataTask, error: NSError,
265259
return reject(error.localizedDescription);
266260
}
267261
const failure: any = {
262+
error,
268263
description: error.description,
269264
reason: error.localizedDescription,
270265
url: failingURL ? failingURL.description : url
@@ -277,6 +272,7 @@ function AFFailure(resolve, reject, task: NSURLSessionDataTask, error: NSError,
277272
resolve(sendi);
278273
} else {
279274
const response: any = {
275+
error,
280276
body: parsedData,
281277
contentLength: sendi.contentLength,
282278
description: error.description,

0 commit comments

Comments
 (0)