Skip to content

Commit ef1470f

Browse files
committed
chore: ios refactoring
1 parent 07bd954 commit ef1470f

File tree

1 file changed

+5
-92
lines changed

1 file changed

+5
-92
lines changed

src/https.ios.ts

Lines changed: 5 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -94,83 +94,6 @@ class HttpsResponse implements Https.HttpsResponseLegacy {
9494
toArrayBufferAsync(): Promise<ArrayBuffer> {
9595
throw new Error('Method not implemented.');
9696
}
97-
// getCallback(resolve, reject) {
98-
// return {
99-
// onBitmap(res) {
100-
// resolve(new ImageSource(res));
101-
// },
102-
// onString(res) {
103-
// resolve(res);
104-
// },
105-
// onByteArray(res) {
106-
// resolve((ArrayBuffer as any).from(res));
107-
// },
108-
// onFile(res) {
109-
// resolve(res);
110-
// },
111-
// onException(err) {
112-
// reject(err);
113-
// },
114-
// };
115-
// }
116-
// toArrayBuffer() {
117-
// return (ArrayBuffer as any).from(this.response.toByteArray());
118-
// }
119-
// toArrayBufferAsync() {
120-
// return new Promise((resolve, reject) => {
121-
// this.response.toByteArrayAsync(this.getCallback(resolve, reject));
122-
// });
123-
// }
124-
125-
// toString(encoding?: HttpResponseEncoding) {
126-
// return this.response.toString();
127-
// }
128-
// toStringAsync() {
129-
// return new Promise((resolve, reject) => {
130-
// this.response.toStringAsync(this.getCallback(resolve, reject));
131-
// });
132-
// }
133-
// toJSON(encoding?: HttpResponseEncoding) {
134-
// return parseJSON(this.response.toString());
135-
// }
136-
137-
// toJSONAsync() {
138-
// return new Promise<string>((resolve, reject) => {
139-
// this.response.toStringAsync(this.getCallback(resolve, reject));
140-
// }).then(parseJSON);
141-
// }
142-
// toImage() {
143-
// return new Promise<any>((resolveImage, rejectImage) => {
144-
// try {
145-
// const image = this.response.toBitmap();
146-
// resolveImage(new ImageSource(image));
147-
// } catch (err) {
148-
// rejectImage(err);
149-
// }
150-
// });
151-
// }
152-
// toImageAsync() {
153-
// return new Promise((resolve, reject) => {
154-
// this.response.toBitmapAsync(this.getCallback(resolve, reject));
155-
// });
156-
// }
157-
// toFile(destinationFilePath: string) {
158-
// if (!destinationFilePath) {
159-
// destinationFilePath = getFilenameFromUrl(this.url);
160-
// }
161-
// return this.response.toFile(destinationFilePath);
162-
// }
163-
// toFileAsync(destinationFilePath: string) {
164-
// if (!destinationFilePath) {
165-
// destinationFilePath = getFilenameFromUrl(this.url);
166-
// }
167-
// return new Promise((resolve, reject) => {
168-
// this.response.toFileAsync(
169-
// destinationFilePath,
170-
// this.getCallback(resolve, reject)
171-
// );
172-
// });
173-
// }
17497

17598
arrayBuffer: ArrayBuffer;
17699
toArrayBuffer() {
@@ -298,17 +221,6 @@ class HttpsResponse implements Https.HttpsResponseLegacy {
298221
}
299222
}
300223

301-
// function AFSuccess(
302-
// resolve,
303-
// task: NSURLSessionDataTask,
304-
// url: string,
305-
// useLegacy: boolean,
306-
// data?: NSDictionary<string, any> & NSData & NSArray<any>
307-
// ) {
308-
// let content = useLegacy ? new HttpsResponse(data, url) : getData(data);
309-
// resolve({ task, content });
310-
// }
311-
312224
function AFFailure(resolve, reject, task: NSURLSessionDataTask, error: NSError, useLegacy: boolean, url) {
313225
if (error.code === -999) {
314226
return reject(new Error(error.localizedDescription));
@@ -374,9 +286,6 @@ function bodyToNative(cont) {
374286
let dict;
375287
if (Array.isArray(cont)) {
376288
dict = NSArray.arrayWithArray(cont.map((item) => bodyToNative(item)));
377-
// cont.forEach(function(item, idx) {
378-
// dict.addObject(bodyToNative(item));
379-
// });
380289
} else if (Utils.isObject(cont)) {
381290
dict = NSMutableDictionary.new<string, any>();
382291
Object.keys(cont).forEach((key) => dict.setValueForKey(bodyToNative(cont[key]), key));
@@ -385,8 +294,10 @@ function bodyToNative(cont) {
385294
}
386295
return dict;
387296
}
297+
const configuration = NSURLSessionConfiguration.defaultSessionConfiguration;
298+
const manager = AFHTTPSessionManager.alloc().initWithSessionConfiguration(configuration);
299+
388300
export function createRequest(opts: Https.HttpsRequestOptions): Https.HttpsRequest {
389-
const manager = AFHTTPSessionManager.alloc().initWithBaseURL(NSURL.URLWithString(opts.url));
390301
const type = opts.headers && opts.headers['Content-Type'] ? (opts.headers['Content-Type'] as string) : 'application/json';
391302
if (type.startsWith('application/json')) {
392303
manager.requestSerializer = AFJSONRequestSerializer.serializer();
@@ -411,6 +322,8 @@ export function createRequest(opts: Https.HttpsRequestOptions): Https.HttpsReque
411322
manager.requestSerializer.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData;
412323
break;
413324
}
325+
} else {
326+
manager.requestSerializer.cachePolicy = NSURLRequestCachePolicy.UseProtocolCachePolicy;
414327
}
415328
const heads = opts.headers;
416329
let headers: NSMutableDictionary<string, any> = null;

0 commit comments

Comments
 (0)